Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ids1024 opened this issue Feb 23, 2024 · 1 comment · May be fixed by #121560
Open

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

ids1024 opened this issue Feb 23, 2024 · 1 comment · May be fixed by #121560
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-unsafe_op_in_unsafe_fn Lint: unsafe_op_in_unsafe_fn S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@ids1024
Copy link
Contributor

ids1024 commented Feb 23, 2024

#![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).

@ids1024 ids1024 added the C-bug Category: This is a bug. label Feb 23, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 23, 2024
@jieyouxu jieyouxu added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 23, 2024
@Noratrieb Noratrieb linked a pull request Feb 24, 2024 that will close this issue
@ZeWaka
Copy link

ZeWaka commented Apr 12, 2024

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

@jieyouxu jieyouxu added the L-unsafe_op_in_unsafe_fn Lint: unsafe_op_in_unsafe_fn label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. L-unsafe_op_in_unsafe_fn Lint: unsafe_op_in_unsafe_fn S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants