Skip to content

Move #[naked] attribute check to attribute parsing stage - #162530

Open
RichardTjokroutomo wants to merge 6 commits into
rust-lang:mainfrom
RichardTjokroutomo:check-attr-naked
Open

RichardTjokroutomo wants to merge 6 commits into
rust-lang:mainfrom
RichardTjokroutomo:check-attr-naked

Conversation

@RichardTjokroutomo

@RichardTjokroutomo RichardTjokroutomo commented Sep 9, 2026 •

Copy link
Copy Markdown

View all comments

Following #161482's idea to add target_item field to FinalizeCheckContext, replace target_item with ast_target, which is an enum that contains all possible types of target Item (obtained by grepping all functions that call lower_attrs()).

This change is needed as methods defined under traits & impls are represented as ast::AssocItem. Lastly, move check_naked to the callback returned by NakedParser::deferred_finalize_check().

Part of #153101. r?@JonathanBrouwer

LLM disclosure: I wrote the code by hand & took inspiration from #161482. However, I did use LLM to learn how attribute checking is done in Rustc.

@rustbot

rustbot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

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

cc @jdonszelmann, @JonathanBrouwer

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 Sep 9, 2026
@rustbot

rustbot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @nnethercote (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

Why was this reviewer chosen?

The reviewer was selected based on:

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

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@RichardTjokroutomo RichardTjokroutomo changed the title move check_naked() from check_attr.rs to codegen_attrs.rs Move #[naked] attribute check to attribute parsing stage Sep 10, 2026
@rustbot

This comment has been minimized.

};

let ItemKind::Fn(fn_item) = &item.kind else {
return;

@JonathanBrouwer JonathanBrouwer Sep 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should never fail right? So please make this panic

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

After implementing panic, through the failing CI I just found out that methods defined under traits & impls are represented as associated item instead of normal item.... lucky...

MethodKind::Trait { body: true } | MethodKind::TraitImpl | MethodKind::Inherent,
) => {
let Some(item) = cx.target_item else {
return;

@JonathanBrouwer JonathanBrouwer Sep 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread compiler/rustc_ast/src/ast.rs Outdated
/// - support unwinding with `-Cpanic=unwind`, unlike `extern "C"`
/// - often diverge from the C ABI
/// - are subject to change between compiler versions
pub fn is_rustic_abi(self) -> bool {

@JonathanBrouwer JonathanBrouwer Sep 11, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we avoid duplicating this logic?
Perhaps by parsing the abi into a ExternAbi instead?

View changes since the review

@RichardTjokroutomo RichardTjokroutomo Sep 11, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I don't think we can. I originally tried to convert it to ExternAbi, but I couldn't compile as it creates circular dependency (haven't checked deeper as to why, though).

Then I saw similar check for CanonAbi, and I thought since the logic is already duplicated elsewhere, perhaps I can also add similar method to Extern.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll doubly insist that this logic should not be duplicated.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

OK; I added a helper method to convert extern to externAbi

Comment thread compiler/rustc_ast_lowering/src/lib.rs Outdated
@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 Sep 11, 2026
@rustbot

rustbot commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

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

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@RichardTjokroutomo

Copy link
Copy Markdown
Author

hi @JonathanBrouwer @mejrs , do you mind taking another look?

@mejrs mejrs left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry we've let this stall a bit. I was already getting around to reviewing again 😅

View changes since this review

Comment thread compiler/rustc_attr_ir/src/target.rs Outdated
Variant(&'a Variant),
WherePredicate(&'a WherePredicate),

None, // Used when it is not possible to get detailed information about the target.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this be a doc comment?

Comment on lines +351 to +352
"`#[naked]` is currently unstable on `extern \"{}\"` functions",
abi.as_str()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"`#[naked]` is currently unstable on `extern \"{}\"` functions",
abi.as_str()
"`#[naked]` is currently unstable on `extern {abi}` functions",

Comment thread compiler/rustc_attr_ir/src/target.rs Outdated
Comment on lines +15 to +18
pub enum AstTarget<'a> {
AssocItem(&'a Item<AssocItemKind>),
ForeignItem(&'a Item<ForeignItemKind>),
Item(&'a Item),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The downside of having Item instead of ItemKind etc here is that lets you access the AttrVec, NodeId etc, which I'm not sure I like. It seems a bit unnecessary (you'll probably end up accessing item.kind 99.9% of the time anyway, and it's a bit dangerous in that someone could accidentally smuggle these (and unlowered Spans) into later compilation stages.

Suggested change
pub enum AstTarget<'a> {
AssocItem(&'a Item<AssocItemKind>),
ForeignItem(&'a Item<ForeignItemKind>),
Item(&'a Item),
pub enum AstTarget<'a> {
AssocItem(&'a AssocItemKind),
ForeignItem(&'a ForeignItemKind),
Item(&'a ItemKind),

}
}
_ => {}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here we have:

if cx.target != Target::Struct {
return;
}

        match cx.ast_target {
            rustc_attr_ir::target::AstTarget::Item(ast_item) => {
                let ItemKind::Struct(_, _, data) = &ast_item.kind else {
                    panic!("expected struct AST target item for Target::Struct");
                };
                if let VariantData::Struct { fields, .. } = data
                    && fields.iter().any(|f| f.default_value().is_some())
                {
                    cx.emit_err(NonExhaustiveWithDefaultFieldValues {
                        attr_span,
                        defn_span: cx.target_span,
                    });
                }
            }
            _ => {}
}

Is this equivalent to this?

        match cx.ast_target {
            rustc_attr_ir::target::AstTarget::Item(ast_item) if let ItemKind::Struct(_, _, data) = &ast_item.kind => {
                if let VariantData::Struct { fields, .. } = data
                    && fields.iter().any(|f| f.default_value().is_some())
                {
                    cx.emit_err(NonExhaustiveWithDefaultFieldValues {
                        attr_span,
                        defn_span: cx.target_span,
                    });
                }
            }
            _ => {}

(Same for some other matches)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done. hopefully I didn't miss any...

@rust-log-analyzer

This comment has been minimized.

@RichardTjokroutomo
RichardTjokroutomo force-pushed the check-attr-naked branch 2 times, most recently from 4f9551c to 6afb9d7 Compare September 27, 2026 12:08
@RichardTjokroutomo

Copy link
Copy Markdown
Author

Thanks for the review! PTAL again when you have time.

@rustbot ready

@rust-bors

This comment has been minimized.

@mejrs mejrs left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm happy to approve this as is, will wait a moment for @JonathanBrouwer to take a look as well.

View changes since this review

@JonathanBrouwer

Copy link
Copy Markdown
Member

I was taking a look at this earlier today but wasn't entirely confident on adding AstTarget as a duplicate of Target.
I think it would be cool if we could remove Target and completely replace it by AstTarget, but we shouldn't do this in this PR as that would be a lot of work.
I'll further review this PR wednesday

@mejrs

mejrs commented Sep 28, 2026

Copy link
Copy Markdown
Member

I was taking a look at this earlier today but wasn't entirely confident on adding AstTarget as a duplicate of Target. I think it would be cool if we could remove Target and completely replace it by AstTarget, but we shouldn't do this in this PR as that would be a lot of work. I'll further review this PR wednesday

What I've been considering is to just not give attribute parsers themselves access to Target, we'd probably want to still use it for AllowedTargets though as I don't want to do "clever" things there.

Signed-off-by: Richard Tjokroutomo <richard.tjokro2@gmail.com>
Signed-off-by: Richard Tjokroutomo <richard.tjokro2@gmail.com>
Signed-off-by: Richard Tjokroutomo <richard.tjokro2@gmail.com>
Signed-off-by: Richard Tjokroutomo <richard.tjokro2@gmail.com>
…functions calling lower_attrs()

Signed-off-by: Richard Tjokroutomo <richard.tjokro2@gmail.com>
@RichardTjokroutomo

Copy link
Copy Markdown
Author

but we shouldn't do this in this PR as that would be a lot of work.

In that case, I'll just do a rebase for this PR. Would be happy to work on it next though :)

Signed-off-by: Richard Tjokroutomo <richard.tjokro2@gmail.com>
@rustbot

rustbot commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

This branch has not been deployed

No deployments
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.

6 participants