summaryrefslogtreecommitdiffstats
path: root/src/test/ui/thread-local
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/thread-local
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/thread-local')
-rw-r--r--src/test/ui/thread-local/name-collision.rs15
-rw-r--r--src/test/ui/thread-local/non-static.rs30
-rw-r--r--src/test/ui/thread-local/non-static.stderr38
-rw-r--r--src/test/ui/thread-local/thread-local-issue-37508.rs36
-rw-r--r--src/test/ui/thread-local/tls.rs14
5 files changed, 133 insertions, 0 deletions
diff --git a/src/test/ui/thread-local/name-collision.rs b/src/test/ui/thread-local/name-collision.rs
new file mode 100644
index 000000000..dcff9183a
--- /dev/null
+++ b/src/test/ui/thread-local/name-collision.rs
@@ -0,0 +1,15 @@
+// check-pass
+
+#[allow(non_camel_case_types)]
+struct u8;
+
+std::thread_local! {
+ pub static A: i32 = f();
+ pub static B: i32 = const { 0 };
+}
+
+fn f() -> i32 {
+ 0
+}
+
+fn main() {}
diff --git a/src/test/ui/thread-local/non-static.rs b/src/test/ui/thread-local/non-static.rs
new file mode 100644
index 000000000..f1c427387
--- /dev/null
+++ b/src/test/ui/thread-local/non-static.rs
@@ -0,0 +1,30 @@
+// Check that #[thread_local] attribute is rejected on non-static items.
+#![feature(thread_local)]
+
+#[thread_local]
+//~^ ERROR attribute should be applied to a static
+const A: u32 = 0;
+
+#[thread_local]
+//~^ ERROR attribute should be applied to a static
+fn main() {
+ #[thread_local] || {};
+ //~^ ERROR attribute should be applied to a static
+}
+
+struct S {
+ #[thread_local]
+ //~^ ERROR attribute should be applied to a static
+ a: String,
+ b: String,
+}
+
+#[thread_local]
+// Static. OK.
+static B: u32 = 0;
+
+extern "C" {
+ #[thread_local]
+ // Foreign static. OK.
+ static C: u32;
+}
diff --git a/src/test/ui/thread-local/non-static.stderr b/src/test/ui/thread-local/non-static.stderr
new file mode 100644
index 000000000..09a1618d6
--- /dev/null
+++ b/src/test/ui/thread-local/non-static.stderr
@@ -0,0 +1,38 @@
+error: attribute should be applied to a static
+ --> $DIR/non-static.rs:4:1
+ |
+LL | #[thread_local]
+ | ^^^^^^^^^^^^^^^
+LL |
+LL | const A: u32 = 0;
+ | ----------------- not a static
+
+error: attribute should be applied to a static
+ --> $DIR/non-static.rs:8:1
+ |
+LL | #[thread_local]
+ | ^^^^^^^^^^^^^^^
+LL |
+LL | / fn main() {
+LL | | #[thread_local] || {};
+LL | |
+LL | | }
+ | |_- not a static
+
+error: attribute should be applied to a static
+ --> $DIR/non-static.rs:11:5
+ |
+LL | #[thread_local] || {};
+ | ^^^^^^^^^^^^^^^ ----- not a static
+
+error: attribute should be applied to a static
+ --> $DIR/non-static.rs:16:5
+ |
+LL | #[thread_local]
+ | ^^^^^^^^^^^^^^^
+LL |
+LL | a: String,
+ | --------- not a static
+
+error: aborting due to 4 previous errors
+
diff --git a/src/test/ui/thread-local/thread-local-issue-37508.rs b/src/test/ui/thread-local/thread-local-issue-37508.rs
new file mode 100644
index 000000000..219108c77
--- /dev/null
+++ b/src/test/ui/thread-local/thread-local-issue-37508.rs
@@ -0,0 +1,36 @@
+// only-x86_64
+// compile-flags: -Ccode-model=large --crate-type lib
+// build-pass
+//
+// Regression test for issue #37508
+
+#![no_main]
+#![no_std]
+#![feature(thread_local, lang_items)]
+
+#[lang = "eh_personality"]
+extern "C" fn eh_personality() {}
+
+use core::panic::PanicInfo;
+
+#[panic_handler]
+fn panic(_panic: &PanicInfo<'_>) -> ! {
+ loop {}
+}
+
+pub struct BB;
+
+#[thread_local]
+static mut KEY: Key = Key { inner: BB, dtor_running: false };
+
+pub unsafe fn set() -> Option<&'static BB> {
+ if KEY.dtor_running {
+ return None;
+ }
+ Some(&KEY.inner)
+}
+
+pub struct Key {
+ inner: BB,
+ dtor_running: bool,
+}
diff --git a/src/test/ui/thread-local/tls.rs b/src/test/ui/thread-local/tls.rs
new file mode 100644
index 000000000..fbd341388
--- /dev/null
+++ b/src/test/ui/thread-local/tls.rs
@@ -0,0 +1,14 @@
+// run-pass
+// ignore-emscripten no threads support
+// compile-flags: -O
+
+#![feature(thread_local)]
+
+#[thread_local]
+static S: u32 = 222;
+
+fn main() {
+ let local = &S as *const u32 as usize;
+ let foreign = std::thread::spawn(|| &S as *const u32 as usize).join().unwrap();
+ assert_ne!(local, foreign);
+}