Skip to content

Commit 089810a

Browse files
committed
Do not suggest similarly named enclosing item
1 parent eef284b commit 089810a

8 files changed

+41
-54
lines changed

src/librustc_resolve/diagnostics.rs

+34-13
Original file line numberDiff line numberDiff line change
@@ -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

src/test/ui/const-generics/struct-with-invalid-const-param.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0573]: expected type, found const parameter `C`
22
--> $DIR/struct-with-invalid-const-param.rs:4:23
33
|
44
LL | struct S<const C: u8>(C);
5-
| ----------------------^--
6-
| | |
7-
| | help: a struct with a similar name exists: `S`
8-
| similarly named struct `S` defined here
5+
| ^ not a type
96

107
warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
118
--> $DIR/struct-with-invalid-const-param.rs:1:12

src/test/ui/issues/issue-31845.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0425]: cannot find function `g` in this scope
22
--> $DIR/issue-31845.rs:7:12
33
|
4-
LL | fn h() {
5-
| ------ similarly named function `h` defined here
64
LL | g();
7-
| ^ help: a function with a similar name exists: `h`
5+
| ^ not found in this scope
86

97
error: aborting due to previous error
108

src/test/ui/issues/issue-64792-bad-unicode-ctor.stderr

+1-10
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,7 @@ LL | struct X {}
55
| ----------- `X` defined here
66
LL |
77
LL | const Y: X = X("ö");
8-
| -------------^^^^^^- similarly named constant `Y` defined here
9-
|
10-
help: use struct literal syntax instead
11-
|
12-
LL | const Y: X = X {};
13-
| ^^^^
14-
help: a constant with a similar name exists
15-
|
16-
LL | const Y: X = Y("ö");
17-
| ^
8+
| ^^^^^^ help: use struct literal syntax instead: `X {}`
189

1910
error: aborting due to previous error
2011

src/test/ui/privacy/legacy-ctor-visibility.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0423]: expected function, tuple struct or tuple variant, found struct `S`
22
--> $DIR/legacy-ctor-visibility.rs:9:13
33
|
4-
LL | fn f() {
5-
| ------ similarly named function `f` defined here
64
LL | S(10);
7-
| ^ help: a function with a similar name exists: `f`
5+
| ^
86

97
error: aborting due to previous error
108

src/test/ui/proc-macro/attributes-on-modules-fail.stderr

+2-10
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,8 @@ error[E0412]: cannot find type `Y` in this scope
4444
--> $DIR/attributes-on-modules-fail.rs:10:14
4545
|
4646
LL | type A = Y;
47-
| ---------^- similarly named type alias `A` defined here
47+
| ^ not found in this scope
4848
|
49-
help: a type alias with a similar name exists
50-
|
51-
LL | type A = A;
52-
| ^
5349
help: consider importing this struct
5450
|
5551
LL | use Y;
@@ -59,12 +55,8 @@ error[E0412]: cannot find type `X` in this scope
5955
--> $DIR/attributes-on-modules-fail.rs:14:10
6056
|
6157
LL | type A = X;
62-
| ---------^- similarly named type alias `A` defined here
63-
|
64-
help: a type alias with a similar name exists
58+
| ^ not found in this scope
6559
|
66-
LL | type A = A;
67-
| ^
6860
help: consider importing this struct
6961
|
7062
LL | use m::X;

src/test/ui/resolve/privacy-enum-ctor.stderr

-7
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ LL | m::Z::Unit;
1616
error[E0423]: expected value, found enum `Z`
1717
--> $DIR/privacy-enum-ctor.rs:25:9
1818
|
19-
LL | fn f() {
20-
| ------ similarly named function `f` defined here
21-
...
2219
LL | Z;
2320
| ^
2421
|
@@ -30,10 +27,6 @@ LL | m::Z::Struct;
3027
| ^^^^^^^^^^^^
3128
LL | m::Z::Unit;
3229
| ^^^^^^^^^^
33-
help: a function with a similar name exists
34-
|
35-
LL | f;
36-
| ^
3730

3831
error[E0423]: expected value, found struct variant `Z::Struct`
3932
--> $DIR/privacy-enum-ctor.rs:29:20

src/test/ui/ui-testing-optout.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0412]: cannot find type `B` in this scope
22
--> $DIR/ui-testing-optout.rs:4:10
33
|
44
4 | type A = B;
5-
| ---------^-
6-
| | |
7-
| | help: a type alias with a similar name exists: `A`
8-
| similarly named type alias `A` defined here
5+
| ^ not found in this scope
96

107
error[E0412]: cannot find type `D` in this scope
118
--> $DIR/ui-testing-optout.rs:7:10

0 commit comments

Comments
 (0)