Skip to content

Implement argument-dependent target checking for the #[repr] parser#157215

Open
JonathanBrouwer wants to merge 5 commits into
rust-lang:mainfrom
JonathanBrouwer:repr-target-checking3
Open

Implement argument-dependent target checking for the #[repr] parser#157215
JonathanBrouwer wants to merge 5 commits into
rust-lang:mainfrom
JonathanBrouwer:repr-target-checking3

Conversation

@JonathanBrouwer
Copy link
Copy Markdown
Contributor

Fixes some of #156499
Fixes some of #153101
Alternative approach to #156569, which was suggested in the comments here: #156569 (review)

How do you feel about this approach?
r? @mejrs

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 31, 2026

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

cc @jdonszelmann

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 31, 2026
@JonathanBrouwer JonathanBrouwer force-pushed the repr-target-checking3 branch from 7758293 to d58ea4a Compare May 31, 2026 19:27
@jdonszelmann
Copy link
Copy Markdown
Contributor

I like this approach of using ManuallyChecked and asserting whether it was! Good solution :3

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.

Ugh, #[repr] gives me a headache.

Anyway, the implementation looks good generally.

What is the plan going forward? I imagine a series of prs like:

  • macrocall checking for the other attrs in #156499
  • some kind of "this attribute does nothing even if this macro call expands into a valid target" note on the warning
  • a PR to raise the lint level to deny by default for repr attrs on macro calls and do something about the repr(simd) thing

View changes since this review


let attr_span = cx.attr_span;
cx.adcx().warn_empty_attribute(attr_span);

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

😠

Comment on lines +143 to +146
cx.check_target(
"(simd)",
&AllowedTargets::AllowList(&[Allow(Target::Struct), Warn(Target::MacroCall)]),
);
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
cx.check_target(
"(simd)",
&AllowedTargets::AllowList(&[Allow(Target::Struct), Warn(Target::MacroCall)]),
);
cx.check_target(
"(simd)",
&AllowedTargets::AllowList(&[
Allow(Target::Struct), // Feature gated in `rustc_ast_passes`
Warn(Target::MacroCall), // FIXME: This is not feature gated (!!)
]),
);

@@ -1,8 +1,10 @@
#### Note: this error code is no longer emitted by the compiler.

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.

Same with the other PR, please direct to the new error code.

&AllowedTargets::AllowList(&[
Allow(Target::Struct),
Allow(Target::Enum),
Allow(Target::Union),
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
Allow(Target::Union),
Allow(Target::Union), // Feature gated in `rustc_hir_analysis`

Comment on lines +205 to 212
if &*path.segments == [sym::repr] && attribute_args == "(align(...))" {
if matches!(target, Target::Fn | Target::Method(..)) {
diag.help("use `#[rustc_align(...)]` instead");
}
if matches!(target, Target::Static) {
diag.help("use `#[rustc_align_static(...)]` instead");
}
}
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.

Gate this on nightly build and/or rustc_attrs perhaps?

Comment on lines +1208 to 1211
let (reprs, _first_attr_span) =
find_attr!(attrs, Repr { reprs, first_span } => (reprs.as_slice(), Some(*first_span)))
.unwrap_or((&[], None));

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
let (reprs, _first_attr_span) =
find_attr!(attrs, Repr { reprs, first_span } => (reprs.as_slice(), Some(*first_span)))
.unwrap_or((&[], None));
let reprs = find_attr!(attrs, Repr { reprs, .. } => reprs.as_slice()).unwrap_or(&[]);

Also do we still need to keep first_span in the AttributeKind::Repr?

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants