Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

estebank
Copy link
Contributor

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 #137343 (comment).

@rustbot
Copy link
Collaborator

rustbot commented Feb 28, 2025

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Feb 28, 2025
@rust-log-analyzer

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).
@compiler-errors compiler-errors changed the title Fix incorrect cfg structured suggestion Fix incorrect cfg structured suggestion and make suggestion verbose Feb 28, 2025
Copy link
Contributor

@oli-obk oli-obk left a 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 */)]
Copy link
Contributor

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"]

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

@estebank estebank Mar 3, 2025

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,
Copy link
Contributor

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,
Copy link
Contributor

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

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants