rust-lang / rust-lang/rust

`#[unsafe(no_mangle)]` erroneously accepted inside `thread_local!`

Open
#146,557 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-attributes A-macros C-bug P-medium regression-from-stable-to-stable T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

thread_local! {
    #[unsafe(no_mangle)]
    static FOO: () = const { () };
}

I expected to see this happen:

error: const items should never be `#[no_mangle]`

(The macro expands to a const item that has the #[unsafe(no_mangle)] applied to it.)

Instead, this happened:

It compiled successfully. The attribute is ignored

Meta

rustc --version --verbose:

rustc 1.91.0-nightly (02c7b1a7a 2025-09-13)
binary: rustc
commit-hash: 02c7b1a7ac1d739663878030510508372e46f254
commit-date: 2025-09-13
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.1

@rustbot label A-attributes A-macros T-compiler

Edit: see https://github.com/rust-lang/rust/issues/146557#issuecomment-3294496645 for root cause

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the provided thread_local! reproducer and inspect its macro expansion, especially how #[unsafe(no_mangle)] reaches the generated const item. Trace the attribute handling in the compiler's macro and attribute diagnostics, then verify that the example produces the expected error without changing valid thread_local! uses.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.