Skip to content

Add allowed list check on EII implementations attributes#159173

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
chenyukang:yukang-fix-159015-no-mangle-eii
Jul 24, 2026
Merged

Add allowed list check on EII implementations attributes#159173
rust-bors[bot] merged 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

@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
@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

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 think the current location is the best we can currently do.
Integrating this into the attribute parsing system somehow would be ideal, but I don't see a way to do this. Doing this after attribute parsing (like in the HIR) is not desirable since the spans are gone then.

@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

This comment has been minimized.

@rust-bors

This comment has been minimized.

Comment on lines +1221 to +1222
sym::cfg_trace,
sym::cfg_attr_trace,

@mejrs mejrs Jul 20, 2026

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.

Do these two still do anything after #159266?

View changes since the review

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.

no, we need to remove them.

@chenyukang
chenyukang force-pushed the yukang-fix-159015-no-mangle-eii branch from 265407a to 4e53ac1 Compare July 20, 2026 13:35
@rustbot

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

@chenyukang

Copy link
Copy Markdown
Member Author

@bors r=JonathanBrouwer

@rust-bors

rust-bors Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4e53ac1 has been approved by JonathanBrouwer

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 23, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 23, 2026
…ngle-eii, r=JonathanBrouwer

Add allowed list check on EII implementations attributes

Fixes rust-lang#158293
Fixes rust-lang#159015

r? @bjorn3
rust-bors Bot pushed a commit that referenced this pull request Jul 23, 2026
Rollup of 18 pull requests

Successful merges:

 - #138618 (Support using const pointers in asm `const` operand)
 - #150161 (Remove 'static requirement on try_as_dyn)
 - #158362 (trait solver: account for universes from replace_bound_vars)
 - #159173 (Add allowed list check on EII implementations attributes)
 - #159466 (cmse: clear variant-dependent padding in `enum`s)
 - #159718 (Make `DocLinkResMap` an `FxIndexMap`)
 - #155795 (constify `vec![1, 2, 3]` macro)
 - #157776 (ci: Enable autodiff tests on x86_64 linux)
 - #157905 (Update comments and add tests for `-Zrandomize-layout` for some guaranteed ZSTs)
 - #159041 (Reorganize `tests/ui/issues` [22/N])
 - #159108 (Reorganize `tests/ui/issues` [23/N])
 - #159138 (doc: document wasm import symbol mangling)
 - #159531 (Reorganize `tests/ui/issues` [28/N])
 - #159608 (early_otherwise: Don't hoist dereferences when the otherwise branch is reachable)
 - #159612 (Reorganize `tests/ui/issues` [29/N])
 - #159653 (run `tests/assembly-llvm/asm/aarch64-types.rs` for `aarch64_be`)
 - #159759 (rustc-dev-guide subtree update)
 - #159761 (Remove outdated comment for resolve_vars_with_obligations)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 23, 2026
…ngle-eii, r=JonathanBrouwer

Add allowed list check on EII implementations attributes

Fixes rust-lang#158293
Fixes rust-lang#159015

r? @bjorn3
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 23, 2026
…ngle-eii, r=JonathanBrouwer

Add allowed list check on EII implementations attributes

Fixes rust-lang#158293
Fixes rust-lang#159015

r? @bjorn3
rust-bors Bot pushed a commit that referenced this pull request Jul 23, 2026
Rollup of 19 pull requests

Successful merges:

 - #150161 (Remove 'static requirement on try_as_dyn)
 - #158362 (trait solver: account for universes from replace_bound_vars)
 - #159173 (Add allowed list check on EII implementations attributes)
 - #159466 (cmse: clear variant-dependent padding in `enum`s)
 - #159718 (Make `DocLinkResMap` an `FxIndexMap`)
 - #155795 (constify `vec![1, 2, 3]` macro)
 - #157776 (ci: Enable autodiff tests on x86_64 linux)
 - #157905 (Update comments and add tests for `-Zrandomize-layout` for some guaranteed ZSTs)
 - #158766 (Promote riscv64-unknown-linux-musl to tier 2 with host tools)
 - #159041 (Reorganize `tests/ui/issues` [22/N])
 - #159108 (Reorganize `tests/ui/issues` [23/N])
 - #159138 (doc: document wasm import symbol mangling)
 - #159531 (Reorganize `tests/ui/issues` [28/N])
 - #159608 (early_otherwise: Don't hoist dereferences when the otherwise branch is reachable)
 - #159612 (Reorganize `tests/ui/issues` [29/N])
 - #159653 (run `tests/assembly-llvm/asm/aarch64-types.rs` for `aarch64_be`)
 - #159667 (Make some parser structured suggestions verbose and tweak their wording)
 - #159759 (rustc-dev-guide subtree update)
 - #159761 (Remove outdated comment for resolve_vars_with_obligations)
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 23, 2026
…ngle-eii, r=JonathanBrouwer

Add allowed list check on EII implementations attributes

Fixes rust-lang#158293
Fixes rust-lang#159015

r? @bjorn3
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 23, 2026
…ngle-eii, r=JonathanBrouwer

Add allowed list check on EII implementations attributes

Fixes rust-lang#158293
Fixes rust-lang#159015

r? @bjorn3
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 24, 2026
…ngle-eii, r=JonathanBrouwer

Add allowed list check on EII implementations attributes

Fixes rust-lang#158293
Fixes rust-lang#159015

r? @bjorn3
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 24, 2026
…ngle-eii, r=JonathanBrouwer

Add allowed list check on EII implementations attributes

Fixes rust-lang#158293
Fixes rust-lang#159015

r? @bjorn3
rust-bors Bot pushed a commit that referenced this pull request Jul 24, 2026
Rollup of 12 pull requests

Successful merges:

 - #159765 (Avoid spurious rebuilds of JSON docs in bootstrap)
 - #159781 (Update bootstrap to use -Zembed-metadata=no instead of -Zno-embed-metadata)
 - #158362 (trait solver: account for universes from replace_bound_vars)
 - #159173 (Add allowed list check on EII implementations attributes)
 - #159718 (Make `DocLinkResMap` an `FxIndexMap`)
 - #159722 ( [rustdoc] Retrieve `cfg_attr` information for derived impls for `doc_cfg` feature)
 - #155795 (constify `vec![1, 2, 3]` macro)
 - #157776 (ci: Enable autodiff tests on x86_64 linux)
 - #158766 (Promote riscv64-unknown-linux-musl to tier 2 with host tools)
 - #159271 (str: add ASCII fast path to word_to_titlecase)
 - #159666 (fix(ld64.lld): route version mismatch warnings to linker_info on macOS)
 - #159667 (Make some parser structured suggestions verbose and tweak their wording)
rust-bors Bot pushed a commit that referenced this pull request Jul 24, 2026
Rollup of 14 pull requests

Successful merges:

 - #159765 (Avoid spurious rebuilds of JSON docs in bootstrap)
 - #159781 (Update bootstrap to use -Zembed-metadata=no instead of -Zno-embed-metadata)
 - #158362 (trait solver: account for universes from replace_bound_vars)
 - #158372 (rustfmt: Discover modules via `cfg_select!`)
 - #159173 (Add allowed list check on EII implementations attributes)
 - #159718 (Make `DocLinkResMap` an `FxIndexMap`)
 - #159722 ( [rustdoc] Retrieve `cfg_attr` information for derived impls for `doc_cfg` feature)
 - #159731 (std: Implement futex on wasip3 targets, update target spec)
 - #159755 (Improve consistency of attribute error messages)
 - #155795 (constify `vec![1, 2, 3]` macro)
 - #157776 (ci: Enable autodiff tests on x86_64 linux)
 - #158766 (Promote riscv64-unknown-linux-musl to tier 2 with host tools)
 - #159271 (str: add ASCII fast path to word_to_titlecase)
 - #159667 (Make some parser structured suggestions verbose and tweak their wording)
@rust-bors
rust-bors Bot merged commit 1b39801 into rust-lang:main Jul 24, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 24, 2026
rust-timer added a commit that referenced this pull request Jul 24, 2026
Rollup merge of #159173 - chenyukang:yukang-fix-159015-no-mangle-eii, r=JonathanBrouwer

Add allowed list check on EII implementations attributes

Fixes #158293
Fixes #159015

r? @bjorn3
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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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