Skip to content

Suppress missing field error when autoderef bottoms out in infer #139423

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 1 commit into from
Apr 10, 2025
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
9 changes: 7 additions & 2 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2915,8 +2915,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
Expand Down
1 change: 0 additions & 1 deletion tests/ui/typeck/issue-65611.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
11 changes: 2 additions & 9 deletions tests/ui/typeck/issue-65611.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Loading