Skip to content

Commit b1575b7

Browse files
committed
Silence unecessary !Sized binding error
When gathering locals, we introduce a `Sized` obligation for each binding in the pattern. *After* doing so, we typecheck the init expression. If this has a type failure, we store `{type error}`, for both the expression and the pattern. But later we store an inference variable for the pattern. We now avoid any override of an existing type on a hir node when they've already been marked as `{type error}`, and on E0277, when it comes from `VariableType` we silence the error in support of the type error. Fix #117846.
1 parent 3f2159f commit b1575b7

File tree

6 files changed

+56
-15
lines changed

6 files changed

+56
-15
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
138138
#[inline]
139139
pub fn write_ty(&self, id: hir::HirId, ty: Ty<'tcx>) {
140140
debug!("write_ty({:?}, {:?}) in fcx {}", id, self.resolve_vars_if_possible(ty), self.tag());
141-
self.typeck_results.borrow_mut().node_types_mut().insert(id, ty);
141+
let mut typeck = self.typeck_results.borrow_mut();
142+
let mut node_ty = typeck.node_types_mut();
143+
if let Some(ty) = node_ty.get(id)
144+
&& let Err(e) = ty.error_reported()
145+
{
146+
// Do not overwrite nodes that were already marked as `{type error}`. This allows us to
147+
// silence unnecessary errors from obligations that were set earlier than a type error
148+
// was produced, but that is overwritten by later analysis. This happens in particular
149+
// for `Sized` obligations introduced in gather_locals. (#117846)
150+
self.set_tainted_by_errors(e);
151+
return;
152+
}
153+
154+
node_ty.insert(id, ty);
142155

143156
if let Err(e) = ty.error_reported() {
144157
self.set_tainted_by_errors(e);

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1892,11 +1892,35 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18921892
pat: &'tcx hir::Pat<'tcx>,
18931893
ty: Ty<'tcx>,
18941894
) {
1895+
struct V<'tcx> {
1896+
tcx: TyCtxt<'tcx>,
1897+
pat_hir_ids: Vec<hir::HirId>,
1898+
}
1899+
1900+
impl<'tcx> Visitor<'tcx> for V<'tcx> {
1901+
type NestedFilter = rustc_middle::hir::nested_filter::All;
1902+
1903+
fn nested_visit_map(&mut self) -> Self::Map {
1904+
self.tcx.hir()
1905+
}
1906+
1907+
fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
1908+
self.pat_hir_ids.push(p.hir_id);
1909+
hir::intravisit::walk_pat(self, p);
1910+
}
1911+
}
18951912
if let Err(guar) = ty.error_reported() {
18961913
// Override the types everywhere with `err()` to avoid knock on errors.
18971914
let err = Ty::new_error(self.tcx, guar);
18981915
self.write_ty(hir_id, err);
18991916
self.write_ty(pat.hir_id, err);
1917+
let mut visitor = V { tcx: self.tcx, pat_hir_ids: vec![] };
1918+
hir::intravisit::walk_pat(&mut visitor, pat);
1919+
// Mark all the subpatterns as `{type error}` as well. This allows errors for specific
1920+
// subpatterns to be silenced.
1921+
for hir_id in visitor.pat_hir_ids {
1922+
self.write_ty(hir_id, err);
1923+
}
19001924
self.locals.borrow_mut().insert(hir_id, err);
19011925
self.locals.borrow_mut().insert(pat.hir_id, err);
19021926
}

compiler/rustc_middle/src/ty/typeck_results.rs

+5
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,11 @@ impl<'a, V> LocalTableInContextMut<'a, V> {
568568
self.data.get_mut(&id.local_id)
569569
}
570570

571+
pub fn get(&mut self, id: hir::HirId) -> Option<&V> {
572+
validate_hir_id_for_typeck_results(self.hir_owner, id);
573+
self.data.get(&id.local_id)
574+
}
575+
571576
pub fn entry(&mut self, id: hir::HirId) -> Entry<'_, hir::ItemLocalId, V> {
572577
validate_hir_id_for_typeck_results(self.hir_owner, id);
573578
self.data.entry(id.local_id)

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2954,6 +2954,16 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
29542954
}
29552955
}
29562956
ObligationCauseCode::VariableType(hir_id) => {
2957+
if let Some(typeck_results) = &self.typeck_results
2958+
&& let Some(ty) = typeck_results.node_type_opt(hir_id)
2959+
&& let ty::Error(_) = ty.kind()
2960+
{
2961+
err.note(format!(
2962+
"`{predicate}` isn't satisfied, but the type of this pattern is \
2963+
`{{type error}}`",
2964+
));
2965+
err.downgrade_to_delayed_bug();
2966+
}
29572967
match tcx.parent_hir_node(hir_id) {
29582968
Node::Local(hir::Local { ty: Some(ty), .. }) => {
29592969
err.span_suggestion_verbose(

tests/ui/sized/expr-type-error-plus-sized-obligation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(warnings)]
22

33
fn issue_117846_repro() {
4-
let (a, _) = if true { //~ ERROR E0277
4+
let (a, _) = if true {
55
produce()
66
} else {
77
(Vec::new(), &[]) //~ ERROR E0308

tests/ui/sized/expr-type-error-plus-sized-obligation.stderr

+2-13
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ LL | | };
1414
= note: expected tuple `(Vec<Foo>, &[Bar])`
1515
found tuple `(Vec<_>, &[_; 0])`
1616

17-
error[E0277]: the size for values of type `[Foo]` cannot be known at compilation time
18-
--> $DIR/expr-type-error-plus-sized-obligation.rs:4:10
19-
|
20-
LL | let (a, _) = if true {
21-
| ^ doesn't have a size known at compile-time
22-
|
23-
= help: the trait `Sized` is not implemented for `[Foo]`
24-
= note: all local variables must have a statically known size
25-
= help: unsized locals are gated as an unstable feature
26-
27-
error: aborting due to 2 previous errors
17+
error: aborting due to 1 previous error
2818

29-
Some errors have detailed explanations: E0277, E0308.
30-
For more information about an error, try `rustc --explain E0277`.
19+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)