Description
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