Skip to content

Add allowed list check on EII implementations attributes#159173

Open
chenyukang wants to merge 1 commit into
rust-lang:mainfrom
chenyukang:yukang-fix-159015-no-mangle-eii
Open

Add allowed list check on EII implementations attributes#159173
chenyukang wants to merge 1 commit into
rust-lang:mainfrom
chenyukang:yukang-fix-159015-no-mangle-eii

Conversation

@chenyukang

@chenyukang chenyukang commented Jul 12, 2026

Copy link
Copy Markdown
Member

Fixes #158293
Fixes #159015

r? @bjorn3

@rustbot

rustbot commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

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

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 Jul 12, 2026
@chenyukang
chenyukang force-pushed the yukang-fix-159015-no-mangle-eii branch from 23be887 to 9c36383 Compare July 12, 2026 08:25
@rustbot

This comment has been minimized.

Comment thread compiler/rustc_passes/src/check_attr.rs Outdated
}

if let Some(no_mangle_span) =
find_attr!(attrs, NoMangle(no_mangle_span) => *no_mangle_span)

@bjorn3 bjorn3 Jul 12, 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 also reject #[export_name] and probably #[link_section].

View changes since the review

@bjorn3 bjorn3 added the F-extern_item_impls `#![feature(extern_item_impls)]` label Jul 12, 2026
@chenyukang
chenyukang force-pushed the yukang-fix-159015-no-mangle-eii branch from 9c36383 to 81a1b49 Compare July 12, 2026 21:38
@rustbot

rustbot commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

@chenyukang
chenyukang force-pushed the yukang-fix-159015-no-mangle-eii branch from 81a1b49 to c7cb405 Compare July 12, 2026 21:41
@rust-log-analyzer

This comment has been minimized.

@mejrs

mejrs commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Maybe we ought to have an allowlist of attributes rather than error on these particular attributes? I'm also a bit worried about someone later down the line adding some new (possibly link related) attribute and not including it in this check.

@chenyukang
chenyukang force-pushed the yukang-fix-159015-no-mangle-eii branch from c7cb405 to 8e3fe6d Compare July 12, 2026 23:43
@rustbot rustbot added the A-rustdoc-json Area: Rustdoc JSON backend label Jul 12, 2026
@chenyukang

chenyukang commented Jul 12, 2026

Copy link
Copy Markdown
Member Author

Maybe we ought to have an allowlist of attributes rather than error on these particular attributes? I'm also a bit worried about someone later down the line adding some new (possibly link related) attribute and not including it in this check.

maybe allowlist contains lint levels, cfg, doc, inline, cold, optimize, coveragesanitize, must_use, deprecated?

@bjorn3

bjorn3 commented Jul 14, 2026

Copy link
Copy Markdown
Member

Seems like a reasonable list.

@chenyukang

Copy link
Copy Markdown
Member Author

Seems like a reasonable list.

I tried to get a more overwhole status on eii, this #125418 is the best place to track.
seems the current code is different with original rfc's syntax now.
so we are moving on the implementation and then draft another rfc in future?

@bjorn3

bjorn3 commented Jul 14, 2026

Copy link
Copy Markdown
Member

I don't know if the current EII syntax is expected to be stabilized eventually or if the intent is for the syntax from the RFC to be implemented eventually with #[eii] just being a temporary thing while we work on the internal implementation.

@mejrs

mejrs commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

For reference, attributes on eii declarations are handled via allowlist:

fn split_attrs(
ecx: &mut ExtCtxt<'_>,
span: Span,
attrs: ThinVec<Attribute>,
) -> (ThinVec<Attribute>, ThinVec<Attribute>, ThinVec<Attribute>) {
let mut macro_attributes = ThinVec::new();
let mut foreign_item_attributes = ThinVec::new();
let mut default_attributes = ThinVec::new();
for attr in attrs {
match attr.name() {
// Inline only matters for the default function being inlined into callsites
Some(sym::inline) => default_attributes.push(attr),
// If an eii is marked a lang item, that's because we want to call its declaration, so
// mark the foreign item as the lang item
Some(sym::lang) => foreign_item_attributes.push(attr),
// Deprecating an eii means deprecating the macro and the foreign item
Some(sym::deprecated) => {
foreign_item_attributes.push(attr.clone());
macro_attributes.push(attr);
}
// The stability of an EII affects the usage of the macro and calling the foreign item
Some(sym::stable) | Some(sym::unstable) => {
foreign_item_attributes.push(attr.clone());
macro_attributes.push(attr);
}
// `#[track_caller]` goes on the foreign item only: it's the symbol callers link
// against, so it must carry the flag for call sites to pass the caller location.
// Implementations derive it during codegen (see `EiiImpls` in `codegen_attrs.rs`),
// so it must not be routed onto the default impl here.
Some(sym::track_caller) => {
foreign_item_attributes.push(attr);
}
// Doc attributes should be forwarded to the macro and the foreign item, since those are
// the two items you interact with as a user.
// FIXME: idk yet how EIIs show up in docs, might want to customize
_ if attr.is_doc_comment() => {
foreign_item_attributes.push(attr.clone());
macro_attributes.push(attr);
}
Some(sym::eii) => unreachable!("should already be filtered out"),
_ => {
ecx.dcx().emit_err(EiiAttributeNotSupported { span, attr_span: attr.span() });
}
}
}
(macro_attributes, foreign_item_attributes, default_attributes)
}

I would expect attributes on implementations to be handled similarly.

seems the current code is different with original rfc's syntax now.
so we are moving on the implementation and then draft another rfc in future?

It's an experiment so we can diverge from the original proposal. Once we are happy with the implementation the rfc should be updated accordingly.

@chenyukang
chenyukang force-pushed the yukang-fix-159015-no-mangle-eii branch 2 times, most recently from 35bc796 to 30cc6b4 Compare July 14, 2026 15:24
}

// Check EII implementation attributes with a allowed list
fn check_eii_impl_attrs(&self, attrs: &[Attribute], eii_impls: &[EiiImpl]) {

@chenyukang chenyukang Jul 14, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I moved the checking to here in ast_validation, since macro expansion finished,
and leaves the final attributes, similar with
https://github.com/chenyukang/rust/blob/30cc6b46e8bb20318b05443cb80f1e50119ba971/compiler/rustc_ast_passes/src/ast_validation.rs#L522-L532

the old place where depends on HIR, HIR converts some attributes into variants without a uniform path or span.

View changes since the review

@chenyukang chenyukang changed the title Reject no_mangle on EII implementations Add allowed list check on EII implementations attributes Jul 14, 2026
@rust-log-analyzer

This comment has been minimized.

@chenyukang
chenyukang force-pushed the yukang-fix-159015-no-mangle-eii branch from 30cc6b4 to 265407a Compare July 15, 2026 00:10
@rustbot

rustbot commented Jul 15, 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.

@rust-bors

rust-bors Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #159293) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

@bjorn3

bjorn3 commented Jul 16, 2026

Copy link
Copy Markdown
Member

I think someone more familiar with attribute parsing/validation should review this. I don't know the advantages and disadvantages of the various locations attributes are validated.

@mejrs

mejrs commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Can you update a test to include #[naked] as well?

I think someone more familiar with attribute parsing/validation should review this. I don't know the advantages and disadvantages of the various locations attributes are validated.

I think it's ~fine to do it like this. We could have the eii impl macro leave behind a trace attribute so we can still do this check post-expansion and keep it in check_attr.rs. Not sure what the best course of action is. If a trace attribute is useful elsewhere, then maybe we should do that.

r? @JonathanBrouwer

@rustbot rustbot assigned JonathanBrouwer and unassigned bjorn3 Jul 16, 2026
@rustbot

rustbot commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

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

@JonathanBrouwer

Copy link
Copy Markdown
Contributor

(might take me a few days to get around to it, but happy to take a look)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-rustdoc-json Area: Rustdoc JSON backend F-extern_item_impls `#![feature(extern_item_impls)]` 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.

#[no_mangle] is not denied on EII definitions Naked EII impl functions are currently not working

6 participants