Skip to content

bad diagnostic suggestion for self access in a format string #105520

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

Open
ehuss opened this issue Dec 10, 2022 · 4 comments
Open

bad diagnostic suggestion for self access in a format string #105520

ehuss opened this issue Dec 10, 2022 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-fmt Area: `core::fmt` A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name/path resolution done by `rustc_resolve` specifically A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ehuss
Copy link
Contributor

ehuss commented Dec 10, 2022

Given the following code:

struct Foo {
    value: i32
}

impl Foo {
    fn f(&self) -> String {
        format!("{value}")
    }
}

The current output is:

error[E0425]: cannot find value `value` in this scope
 --> src/lib.rs:7:19
  |
7 |         format!("{value}")
  |                   ^^^^^ help: you might have meant to use the available field: `self.value`

Applying the suggestion doesn't work since format strings don't allow non-identifier expressions. Fortunately applying the suggestion and compiling again will lead to another suggestion with the correct syntax. Ideally the original diagnostic should provide the correct syntax of format!("{}", self.value)

@ehuss ehuss 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. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Dec 10, 2022
@chenyukang
Copy link
Member

Similar issue with #104884

@estebank
We may have bad diagnostic suggestions for macro code, this may happened in many similar reports.
Do we have some centralized way to handle it well?

@estebank
Copy link
Contributor

@chenyukang for macros in general we do get quite a few issues around spans, particularly for bindings that go through some transformation inside the macro (like borrowing) making us point at the whole macro instead of the binding directly. For this case in particular, we can focus only on the behavior of the format! macros. I believe fixing #96999 would be enough to mitigate the issue here, but it should still be fixed independently (not suggest self.value, instead suggest adding a new named arg).

@fmease fmease added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-fmt Area: `core::fmt` A-resolve Area: Name/path resolution done by `rustc_resolve` specifically labels Mar 30, 2024
@jendrikw
Copy link
Contributor

Current output is even more explicit about the wrong suggestion.

error[E0425]: cannot find value `value` in this scope
  --> src/lib.rs:39:52
  |
7 |         format!("{value}")
  |                   ^^^^^
  |
help: you might have meant to use the available field
  |
7 |         format!("{self.value}")
  |                   +++++

@estebank
Copy link
Contributor

@jendrikw do note that when writing that you get

error: invalid format string: field access isn't supported
 --> src/lib.rs:9:19
  |
9 |         format!("{self.value}")
  |                   ^^^^^^^^^^ not supported in format string
  |
help: consider using a positional formatting argument instead
  |
9 |         format!("{0}", self.value)
  |                   ~  ++++++++++++

We consider suggestions that get you towards working code to be fine. We do fix invalid suggestions when possible, but if there are technical reasons that make fixing a suggestion that gets you a subsequent correct suggestion, we allow ourselves to be incorrect in the short term.

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 A-fmt Area: `core::fmt` A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name/path resolution done by `rustc_resolve` specifically A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants