thread_local with const { } fails to compile in file with #![forbid(unsafe_op_in_unsafe_fn)] · Issue #121483 · rust-lang/rust (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

@ids1024

Description

@ids1024

#![forbid(unsafe_op_in_unsafe_fn)]

use std::cell::Cell;

thread_local! { pub static FOO: Cell = 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).