Skip to content

#[no_mangle] in thread_local! requires unsafe wrapping in edition 2021 #142182

Closed
@elftausend

Description

@elftausend

I tried this code to compile in Edition 2021:

#[no_mangle] // fine
static TEST_OUTSIDE: usize = 0;

thread_local! {
    #[no_mangle] // unsafe attribute used without unsafe wrap error
    static TEST: usize = 0;
}

However, compilation fails because the attribute must be wrapped in unsafe(...).
This breaks existing libraries like litequad, even though it wasn't upgraded to edition 2024.

Meta

[package]
name = "no_mangle_test"
version = "0.1.0"
edition = "2021"

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a63572490a930b126b003b57c58c9df0

error: unsafe attribute used without unsafe
 --> src/main.rs:6:7
  |
6 |     #[no_mangle]
  |       ^^^^^^^^^ usage of unsafe attribute
  |
help: wrap the attribute in `unsafe(...)`
  |
6 |     #[unsafe(no_mangle)]
  |       +++++++         +

error: could not compile `no_mangle_test` (bin "no_mangle_test") due to 1 previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-edition-2024Area: The 2024 editionA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.P-highHigh priorityT-editionRelevant to the edition team.T-libsRelevant to the library 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