about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorTomasz Miąsko <tomasz.miasko@gmail.com>2022-03-16 00:00:00 +0000
committerTomasz Miąsko <tomasz.miasko@gmail.com>2022-03-16 13:00:33 +0100
commit5e7610378f5dce4f3cfdaf05fbafc962d000ffcd (patch)
tree293ff233b52ccb0739973ffd2756212d741bd33c /compiler
parentd8e564715e0eb17130e99e8fcc92a36fce7feaf5 (diff)
Reject `#[thread_local]` attribute on non-static items
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_passes/src/check_attr.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index 06184b47972..8c1d6188e7c 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)
                 }
@@ -521,6 +522,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