Skip to content

Commit df13600

Browse files
authored
Rollup merge of rust-lang#60061 - estebank:field-sugg, r=davidtwco
Change suggestion of field when not in self context Fix rust-lang#60057.
2 parents 9eced3a + 9daeaa8 commit df13600

9 files changed

+60
-30
lines changed

src/librustc_resolve/diagnostics.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,18 @@ impl<'a> Resolver<'a> {
166166
let self_is_available = self.self_value_is_available(path[0].ident.span, span);
167167
match candidate {
168168
AssocSuggestion::Field => {
169-
err.span_suggestion(
170-
span,
171-
"try",
172-
format!("self.{}", path_str),
173-
Applicability::MachineApplicable,
174-
);
175-
if !self_is_available {
176-
err.span_label(span, format!("`self` value is a keyword \
177-
only available in \
178-
methods with `self` parameter"));
169+
if self_is_available {
170+
err.span_suggestion(
171+
span,
172+
"you might have meant to use the available field",
173+
format!("self.{}", path_str),
174+
Applicability::MachineApplicable,
175+
);
176+
} else {
177+
err.span_label(
178+
span,
179+
"a field by this name exists in `Self`",
180+
);
179181
}
180182
}
181183
AssocSuggestion::MethodWithSelf if self_is_available => {

src/test/ui/class-missing-self.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0425]: cannot find value `meows` in this scope
22
--> $DIR/class-missing-self.rs:9:7
33
|
44
LL | meows += 1;
5-
| ^^^^^ help: try: `self.meows`
5+
| ^^^^^ help: you might have meant to use the available field: `self.meows`
66

77
error[E0425]: cannot find function `sleep` in this scope
88
--> $DIR/class-missing-self.rs:10:7

src/test/ui/issues/issue-60057.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
struct A {
2+
banana: u8,
3+
}
4+
5+
impl A {
6+
fn new(peach: u8) -> A {
7+
A {
8+
banana: banana //~ ERROR cannot find value `banana` in this scope
9+
}
10+
}
11+
12+
fn foo(&self, peach: u8) -> A {
13+
A {
14+
banana: banana //~ ERROR cannot find value `banana` in this scope
15+
}
16+
}
17+
}

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

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0425]: cannot find value `banana` in this scope
2+
--> $DIR/issue-60057.rs:8:21
3+
|
4+
LL | banana: banana
5+
| ^^^^^^ a field by this name exists in `Self`
6+
7+
error[E0425]: cannot find value `banana` in this scope
8+
--> $DIR/issue-60057.rs:14:21
9+
|
10+
LL | banana: banana
11+
| ^^^^^^ help: you might have meant to use the available field: `self.banana`
12+
13+
error[E0601]: `main` function not found in crate `issue_60057`
14+
|
15+
= note: consider adding a `main` function to `$DIR/issue-60057.rs`
16+
17+
error: aborting due to 3 previous errors
18+
19+
Some errors occurred: E0425, E0601.
20+
For more information about an error, try `rustc --explain E0425`.

src/test/ui/resolve/issue-14254.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ error[E0425]: cannot find value `x` in this scope
2020
--> $DIR/issue-14254.rs:30:9
2121
|
2222
LL | x;
23-
| ^ help: try: `self.x`
23+
| ^ help: you might have meant to use the available field: `self.x`
2424

2525
error[E0425]: cannot find value `y` in this scope
2626
--> $DIR/issue-14254.rs:32:9
2727
|
2828
LL | y;
29-
| ^ help: try: `self.y`
29+
| ^ help: you might have meant to use the available field: `self.y`
3030

3131
error[E0425]: cannot find value `a` in this scope
3232
--> $DIR/issue-14254.rs:34:9
@@ -56,13 +56,13 @@ error[E0425]: cannot find value `x` in this scope
5656
--> $DIR/issue-14254.rs:47:9
5757
|
5858
LL | x;
59-
| ^ help: try: `self.x`
59+
| ^ help: you might have meant to use the available field: `self.x`
6060

6161
error[E0425]: cannot find value `y` in this scope
6262
--> $DIR/issue-14254.rs:49:9
6363
|
6464
LL | y;
65-
| ^ help: try: `self.y`
65+
| ^ help: you might have meant to use the available field: `self.y`
6666

6767
error[E0425]: cannot find value `a` in this scope
6868
--> $DIR/issue-14254.rs:51:9

src/test/ui/resolve/issue-2356.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ error[E0425]: cannot find value `whiskers` in this scope
2020
--> $DIR/issue-2356.rs:39:5
2121
|
2222
LL | whiskers -= other;
23-
| ^^^^^^^^
24-
| |
25-
| `self` value is a keyword only available in methods with `self` parameter
26-
| help: try: `self.whiskers`
23+
| ^^^^^^^^ a field by this name exists in `Self`
2724

2825
error[E0425]: cannot find function `shave` in this scope
2926
--> $DIR/issue-2356.rs:41:5
@@ -83,16 +80,13 @@ error[E0425]: cannot find value `whiskers` in this scope
8380
--> $DIR/issue-2356.rs:79:5
8481
|
8582
LL | whiskers = 0;
86-
| ^^^^^^^^ help: try: `self.whiskers`
83+
| ^^^^^^^^ help: you might have meant to use the available field: `self.whiskers`
8784

8885
error[E0425]: cannot find value `whiskers` in this scope
8986
--> $DIR/issue-2356.rs:84:5
9087
|
9188
LL | whiskers = 4;
92-
| ^^^^^^^^
93-
| |
94-
| `self` value is a keyword only available in methods with `self` parameter
95-
| help: try: `self.whiskers`
89+
| ^^^^^^^^ a field by this name exists in `Self`
9690

9791
error[E0425]: cannot find function `purr_louder` in this scope
9892
--> $DIR/issue-2356.rs:86:5

src/test/ui/resolve/resolve-assoc-suggestions.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0425]: cannot find value `field` in this scope
1414
--> $DIR/resolve-assoc-suggestions.rs:20:9
1515
|
1616
LL | field;
17-
| ^^^^^ help: try: `self.field`
17+
| ^^^^^ help: you might have meant to use the available field: `self.field`
1818

1919
error[E0412]: cannot find type `Type` in this scope
2020
--> $DIR/resolve-assoc-suggestions.rs:23:16

src/test/ui/resolve/resolve-speculative-adjustment.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0425]: cannot find value `field` in this scope
1414
--> $DIR/resolve-speculative-adjustment.rs:23:9
1515
|
1616
LL | field;
17-
| ^^^^^ help: try: `self.field`
17+
| ^^^^^ help: you might have meant to use the available field: `self.field`
1818

1919
error[E0425]: cannot find function `method` in this scope
2020
--> $DIR/resolve-speculative-adjustment.rs:25:9

src/test/ui/resolve/unresolved_static_type_field.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0425]: cannot find value `cx` in this scope
22
--> $DIR/unresolved_static_type_field.rs:9:11
33
|
44
LL | f(cx);
5-
| ^^
6-
| |
7-
| `self` value is a keyword only available in methods with `self` parameter
8-
| help: try: `self.cx`
5+
| ^^ a field by this name exists in `Self`
96

107
error: aborting due to previous error
118

0 commit comments

Comments
 (0)