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

Closed
ids1024 opened this issue Feb 23, 2024 · 1 comment · Fixed by #121560
Closed
Labels
A-lints 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-lints 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
@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
@bors bors closed this as completed in 7b714d4 Oct 20, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 20, 2024
Rollup merge of rust-lang#121560 - Noratrieb:stop-lint-macro-nonsense, r=jieyouxu

Allow `#[deny]` inside `#[forbid]` as a no-op

Forbid cannot be overriden. When someome tries to do this anyways, it results in a hard error. That makes sense.

Except it doesn't, because macros. Macros may reasonably use `#[deny]` (or `#[warn]` for an allow-by-default lint) in their expansion to assert that their expanded code follows the lint. This is doesn't work when the output gets expanded into a `forbid()` context. This is pretty silly, since both the macros and the code agree on the lint!

By making it a warning instead, we remove the problem with the macro, which is now nothing as warnings are suppressed in macro expanded code, while still telling users that something is up.

fixes rust-lang#121483
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints 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