rust/tests/ui/threads-sendsync/tls-dont-move-after-init.rs
Orson Peters 22c5e1d686 Add test
2025-05-29 02:47:23 +02:00

40 lines
650 B
Rust

//@ run-pass
#![allow(stable_features)]
//@ needs-threads
#![feature(thread_local_try_with)]
use std::cell::Cell;
use std::thread;
#[derive(Default)]
struct Foo {
ptr: Cell<*const Foo>,
}
impl Foo {
fn touch(&self) {
if self.ptr.get().is_null() {
self.ptr.set(self);
} else {
assert!(self.ptr.get() == self);
}
}
}
impl Drop for Foo {
fn drop(&mut self) {
self.touch();
}
}
thread_local!(static FOO: Foo = Foo::default());
fn main() {
thread::spawn(|| {
FOO.with(|foo| foo.touch());
FOO.with(|foo| foo.touch());
})
.join()
.unwrap();
}