diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 9502be728de..de92204a7c2 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -942,6 +942,45 @@ fn lookup_import_candidates_from_module( Some(suggestion) if suggestion.candidate == kw::Underscore => return false, Some(suggestion) => suggestion, }; + let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate { + LOCAL_CRATE => self.opt_span(def_id), + _ => Some( + self.session + .source_map() + .guess_head_span(self.cstore().get_span_untracked(def_id, self.session)), + ), + }); + if let Some(def_span) = def_span { + if span.overlaps(def_span) { + // Don't suggest typo suggestion for itself like in the followoing: + // error[E0423]: expected function, tuple struct or tuple variant, found struct `X` + // --> $DIR/issue-64792-bad-unicode-ctor.rs:3:14 + // | + // LL | struct X {} + // | ----------- `X` defined here + // LL | + // LL | const Y: X = X("ö"); + // | -------------^^^^^^- similarly named constant `Y` defined here + // | + // help: use struct literal syntax instead + // | + // LL | const Y: X = X {}; + // | ^^^^ + // help: a constant with a similar name exists + // | + // LL | const Y: X = Y("ö"); + // | ^ + return false; + } + err.span_label( + self.session.source_map().guess_head_span(def_span), + &format!( + "similarly named {} `{}` defined here", + suggestion.res.descr(), + suggestion.candidate.as_str(), + ), + ); + } let msg = format!( "{} {} with a similar name exists", suggestion.res.article(), @@ -953,24 +992,6 @@ fn lookup_import_candidates_from_module( suggestion.candidate.to_string(), Applicability::MaybeIncorrect, ); - let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate { - LOCAL_CRATE => self.opt_span(def_id), - _ => Some( - self.session - .source_map() - .guess_head_span(self.cstore().get_span_untracked(def_id, self.session)), - ), - }); - if let Some(span) = def_span { - err.span_label( - self.session.source_map().guess_head_span(span), - &format!( - "similarly named {} `{}` defined here", - suggestion.res.descr(), - suggestion.candidate.as_str(), - ), - ); - } true } diff --git a/src/test/ui/const-generics/struct-with-invalid-const-param.stderr b/src/test/ui/const-generics/struct-with-invalid-const-param.stderr index a968b26bc26..47617c7849f 100644 --- a/src/test/ui/const-generics/struct-with-invalid-const-param.stderr +++ b/src/test/ui/const-generics/struct-with-invalid-const-param.stderr @@ -2,10 +2,7 @@ error[E0573]: expected type, found const parameter `C` --> $DIR/struct-with-invalid-const-param.rs:4:23 | LL | struct S(C); - | ----------------------^-- - | | | - | | help: a struct with a similar name exists: `S` - | similarly named struct `S` defined here + | ^ not a type warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes --> $DIR/struct-with-invalid-const-param.rs:1:12 diff --git a/src/test/ui/issues/issue-31845.stderr b/src/test/ui/issues/issue-31845.stderr index fe51fa0699f..56281938559 100644 --- a/src/test/ui/issues/issue-31845.stderr +++ b/src/test/ui/issues/issue-31845.stderr @@ -1,10 +1,8 @@ error[E0425]: cannot find function `g` in this scope --> $DIR/issue-31845.rs:7:12 | -LL | fn h() { - | ------ similarly named function `h` defined here LL | g(); - | ^ help: a function with a similar name exists: `h` + | ^ not found in this scope error: aborting due to previous error diff --git a/src/test/ui/issues/issue-64792-bad-unicode-ctor.stderr b/src/test/ui/issues/issue-64792-bad-unicode-ctor.stderr index aa30a7e0d64..c3dda704b0e 100644 --- a/src/test/ui/issues/issue-64792-bad-unicode-ctor.stderr +++ b/src/test/ui/issues/issue-64792-bad-unicode-ctor.stderr @@ -5,16 +5,7 @@ LL | struct X {} | ----------- `X` defined here LL | LL | const Y: X = X("ö"); - | -------------^^^^^^- similarly named constant `Y` defined here - | -help: use struct literal syntax instead - | -LL | const Y: X = X {}; - | ^^^^ -help: a constant with a similar name exists - | -LL | const Y: X = Y("ö"); - | ^ + | ^^^^^^ help: use struct literal syntax instead: `X {}` error: aborting due to previous error diff --git a/src/test/ui/privacy/legacy-ctor-visibility.stderr b/src/test/ui/privacy/legacy-ctor-visibility.stderr index 4f0d72de6f1..c8057d85ed1 100644 --- a/src/test/ui/privacy/legacy-ctor-visibility.stderr +++ b/src/test/ui/privacy/legacy-ctor-visibility.stderr @@ -1,10 +1,8 @@ error[E0423]: expected function, tuple struct or tuple variant, found struct `S` --> $DIR/legacy-ctor-visibility.rs:9:13 | -LL | fn f() { - | ------ similarly named function `f` defined here LL | S(10); - | ^ help: a function with a similar name exists: `f` + | ^ error: aborting due to previous error diff --git a/src/test/ui/proc-macro/attributes-on-modules-fail.stderr b/src/test/ui/proc-macro/attributes-on-modules-fail.stderr index b37f1bd393c..97b2f22e161 100644 --- a/src/test/ui/proc-macro/attributes-on-modules-fail.stderr +++ b/src/test/ui/proc-macro/attributes-on-modules-fail.stderr @@ -44,12 +44,8 @@ error[E0412]: cannot find type `Y` in this scope --> $DIR/attributes-on-modules-fail.rs:10:14 | LL | type A = Y; - | ---------^- similarly named type alias `A` defined here + | ^ not found in this scope | -help: a type alias with a similar name exists - | -LL | type A = A; - | ^ help: consider importing this struct | LL | use Y; @@ -59,12 +55,8 @@ error[E0412]: cannot find type `X` in this scope --> $DIR/attributes-on-modules-fail.rs:14:10 | LL | type A = X; - | ---------^- similarly named type alias `A` defined here + | ^ not found in this scope | -help: a type alias with a similar name exists - | -LL | type A = A; - | ^ help: consider importing this struct | LL | use m::X; diff --git a/src/test/ui/resolve/privacy-enum-ctor.stderr b/src/test/ui/resolve/privacy-enum-ctor.stderr index f3cc338f13c..f1ed7aaa867 100644 --- a/src/test/ui/resolve/privacy-enum-ctor.stderr +++ b/src/test/ui/resolve/privacy-enum-ctor.stderr @@ -16,9 +16,6 @@ LL | m::Z::Unit; error[E0423]: expected value, found enum `Z` --> $DIR/privacy-enum-ctor.rs:25:9 | -LL | fn f() { - | ------ similarly named function `f` defined here -... LL | Z; | ^ | @@ -30,10 +27,6 @@ LL | m::Z::Struct; | ^^^^^^^^^^^^ LL | m::Z::Unit; | ^^^^^^^^^^ -help: a function with a similar name exists - | -LL | f; - | ^ error[E0423]: expected value, found struct variant `Z::Struct` --> $DIR/privacy-enum-ctor.rs:29:20 diff --git a/src/test/ui/ui-testing-optout.stderr b/src/test/ui/ui-testing-optout.stderr index f562bb74c11..652c472c0bc 100644 --- a/src/test/ui/ui-testing-optout.stderr +++ b/src/test/ui/ui-testing-optout.stderr @@ -2,10 +2,7 @@ error[E0412]: cannot find type `B` in this scope --> $DIR/ui-testing-optout.rs:4:10 | 4 | type A = B; - | ---------^- - | | | - | | help: a type alias with a similar name exists: `A` - | similarly named type alias `A` defined here + | ^ not found in this scope error[E0412]: cannot find type `D` in this scope --> $DIR/ui-testing-optout.rs:7:10