Skip to content

Commit 39caed0

Browse files
committed
add typo suggestions for all AssocSuggestion variants
1 parent 42e9f2d commit 39caed0

File tree

3 files changed

+105
-5
lines changed

3 files changed

+105
-5
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,6 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
460460
} else {
461461
err.span_label(span, "a field by this name exists in `Self`");
462462
}
463-
self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
464463
}
465464
AssocSuggestion::MethodWithSelf if self_is_available => {
466465
err.span_suggestion(
@@ -482,6 +481,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
482481
);
483482
}
484483
}
484+
self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
485485
return (err, candidates);
486486
}
487487

src/test/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
struct Foo {
1+
struct A {
22
config: String,
33
}
44

5-
impl Foo {
5+
impl A {
66
fn new(cofig: String) -> Self {
77
Self { config } //~ Error cannot find value `config` in this scope
88
}
@@ -16,4 +16,31 @@ impl Foo {
1616
}
1717
}
1818

19+
trait B {
20+
const BAR: u32 = 3;
21+
type Baz;
22+
fn bar(&self);
23+
fn baz(&self) {}
24+
fn bah() {}
25+
}
26+
27+
impl B for Box<isize> {
28+
type Baz = String;
29+
fn bar(&self) {
30+
// let baz = 3;
31+
baz();
32+
//~^ ERROR cannot find function `baz`
33+
bah;
34+
//~^ ERROR cannot find value `bah`
35+
BAR;
36+
//~^ ERROR cannot find value `BAR` in this scope
37+
let foo: Baz = "".to_string();
38+
//~^ ERROR cannot find type `Baz` in this scope
39+
}
40+
}
41+
42+
fn ba() {}
43+
const BARR: u32 = 3;
44+
type Bar = String;
45+
1946
fn main() {}

src/test/ui/resolve/typo-suggestion-for-variable-with-name-similar-to-struct-field.stderr

+75-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,79 @@ help: a local variable with a similar name exists
3131
LL | println!("{cofig}");
3232
| ~~~~~
3333

34-
error: aborting due to 3 previous errors
34+
error[E0425]: cannot find function `baz` in this scope
35+
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:31:9
36+
|
37+
LL | baz();
38+
| ^^^
39+
...
40+
LL | fn ba() {}
41+
| ------- similarly named function `ba` defined here
42+
|
43+
help: you might have meant to call the method
44+
|
45+
LL | self.baz();
46+
| ~~~~~~~~
47+
help: a function with a similar name exists
48+
|
49+
LL | ba();
50+
| ~~
51+
52+
error[E0425]: cannot find value `bah` in this scope
53+
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:33:9
54+
|
55+
LL | bah;
56+
| ^^^
57+
...
58+
LL | fn ba() {}
59+
| ------- similarly named function `ba` defined here
60+
|
61+
help: you might have meant to call the associated function
62+
|
63+
LL | Self::bah;
64+
| ~~~~~~~~~
65+
help: a function with a similar name exists
66+
|
67+
LL | ba;
68+
| ~~
69+
70+
error[E0425]: cannot find value `BAR` in this scope
71+
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:35:9
72+
|
73+
LL | BAR;
74+
| ^^^
75+
...
76+
LL | const BARR: u32 = 3;
77+
| -------------------- similarly named constant `BARR` defined here
78+
|
79+
help: you might have meant to use the associated `const`
80+
|
81+
LL | Self::BAR;
82+
| ~~~~~~~~~
83+
help: a constant with a similar name exists
84+
|
85+
LL | BARR;
86+
| ~~~~
87+
88+
error[E0412]: cannot find type `Baz` in this scope
89+
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:37:18
90+
|
91+
LL | let foo: Baz = "".to_string();
92+
| ^^^
93+
...
94+
LL | type Bar = String;
95+
| ------------------ similarly named type alias `Bar` defined here
96+
|
97+
help: you might have meant to use the associated type
98+
|
99+
LL | let foo: Self::Baz = "".to_string();
100+
| ~~~~~~~~~
101+
help: a type alias with a similar name exists
102+
|
103+
LL | let foo: Bar = "".to_string();
104+
| ~~~
105+
106+
error: aborting due to 7 previous errors
35107

36-
For more information about this error, try `rustc --explain E0425`.
108+
Some errors have detailed explanations: E0412, E0425.
109+
For more information about an error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)