summary refs log tree commit diff
path: root/tests/ui/thread-local
diff options
context:
space:
mode:
authorAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-05 09:13:28 +0100
committerAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-11 09:32:08 +0000
commitcf2dff2b1e3fa55fa5415d524200070d0d7aacfe (patch)
tree40a88d9a46aaf3e8870676eb2538378b75a263eb /tests/ui/thread-local
parentca855e6e42787ecd062d81d53336fe6788ef51a9 (diff)
downloadrust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.tar.gz
rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.zip
Move /src/test to /tests
Diffstat (limited to 'tests/ui/thread-local')
-rw-r--r--tests/ui/thread-local/name-collision.rs15
-rw-r--r--tests/ui/thread-local/non-static.rs30
-rw-r--r--tests/ui/thread-local/non-static.stderr38
-rw-r--r--tests/ui/thread-local/thread-local-issue-37508.rs36
-rw-r--r--tests/ui/thread-local/tls.rs14
5 files changed, 133 insertions, 0 deletions
diff --git a/tests/ui/thread-local/name-collision.rs b/tests/ui/thread-local/name-collision.rs
new file mode 100644
index 00000000000..dcff9183ad9
--- /dev/null
+++ b/tests/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/tests/ui/thread-local/non-static.rs b/tests/ui/thread-local/non-static.rs
new file mode 100644
index 00000000000..f1c4273870b
--- /dev/null
+++ b/tests/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/tests/ui/thread-local/non-static.stderr b/tests/ui/thread-local/non-static.stderr
new file mode 100644
index 00000000000..09a1618d6e7
--- /dev/null
+++ b/tests/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/tests/ui/thread-local/thread-local-issue-37508.rs b/tests/ui/thread-local/thread-local-issue-37508.rs
new file mode 100644
index 00000000000..219108c77f7
--- /dev/null
+++ b/tests/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/tests/ui/thread-local/tls.rs b/tests/ui/thread-local/tls.rs
new file mode 100644
index 00000000000..fbd3413885f
--- /dev/null
+++ b/tests/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);
+}