Skip to content

thread_local with const { } fails to compile in file with #![forbid(unsafe_op_in_unsafe_fn)]  #121483

Closed

Description

@ids1024
#![forbid(unsafe_op_in_unsafe_fn)]

use std::cell::Cell;

thread_local! {
    pub static FOO: Cell<u32> = const { Cell::new(1) };
}

I expected to see this happen: Compiles without error.

Instead, this happened: Fails to build with this error:

error[E0453]: deny(unsafe_op_in_unsafe_fn) incompatible with previous forbid
 --> src/lib.rs:5:1
  |
1 |   #![forbid(unsafe_op_in_unsafe_fn)]
  |             ---------------------- `forbid` level set here
...
5 | / thread_local! {
6 | |     pub static FOO: Cell<u32> = const { Cell::new(1) };
7 | | }
  | |_^ overruled by previous forbid
  |
  = note: this error originates in the macro `$crate::thread::local_impl::thread_local_inner` which comes from the expansion of the macro `thread_local` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0453`.

It seems https://github.com/rust-lang/rust/blob/master/library/std/src/sys/pal/common/thread_local/os_local.rs is using #[deny(unsafe_op_in_unsafe_fn)]. But the existence of forbid makes somewhat problematic in a macro.

Meta

Occurs on rustc 1.76.0 (07dca489a 2024-02-04) and rustc 1.78.0-nightly (3406ada96 2024-02-21).

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 23, 2024
  2. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issue
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 23, 2024
  3. ZeWaka commented on Apr 12, 2024

    @ZeWaka

    Yeah, this isn't playing nice with the thread_local_initializer_can_be_made_const clippy lint.

  4. added a commit that references this issue on Oct 20, 2024
    7b714d4
  5. added a commit that references this issue on Oct 20, 2024
    8f02f30
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-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.L-unsafe_op_in_unsafe_fnLint: unsafe_op_in_unsafe_fnS-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions