@@ -942,17 +942,6 @@ impl<'a> Resolver<'a> {
942942 Some ( suggestion) if suggestion. candidate == kw:: Underscore => return false ,
943943 Some ( suggestion) => suggestion,
944944 } ;
945- let msg = format ! (
946- "{} {} with a similar name exists" ,
947- suggestion. res. article( ) ,
948- suggestion. res. descr( )
949- ) ;
950- err. span_suggestion (
951- span,
952- & msg,
953- suggestion. candidate . to_string ( ) ,
954- Applicability :: MaybeIncorrect ,
955- ) ;
956945 let def_span = suggestion. res . opt_def_id ( ) . and_then ( |def_id| match def_id. krate {
957946 LOCAL_CRATE => self . opt_span ( def_id) ,
958947 _ => Some (
@@ -961,16 +950,48 @@ impl<'a> Resolver<'a> {
961950 . guess_head_span ( self . cstore ( ) . get_span_untracked ( def_id, self . session ) ) ,
962951 ) ,
963952 } ) ;
964- if let Some ( span) = def_span {
953+ if let Some ( def_span) = def_span {
954+ if span. overlaps ( def_span) {
955+ // Don't suggest typo suggestion for itself like in the followoing:
956+ // error[E0423]: expected function, tuple struct or tuple variant, found struct `X`
957+ // --> $DIR/issue-64792-bad-unicode-ctor.rs:3:14
958+ // |
959+ // LL | struct X {}
960+ // | ----------- `X` defined here
961+ // LL |
962+ // LL | const Y: X = X("ö");
963+ // | -------------^^^^^^- similarly named constant `Y` defined here
964+ // |
965+ // help: use struct literal syntax instead
966+ // |
967+ // LL | const Y: X = X {};
968+ // | ^^^^
969+ // help: a constant with a similar name exists
970+ // |
971+ // LL | const Y: X = Y("ö");
972+ // | ^
973+ return false ;
974+ }
965975 err. span_label (
966- self . session . source_map ( ) . guess_head_span ( span ) ,
976+ self . session . source_map ( ) . guess_head_span ( def_span ) ,
967977 & format ! (
968978 "similarly named {} `{}` defined here" ,
969979 suggestion. res. descr( ) ,
970980 suggestion. candidate. as_str( ) ,
971981 ) ,
972982 ) ;
973983 }
984+ let msg = format ! (
985+ "{} {} with a similar name exists" ,
986+ suggestion. res. article( ) ,
987+ suggestion. res. descr( )
988+ ) ;
989+ err. span_suggestion (
990+ span,
991+ & msg,
992+ suggestion. candidate . to_string ( ) ,
993+ Applicability :: MaybeIncorrect ,
994+ ) ;
974995 true
975996 }
976997
0 commit comments