]> git.lizzy.rs Git - rust.git/commitdiff
validate rustc_allow_const_fn_unstable targets
authorFlorian Warzecha <liketechnik@disroot.org>
Wed, 21 Oct 2020 22:02:26 +0000 (00:02 +0200)
committerFlorian Warzecha <liketechnik@disroot.org>
Wed, 21 Oct 2020 22:02:26 +0000 (00:02 +0200)
Adds a check to make sure `#[rustc_allow_const_fn_unstable]`
can be applied only to functions.

compiler/rustc_passes/src/check_attr.rs

index 95e6cc3b863c41548c17204fbb812f2e4084b253..d6936ae942cdd1ab1b7a9066aa3ddb2cc6ec1678 100644 (file)
@@ -87,6 +87,8 @@ fn check_attributes(
                 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 if self.tcx.sess.check_name(attr, sym::rustc_allow_const_fn_unstable) {
+                self.check_rustc_allow_const_fn_unstable(&attr, span, target)
             } else {
                 // lint-only checks
                 if self.tcx.sess.check_name(attr, sym::cold) {
@@ -791,6 +793,26 @@ fn check_allow_internal_unstable(
             .emit();
         false
     }
+
+    /// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
+    /// (Allows proc_macro functions)
+    fn check_rustc_allow_const_fn_unstable(
+        &self,
+        attr: &Attribute,
+        span: &Span,
+        target: Target,
+    ) -> bool {
+        if let Target::Fn | Target::Method(_) = target {
+            // FIXME Check that this isn't just a function, but a const fn
+            return true;
+        }
+        self.tcx
+            .sess
+            .struct_span_err(attr.span, "attribute should be applied to `const fn`")
+            .span_label(*span, "not a `const fn`")
+            .emit();
+        false
+    }
 }
 
 impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {