about summary refs log tree commit diff
path: root/compiler/rustc_passes/src
diff options
context:
space:
mode:
authorFlorian Warzecha <liketechnik@disroot.org>2020-10-21 22:49:08 +0200
committerFlorian Warzecha <liketechnik@disroot.org>2020-10-21 22:49:08 +0200
commit7258740509877ff97c32c52cea7ac236ba80c51a (patch)
tree8efa2eb4a04e000c2bbc4f493352bc92988914d7 /compiler/rustc_passes/src
parent05f4a9a42abbbdb64d3ec4ed0ae6883c10c66e3f (diff)
downloadrust-7258740509877ff97c32c52cea7ac236ba80c51a.tar.gz
rust-7258740509877ff97c32c52cea7ac236ba80c51a.zip
validate allow_internal_unstable target
Adds a check to make sure `#[allow_internal_unstable]`
can be applied only to macro definitions.
Diffstat (limited to 'compiler/rustc_passes/src')
-rw-r--r--compiler/rustc_passes/src/check_attr.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index 1acaa4c6eff..95e6cc3b863 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -85,6 +85,8 @@ impl CheckAttrVisitor<'tcx> {
                 self.check_export_name(&attr, span, target)
             } else if self.tcx.sess.check_name(attr, sym::rustc_args_required_const) {
                 self.check_rustc_args_required_const(&attr, span, target, item)
+            } else if self.tcx.sess.check_name(attr, sym::allow_internal_unstable) {
+                self.check_allow_internal_unstable(&attr, span, target, &attrs)
             } else {
                 // lint-only checks
                 if self.tcx.sess.check_name(attr, sym::cold) {
@@ -762,6 +764,33 @@ impl CheckAttrVisitor<'tcx> {
             }
         }
     }
+
+    /// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
+    /// (Allows proc_macro functions)
+    fn check_allow_internal_unstable(
+        &self,
+        attr: &Attribute,
+        span: &Span,
+        target: Target,
+        attrs: &[Attribute],
+    ) -> bool {
+        debug!("Checking target: {:?}", target);
+        if target == Target::Fn {
+            for attr in attrs {
+                if self.tcx.sess.is_proc_macro_attr(attr) {
+                    debug!("Is proc macro attr");
+                    return true;
+                }
+            }
+            debug!("Is not proc macro attr");
+        }
+        self.tcx
+            .sess
+            .struct_span_err(attr.span, "attribute should be applied to a macro")
+            .span_label(*span, "not a macro")
+            .emit();
+        false
+    }
 }
 
 impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {