Skip to content

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

Closed

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

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jun 7, 2025
  2. workingjubilee commented on Jun 7, 2025

    @workingjubilee
    Member

    this seems to be a similar class of bug as to what we fixed via #138717 because the spans get their edition based on the crate the macro is defined in, and for a more recent rustc, std is Edition 2024

  3. added
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    D-editionDiagnostics: An error or lint that should account for edition differences.
    T-editionRelevant to the edition team.
    on Jun 7, 2025
  4. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jun 7, 2025
  5. added
    A-attributesArea: Attributes (`#[…]`, `#![…]`)
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    D-editionDiagnostics: An error or lint that should account for edition differences.
    on Jun 7, 2025
  6. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jun 8, 2025
  7. bjorn3 commented on Jun 8, 2025

    @bjorn3
    Member

    I don't think #[no_mangle] inside thread_local! {} is supposed to work. What would it actually apply to? The #[thread_local] static? (there may not be any), the static containing the pthread_key_t (again there may not be any), the std::thread::LocalKey (likely to surprise people who want to interoperate with C and it may not even be stored in a static in the first place) or something else?

  8. workingjubilee commented on Jun 8, 2025

    @workingjubilee
    Member

    ...that is an interesting question, yeah.

  9. apiraino commented on Jun 9, 2025

    @apiraino
    Contributor

    @elftausend I'm slightly confused about which edition that snippet used to compile for you (IIUC your report). Does it compile with --edition=2018 or --edition=2021? Can you share how do you exactly compile that snippet? Thanks

  10. hanna-kruppe commented on Jun 9, 2025

    @hanna-kruppe
    Contributor

    It not compiling with older edition but new Rust release is the bug. The playground only has the newest stable Rust, but you can compare 1.87 vs 1.86 on godbolt.

  11. apiraino commented on Jun 9, 2025

    @apiraino
    Contributor

    thanks @hanna-kruppe. Yes, as probably expected this started in #138162 when the standard library moved to Edition 2024

  12. ehuss commented on Jun 9, 2025

    @ehuss
    Contributor

    As for why this is happening, this is mostly an unfortunate result of needing to use $meta. The edition checking code here only looks at the entire span of the attribute. This means that the # token is the load-bearing token for determining the edition. The thread_local attribute is ignoring/dropping the # token from the input and substituting it with its own here.

    My expectation would be that the name of the attribute should be the span that it checks for the edition.

    I think that could be implemented by calling attr.get_normal_item().span() instead of attr.span() (or something like that).
    cc @compiler-errors @folkertdev if that would make sense?

  13. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jun 11, 2025
  14. added
    P-highHigh priority
    and removed on Jun 11, 2025
  15. added a commit that references this issue on Jun 12, 2025
  16. added a commit that references this issue on Jun 12, 2025
  17. folkertdev commented on Jun 12, 2025

    @folkertdev
    Contributor

    We're kind of close to the next release, but should we backport this to beta and unbreak the affected crates 6 weeks earlier?

  18. apiraino commented on Jun 12, 2025

    @apiraino
    Contributor

    @folkertdev just for my understanding : by "this" you mean #142261 (the patch fixing this issue), right?

  19. folkertdev commented on Jun 12, 2025

    @folkertdev
    Contributor

    yes, exactly

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