about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-04-16 19:42:02 +0200
committerGitHub <noreply@github.com>2022-04-16 19:42:02 +0200
commit3dced8029843ac9cfdeaa11400dec99d331a2779 (patch)
tree758ce0c05e34f57c4e05e78bb69f54265019a3dd
parent22d554657d08cff395911c5128be297e366899ec (diff)
parent5e7610378f5dce4f3cfdaf05fbafc962d000ffcd (diff)
downloadrust-3dced8029843ac9cfdeaa11400dec99d331a2779.tar.gz
rust-3dced8029843ac9cfdeaa11400dec99d331a2779.zip
Rollup merge of #95006 - tmiasko:thread-local-static, r=wesleywiser
Reject `#[thread_local]` attribute on non-static items
-rw-r--r--compiler/rustc_passes/src/check_attr.rs16
-rw-r--r--src/test/ui/thread-local/non-static.rs30
-rw-r--r--src/test/ui/thread-local/non-static.stderr38
3 files changed, 84 insertions, 0 deletions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index c45326e1e6e..a9444972130 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -80,6 +80,7 @@ impl CheckAttrVisitor<'_> {
                     self.check_rustc_must_implement_one_of(attr, span, target)
                 }
                 sym::target_feature => self.check_target_feature(hir_id, attr, span, target),
+                sym::thread_local => self.check_thread_local(attr, span, target),
                 sym::track_caller => {
                     self.check_track_caller(hir_id, attr.span, attrs, span, target)
                 }
@@ -523,6 +524,21 @@ impl CheckAttrVisitor<'_> {
         }
     }
 
+    /// Checks if the `#[thread_local]` attribute on `item` is valid. Returns `true` if valid.
+    fn check_thread_local(&self, attr: &Attribute, span: Span, target: Target) -> bool {
+        match target {
+            Target::ForeignStatic | Target::Static => true,
+            _ => {
+                self.tcx
+                    .sess
+                    .struct_span_err(attr.span, "attribute should be applied to a static")
+                    .span_label(span, "not a static")
+                    .emit();
+                false
+            }
+        }
+    }
+
     fn doc_attr_str_error(&self, meta: &NestedMetaItem, attr_name: &str) {
         self.tcx
             .sess
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 00000000000..f1c4273870b
--- /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 00000000000..09a1618d6e7
--- /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
+