]> git.lizzy.rs Git - rust.git/commitdiff
validate allow_internal_unstable target
authorFlorian Warzecha <liketechnik@disroot.org>
Wed, 21 Oct 2020 20:49:08 +0000 (22:49 +0200)
committerFlorian Warzecha <liketechnik@disroot.org>
Wed, 21 Oct 2020 20:49:08 +0000 (22:49 +0200)
Adds a check to make sure `#[allow_internal_unstable]`
can be applied only to macro definitions.

compiler/rustc_passes/src/check_attr.rs
src/test/ui/feature-gates/feature-gate-allow-internal-unstable-struct.rs
src/test/ui/feature-gates/feature-gate-allow-internal-unstable-struct.stderr

index 1acaa4c6eff5d60e22cc53fe0ccc050ba81c481e..95e6cc3b863c41548c17204fbb812f2e4084b253 100644 (file)
@@ -85,6 +85,8 @@ fn check_attributes(
                 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 @@ fn check_used(&self, attrs: &'hir [Attribute], target: Target) {
             }
         }
     }
+
+    /// 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> {
index ede969097d5ae2209b8f5c6cfbc0c766b68bbcf9..8b13f1bf2788e60868d6498c516dd80b0e95813e 100644 (file)
@@ -2,6 +2,7 @@
 // this needs a different test since this is done after expansion
 
 #[allow_internal_unstable()] //~ ERROR allow_internal_unstable side-steps
+//~| ERROR attribute should
 struct S;
 
 fn main() {}
index a1acfd553738f04a9fcc76b9f978456c63e6248c..df7773ba4fb65a7ccb23b9041244d52412d1c092 100644 (file)
@@ -6,6 +6,15 @@ LL | #[allow_internal_unstable()]
    |
    = help: add `#![feature(allow_internal_unstable)]` to the crate attributes to enable
 
-error: aborting due to previous error
+error: attribute should be applied to a macro
+  --> $DIR/feature-gate-allow-internal-unstable-struct.rs:4:1
+   |
+LL | #[allow_internal_unstable()]
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+LL |
+LL | struct S;
+   | --------- not a macro
+
+error: aborting due to 2 previous errors
 
 For more information about this error, try `rustc --explain E0658`.