about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-04-10 11:10:14 +0200
committerGitHub <noreply@github.com>2025-04-10 11:10:14 +0200
commitb131828f0dbda6e3c3e07b06a11af5c3a3cac120 (patch)
treebf4592a110cc8185735c7c1a1a8e17b876d78700
parentaecce2161dbfdd85f53203dbe8729f1bc1ee95c0 (diff)
parentb8c4c163f0e2e3438eb474c6b0ae5fa545061aa9 (diff)
downloadrust-b131828f0dbda6e3c3e07b06a11af5c3a3cac120.tar.gz
rust-b131828f0dbda6e3c3e07b06a11af5c3a3cac120.zip
Rollup merge of #139423 - compiler-errors:field-autoderef, r=oli-obk
Suppress missing field error when autoderef bottoms out in infer

I see this error repeatedly when doing refactorings, and it's pretty misleading b/c it's not the source of the error.
-rw-r--r--compiler/rustc_hir_typeck/src/expr.rs9
-rw-r--r--tests/ui/typeck/issue-65611.rs1
-rw-r--r--tests/ui/typeck/issue-65611.stderr11
3 files changed, 9 insertions, 12 deletions
diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs
index 3475d15e948..de2f039cb1c 100644
--- a/compiler/rustc_hir_typeck/src/expr.rs
+++ b/compiler/rustc_hir_typeck/src/expr.rs
@@ -2920,8 +2920,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         }
         // We failed to check the expression, report an error.
 
-        // Emits an error if we deref an infer variable, like calling `.field` on a base type of &_.
-        self.structurally_resolve_type(autoderef.span(), autoderef.final_ty(false));
+        // Emits an error if we deref an infer variable, like calling `.field` on a base type
+        // of `&_`. We can also use this to suppress unnecessary "missing field" errors that
+        // will follow ambiguity errors.
+        let final_ty = self.structurally_resolve_type(autoderef.span(), autoderef.final_ty(false));
+        if let ty::Error(_) = final_ty.kind() {
+            return final_ty;
+        }
 
         if let Some((adjustments, did)) = private_candidate {
             // (#90483) apply adjustments to avoid ExprUseVisitor from
diff --git a/tests/ui/typeck/issue-65611.rs b/tests/ui/typeck/issue-65611.rs
index 7645311496d..0dae75927a8 100644
--- a/tests/ui/typeck/issue-65611.rs
+++ b/tests/ui/typeck/issue-65611.rs
@@ -58,6 +58,5 @@ fn main() {
     let mut buffer = ArrayVec::new();
     let x = buffer.last().unwrap().0.clone();
     //~^ ERROR type annotations needed
-    //~| ERROR no field `0` on type `&_`
     buffer.reverse();
 }
diff --git a/tests/ui/typeck/issue-65611.stderr b/tests/ui/typeck/issue-65611.stderr
index 2278450a6d8..52f0f0cffff 100644
--- a/tests/ui/typeck/issue-65611.stderr
+++ b/tests/ui/typeck/issue-65611.stderr
@@ -4,13 +4,6 @@ error[E0282]: type annotations needed
 LL |     let x = buffer.last().unwrap().0.clone();
    |             ^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
 
-error[E0609]: no field `0` on type `&_`
-  --> $DIR/issue-65611.rs:59:36
-   |
-LL |     let x = buffer.last().unwrap().0.clone();
-   |                                    ^ unknown field
-
-error: aborting due to 2 previous errors
+error: aborting due to 1 previous error
 
-Some errors have detailed explanations: E0282, E0609.
-For more information about an error, try `rustc --explain E0282`.
+For more information about this error, try `rustc --explain E0282`.