Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0045ef9

Browse files
authoredNov 6, 2024
Unrolled build for rust-lang#132498
Rollup merge of rust-lang#132498 - uellenberg:typo-and-let-suggestions, r=estebank Suggest fixing typos and let bindings at the same time Fixes rust-lang#132483 Currently, a suggestion for adding a let binding won't be shown if we suggest fixing a typo. This changes that behavior to always show both, if possible. Essentially, this turns the suggestion from ```rust error[E0425]: cannot find value `x2` in this scope --> src/main.rs:4:5 | 4 | x2 = 2; | ^^ help: a local variable with a similar name exists: `x1` For more information about this error, try `rustc --explain E0425`. ``` to ```rust error[E0425]: cannot find value `x2` in this scope --> src/main.rs:4:5 | 4 | x2 = 2; | ^^ | help: a local variable with a similar name exists | 4 | x1 = 2; | ~~ help: you might have meant to introduce a new binding | 4 | let x2 = 2; | +++ For more information about this error, try `rustc --explain E0425`. ``` for the following code: ```rust fn main() { let x1 = 1; x2 = 2; } ``` The original behavior only shows the suggestion for a let binding if a typo suggestion wasn't already displayed. However, this falls apart in the cases like the one above where we have multiple similar variables. I don't think it makes sense to hide this suggestion if there's a similar variable, since that defeats the purpose of this suggestion in that case (it's meant to help those coming from languages like Python).
2 parents bc5cf99 + 67a8592 commit 0045ef9

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed
 

‎compiler/rustc_resolve/src/late/diagnostics.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -894,10 +894,13 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
894894

895895
// If the trait has a single item (which wasn't matched by the algorithm), suggest it
896896
let suggestion = self.get_single_associated_item(path, &source, is_expected);
897-
if !self.r.add_typo_suggestion(err, suggestion, ident_span) {
898-
fallback = !self.let_binding_suggestion(err, ident_span);
899-
}
897+
self.r.add_typo_suggestion(err, suggestion, ident_span);
898+
}
899+
900+
if self.let_binding_suggestion(err, ident_span) {
901+
fallback = false;
900902
}
903+
901904
fallback
902905
}
903906

‎tests/ui/error-festival.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ error[E0425]: cannot find value `y` in this scope
22
--> $DIR/error-festival.rs:14:5
33
|
44
LL | y = 2;
5-
| ^ help: a local variable with a similar name exists: `x`
5+
| ^
6+
|
7+
help: a local variable with a similar name exists
8+
|
9+
LL | x = 2;
10+
| ~
11+
help: you might have meant to introduce a new binding
12+
|
13+
LL | let y = 2;
14+
| +++
615

716
error[E0603]: constant `FOO` is private
817
--> $DIR/error-festival.rs:22:10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let x1 = 0;
3+
x2 = 1;
4+
//~^ ERROR E0425
5+
other_val = 2;
6+
//~^ ERROR E0425
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0425]: cannot find value `x2` in this scope
2+
--> $DIR/suggest-let-and-typo-issue-132483.rs:3:5
3+
|
4+
LL | x2 = 1;
5+
| ^^
6+
|
7+
help: a local variable with a similar name exists
8+
|
9+
LL | x1 = 1;
10+
| ~~
11+
help: you might have meant to introduce a new binding
12+
|
13+
LL | let x2 = 1;
14+
| +++
15+
16+
error[E0425]: cannot find value `other_val` in this scope
17+
--> $DIR/suggest-let-and-typo-issue-132483.rs:5:5
18+
|
19+
LL | other_val = 2;
20+
| ^^^^^^^^^
21+
|
22+
help: you might have meant to introduce a new binding
23+
|
24+
LL | let other_val = 2;
25+
| +++
26+
27+
error: aborting due to 2 previous errors
28+
29+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)
Failed to load comments.