rust-clippy
rust-clippy copied to clipboard
Fix FP in `threadlocal!` when falling back to `os_local`
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:
- Outside
threadlocal!, then exit early. - In
threadlocal!and__initdoes not exist => is const already, exit early. - In
threadlocal!and__initexists and__initbody can beconst=> we lint.
Lint overview after the patch:
- Outside
threadlocal!, then exit early. - In
threadlocal!and__initdoes not exist => is const already, exit early. - In
threadlocal!and__initexists and isconst fn=> is const already, exit early. - In
threadlocal!and__initexists and is notconst fnand__initbody can beconst=> 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 ]:
- switched to master, ran bless. Found a failure for windows-gnu.
- 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
@rustbot label +beta-nominated
I think this should be merged upstream as well.
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.
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.
Okis, sorry for the delay. I finally have the benchmark results: There isn't really a big performance change
If this is fine, maybe we can merge this to bundle it with the release?
Done, thanks! ☺️
@bors r+
:pushpin: Commit bb1ee8746ee0734fac664e78b59be589459c2026 has been approved by blyxyas
It is now in the queue for this repository.
:hourglass: Testing commit bb1ee8746ee0734fac664e78b59be589459c2026 with merge 225d3771d744acc59b2a8cd778259bd3aa2383cf...
:sunny: Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test Approved by: blyxyas Pushing 225d3771d744acc59b2a8cd778259bd3aa2383cf to master...
https://github.com/rust-lang/rust/pull/122510