From 3e6b84474d7cd813bda0956568966024915d0ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 21 Sep 2019 16:31:27 -0700 Subject: [PATCH 1/2] Propagate `types.err` in locals further to avoid spurious knock-down errors --- src/librustc_typeck/check/coercion.rs | 8 ++++++-- src/librustc_typeck/check/mod.rs | 23 ++++++++++++++++++++++- src/test/ui/issues/issue-33575.rs | 4 ++++ src/test/ui/issues/issue-33575.stderr | 9 +++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/issues/issue-33575.rs create mode 100644 src/test/ui/issues/issue-33575.stderr diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index ee4f0a868c10a..ac1bf8e91a149 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -163,7 +163,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { // Just ignore error types. if a.references_error() || b.references_error() { - return success(vec![], b, vec![]); + return success(vec![], self.fcx.tcx.types.err, vec![]); } if a.is_never() { @@ -821,7 +821,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let (adjustments, _) = self.register_infer_ok_obligations(ok); self.apply_adjustments(expr, adjustments); - Ok(target) + if expr_ty.references_error() { + Ok(self.tcx.types.err) + } else { + Ok(target) + } } /// Same as `try_coerce()`, but without side-effects. diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 0eeeee01c82f1..75942731d8790 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -153,7 +153,7 @@ use self::method::{MethodCallee, SelfSource}; use self::TupleArgumentsFlag::*; /// The type of a local binding, including the revealed type for anon types. -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] pub struct LocalTy<'tcx> { decl_ty: Ty<'tcx>, revealed_ty: Ty<'tcx> @@ -3752,14 +3752,35 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let Some(ref init) = local.init { let init_ty = self.check_decl_initializer(local, &init); if init_ty.references_error() { + // Override the types everywhere with `types.err` to avoid knock down errors. self.write_ty(local.hir_id, init_ty); + self.write_ty(local.pat.hir_id, init_ty); + self.locals.borrow_mut().insert(local.hir_id, LocalTy { + decl_ty: t, + revealed_ty: init_ty, + }); + self.locals.borrow_mut().insert(local.pat.hir_id, LocalTy { + decl_ty: t, + revealed_ty: init_ty, + }); } } self.check_pat_top(&local.pat, t, None); let pat_ty = self.node_ty(local.pat.hir_id); + debug!("check_decl_local pat_ty {:?}", pat_ty); if pat_ty.references_error() { + // Override the types everywhere with `types.err` to avoid knock down errors. self.write_ty(local.hir_id, pat_ty); + self.write_ty(local.pat.hir_id, pat_ty); + self.locals.borrow_mut().insert(local.hir_id, LocalTy { + decl_ty: t, + revealed_ty: pat_ty, + }); + self.locals.borrow_mut().insert(local.pat.hir_id, LocalTy { + decl_ty: t, + revealed_ty: pat_ty, + }); } } diff --git a/src/test/ui/issues/issue-33575.rs b/src/test/ui/issues/issue-33575.rs new file mode 100644 index 0000000000000..d97afc3d31d5c --- /dev/null +++ b/src/test/ui/issues/issue-33575.rs @@ -0,0 +1,4 @@ +fn main() { + let baz = ().foo(); //~ ERROR no method named `foo` found for type `()` in the current scope + ::from_str(&baz); // No complains about `str` being unsized +} diff --git a/src/test/ui/issues/issue-33575.stderr b/src/test/ui/issues/issue-33575.stderr new file mode 100644 index 0000000000000..e6b74d262c340 --- /dev/null +++ b/src/test/ui/issues/issue-33575.stderr @@ -0,0 +1,9 @@ +error[E0599]: no method named `foo` found for type `()` in the current scope + --> $DIR/issue-33575.rs:2:18 + | +LL | let baz = ().foo(); + | ^^^ method not found in `()` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0599`. From daed67481511b65475069214cd8325ca9d018509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 21 Sep 2019 17:28:07 -0700 Subject: [PATCH 2/2] review comments --- src/librustc_typeck/check/coercion.rs | 8 +++--- src/librustc_typeck/check/mod.rs | 39 ++++++++++----------------- src/test/ui/issues/issue-33575.rs | 2 +- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index ac1bf8e91a149..d98e1f3e1283f 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -821,11 +821,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let (adjustments, _) = self.register_infer_ok_obligations(ok); self.apply_adjustments(expr, adjustments); - if expr_ty.references_error() { - Ok(self.tcx.types.err) + Ok(if expr_ty.references_error() { + self.tcx.types.err } else { - Ok(target) - } + target + }) } /// Same as `try_coerce()`, but without side-effects. diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 75942731d8790..07eb034054c95 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3751,36 +3751,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let Some(ref init) = local.init { let init_ty = self.check_decl_initializer(local, &init); - if init_ty.references_error() { - // Override the types everywhere with `types.err` to avoid knock down errors. - self.write_ty(local.hir_id, init_ty); - self.write_ty(local.pat.hir_id, init_ty); - self.locals.borrow_mut().insert(local.hir_id, LocalTy { - decl_ty: t, - revealed_ty: init_ty, - }); - self.locals.borrow_mut().insert(local.pat.hir_id, LocalTy { - decl_ty: t, - revealed_ty: init_ty, - }); - } + self.overwrite_local_ty_if_err(local, t, init_ty); } self.check_pat_top(&local.pat, t, None); let pat_ty = self.node_ty(local.pat.hir_id); - debug!("check_decl_local pat_ty {:?}", pat_ty); - if pat_ty.references_error() { + self.overwrite_local_ty_if_err(local, t, pat_ty); + } + + fn overwrite_local_ty_if_err(&self, local: &'tcx hir::Local, decl_ty: Ty<'tcx>, ty: Ty<'tcx>) { + if ty.references_error() { // Override the types everywhere with `types.err` to avoid knock down errors. - self.write_ty(local.hir_id, pat_ty); - self.write_ty(local.pat.hir_id, pat_ty); - self.locals.borrow_mut().insert(local.hir_id, LocalTy { - decl_ty: t, - revealed_ty: pat_ty, - }); - self.locals.borrow_mut().insert(local.pat.hir_id, LocalTy { - decl_ty: t, - revealed_ty: pat_ty, - }); + self.write_ty(local.hir_id, ty); + self.write_ty(local.pat.hir_id, ty); + let local_ty = LocalTy { + decl_ty, + revealed_ty: ty, + }; + self.locals.borrow_mut().insert(local.hir_id, local_ty); + self.locals.borrow_mut().insert(local.pat.hir_id, local_ty); } } diff --git a/src/test/ui/issues/issue-33575.rs b/src/test/ui/issues/issue-33575.rs index d97afc3d31d5c..09c499452adb6 100644 --- a/src/test/ui/issues/issue-33575.rs +++ b/src/test/ui/issues/issue-33575.rs @@ -1,4 +1,4 @@ fn main() { let baz = ().foo(); //~ ERROR no method named `foo` found for type `()` in the current scope - ::from_str(&baz); // No complains about `str` being unsized + ::from_str(&baz); // No complaints about `str` being unsized }