Skip to content

Commit b66e732

Browse files
authored
Rollup merge of #64674 - estebank:knock-down-the-wall, r=Centril
Propagate `types.err` in locals further to avoid spurious knock-down errors Fix #33575, fix #44504.
2 parents da58e11 + daed674 commit b66e732

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

src/librustc_typeck/check/coercion.rs

+6-2
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+
Ok(if expr_ty.references_error() {
825+
self.tcx.types.err
826+
} else {
827+
target
828+
})
825829
}
826830

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

src/librustc_typeck/check/mod.rs

+16-6
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>
@@ -3754,15 +3754,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
37543754

37553755
if let Some(ref init) = local.init {
37563756
let init_ty = self.check_decl_initializer(local, &init);
3757-
if init_ty.references_error() {
3758-
self.write_ty(local.hir_id, init_ty);
3759-
}
3757+
self.overwrite_local_ty_if_err(local, t, init_ty);
37603758
}
37613759

37623760
self.check_pat_top(&local.pat, t, None);
37633761
let pat_ty = self.node_ty(local.pat.hir_id);
3764-
if pat_ty.references_error() {
3765-
self.write_ty(local.hir_id, pat_ty);
3762+
self.overwrite_local_ty_if_err(local, t, pat_ty);
3763+
}
3764+
3765+
fn overwrite_local_ty_if_err(&self, local: &'tcx hir::Local, decl_ty: Ty<'tcx>, ty: Ty<'tcx>) {
3766+
if ty.references_error() {
3767+
// Override the types everywhere with `types.err` to avoid knock down errors.
3768+
self.write_ty(local.hir_id, ty);
3769+
self.write_ty(local.pat.hir_id, ty);
3770+
let local_ty = LocalTy {
3771+
decl_ty,
3772+
revealed_ty: ty,
3773+
};
3774+
self.locals.borrow_mut().insert(local.hir_id, local_ty);
3775+
self.locals.borrow_mut().insert(local.pat.hir_id, local_ty);
37663776
}
37673777
}
37683778

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

+4
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 complaints about `str` being unsized
4+
}

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

+9
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)