Skip to content

Commit d00c7e7

Browse files
committed
Point at field definition when unresolved name exists in Self
1 parent 81bca5f commit d00c7e7

6 files changed

+39
-21
lines changed

Diff for: compiler/rustc_resolve/src/late/diagnostics.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type Res = def::Res<ast::NodeId>;
4141

4242
/// A field or associated item from self type suggested in case of resolution failure.
4343
enum AssocSuggestion {
44-
Field,
44+
Field(Span),
4545
MethodWithSelf { called: bool },
4646
AssocFn { called: bool },
4747
AssocType,
@@ -51,7 +51,7 @@ enum AssocSuggestion {
5151
impl AssocSuggestion {
5252
fn action(&self) -> &'static str {
5353
match self {
54-
AssocSuggestion::Field => "use the available field",
54+
AssocSuggestion::Field(_) => "use the available field",
5555
AssocSuggestion::MethodWithSelf { called: true } => {
5656
"call the method with the fully-qualified path"
5757
}
@@ -670,7 +670,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
670670
_ => String::new(),
671671
};
672672
match candidate {
673-
AssocSuggestion::Field => {
673+
AssocSuggestion::Field(field_span) => {
674674
if self_is_available {
675675
err.span_suggestion_verbose(
676676
span.shrink_to_lo(),
@@ -679,7 +679,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
679679
Applicability::MachineApplicable,
680680
);
681681
} else {
682-
err.span_label(span, "a field by this name exists in `Self`");
682+
err.span_label(field_span, "a field by that name exists in `Self`");
683683
}
684684
}
685685
AssocSuggestion::MethodWithSelf { called } if self_is_available => {
@@ -1715,11 +1715,11 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17151715
resolution.full_res()
17161716
{
17171717
if let Some(field_ids) = self.r.field_def_ids(did) {
1718-
if field_ids
1718+
if let Some(field_id) = field_ids
17191719
.iter()
1720-
.any(|&field_id| ident.name == self.r.tcx.item_name(field_id))
1720+
.find(|&&field_id| ident.name == self.r.tcx.item_name(field_id))
17211721
{
1722-
return Some(AssocSuggestion::Field);
1722+
return Some(AssocSuggestion::Field(self.r.def_span(*field_id)));
17231723
}
17241724
}
17251725
}

Diff for: tests/ui/resolve/field-and-method-in-self-not-available-in-assoc-fn.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
error[E0425]: cannot find value `field` in this scope
22
--> $DIR/field-and-method-in-self-not-available-in-assoc-fn.rs:11:9
33
|
4+
LL | field: u32,
5+
| ---------- a field by that name exists in `Self`
6+
...
47
LL | fn field(&self) -> u32 {
58
| ----- a method by that name is available on `Self` here
69
...
710
LL | field;
8-
| ^^^^^ a field by this name exists in `Self`
11+
| ^^^^^
912

1013
error[E0425]: cannot find value `field` in this scope
1114
--> $DIR/field-and-method-in-self-not-available-in-assoc-fn.rs:12:15
1215
|
16+
LL | field: u32,
17+
| ---------- a field by that name exists in `Self`
18+
...
1319
LL | fn field(&self) -> u32 {
1420
| ----- a method by that name is available on `Self` here
1521
...
1622
LL | Foo { field }
17-
| ^^^^^ a field by this name exists in `Self`
23+
| ^^^^^
1824

1925
error[E0425]: cannot find value `field` in this scope
2026
--> $DIR/field-and-method-in-self-not-available-in-assoc-fn.rs:15:15

Diff for: tests/ui/resolve/issue-2356.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
error[E0425]: cannot find value `whiskers` in this scope
22
--> $DIR/issue-2356.rs:39:5
33
|
4+
LL | whiskers: isize,
5+
| --------------- a field by that name exists in `Self`
6+
...
47
LL | whiskers -= other;
5-
| ^^^^^^^^ a field by this name exists in `Self`
8+
| ^^^^^^^^
69

710
error[E0424]: expected value, found module `self`
811
--> $DIR/issue-2356.rs:65:8
@@ -31,8 +34,11 @@ LL | self.whiskers = 0;
3134
error[E0425]: cannot find value `whiskers` in this scope
3235
--> $DIR/issue-2356.rs:84:5
3336
|
37+
LL | whiskers: isize,
38+
| --------------- a field by that name exists in `Self`
39+
...
3440
LL | whiskers = 4;
35-
| ^^^^^^^^ a field by this name exists in `Self`
41+
| ^^^^^^^^
3642

3743
error[E0424]: expected value, found module `self`
3844
--> $DIR/issue-2356.rs:92:5

Diff for: tests/ui/resolve/issue-60057.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
error[E0425]: cannot find value `banana` in this scope
22
--> $DIR/issue-60057.rs:8:21
33
|
4+
LL | banana: u8,
5+
| ---------- a field by that name exists in `Self`
6+
...
47
LL | banana: banana
5-
| ^^^^^^ a field by this name exists in `Self`
8+
| ^^^^^^
69

710
error[E0425]: cannot find value `banana` in this scope
811
--> $DIR/issue-60057.rs:14:21

Diff for: tests/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
error[E0425]: cannot find value `config` in this scope
22
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:7:16
33
|
4+
LL | config: String,
5+
| -------------- a field by that name exists in `Self`
6+
...
47
LL | Self { config }
5-
| ^^^^^^
6-
| |
7-
| a field by this name exists in `Self`
8-
| help: a local variable with a similar name exists: `cofig`
8+
| ^^^^^^ help: a local variable with a similar name exists: `cofig`
99

1010
error[E0425]: cannot find value `config` in this scope
1111
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:11:20
1212
|
13+
LL | config: String,
14+
| -------------- a field by that name exists in `Self`
15+
...
1316
LL | println!("{config}");
14-
| ^^^^^^
15-
| |
16-
| a field by this name exists in `Self`
17-
| help: a local variable with a similar name exists: `cofig`
17+
| ^^^^^^ help: a local variable with a similar name exists: `cofig`
1818

1919
error[E0425]: cannot find value `config` in this scope
2020
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:15:20

Diff for: tests/ui/resolve/unresolved_static_type_field.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
error[E0425]: cannot find value `cx` in this scope
22
--> $DIR/unresolved_static_type_field.rs:9:11
33
|
4+
LL | cx: bool,
5+
| -------- a field by that name exists in `Self`
6+
...
47
LL | f(cx);
5-
| ^^ a field by this name exists in `Self`
8+
| ^^
69

710
error: aborting due to previous error
811

0 commit comments

Comments
 (0)