Skip to content

Commit b7dcabe

Browse files
committed
Auto merge of #122119 - estebank:issue-117846, r=Nadrieril
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
2 parents a77c20c + b1575b7 commit b7dcabe

File tree

6 files changed

+94
-1
lines changed

6 files changed

+94
-1
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
@@ -2953,6 +2953,16 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
29532953
}
29542954
}
29552955
ObligationCauseCode::VariableType(hir_id) => {
2956+
if let Some(typeck_results) = &self.typeck_results
2957+
&& let Some(ty) = typeck_results.node_type_opt(hir_id)
2958+
&& let ty::Error(_) = ty.kind()
2959+
{
2960+
err.note(format!(
2961+
"`{predicate}` isn't satisfied, but the type of this pattern is \
2962+
`{{type error}}`",
2963+
));
2964+
err.downgrade_to_delayed_bug();
2965+
}
29562966
match tcx.parent_hir_node(hir_id) {
29572967
Node::Local(hir::Local { ty: Some(ty), .. }) => {
29582968
err.span_suggestion_verbose(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![allow(warnings)]
2+
3+
fn issue_117846_repro() {
4+
let (a, _) = if true {
5+
produce()
6+
} else {
7+
(Vec::new(), &[]) //~ ERROR E0308
8+
};
9+
10+
accept(&a);
11+
}
12+
13+
struct Foo;
14+
struct Bar;
15+
16+
fn produce() -> (Vec<Foo>, &'static [Bar]) {
17+
todo!()
18+
}
19+
20+
fn accept(c: &[Foo]) {}
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0308]: `if` and `else` have incompatible types
2+
--> $DIR/expr-type-error-plus-sized-obligation.rs:7:9
3+
|
4+
LL | let (a, _) = if true {
5+
| __________________-
6+
LL | | produce()
7+
| | --------- expected because of this
8+
LL | | } else {
9+
LL | | (Vec::new(), &[])
10+
| | ^^^^^^^^^^^^^^^^^ expected `(Vec<Foo>, &[Bar])`, found `(Vec<_>, &[_; 0])`
11+
LL | | };
12+
| |_____- `if` and `else` have incompatible types
13+
|
14+
= note: expected tuple `(Vec<Foo>, &[Bar])`
15+
found tuple `(Vec<_>, &[_; 0])`
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)