-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing help message when forgetting to use the let
keyword
#132483
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Comments
shubham-93
added
A-diagnostics
Area: Messages for errors, warnings, and lints
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
labels
Nov 2, 2024
shubham-93
changed the title
Missing help message when forgetting to use the let keyword
Missing help message when forgetting to use the Nov 2, 2024
let
keyword
This is the relevant code: rust/compiler/rustc_resolve/src/late/diagnostics.rs Lines 897 to 899 in ef972a3
Making typo and let binding suggestions mutually exclusive seems to be an intentional decision, so I'm not sure whether it's something to be fixed. Reworking the logic yields the following error: error[E0425]: cannot find value `n1` in this scope
--> src/main.rs:3:5
|
3 | n1 = 2;
| ^^
|
help: a local variable with a similar name exists
|
3 | n = 2;
| ~
help: you might have meant to introduce a new binding
|
3 | let n1 = 2;
| +++
For more information about this error, try `rustc --explain E0425`.
error: could not compile `rust-lang-test` (bin "rust-lang-test") due to 1 previous error |
@rustbot claim |
uellenberg
added a commit
to uellenberg/rust
that referenced
this issue
Nov 2, 2024
matthiaskrgr
added a commit
to matthiaskrgr/rust
that referenced
this issue
Nov 5, 2024
…s, 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).
matthiaskrgr
added a commit
to matthiaskrgr/rust
that referenced
this issue
Nov 5, 2024
…s, 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).
rust-timer
added a commit
to rust-lang-ci/rust
that referenced
this issue
Nov 6, 2024
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).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Code
Current output
Desired output
Rationale and extra context
Coming from a Python background, I sometimes forget to use the
let
keyword in Rust when binding a value to a new variable. In such cases, it would be useful to get a help message from the compiler that asks the programmer to do so (help: you might have meant to introduce a new binding
). I noticed that sometimes the compiler does give such a message, but sometimes not. Suppose I want to define two variables and I forget to use thelet
statement for the second variable. After some experimentation, it seems like the desired help message appears after a pattern common to both variable names is matched and the non-matched part has a length that crosses a certain threshold. I will give some examples below. Assume that the first variable's name is never changed, and we tweak the second variable's name slightly to see when the messagehelp: you might have meant to introduce a new binding
is triggered. Note that the other help messagehelp: a local variable with a similar name exists
is always triggered, which is not always helpful/relevant, and is therefore one of the reasons why I raised this issue.abc = 2
does not trigger the message).abcdefghi = 2
does not trigger the message).where the first 2 characters of the 2 variables are flipped (
ab
versusba
). Then, I will start adding one new (and different) character (in alphabetical order) at a time to the second variable's name and compile to see when the help message appears. We see the message in the following cases:ba
,bac
,bacd
,bacde
. Then the message disappears in the following cases:bacdef
,bacdefg
. And then reappears in the casebacdefgh
, etc.data_attribute
. Then they name another variable with a similar name but with an underscore '_' followed by a numerical suffix. They will not get the desired help message unless they append at least 7 digits. So this means thatis the first time the help message appears. The message does not appear when the second variable's name is one of the following:
data_attribute_1
,data_attribute_12
,data_attribute_123
,data_attribute_1234
,data_attribute_12345
,data_attribute_123456
.These are just some random cases I quickly thought of, but perhaps there are more rules to when a message is triggered. The variable names in the examples above (except the last one) are not the best, but I used them to show which events trigger the message. But from especially the last example, one would think that
data_attribute
is completely different from e.g.data_attribute_71
for many use cases, and so the desired help message would be quite useful.Can we not have the message triggered every time a
let
statement is forgotten?Other cases
No response
Rust Version
rustc 1.82.0 (f6e511e 2024-10-15)
binary: rustc
commit-hash: f6e511e
commit-date: 2024-10-15
host: aarch64-apple-darwin
release: 1.82.0
LLVM version: 19.1.1
Anything else?
No response
The text was updated successfully, but these errors were encountered: