about summary refs log tree commit diff
diff options
context:
space:
mode:
authorxizheyin <xizheyin@smail.nju.edu.cn>2025-08-13 23:23:18 +0800
committerxizheyin <xizheyin@smail.nju.edu.cn>2025-08-13 23:23:18 +0800
commite0cc2beea3d52ce34b16df9730b65a670c68c623 (patch)
treec407e238e3b5faf672d2433adc2316c9bd24b120
parent12d1665d1163d62de0dd2c2fcb2f508fea8123f1 (diff)
downloadrust-e0cc2beea3d52ce34b16df9730b65a670c68c623.tar.gz
rust-e0cc2beea3d52ce34b16df9730b65a670c68c623.zip
Suppress wrapper suggestion when expected and actual ty are the same adt and the variant is unresolved
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs9
-rw-r--r--tests/ui/typeck/suggestions/suggest-add-wrapper-issue-145294.stderr8
2 files changed, 9 insertions, 8 deletions
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
index 2345cdab208..c44e007dbdb 100644
--- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
+++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
@@ -2378,6 +2378,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 .filter_map(|variant| {
                     let sole_field = &variant.single_field();
 
+                    // When expected_ty and expr_ty are the same ADT, we prefer to compare their internal generic params,
+                    // When the current variant has a sole field whose type is still an unresolved inference variable,
+                    // suggestions would be often wrong. So suppress the suggestion. See #145294.
+                    if let (ty::Adt(exp_adt, _), ty::Adt(act_adt, _)) = (expected.kind(), expr_ty.kind())
+                        && exp_adt.did() == act_adt.did()
+                        && sole_field.ty(self.tcx, args).is_ty_var() {
+                            return None;
+                    }
+
                     let field_is_local = sole_field.did.is_local();
                     let field_is_accessible =
                         sole_field.vis.is_accessible_from(expr.hir_id.owner.def_id, self.tcx)
diff --git a/tests/ui/typeck/suggestions/suggest-add-wrapper-issue-145294.stderr b/tests/ui/typeck/suggestions/suggest-add-wrapper-issue-145294.stderr
index ef5eeb29cf5..5e4ad132210 100644
--- a/tests/ui/typeck/suggestions/suggest-add-wrapper-issue-145294.stderr
+++ b/tests/ui/typeck/suggestions/suggest-add-wrapper-issue-145294.stderr
@@ -6,10 +6,6 @@ LL |     assert_eq!(Ok(Some("hi")), foo());
    |
    = note: expected enum `Result<Option<&str>, _>`
               found enum `Result<Option<String>, ()>`
-help: try wrapping the expression in `Err`
-   |
-LL |     assert_eq!(Ok(Some("hi")), Err(foo()));
-   |                                ++++     +
 
 error[E0308]: mismatched types
   --> $DIR/suggest-add-wrapper-issue-145294.rs:25:30
@@ -19,10 +15,6 @@ LL |     assert_eq!(Bar::A("hi"), bar());
    |
    = note: expected enum `Bar<&str, _>`
               found enum `Bar<String, ()>`
-help: try wrapping the expression in `Bar::B`
-   |
-LL |     assert_eq!(Bar::A("hi"), Bar::B(bar()));
-   |                              +++++++     +
 
 error: aborting due to 2 previous errors