Skip to content
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

Confusing compiler suggestion with type/variable with underscore prefix #125650

Closed
Patiga opened this issue May 28, 2024 · 0 comments · Fixed by #125795
Closed

Confusing compiler suggestion with type/variable with underscore prefix #125650

Patiga opened this issue May 28, 2024 · 0 comments · Fixed by #125795
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

@Patiga
Copy link
Contributor

Patiga commented May 28, 2024

Code

fn a(_p: i32) {
    let _ = p;
}

Current output

error[E0425]: cannot find value `p` in this scope
 --> <source>:2:13
  |
2 |     let _ = p;
  |             ^
  |
help: a local variable with a similar name exists, consider renaming `_p` into `p`
  |
1 | fn a(p: i32) {
  |      ~

Desired output

error[E0425]: cannot find value `p` in this scope
 --> <source>:2:13
  |
2 |     let _ = p;
  |             ^ help: a local variable with a similar name exists: `_p`
  |
help: The leading underscore in `_p` marks it as unused, consider changing its name to `p`
  |
1 | fn a(_p: i32) {
  |      --

Rationale and extra context

Compiler explorer link: https://godbolt.org/z/heYWW481h

This kind of diagnostic happens when you have a variable or type name with an underscore prefix.
When you try to access the variable/type without the underscore, the compiler tries to get the programmer to change the name of the variable/type, instead of the usage of it.

The compiler output was improved in 1.78 with #121776/#121792 by @GuillaumeGomez.

I personally find the current output still confusing. It shows the suggested improvement of changing the type/variable name, without showing the original code. As the compiler tells me about an error in the usage, I expect the other code it shows me to be unmodified.

You also have to read very closely that it wants you to change the variable, and not the usage. As the original error is in the line with the usage, I expected Rust to tell me how to correct that line. To be fair, I was on rustc version 1.77, but the error messages I am talking about here are from 1.78, for which I technically don't have first-hand-experience.

Other cases

No response

Rust Version

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2

Anything else?

No response

@Patiga Patiga 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 May 28, 2024
@bors bors closed this as completed in 7e5528f Jun 4, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 4, 2024
Rollup merge of rust-lang#125795 - lucasscharenbroch:undescore-prefix-suggestion, r=compiler-errors

Improve renaming suggestion for names with leading underscores

Fixes rust-lang#125650

Before:
```
error[E0425]: cannot find value `p` in this scope
 --> test.rs:2:13
  |
2 |     let _ = p;
  |             ^
  |
help: a local variable with a similar name exists, consider renaming `_p` into `p`
  |
1 | fn a(p: i32) {
  |      ~
```

After:
```
error[E0425]: cannot find value `p` in this scope
 --> test.rs:2:13
  |
1 | fn a(_p: i32) {
  |      -- `_p` defined here
2 |     let _ = p;
  |             ^
  |
help: the leading underscore in `_p` marks it as unused, consider renaming it to `p`
  |
1 | fn a(p: i32) {
  |      ~
```

This change doesn't exactly conform to what was proposed in the issue:

1. I've kept the suggested code instead of solely replacing it with the label
2. I've removed the "...similar name exists..." message instead of relocating to the usage span
3. You could argue that it still isn't completely clear that the change is referring to the definition (not the usage), but I'm not sure how to do this without playing down the fact that the error was caused by the usage of an undefined name.
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant