Skip to content

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

Closed
@ids1024

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

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