-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Fix incorrect cfg
structured suggestion and make suggestion verbose
#137773
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
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
Keep a span for the attribute *within* the square brackets as part of the `AttrKind`. ``` error: `cfg` is not followed by parentheses --> $DIR/cfg-attr-syntax-validation.rs:4:1 | LL | #[cfg = 10] | ^^^^^^^^^^^ | help: expected syntax is | LL - #[cfg = 10] LL + #[cfg(/* predicate */)] | ``` Noticed in rust-lang#137343 (comment).
cfg
structured suggestioncfg
structured suggestion and make suggestion verbose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give it another review after the duplicate arg function is duplicating it internally. I can't tell what else is simplifyable without checking out the PR locally and playing with it
help: expected syntax is | ||
| | ||
LL - #[cfg = 10] | ||
LL + #[cfg(/* predicate */)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe keep the value? Seems like often ppl may have written #[cfg = "linux"]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do that only if we are certain that the value is a valid flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user provided input, why would we just overwrite it with a comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The predicates can only be either bare identifiers or ident = lit
.
If they write #[cfg = ident]
they get
error: attribute value must be a literal
--> $DIR/cfg-attr-syntax-validation.rs:28:9
|
LL | #[cfg = a]
| ^
which is a separate error.
If they write #[cfg = "linux"]
, then we have to inspect the literal and reverse map it to target_os
so that we suggest #[cfg(target_os = "linux)]
. If we don't do that, and someone writes #[cfg = "foo"]
or #[cfg = true]
, what should we suggest other than the generic predicate
? In those cases we should link to https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options, which is the most comprehensive explanation of what is valid there (even though I don't like linking to the reference if there are more newbie friendly documents, which we don't seem to have for cfg
). If we keep that reverse mapping, does that mean we also have to dynamically create the reverse mapping for all features
in the current crate as well?
Note that I am not against doing any of that, it's just that this PR was to fix the incorrect suggestion, which needs to happen regardless of additional logic to make the suggestion more comprehensive.
@@ -3047,6 +3048,7 @@ pub struct AttrItem { | |||
pub args: AttrArgs, | |||
// Tokens for the meta item, e.g. just the `foo` within `#[foo]` or `#![foo]`. | |||
pub tokens: Option<LazyAttrTokenStream>, | |||
pub span: Span, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pls add docs with examples what it refers to
@@ -645,10 +652,11 @@ pub fn mk_attr_word( | |||
unsafety: Safety, | |||
name: Symbol, | |||
span: Span, | |||
inner_span: Span, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think no caller needs this to be different as there are no args
Keep a span for the attribute within the square brackets as part of the
AttrKind
.Noticed in #137343 (comment).