Skip to content

Propagate types.err in locals further to avoid spurious knock-down errors #64674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/librustc_typeck/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Ok(if expr_ty.references_error() {
self.tcx.types.err
} else {
target
})
}

/// Same as `try_coerce()`, but without side-effects.
Expand Down
22 changes: 16 additions & 6 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -3751,15 +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() {
self.write_ty(local.hir_id, 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);
if pat_ty.references_error() {
self.write_ty(local.hir_id, pat_ty);
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, 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);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/issues/issue-33575.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let baz = ().foo(); //~ ERROR no method named `foo` found for type `()` in the current scope
<i32 as std::str::FromStr>::from_str(&baz); // No complaints about `str` being unsized
}
9 changes: 9 additions & 0 deletions src/test/ui/issues/issue-33575.stderr
Original file line number Diff line number Diff line change
@@ -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`.