Skip to content

Commit 3e6b844

Browse files
committed
Propagate types.err in locals further to avoid spurious knock-down errors
1 parent 9ad1e7c commit 3e6b844

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

src/librustc_typeck/check/coercion.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
163163

164164
// Just ignore error types.
165165
if a.references_error() || b.references_error() {
166-
return success(vec![], b, vec![]);
166+
return success(vec![], self.fcx.tcx.types.err, vec![]);
167167
}
168168

169169
if a.is_never() {
@@ -821,7 +821,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
821821

822822
let (adjustments, _) = self.register_infer_ok_obligations(ok);
823823
self.apply_adjustments(expr, adjustments);
824-
Ok(target)
824+
if expr_ty.references_error() {
825+
Ok(self.tcx.types.err)
826+
} else {
827+
Ok(target)
828+
}
825829
}
826830

827831
/// Same as `try_coerce()`, but without side-effects.

src/librustc_typeck/check/mod.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ use self::method::{MethodCallee, SelfSource};
153153
use self::TupleArgumentsFlag::*;
154154

155155
/// The type of a local binding, including the revealed type for anon types.
156-
#[derive(Copy, Clone)]
156+
#[derive(Copy, Clone, Debug)]
157157
pub struct LocalTy<'tcx> {
158158
decl_ty: Ty<'tcx>,
159159
revealed_ty: Ty<'tcx>
@@ -3752,14 +3752,35 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
37523752
if let Some(ref init) = local.init {
37533753
let init_ty = self.check_decl_initializer(local, &init);
37543754
if init_ty.references_error() {
3755+
// Override the types everywhere with `types.err` to avoid knock down errors.
37553756
self.write_ty(local.hir_id, init_ty);
3757+
self.write_ty(local.pat.hir_id, init_ty);
3758+
self.locals.borrow_mut().insert(local.hir_id, LocalTy {
3759+
decl_ty: t,
3760+
revealed_ty: init_ty,
3761+
});
3762+
self.locals.borrow_mut().insert(local.pat.hir_id, LocalTy {
3763+
decl_ty: t,
3764+
revealed_ty: init_ty,
3765+
});
37563766
}
37573767
}
37583768

37593769
self.check_pat_top(&local.pat, t, None);
37603770
let pat_ty = self.node_ty(local.pat.hir_id);
3771+
debug!("check_decl_local pat_ty {:?}", pat_ty);
37613772
if pat_ty.references_error() {
3773+
// Override the types everywhere with `types.err` to avoid knock down errors.
37623774
self.write_ty(local.hir_id, pat_ty);
3775+
self.write_ty(local.pat.hir_id, pat_ty);
3776+
self.locals.borrow_mut().insert(local.hir_id, LocalTy {
3777+
decl_ty: t,
3778+
revealed_ty: pat_ty,
3779+
});
3780+
self.locals.borrow_mut().insert(local.pat.hir_id, LocalTy {
3781+
decl_ty: t,
3782+
revealed_ty: pat_ty,
3783+
});
37633784
}
37643785
}
37653786

src/test/ui/issues/issue-33575.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let baz = ().foo(); //~ ERROR no method named `foo` found for type `()` in the current scope
3+
<i32 as std::str::FromStr>::from_str(&baz); // No complains about `str` being unsized
4+
}

src/test/ui/issues/issue-33575.stderr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0599]: no method named `foo` found for type `()` in the current scope
2+
--> $DIR/issue-33575.rs:2:18
3+
|
4+
LL | let baz = ().foo();
5+
| ^^^ method not found in `()`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)