Skip to content

Commit b8c4c16

Browse files
Suppress missing field error when autoderef bottoms out in infer
1 parent 0c478fd commit b8c4c16

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2915,8 +2915,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29152915
}
29162916
// We failed to check the expression, report an error.
29172917

2918-
// Emits an error if we deref an infer variable, like calling `.field` on a base type of &_.
2919-
self.structurally_resolve_type(autoderef.span(), autoderef.final_ty(false));
2918+
// Emits an error if we deref an infer variable, like calling `.field` on a base type
2919+
// of `&_`. We can also use this to suppress unnecessary "missing field" errors that
2920+
// will follow ambiguity errors.
2921+
let final_ty = self.structurally_resolve_type(autoderef.span(), autoderef.final_ty(false));
2922+
if let ty::Error(_) = final_ty.kind() {
2923+
return final_ty;
2924+
}
29202925

29212926
if let Some((adjustments, did)) = private_candidate {
29222927
// (#90483) apply adjustments to avoid ExprUseVisitor from

tests/ui/typeck/issue-65611.rs

-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,5 @@ fn main() {
5858
let mut buffer = ArrayVec::new();
5959
let x = buffer.last().unwrap().0.clone();
6060
//~^ ERROR type annotations needed
61-
//~| ERROR no field `0` on type `&_`
6261
buffer.reverse();
6362
}

tests/ui/typeck/issue-65611.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ error[E0282]: type annotations needed
44
LL | let x = buffer.last().unwrap().0.clone();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
66

7-
error[E0609]: no field `0` on type `&_`
8-
--> $DIR/issue-65611.rs:59:36
9-
|
10-
LL | let x = buffer.last().unwrap().0.clone();
11-
| ^ unknown field
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
148

15-
Some errors have detailed explanations: E0282, E0609.
16-
For more information about an error, try `rustc --explain E0282`.
9+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)