Skip to content

Show crate root path in misplaced crate-level attribute diagnostic#157025

Open
Dnreikronos wants to merge 4 commits into
rust-lang:mainfrom
Dnreikronos:fix/crate-root-path-help-in-attr-style-diagnostic
Open

Show crate root path in misplaced crate-level attribute diagnostic#157025
Dnreikronos wants to merge 4 commits into
rust-lang:mainfrom
Dnreikronos:fix/crate-root-path-help-in-attr-style-diagnostic

Conversation

@Dnreikronos
Copy link
Copy Markdown
Contributor

@Dnreikronos Dnreikronos commented May 27, 2026

Fixes #157001

If you put #![feature(...)] inside a non-root module, the diagnostic tells you it can only be used at the crate root, but never says where the crate root actually is. Not great if you're new to Rust or working in a big workspace with nested modules.

Now the diagnostic includes a help line with the crate root file path when the misplaced attribute is an inner attribute. Outer attributes already get a span note pointing at what they're applied to, so they don't need this. Path goes through RemapPathScopeComponents::DIAGNOSTICS so --remap-path-prefix is respected.

warning: the `#![feature]` attribute can only be used at the crate root
  --> src/foo.rs:1:1
   |
LL | #![feature(test)]
   | ^^^^^^^^^^^^^^^^^
   |
   = help: the crate root is at `src/main.rs`

When a crate-level attribute like `#![feature(...)]` is used as an inner
attribute inside a non-root module, the diagnostic now includes a help
message pointing to the crate root file path, making it easier for users
to find where the attribute should be placed.
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 27, 2026

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 May 27, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 27, 2026

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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 18 candidates

Comment thread tests/ui/attributes/attr-mix-new.stderr Outdated
LL | #![feature(globs)]
| ^^^^^^^^^^^^^^^^^^
|
= help: the crate root is at `$DIR/attr-mix-new.rs`
Copy link
Copy Markdown
Contributor

@JonathanBrouwer JonathanBrouwer May 27, 2026

Choose a reason for hiding this comment

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

Hmmm, can we remove or adjust this suggestion if the crate root is in the same file? I don't the help message is useful in that case

View changes since the review

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.

I think that all changed tests have the crate root being in the same file, so then please add a new test that triggers this help message in a different file

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.

Good catch, done! Suppressed the help when the attribute is already in the crate root file. Added a cross-file test too (crate root + submodule in a separate file) so we still have coverage for the case where the help actually matters.

@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 May 27, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 27, 2026

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

Skip the "the crate root is at ..." help message when the misplaced
crate-level attribute is already in the crate root file, since it
adds no useful information in that case. Add a cross-file test to
verify the help still appears when the attribute is in a different
file from the crate root.
@oli-obk
Copy link
Copy Markdown
Contributor

oli-obk commented May 27, 2026

r? @JonathanBrouwer (feel free to assign back, but you seem to be on top of this already)

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 27, 2026

Requested reviewer is already assigned to this pull request.

Please choose another assignee.

@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 27, 2026

@Dnreikronos: 🔑 Insufficient privileges: not in review users

@Dnreikronos
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 May 27, 2026
@@ -0,0 +1,7 @@
#![deny(unused_attributes)]
Copy link
Copy Markdown
Contributor

@JonathanBrouwer JonathanBrouwer May 27, 2026

Choose a reason for hiding this comment

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

The preferred structure is afaik that:

  • submod.rs is in the tests/ui/attributes/auxiliary folder (the file then doesn't need the //@ ignote-auxiliary
  • The crate-root-path-in-different-file.rs is in the tests/ui/attributes folder. I think the file might then need an aux-build annotation? I forgot how this works exactly, lmk if you can't figure it out then I'll take a look

View changes since the review

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.

Other than that I think the PR is good now :)

@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 May 27, 2026
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-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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

actual "crate root" file not identified in diagnostics

5 participants