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

Diagnostic for #[cfg(ident=ident)] fails to suggest #[cfg(ident="string")] #58462

Closed
pnkfelix opened this issue Feb 14, 2019 · 1 comment · Fixed by #119341
Closed

Diagnostic for #[cfg(ident=ident)] fails to suggest #[cfg(ident="string")] #58462

pnkfelix opened this issue Feb 14, 2019 · 1 comment · Fixed by #119341
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Feb 14, 2019

Consider the following code (play):

fn main() {
    #[cfg(key=whoops)]
    println!("wont compile");
    
    #[cfg(key="correct syntax")]
    println!("lets make the diagnostic better");
}

On nightly today, this errors (correctly), but gives the diagnostic feedback:

error: expected unsuffixed literal or identifier, found whoops
 --> src/main.rs:2:11
  |
2 |     #[cfg(key=whoops)]
  |           ^^^

This highlight is totally misleading and the message is not great either. The problem in the above input is not the identifier key in the left-hand side of the predicate; it is the use of an identifier for the right-hand side of the predicate. We should be highlighting the right-hand side and probably also suggesting they turn it into a string.

Also, the fact that we say "expected unsuffixed literal or identifier, found whoops", where "whoops" is an identifier, is also very confusing in the above.

I'll also note that the behavior here shifted between the current stable+beta and nightly; on beta the diagnostic is not quite as actively misleading as the above, but it isn't really helpful either:

error: `cfg` is not a well-formed meta-item
 --> src/main.rs:2:5
  |
2 |     #[cfg(key=whoops)]
  |     ^^^^^^^^^^^^^^^^^^ help: expected syntax is: `#[cfg(/* predicate */)]`

It would probably also be a decent idea to stress the point that the right-hand side must be a string in the various docs that use this form of #[cfg(...)]. I am thinking in particular of the #[cfg(feature = "this")] syntax that integrates with features on a crate

@pnkfelix pnkfelix 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-attributes Area: Attributes (`#[…]`, `#![…]`) labels Feb 14, 2019
pietroalbini added a commit to pietroalbini/rust that referenced this issue Mar 8, 2019
On incorrect cfg literal/identifier, point at the right span

CC rust-lang#58462
@estebank
Copy link
Contributor

Current output:

error: expected unsuffixed literal or identifier, found `whoops`
 --> src/main.rs:2:15
  |
2 |     #[cfg(key=whoops)]
  |               ^^^^^^

@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. labels Oct 9, 2019
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@bors bors closed this as completed in aa5f781 Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. 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.

3 participants