Skip to content

Rework target checking for built-in attributes #145085

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 6 commits into
base: master
Choose a base branch
from

Conversation

JonathanBrouwer
Copy link
Contributor

@JonathanBrouwer JonathanBrouwer commented Aug 8, 2025

This is a refactoring of target checking for built-in attributes.
This PR has the following goals:

  • Only refactor the 80% of the attributes that are simple to target check. More complicated ones like #[repr] will be in a future PR. Tho I have written the code in such a way that this will be possible to add in the future.
  • No breaking changes.
    • This part of the codebase is not very well tested though, we can do a crater run if we want to be sure.
    • I've spotted quite a few weird situations (like I don't think an impl block should be deprecated?). We can propose fixing these to in a future PR

I've split it in commits and left a description on some of the commits to help review.
r? @jdonszelmann

@rustbot rustbot added 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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 8, 2025
@JonathanBrouwer JonathanBrouwer force-pushed the target_checking branch 2 times, most recently from 66feb37 to a1c6f2a Compare August 9, 2025 18:02
- Added a few more variants which are needed for various attributes
- Previously a trait method with default block had the same target representation as a method in a `impl trait for` block, this has been changed (See `MethodKind`)
- Added both `singular_name` and `plural_name` for more precision on the form of the name
@rust-log-analyzer

This comment has been minimized.

Every acceptor gets an `ALLOWED_TARGETS` specification which can specify per target whether it is allowed, warned, or errored.
@rustbot
Copy link
Collaborator

rustbot commented Aug 9, 2025

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

@JonathanBrouwer JonathanBrouwer marked this pull request as ready for review August 9, 2025 19:06
@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 Aug 9, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 9, 2025

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

This PR modifies tests/ui/issues/. If this PR is adding new tests to tests/ui/issues/,
please refrain from doing so, and instead add it to more descriptive subdirectories.

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

@@ -64,27 +71,32 @@ pub(crate) struct ExportStableParser;
impl<S: Stage> NoArgsAttributeParser<S> for ExportStableParser {
const PATH: &[Symbol] = &[sym::export_stable];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowAll; //FIXME Still checked fully in `check_attr.rs`
Copy link
Contributor Author

@JonathanBrouwer JonathanBrouwer Aug 9, 2025

Choose a reason for hiding this comment

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

Some attributes were a little non-trivial to check, I will do these soon but I think let's get all the easy ones done first

Allow(Target::AssocTy),
Allow(Target::AssocConst),
Allow(Target::Variant),
Allow(Target::Impl { of_trait: false }), //FIXME This does not make sense
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 can make breaking changes in a future PR, for example to fix this.

|
= note: `#[deny(useless_deprecated)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've made all attributes that were previously warnings a future error. I can try to replicate the previous behaviour if you wish but I think this should be fine.

--> $DIR/issue-31769.rs:2:5
|
LL | #[inline] struct Foo;
| ^^^^^^^^^
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm I've just noticed this is super ugly with the help getting its own codespan, let me see if I can fix that

@@ -57,7 +57,7 @@ LL | #[lang = "fn"]
| ^^^^^^^^^^^^^^
...
LL | trait Fn {
| - this trait has 0 generic arguments
| - this a trait has 0 generic arguments
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, I'll have to fix that

@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

test result: FAILED. 63 passed; 1 failed; 2 ignored; 0 measured; 0 filtered out; finished in 1.11s

all doctests ran in 3.21s; merged doctests compilation took 2.07s
error: doctest failed, to rerun pass `-p rustc_lint_defs --doc`
Build completed unsuccessfully in 0:28:54
  local time: Sat Aug  9 19:32:31 UTC 2025
  network time: Sat, 09 Aug 2025 19:32:31 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

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-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-libs Relevant to the library 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