Allow elided ('static) lifetimes in thread_local!#159564
Conversation
with or without a const initializer, on all platforms.
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
Question for reviewer: (how) should I add a test for this? I tried adding a test to attempted UI test// Test each of the three `thread_local!` implementations,
// that it defaults elided lifetimes in the type to `'static`.
// Regression test for 159358.
//@ check-pass
// no-threads
//@ revisions: x86_64-uefi
//@[x86_64-uefi] compile-flags: --target x86_64-unknown-uefi
//@[x86_64-uefi] needs-llvm-components: x86
// target_thread_local
//@ revisions: x86_64-linux
//@[x86_64-linux] compile-flags: --target x86_64-unknown-linux-gnu
//@[x86_64-linux] needs-llvm-components: x86
// os
//@ revisions: x86_64-windows-gnu
//@[x86_64-windows-gnu] compile-flags: --target x86_64-pc-windows-gnu
//@[x86_64-windows-gnu] needs-llvm-components: x86
// Const initializer, non-Drop
std::thread_local!(static A: &str = const { "" });
// Non-const initializer, non-Drop
std::thread_local!(static B: &str = "");
fn main() {}( |
#[thread_local]thread_local!
|
@rustbot label T-libs-api I think this needs T-libs-api approval, at least for the non- |
This comment has been minimized.
This comment has been minimized.
It was testing diagnostics emitted for missing lifetime specifiers in `thread_local!`, but now elided lifetimes in `thread_local!` are `'static`, so there's nothing to test.
with or without a const initializer, on all platforms.
Lifetime elision includes named lifetimes and
'static, so if we give the macro-generated__rust_std_internal_init_fnfunction an argument mentioning'static, then elided lifetimes in the return type default to'static. This usesPhantomData<&'static ()>so that it should probably compile down to nothing (at least in release mode).Before this change, elided
'staticlifetimes were allowed only withconstinitializers on the "no-threads" and "native"thread_local!implementations, not on the "os" implementation, and not with non-constinitializers.After this change, they are allowed in all
thread_local!implementations, with or without aconstinitializer (AandBboth compile on targets with all threethread_local!implementations.)thread_local!implementationconstinitializer (A)constinitializer (B)x86_64-unknown-uefi)target_thread_local(e.g.x86_64-unknown-linux-gnu)x86_64-pc-windows-gnu)An alternative implementation that would only fix the inconsistency, but not add support for elision with non-
constinitializers, would be to do this same thing, but only on theosimplementation, and only if the initializer isconst(i.e. split const initializers to a different macro arm; currently const and non-const initializers generate the same code under theosthread_local!implementation).Fixes #159538