Skip to content

Commit 3d9ada6

Browse files
committed
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`
2 parents d1741e5 + 75eb72c commit 3d9ada6

File tree

6 files changed

+147
-5
lines changed

6 files changed

+147
-5
lines changed

compiler/rustc_hir/src/target.rs

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ use crate::{Item, ItemKind, TraitItem, TraitItemKind};
99

1010
use std::fmt::{self, Display};
1111

12+
#[derive(Copy, Clone, PartialEq, Debug)]
13+
pub enum GenericParamKind {
14+
Type,
15+
Lifetime,
16+
Const,
17+
}
18+
1219
#[derive(Copy, Clone, PartialEq, Debug)]
1320
pub enum MethodKind {
1421
Trait { body: bool },
@@ -43,6 +50,7 @@ pub enum Target {
4350
ForeignFn,
4451
ForeignStatic,
4552
ForeignTy,
53+
GenericParam(GenericParamKind),
4654
}
4755

4856
impl Display for Target {
@@ -77,6 +85,11 @@ impl Display for Target {
7785
Target::ForeignFn => "foreign function",
7886
Target::ForeignStatic => "foreign static item",
7987
Target::ForeignTy => "foreign type",
88+
Target::GenericParam(kind) => match kind {
89+
GenericParamKind::Type => "type parameter",
90+
GenericParamKind::Lifetime => "lifetime parameter",
91+
GenericParamKind::Const => "const parameter",
92+
},
8093
}
8194
)
8295
}
@@ -124,4 +137,14 @@ impl Target {
124137
hir::ForeignItemKind::Type => Target::ForeignTy,
125138
}
126139
}
140+
141+
pub fn from_generic_param(generic_param: &hir::GenericParam<'_>) -> Target {
142+
match generic_param.kind {
143+
hir::GenericParamKind::Type { .. } => Target::GenericParam(GenericParamKind::Type),
144+
hir::GenericParamKind::Lifetime { .. } => {
145+
Target::GenericParam(GenericParamKind::Lifetime)
146+
}
147+
hir::GenericParamKind::Const { .. } => Target::GenericParam(GenericParamKind::Const),
148+
}
149+
}
127150
}

compiler/rustc_passes/src/check_attr.rs

+12
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,18 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
882882
intravisit::walk_item(self, item)
883883
}
884884

885+
fn visit_generic_param(&mut self, generic_param: &'tcx hir::GenericParam<'tcx>) {
886+
let target = Target::from_generic_param(generic_param);
887+
self.check_attributes(
888+
generic_param.hir_id,
889+
generic_param.attrs,
890+
&generic_param.span,
891+
target,
892+
None,
893+
);
894+
intravisit::walk_generic_param(self, generic_param)
895+
}
896+
885897
fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem<'tcx>) {
886898
let target = Target::from_trait_item(trait_item);
887899
self.check_attributes(trait_item.hir_id, &trait_item.attrs, &trait_item.span, target, None);

src/test/ui/issues/issue-78957.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![deny(unused_attributes)]
2+
#![feature(min_const_generics)]
3+
4+
use std::marker::PhantomData;
5+
6+
pub struct Foo<#[inline] const N: usize>;
7+
//~^ ERROR attribute should be applied to function or closure
8+
pub struct Bar<#[cold] const N: usize>;
9+
//~^ ERROR attribute should be applied to a function
10+
//~| WARN this was previously accepted
11+
pub struct Baz<#[repr(C)] const N: usize>;
12+
//~^ ERROR attribute should be applied to a struct, enum, or union
13+
//
14+
pub struct Foo2<#[inline] 'a>(PhantomData<&'a ()>);
15+
//~^ ERROR attribute should be applied to function or closure
16+
pub struct Bar2<#[cold] 'a>(PhantomData<&'a ()>);
17+
//~^ ERROR attribute should be applied to a function
18+
//~| WARN this was previously accepted
19+
pub struct Baz2<#[repr(C)] 'a>(PhantomData<&'a ()>);
20+
//~^ ERROR attribute should be applied to a struct, enum, or union
21+
//
22+
pub struct Foo3<#[inline] T>(PhantomData<T>);
23+
//~^ ERROR attribute should be applied to function or closure
24+
pub struct Bar3<#[cold] T>(PhantomData<T>);
25+
//~^ ERROR attribute should be applied to a function
26+
//~| WARN this was previously accepted
27+
pub struct Baz3<#[repr(C)] T>(PhantomData<T>);
28+
//~^ ERROR attribute should be applied to a struct, enum, or union
29+
30+
fn main() {}

src/test/ui/issues/issue-78957.stderr

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
error[E0518]: attribute should be applied to function or closure
2+
--> $DIR/issue-78957.rs:6:16
3+
|
4+
LL | pub struct Foo<#[inline] const N: usize>;
5+
| ^^^^^^^^^ - not a function or closure
6+
7+
error: attribute should be applied to a function
8+
--> $DIR/issue-78957.rs:8:16
9+
|
10+
LL | pub struct Bar<#[cold] const N: usize>;
11+
| ^^^^^^^ - not a function
12+
|
13+
note: the lint level is defined here
14+
--> $DIR/issue-78957.rs:1:9
15+
|
16+
LL | #![deny(unused_attributes)]
17+
| ^^^^^^^^^^^^^^^^^
18+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
19+
20+
error[E0517]: attribute should be applied to a struct, enum, or union
21+
--> $DIR/issue-78957.rs:11:23
22+
|
23+
LL | pub struct Baz<#[repr(C)] const N: usize>;
24+
| ^ - not a struct, enum, or union
25+
26+
error[E0518]: attribute should be applied to function or closure
27+
--> $DIR/issue-78957.rs:14:17
28+
|
29+
LL | pub struct Foo2<#[inline] 'a>(PhantomData<&'a ()>);
30+
| ^^^^^^^^^ -- not a function or closure
31+
32+
error: attribute should be applied to a function
33+
--> $DIR/issue-78957.rs:16:17
34+
|
35+
LL | pub struct Bar2<#[cold] 'a>(PhantomData<&'a ()>);
36+
| ^^^^^^^ -- not a function
37+
|
38+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
39+
40+
error[E0517]: attribute should be applied to a struct, enum, or union
41+
--> $DIR/issue-78957.rs:19:24
42+
|
43+
LL | pub struct Baz2<#[repr(C)] 'a>(PhantomData<&'a ()>);
44+
| ^ -- not a struct, enum, or union
45+
46+
error[E0518]: attribute should be applied to function or closure
47+
--> $DIR/issue-78957.rs:22:17
48+
|
49+
LL | pub struct Foo3<#[inline] T>(PhantomData<T>);
50+
| ^^^^^^^^^ - not a function or closure
51+
52+
error: attribute should be applied to a function
53+
--> $DIR/issue-78957.rs:24:17
54+
|
55+
LL | pub struct Bar3<#[cold] T>(PhantomData<T>);
56+
| ^^^^^^^ - not a function
57+
|
58+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
59+
60+
error[E0517]: attribute should be applied to a struct, enum, or union
61+
--> $DIR/issue-78957.rs:27:24
62+
|
63+
LL | pub struct Baz3<#[repr(C)] T>(PhantomData<T>);
64+
| ^ - not a struct, enum, or union
65+
66+
error: aborting due to 9 previous errors
67+
68+
Some errors have detailed explanations: E0517, E0518.
69+
For more information about an error, try `rustc --explain E0517`.

src/test/ui/proc-macro/ambiguous-builtin-attrs.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ fn test() {}
1717
#[bench] // OK, shadowed
1818
fn bench() {}
1919

20-
fn non_macro_expanded_location<#[repr(C)] T>() { //~ ERROR `repr` is ambiguous
20+
fn non_macro_expanded_location<#[repr(C)] T>() {
21+
//~^ ERROR `repr` is ambiguous
22+
//~| ERROR attribute should be applied to a struct, enum, or union
2123
match 0u8 {
2224
#[repr(C)] //~ ERROR `repr` is ambiguous
2325
_ => {}

src/test/ui/proc-macro/ambiguous-builtin-attrs.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0425]: cannot find value `NonExistent` in this scope
2-
--> $DIR/ambiguous-builtin-attrs.rs:30:5
2+
--> $DIR/ambiguous-builtin-attrs.rs:32:5
33
|
44
LL | NonExistent;
55
| ^^^^^^^^^^^ not found in this scope
@@ -47,7 +47,7 @@ LL | use builtin_attrs::*;
4747
= help: use `crate::repr` to refer to this attribute macro unambiguously
4848

4949
error[E0659]: `repr` is ambiguous (built-in attribute vs any other name)
50-
--> $DIR/ambiguous-builtin-attrs.rs:22:11
50+
--> $DIR/ambiguous-builtin-attrs.rs:24:11
5151
|
5252
LL | #[repr(C)]
5353
| ^^^^ ambiguous name
@@ -74,7 +74,13 @@ LL | use builtin_attrs::*;
7474
| ^^^^^^^^^^^^^^^^
7575
= help: use `crate::feature` to refer to this attribute macro unambiguously
7676

77-
error: aborting due to 6 previous errors
77+
error[E0517]: attribute should be applied to a struct, enum, or union
78+
--> $DIR/ambiguous-builtin-attrs.rs:20:39
79+
|
80+
LL | fn non_macro_expanded_location<#[repr(C)] T>() {
81+
| ^ - not a struct, enum, or union
82+
83+
error: aborting due to 7 previous errors
7884

79-
Some errors have detailed explanations: E0425, E0659.
85+
Some errors have detailed explanations: E0425, E0517, E0659.
8086
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)