rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Fix FP in `threadlocal!` when falling back to `os_local`

Open PartiallyUntyped opened this issue 1 year ago • 4 comments

Issue: The lint always triggers for some targets.
Why: The lint assumes an __init function is not present for const initializers.
This does not hold for all targets. Some targets always have an initializer function.
Fix: If an initializer function exists, we check that it is not a const fn.

Lint overview before the patch:

  1. Outside threadlocal!, then exit early.
  2. In threadlocal! and __init does not exist => is const already, exit early.
  3. In threadlocal! and __init exists and __init body can be const => we lint.

Lint overview after the patch:

  1. Outside threadlocal!, then exit early.
  2. In threadlocal! and __init does not exist => is const already, exit early.
  3. In threadlocal! and __init exists and is const fn => is const already, exit early.
  4. In threadlocal! and __init exists and is not const fn and __init body can be const => we lint.

The patch adds step 3.

changelog: Fixed fp in [thread_local_initializer_can_be_made_const] where fallback implementation of threadlocal! was always linted.

Verifying the changes

For each of these platforms [ mac, x86_64-windows-gnu, x86_64-windows-msvc ]:

  1. switched to master, ran bless. Found a failure for windows-gnu.
  2. switched to patch, ran bless. Success for all three platforms.

How platform affects the lint:

The compiler performs a conditional compilation depending on platform features. The os_local fallback includes a call to __init, without step 3, we lint in all cases.

cfg_if::cfg_if! {
    if #[cfg(any(all(target_family = "wasm", not(target_feature = "atomics")), target_os = "uefi"))] {
        #[doc(hidden)]
        mod static_local;
        #[doc(hidden)]
        pub use static_local::{Key, thread_local_inner};
    } else if #[cfg(target_thread_local)] {
        #[doc(hidden)]
        mod fast_local;
        #[doc(hidden)]
        pub use fast_local::{Key, thread_local_inner};
    } else {
        #[doc(hidden)]
        mod os_local;
        #[doc(hidden)]
        pub use os_local::{Key, thread_local_inner};
    }
}

r? @llogiq

PartiallyUntyped avatar Feb 11 '24 17:02 PartiallyUntyped

@rustbot label +beta-nominated

I think this should be merged upstream as well.

PartiallyUntyped avatar Feb 12 '24 19:02 PartiallyUntyped

Just tested this patch on an ARM 64-bit Linux, that's a target that the CI doesn't look for. Quinn has checked for Windows (which I'd think she'd be able to check for both MSVC and GNU), and nobody uses 32-bit Windows. I think that this patch is covered on the test department.

blyxyas avatar Feb 13 '24 19:02 blyxyas

Thank you Alejandra I appreciate your time and effort!

I found another case I hadn't considered initially, and will add that with the changes.

PartiallyUntyped avatar Feb 13 '24 20:02 PartiallyUntyped

Okis, sorry for the delay. I finally have the benchmark results: There isn't really a big performance change

blyxyas avatar Feb 17 '24 18:02 blyxyas

If this is fine, maybe we can merge this to bundle it with the release?

PartiallyUntyped avatar Feb 29 '24 23:02 PartiallyUntyped

Done, thanks! ☺️

PartiallyUntyped avatar Feb 29 '24 23:02 PartiallyUntyped

@bors r+

blyxyas avatar Mar 01 '24 00:03 blyxyas

:pushpin: Commit bb1ee8746ee0734fac664e78b59be589459c2026 has been approved by blyxyas

It is now in the queue for this repository.

bors avatar Mar 01 '24 00:03 bors

:hourglass: Testing commit bb1ee8746ee0734fac664e78b59be589459c2026 with merge 225d3771d744acc59b2a8cd778259bd3aa2383cf...

bors avatar Mar 01 '24 00:03 bors

:sunny: Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test Approved by: blyxyas Pushing 225d3771d744acc59b2a8cd778259bd3aa2383cf to master...

bors avatar Mar 01 '24 00:03 bors

https://github.com/rust-lang/rust/pull/122510

flip1995 avatar Mar 14 '24 19:03 flip1995