Skip to content

Improve workings of attribute suggestions#159633

Open
nnethercote wants to merge 6 commits into
rust-lang:mainfrom
nnethercote:attr-suggestions
Open

Improve workings of attribute suggestions#159633
nnethercote wants to merge 6 commits into
rust-lang:mainfrom
nnethercote:attr-suggestions

Conversation

@nnethercote

@nnethercote nnethercote commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

AttrItem span tracking improved in #159587, which lets us slightly simplify the workings of attribute suggestions.

r? @JonathanBrouwer

@rustbot

rustbot commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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. labels Jul 21, 2026
@rustbot

rustbot commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

JonathanBrouwer is currently at their maximum review capacity.
They may take a while to respond.

@nnethercote nnethercote changed the title Attr suggestions Improve workings of attribute suggestions Jul 21, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

AttrSuggestionStyle::EmbeddedAttribute
}
ParsedDescription::Attribute => AttrSuggestionStyle::Attribute(self.attr_style),
ParsedDescription::Attribute => AttrSuggestionStyle::EmbeddedAttribute,

@JonathanBrouwer JonathanBrouwer Jul 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm this file has two functions

template_suggestions: https://github.com/nnethercote/rust/blob/ae3bbe78ec2a9bb57a03f10ad6ee0388e12bcefb/compiler/rustc_attr_parsing/src/context.rs#L946
suggestions:
https://github.com/nnethercote/rust/blob/ae3bbe78ec2a9bb57a03f10ad6ee0388e12bcefb/compiler/rustc_attr_parsing/src/context.rs#L1141

This PR changes template_suggestions, but not suggestions. These methods seem identical to me so the fact that there are two is probably a mistake?
suggestions is the last usage of AttrSuggestionStyle::Attribute, so would be nice to also change that

View changes since the review

@nnethercote nnethercote Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a mistake and as initially written the difference was necessary. But I have now added some additional commits that adjust some more error messages, such that the difference is no longer necessary, and I have merged the two methods and done some other related cleanups.

@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 Jul 24, 2026
@rustbot

rustbot commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Attribute templated mismatch suggestions come in two forms:
- for a normal attribute, something like `#[inline(always)]`;
- for an attribute embedded in `cfg_attr`, something like
  `inline(always)`.

This commit replaces the former with the latter, and removes the need
for the fragile span-based test for detecting embedded attributes in
`template_suggestions`, and also `AttributeParseError::attr_span`. It
takes advantage of the newly added (and accurate) `AttrItem::span`
field.

This change has a minor effect on error messages: when there are
multiple suggestions their order can change because the `#[`/`]` are
no longer included. This actually improves things, because e.g. if you
write the invalid `#[inline(please)]`, the simpler `#[inline]` will be
suggested ahead of the longer `#[inline(always)]` and `#[inline(never)`]
alternatives.
The notable thing about this is the inconsistency. In the normal case we
get this output:
```
warning: valid forms for the attribute are `#[macro_export(local_inner_macros)]` and `#[macro_export]`
  --> $DIR/invalid_macro_export_argument.rs:7:1
   |
LL | #[macro_export(hello, world)]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
In the `cfg_attr` case we get this output:
```
warning: valid forms for the attribute are `macro_export` and `macro_export(local_inner_macros)`
  --> $DIR/invalid_macro_export_argument.rs:14:18
   |
LL | #[cfg_attr(true, macro_export(hello, world))]
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
```
The difference being the presence/absence of the `#[`/`]` delimiters in
both the "warning" line and the carets. This is exactly the kind of
inconsistency that rust-lang#159755 started eliminating. The next commit will do
so for this case.
@rustbot

rustbot commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

rustbot commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@nnethercote

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 26, 2026

@JonathanBrouwer JonathanBrouwer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors r+ rollup
Thanks a lot!

View changes since this review

@rust-bors

rust-bors Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 656151a has been approved by JonathanBrouwer

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

Reason for tree closure: spurious failures

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 26, 2026
@JonathanBrouwer

JonathanBrouwer commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

@bors r-
Actually CI seems sad, even tho there was no rust-log-analyzer message for it (yet?)
r=me after blessing tests
@rustbot author

@rust-bors rust-bors Bot 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 26, 2026
@rust-bors

rust-bors Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

View changes since this unapproval

@rust-log-analyzer

This comment has been minimized.

Similar to the commit before the last.
It's identical to `AttributeDiagnosticContext::suggestions`.
It's no longer used.

Also remove the `start` and `end` local variables in
`AttributeTemplate::suggestions`, because they're now always empty.
It's identical to `ParsedDescription`, modulo the `Attribute` vs
`EmbeddedAttribute` naming, but thanks to the previous changes the
`Embedded` prefix is no longer relevant.
@nnethercote

Copy link
Copy Markdown
Contributor Author

I fixed the rustdoc-ui failure.

@bors r=JonathanBrouwer

@rust-bors

rust-bors Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4981cac has been approved by JonathanBrouwer

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

Reason for tree closure: spurious failures

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 26, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 26, 2026
…nathanBrouwer

Improve workings of attribute suggestions

`AttrItem` span tracking improved in rust-lang#159587, which lets us slightly simplify the workings of attribute suggestions.

r? @JonathanBrouwer
rust-bors Bot pushed a commit that referenced this pull request Jul 26, 2026
…uwer

Rollup of 28 pull requests

Successful merges:

 - #159638 (bootstrap: Split the `Step` trait into multiple traits)
 - #159774 (rustc_trait_selection: fix trait solver hang caused by degenerate obligations)
 - #159837 (line-tables-only test: check that the line number matches the function name)
 - #159946 (Update Enzyme submodule to imporve llvm-cov)
 - #159962 (miri subtree update)
 - #156570 (tests: extend remap-path-prefix-std to all stdlib rlibs)
 - #159617 (Fix up `#[linkage]` target checking)
 - #159633 (Improve workings of attribute suggestions)
 - #159733 (std: Switch implementations of `thread_local!` for WASI)
 - #159783 (Check unsafe impls on safe EIIs)
 - #159810 (Add tuple never coercion collection regression test)
 - #159826 (Remove redundant `#[rustc_paren_sugar]` feature gate)
 - #159846 (Implement `str::copy_from_str`)
 - #159849 (rustc_parse: Stop returning `Option` from statement parsing)
 - #159853 (Updated expect messages for `CString` struct and method documentation)
 - #159875 (More cleanup in `rustc_attr_parsing`)
 - #159882 (Update expect messages in library/alloc/boxed.rs and library/alloc/string.rs to follow the style guide)
 - #159891 (Split multiline derives into std/rustc macros)
 - #159893 (Fix `find_attr` hygiene and `rustc_hir` cleanups)
 - #159895 (rustc-dev-guide subtree update)
 - #159902 (Clarify that the expected runtime symbols signature is for the current target only)
 - #159914 (Fix error in diagnostic on_unmatched_args)
 - #159917 (spare capacity mut constification)
 - #159918 (rename abort_unwind → abort_on_unwind)
 - #159927 (Remove sve2 from the ImpliedFeatures of AArch64 v9a.)
 - #159936 (Minor `rustc_ast::ast` doc cleanups)
 - #159945 (Update expect messages in library/core/src/ptr/non_null.rs)
 - #159950 (Add CFI tests for return types and never type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler 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