Skip to content

Fix target checking for some attributes on macro calls#156569

Open
JonathanBrouwer wants to merge 2 commits into
rust-lang:mainfrom
JonathanBrouwer:fix-macro-call-target
Open

Fix target checking for some attributes on macro calls#156569
JonathanBrouwer wants to merge 2 commits into
rust-lang:mainfrom
JonathanBrouwer:fix-macro-call-target

Conversation

@JonathanBrouwer
Copy link
Copy Markdown
Contributor

Fixes #156499

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 14, 2026

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann

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

rustbot commented May 14, 2026

r? @jieyouxu

rustbot has assigned @jieyouxu.
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 17 candidates

@JonathanBrouwer JonathanBrouwer force-pushed the fix-macro-call-target branch from c960552 to 5f14882 Compare May 14, 2026 10:44
@JonathanBrouwer
Copy link
Copy Markdown
Contributor Author

r? @mejrs

@rustbot rustbot assigned mejrs and unassigned jieyouxu May 14, 2026
/// This is a list of default targets to which a attribute can be applied
/// This is used for attributes that are not parted to the new target checking system yet can use this list as a placeholder.
/// This excludes `Target::MacroCall`, as attributes on macro calls are otherwise not checked for parsed attributes.
pub(crate) const DEFAULT_TARGETS: &'static [Policy] = {
Copy link
Copy Markdown
Contributor Author

@JonathanBrouwer JonathanBrouwer May 14, 2026

Choose a reason for hiding this comment

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

This being a separate list is quite ugly, I'm planning on fixing this in the future by having some way to pass a pattern to the target checking, for example by having AllowedTargets::AllowFn(|target| match target { ... })

View changes since the review

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.

Note that ALL_TARGETS is still used by the diagnostic attrs & rustc_dummy

@JonathanBrouwer JonathanBrouwer force-pushed the fix-macro-call-target branch from 5f14882 to d98e484 Compare May 14, 2026 10:48
@JonathanBrouwer JonathanBrouwer force-pushed the fix-macro-call-target branch from d98e484 to d69f6ba Compare May 14, 2026 10:52
}

/// This is a list of default targets to which a attribute can be applied
/// This is used for attributes that are not parted to the new target checking system yet can use this list as a placeholder.
Copy link
Copy Markdown
Contributor

@fbstj fbstj May 14, 2026

Choose a reason for hiding this comment

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

Suggested change
/// This is used for attributes that are not parted to the new target checking system yet can use this list as a placeholder.
/// This is used for attributes that are not ported to the new target checking system yet and can use this list as a placeholder.

two nits:

  • parted => ported
  • it might be two sentences?

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.

These attributes don't have target checks during parsing because they need access to hir, not because we haven't gotten around to moving them around.

Unless that's something you're actively planning to change, we shouldn't say "yet".

Copy link
Copy Markdown
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

I know I suggested it but now I'm not sure CHECKED_LATER is a good idea. I think we should just move as many checks as we can to attribute parsing instead.

Take for example may_dangle: currently it checks in check_attr that it's on a generic param, and then that it's on a lifetime/type generic in a Drop impl.

Instead we should just move that first check into attr parsing and leave the drop impl check in check_attr.

Similar for the other attributes. Thoughts?

View changes since this review

/// This is used for attributes that are not parted to the new target checking system yet can use this list as a placeholder.
/// This excludes `Target::MacroCall`, as attributes on macro calls are otherwise not checked for parsed attributes.
pub(crate) const CHECKED_LATER: &'static [Policy] = {
use Policy::Allow;
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.

Suggested change
use Policy::Allow;

Redundant import.


/// This is a list of default targets to which a attribute can be applied
/// This is used for attributes that are not parted to the new target checking system yet can use this list as a placeholder.
/// This excludes `Target::MacroCall`, as attributes on macro calls are otherwise not checked for parsed attributes.
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.

Suggested change
/// This excludes `Target::MacroCall`, as attributes on macro calls are otherwise not checked for parsed attributes.
/// This excludes `Target::MacroCall`, as attributes on macro calls are otherwise not checked for parsed attributes because `check_attr.rs` runs after macro expansion.

or something similar

//~| WARN previously accepted
test!();

fn main() {}
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.

Comment on lines +2 to +6
#![feature(sanitize)]
#![feature(register_tool)]
#![feature(export_stable)]
#![feature(lang_items)]
#![feature(dropck_eyepatch)]
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 propose we just error for all these unstable attributes on macro call positions, no need to bother with a lint or fcw.

added_fake_targets.push(target_group_name);
}

/// This is a list of default targets to which a attribute can be applied
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.

Suggested change
/// This is a list of default targets to which a attribute can be applied
/// This is a list of default targets to which a attribute can be applied
///

Let's not have this big paragraph included in the module level docs :)

}

/// This is a list of default targets to which a attribute can be applied
/// This is used for attributes that are not parted to the new target checking system yet can use this list as a placeholder.
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.

These attributes don't have target checks during parsing because they need access to hir, not because we haven't gotten around to moving them around.

Unless that's something you're actively planning to change, we shouldn't say "yet".

| ^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[link]` can be applied to associated consts, associated types, const parameters, const parameters, constants, crates, data types, enum variants, extern crates, foreign modules, foreign statics, function params, functions, global asms, impl blocks, lifetime parameters, lifetime parameters, macro defs, match arms, modules, pattern fields, statics, struct fields, struct fields, trait aliases, traits, type aliases, type parameters, type parameters, use statements, and where predicates
Copy link
Copy Markdown
Contributor

@mejrs mejrs May 14, 2026

Choose a reason for hiding this comment

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

We shouldn't be saying this huge list of "valid" targets.

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

rustbot commented May 14, 2026

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

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.

rustc has stopped emitting unused repr/link/panic_handler attribute lints on macro invocations

5 participants