Show crate root path in misplaced crate-level attribute diagnostic#157025
Show crate root path in misplaced crate-level attribute diagnostic#157025Dnreikronos wants to merge 4 commits into
Conversation
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.
|
Some changes occurred in compiler/rustc_attr_parsing |
|
r? @oli-obk rustbot has assigned @oli-obk. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| LL | #![feature(globs)] | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: the crate root is at `$DIR/attr-mix-new.rs` |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
Reminder, once the PR becomes ready for a review, use |
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.
|
r? @JonathanBrouwer (feel free to assign back, but you seem to be on top of this already) |
|
Requested reviewer is already assigned to this pull request. Please choose another assignee. |
This comment has been minimized.
This comment has been minimized.
|
@Dnreikronos: 🔑 Insufficient privileges: not in review users |
|
@rustbot ready |
| @@ -0,0 +1,7 @@ | |||
| #![deny(unused_attributes)] | |||
There was a problem hiding this comment.
The preferred structure is afaik that:
submod.rsis in thetests/ui/attributes/auxiliaryfolder (the file then doesn't need the//@ ignote-auxiliary- The
crate-root-path-in-different-file.rsis in thetests/ui/attributesfolder. I think the file might then need anaux-buildannotation? I forgot how this works exactly, lmk if you can't figure it out then I'll take a look
There was a problem hiding this comment.
Other than that I think the PR is good now :)
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::DIAGNOSTICSso--remap-path-prefixis respected.