Skip to content

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

Open

Description

@Jules-Bertholet

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 #146557 (comment) for root cause

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    A-attributesArea: Attributes (`#[…]`, `#![…]`)
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Sep 14, 2025
  2. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Sep 16, 2025
  3. jieyouxu commented on Sep 16, 2025

    @jieyouxu
    Member

    Accepted since 1.82.0.

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

    accepted since 1.59.0 when thread_local_const_init was stabilized.

  4. Jules-Bertholet commented on Sep 16, 2025

    @Jules-Bertholet
    ContributorAuthor

    Without the const, it’s since 1.29.0. (I don’t think this is an issue with the thread_local macro specifically, but I haven’t been able to make another reproducer yet.)

    @rustbot label regression-from-stable-to-stable

  5. added
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Sep 16, 2025
  6. added and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Sep 16, 2025
  7. Jules-Bertholet commented on Sep 16, 2025

    @Jules-Bertholet
    ContributorAuthor

    cargo bisect-rustc:

    searched toolchains nightly-2018-06-01 through nightly-2018-08-01

    Regression in nightly-2018-07-22

  8. Jules-Bertholet commented on Sep 16, 2025

    @Jules-Bertholet
    ContributorAuthor

    Ahhh… this is probably just #52467 + #146567. unused_attributes and no_mangle_const_items are FCW, not hard errors.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions