about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-12-19 04:32:50 +0000
committerbors <bors@rust-lang.org>2020-12-19 04:32:50 +0000
commit3d9ada686fb42bd036b3a4916526f413f1d5d1f8 (patch)
treefe8db8ba112aba65766fd64d94c3179d0552d115 /compiler
parentd1741e59cb87d3c8c794eebf6d5430d1b383f51f (diff)
parent75eb72cc9c23d31f10bd7b4ec6331dd8383cf829 (diff)
downloadrust-3d9ada686fb42bd036b3a4916526f413f1d5d1f8.tar.gz
rust-3d9ada686fb42bd036b3a4916526f413f1d5d1f8.zip
Auto merge of #79073 - davidtwco:issue-78957-const-param-attrs, r=lcnr
passes: prohibit invalid attrs on generic params

Fixes #78957.

This PR modifies the `check_attr` pass so that attribute placement on generic parameters is checked for validity.

r? `@lcnr`
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_hir/src/target.rs23
-rw-r--r--compiler/rustc_passes/src/check_attr.rs12
2 files changed, 35 insertions, 0 deletions
diff --git a/compiler/rustc_hir/src/target.rs b/compiler/rustc_hir/src/target.rs
index b870e4c6ead..2774cc9c08e 100644
--- a/compiler/rustc_hir/src/target.rs
+++ b/compiler/rustc_hir/src/target.rs
@@ -10,6 +10,13 @@ use crate::{Item, ItemKind, TraitItem, TraitItemKind};
 use std::fmt::{self, Display};
 
 #[derive(Copy, Clone, PartialEq, Debug)]
+pub enum GenericParamKind {
+    Type,
+    Lifetime,
+    Const,
+}
+
+#[derive(Copy, Clone, PartialEq, Debug)]
 pub enum MethodKind {
     Trait { body: bool },
     Inherent,
@@ -43,6 +50,7 @@ pub enum Target {
     ForeignFn,
     ForeignStatic,
     ForeignTy,
+    GenericParam(GenericParamKind),
 }
 
 impl Display for Target {
@@ -77,6 +85,11 @@ impl Display for Target {
                 Target::ForeignFn => "foreign function",
                 Target::ForeignStatic => "foreign static item",
                 Target::ForeignTy => "foreign type",
+                Target::GenericParam(kind) => match kind {
+                    GenericParamKind::Type => "type parameter",
+                    GenericParamKind::Lifetime => "lifetime parameter",
+                    GenericParamKind::Const => "const parameter",
+                },
             }
         )
     }
@@ -124,4 +137,14 @@ impl Target {
             hir::ForeignItemKind::Type => Target::ForeignTy,
         }
     }
+
+    pub fn from_generic_param(generic_param: &hir::GenericParam<'_>) -> Target {
+        match generic_param.kind {
+            hir::GenericParamKind::Type { .. } => Target::GenericParam(GenericParamKind::Type),
+            hir::GenericParamKind::Lifetime { .. } => {
+                Target::GenericParam(GenericParamKind::Lifetime)
+            }
+            hir::GenericParamKind::Const { .. } => Target::GenericParam(GenericParamKind::Const),
+        }
+    }
 }
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index 73fb28e5c9a..aeaa862f5fd 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -882,6 +882,18 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
         intravisit::walk_item(self, item)
     }
 
+    fn visit_generic_param(&mut self, generic_param: &'tcx hir::GenericParam<'tcx>) {
+        let target = Target::from_generic_param(generic_param);
+        self.check_attributes(
+            generic_param.hir_id,
+            generic_param.attrs,
+            &generic_param.span,
+            target,
+            None,
+        );
+        intravisit::walk_generic_param(self, generic_param)
+    }
+
     fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem<'tcx>) {
         let target = Target::from_trait_item(trait_item);
         self.check_attributes(trait_item.hir_id, &trait_item.attrs, &trait_item.span, target, None);