Skip to content

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

Closed
@pnkfelix

Description

@pnkfelix

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions