@@ -942,17 +942,6 @@ impl<'a> Resolver<'a> {
942
942
Some(suggestion) if suggestion.candidate == kw::Underscore => return false,
943
943
Some(suggestion) => suggestion,
944
944
};
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
- );
956
945
let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate {
957
946
LOCAL_CRATE => self.opt_span(def_id),
958
947
_ => Some(
@@ -961,16 +950,48 @@ impl<'a> Resolver<'a> {
961
950
.guess_head_span(self.cstore().get_span_untracked(def_id, self.session)),
962
951
),
963
952
});
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
+ }
965
975
err.span_label(
966
- self.session.source_map().guess_head_span(span ),
976
+ self.session.source_map().guess_head_span(def_span ),
967
977
&format!(
968
978
"similarly named {} `{}` defined here",
969
979
suggestion.res.descr(),
970
980
suggestion.candidate.as_str(),
971
981
),
972
982
);
973
983
}
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
+ );
974
995
true
975
996
}
976
997
0 commit comments