Skip to content

Error when #[export_name] is used on invalid targets - #166351

Open
JonathanBrouwer wants to merge 1 commit into
rust-lang:mainfrom
JonathanBrouwer:export-name-cleanup
Open

JonathanBrouwer wants to merge 1 commit into
rust-lang:mainfrom
JonathanBrouwer:export-name-cleanup

Conversation

@JonathanBrouwer

@JonathanBrouwer JonathanBrouwer commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Currently, #[export_name] has the following behaviour regarding targets it is allowed on:

  • It is allowed on free functions, statics, methods in inherent and trait impl blocks, and provided trait methods
  • It FCWs on fields, match arms, macro definitions and macro calls
  • It errors on other targets

This PR makes the following changes to that:

  • provided trait methods: Allowed -> Error
  • fields, match arms, macro definitions and macro calls: FCW -> Error
    • These currently have no effect, they produce a FCW and are ignored.

In summary, the following 4 examples will produce an error after this PR:

pub trait Trait {
    #[unsafe(export_name = "meow1")]
    fn meow2() { /* provided */ }
}

#[unsafe(export_name = "meow2")]
macro_rules! meow2 {
    () => {};
}

fn main() {
    #[unsafe(export_name = "meow3")]
    meow2!();

    match () {
        #[unsafe(export_name = "meow4")]
        _ => {}
    }
}

A crater run was performed here: rust-lang/rust#164789 (comment)
This crater run shows no regressions. That is, all 212 regressions were for no_mangle, 0 for export_name.

@JonathanBrouwer JonathanBrouwer added the I-lang-nominated Nominated for discussion during a lang team meeting. label Nov 22, 2026
@rustbot

rustbot commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

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 Nov 22, 2026

impl SingleAttributeParser for ExportNameParser {
const PATH: &[rustc_span::Symbol] = &[sym::export_name];
const ON_DUPLICATE: OnDuplicate = OnDuplicate::WarnButFutureError;

@mejrs mejrs Nov 22, 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 duplicates be an error as well?

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.

I agree that would be desirable. We need to do a crater run for that tho, so let's remove the lang nomination and do that first

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.

Actually I'll make this a separate PR

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 can roll it up in #165441

@JonathanBrouwer JonathanBrouwer removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Nov 22, 2026
@JonathanBrouwer

This comment was marked as outdated.

@rust-bors

This comment was marked as outdated.

rust-bors Bot pushed a commit that referenced this pull request Nov 22, 2026
Error when `export_name` is applied to wrong targets
@JonathanBrouwer JonathanBrouwer changed the title Error when export_name is applied to wrong targets Error when export_name is used incorrectly Nov 22, 2026
@JonathanBrouwer

This comment was marked as outdated.

@rust-bors

This comment was marked as outdated.

@JonathanBrouwer
JonathanBrouwer marked this pull request as draft November 22, 2026 15:09
@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 Nov 22, 2026
@JonathanBrouwer JonathanBrouwer changed the title Error when export_name is used incorrectly Error when #[export_name] is used on invalid targets Nov 22, 2026
@JonathanBrouwer JonathanBrouwer added the I-lang-nominated Nominated for discussion during a lang team meeting. label Nov 22, 2026
@JonathanBrouwer
JonathanBrouwer marked this pull request as ready for review November 22, 2026 15:15
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 22, 2026

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 (`#[…]`, `#![…]`) I-lang-nominated Nominated for discussion during a lang team meeting. 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.

export_name on provided trait associated functions has funny behavior

3 participants