Do not suggest similarly named enclosing item

This commit is contained in:
Esteban Küber 2020-08-09 21:02:58 -07:00
parent eef284be59
commit 089810a1cb
8 changed files with 46 additions and 59 deletions

View File

@ -942,6 +942,45 @@ fn lookup_import_candidates_from_module<FilterFn>(
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<FilterFn>(
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
}

View File

@ -2,10 +2,7 @@ error[E0573]: expected type, found const parameter `C`
--> $DIR/struct-with-invalid-const-param.rs:4:23
|
LL | struct S<const C: u8>(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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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