diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-11-05 20:10:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-05 20:10:50 +0100 |
| commit | b23655893e0be24a50a0921e77c638c4f0f75680 (patch) | |
| tree | c56338cbe6f88c61065a73f7866a88cb463d20f8 | |
| parent | 0078e644398015550cc367a15288f4f4b22e44e7 (diff) | |
| parent | 67a85925b14210f5337c30196e1cc15a08686110 (diff) | |
| download | rust-b23655893e0be24a50a0921e77c638c4f0f75680.tar.gz rust-b23655893e0be24a50a0921e77c638c4f0f75680.zip | |
Rollup merge of #132498 - uellenberg:typo-and-let-suggestions, r=estebank
Suggest fixing typos and let bindings at the same time
Fixes #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).
| -rw-r--r-- | compiler/rustc_resolve/src/late/diagnostics.rs | 9 | ||||
| -rw-r--r-- | tests/ui/error-festival.stderr | 11 | ||||
| -rw-r--r-- | tests/ui/suggestions/suggest-let-and-typo-issue-132483.rs | 7 | ||||
| -rw-r--r-- | tests/ui/suggestions/suggest-let-and-typo-issue-132483.stderr | 29 |
4 files changed, 52 insertions, 4 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index f0632a21091..1493d5cc158 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -894,10 +894,13 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { // If the trait has a single item (which wasn't matched by the algorithm), suggest it let suggestion = self.get_single_associated_item(path, &source, is_expected); - if !self.r.add_typo_suggestion(err, suggestion, ident_span) { - fallback = !self.let_binding_suggestion(err, ident_span); - } + self.r.add_typo_suggestion(err, suggestion, ident_span); + } + + if self.let_binding_suggestion(err, ident_span) { + fallback = false; } + fallback } diff --git a/tests/ui/error-festival.stderr b/tests/ui/error-festival.stderr index 26393352b2b..f71fa7e685c 100644 --- a/tests/ui/error-festival.stderr +++ b/tests/ui/error-festival.stderr @@ -2,7 +2,16 @@ error[E0425]: cannot find value `y` in this scope --> $DIR/error-festival.rs:14:5 | LL | y = 2; - | ^ help: a local variable with a similar name exists: `x` + | ^ + | +help: a local variable with a similar name exists + | +LL | x = 2; + | ~ +help: you might have meant to introduce a new binding + | +LL | let y = 2; + | +++ error[E0603]: constant `FOO` is private --> $DIR/error-festival.rs:22:10 diff --git a/tests/ui/suggestions/suggest-let-and-typo-issue-132483.rs b/tests/ui/suggestions/suggest-let-and-typo-issue-132483.rs new file mode 100644 index 00000000000..d56a6b78d37 --- /dev/null +++ b/tests/ui/suggestions/suggest-let-and-typo-issue-132483.rs @@ -0,0 +1,7 @@ +fn main() { + let x1 = 0; + x2 = 1; + //~^ ERROR E0425 + other_val = 2; + //~^ ERROR E0425 +} diff --git a/tests/ui/suggestions/suggest-let-and-typo-issue-132483.stderr b/tests/ui/suggestions/suggest-let-and-typo-issue-132483.stderr new file mode 100644 index 00000000000..c84f9363f03 --- /dev/null +++ b/tests/ui/suggestions/suggest-let-and-typo-issue-132483.stderr @@ -0,0 +1,29 @@ +error[E0425]: cannot find value `x2` in this scope + --> $DIR/suggest-let-and-typo-issue-132483.rs:3:5 + | +LL | x2 = 1; + | ^^ + | +help: a local variable with a similar name exists + | +LL | x1 = 1; + | ~~ +help: you might have meant to introduce a new binding + | +LL | let x2 = 1; + | +++ + +error[E0425]: cannot find value `other_val` in this scope + --> $DIR/suggest-let-and-typo-issue-132483.rs:5:5 + | +LL | other_val = 2; + | ^^^^^^^^^ + | +help: you might have meant to introduce a new binding + | +LL | let other_val = 2; + | +++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0425`. |
