Skip to content

Commit e0cc2be

Browse files
committed
Suppress wrapper suggestion when expected and actual ty are the same adt and the variant is unresolved
Signed-off-by: xizheyin <[email protected]>
1 parent 12d1665 commit e0cc2be

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23782378
.filter_map(|variant| {
23792379
let sole_field = &variant.single_field();
23802380

2381+
// When expected_ty and expr_ty are the same ADT, we prefer to compare their internal generic params,
2382+
// When the current variant has a sole field whose type is still an unresolved inference variable,
2383+
// suggestions would be often wrong. So suppress the suggestion. See #145294.
2384+
if let (ty::Adt(exp_adt, _), ty::Adt(act_adt, _)) = (expected.kind(), expr_ty.kind())
2385+
&& exp_adt.did() == act_adt.did()
2386+
&& sole_field.ty(self.tcx, args).is_ty_var() {
2387+
return None;
2388+
}
2389+
23812390
let field_is_local = sole_field.did.is_local();
23822391
let field_is_accessible =
23832392
sole_field.vis.is_accessible_from(expr.hir_id.owner.def_id, self.tcx)

tests/ui/typeck/suggestions/suggest-add-wrapper-issue-145294.stderr

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ LL | assert_eq!(Ok(Some("hi")), foo());
66
|
77
= note: expected enum `Result<Option<&str>, _>`
88
found enum `Result<Option<String>, ()>`
9-
help: try wrapping the expression in `Err`
10-
|
11-
LL | assert_eq!(Ok(Some("hi")), Err(foo()));
12-
| ++++ +
139

1410
error[E0308]: mismatched types
1511
--> $DIR/suggest-add-wrapper-issue-145294.rs:25:30
@@ -19,10 +15,6 @@ LL | assert_eq!(Bar::A("hi"), bar());
1915
|
2016
= note: expected enum `Bar<&str, _>`
2117
found enum `Bar<String, ()>`
22-
help: try wrapping the expression in `Bar::B`
23-
|
24-
LL | assert_eq!(Bar::A("hi"), Bar::B(bar()));
25-
| +++++++ +
2618

2719
error: aborting due to 2 previous errors
2820

0 commit comments

Comments
 (0)