From 32c648c48fb6ca0bc22ce1023d8ab09a8732736d Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 22 Jun 2024 15:25:46 +0200 Subject: [PATCH] add test for main thread thread-local destructors --- tests/ui/thread-local/main-thread-dtor.rs | 33 +++++++++++++++++++ .../thread-local/main-thread-dtor.run.stdout | 2 ++ 2 files changed, 35 insertions(+) create mode 100644 tests/ui/thread-local/main-thread-dtor.rs create mode 100644 tests/ui/thread-local/main-thread-dtor.run.stdout diff --git a/tests/ui/thread-local/main-thread-dtor.rs b/tests/ui/thread-local/main-thread-dtor.rs new file mode 100644 index 0000000000000..b17c601a6ce4e --- /dev/null +++ b/tests/ui/thread-local/main-thread-dtor.rs @@ -0,0 +1,33 @@ +//! Ensure that TLS destructors run on the main thread. +//@ run-pass +//@ check-run-results +// targets without threads tend to implement thread-locals as `static`s so no dtors are running +//@ needs-threads +// some targets do not run dtors on the main thread (issue #126858) +//@ ignore-musl +//@ ignore-android + +struct Bar; + +impl Drop for Bar { + fn drop(&mut self) { + println!("Bar dtor"); + } +} + +struct Foo; + +impl Drop for Foo { + fn drop(&mut self) { + println!("Foo dtor"); + // We initialize another thread-local inside the dtor, which is an interesting corner case. + thread_local!(static BAR: Bar = Bar); + BAR.with(|_| {}); + } +} + +thread_local!(static FOO: Foo = Foo); + +fn main() { + FOO.with(|_| {}); +} diff --git a/tests/ui/thread-local/main-thread-dtor.run.stdout b/tests/ui/thread-local/main-thread-dtor.run.stdout new file mode 100644 index 0000000000000..6160f2726492d --- /dev/null +++ b/tests/ui/thread-local/main-thread-dtor.run.stdout @@ -0,0 +1,2 @@ +Foo dtor +Bar dtor