Skip to content

Commit 8d9f258

Browse files
authored
Rollup merge of #97240 - TaKO8Ki:improve-errors-about-typos-on-variables, r=compiler-errors
Typo suggestion for a variable with a name similar to struct fields closes #97133
2 parents ee160f2 + 39caed0 commit 8d9f258

File tree

3 files changed

+163
-7
lines changed

3 files changed

+163
-7
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
445445
);
446446
}
447447
}
448+
// Try Levenshtein algorithm.
449+
let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected);
448450
if path.len() == 1 && self.self_type_is_available() {
449451
if let Some(candidate) = self.lookup_assoc_candidate(ident, ns, is_expected) {
450452
let self_is_available = self.self_value_is_available(path[0].ident.span);
@@ -454,7 +456,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
454456
err.span_suggestion(
455457
span,
456458
"you might have meant to use the available field",
457-
format!("self.{}", path_str),
459+
format!("self.{path_str}"),
458460
Applicability::MachineApplicable,
459461
);
460462
} else {
@@ -465,7 +467,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
465467
err.span_suggestion(
466468
span,
467469
"you might have meant to call the method",
468-
format!("self.{}", path_str),
470+
format!("self.{path_str}"),
469471
Applicability::MachineApplicable,
470472
);
471473
}
@@ -476,11 +478,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
476478
err.span_suggestion(
477479
span,
478480
&format!("you might have meant to {}", candidate.action()),
479-
format!("Self::{}", path_str),
481+
format!("Self::{path_str}"),
480482
Applicability::MachineApplicable,
481483
);
482484
}
483485
}
486+
self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span);
484487
return (err, candidates);
485488
}
486489

@@ -495,16 +498,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
495498

496499
err.span_suggestion(
497500
call_span,
498-
&format!("try calling `{}` as a method", ident),
499-
format!("self.{}({})", path_str, args_snippet),
501+
&format!("try calling `{ident}` as a method"),
502+
format!("self.{path_str}({args_snippet})"),
500503
Applicability::MachineApplicable,
501504
);
502505
return (err, candidates);
503506
}
504507
}
505508

506-
// Try Levenshtein algorithm.
507-
let typo_sugg = self.lookup_typo_candidate(path, ns, is_expected);
508509
// Try context-dependent help if relaxed lookup didn't work.
509510
if let Some(res) = res {
510511
if self.smart_resolve_context_dependent_help(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
struct A {
2+
config: String,
3+
}
4+
5+
impl A {
6+
fn new(cofig: String) -> Self {
7+
Self { config } //~ Error cannot find value `config` in this scope
8+
}
9+
10+
fn do_something(cofig: String) {
11+
println!("{config}"); //~ Error cannot find value `config` in this scope
12+
}
13+
14+
fn self_is_available(self, cofig: String) {
15+
println!("{config}"); //~ Error cannot find value `config` in this scope
16+
}
17+
}
18+
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+
46+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
error[E0425]: cannot find value `config` in this scope
2+
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:7:16
3+
|
4+
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`
9+
10+
error[E0425]: cannot find value `config` in this scope
11+
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:11:20
12+
|
13+
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`
18+
19+
error[E0425]: cannot find value `config` in this scope
20+
--> $DIR/typo-suggestion-for-variable-with-name-similar-to-struct-field.rs:15:20
21+
|
22+
LL | println!("{config}");
23+
| ^^^^^^
24+
|
25+
help: you might have meant to use the available field
26+
|
27+
LL | println!("{self.config}");
28+
| ~~~~~~~~~~~
29+
help: a local variable with a similar name exists
30+
|
31+
LL | println!("{cofig}");
32+
| ~~~~~
33+
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
107+
108+
Some errors have detailed explanations: E0412, E0425.
109+
For more information about an error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)