Skip to content

Commit ac1d26b

Browse files
authoredFeb 14, 2021
Rollup merge of rust-lang#80920 - rylev:check_attr-refactor, r=davidtwco
Visit more targets when validating attributes This begins to address rust-lang#80048, allowing for additional validation of attributes. There are more refactorings that can be done, though I think they should be tackled in additional PRs: * ICE when a builtin attribute is encountered that is not checked * Move some of the attr checking done `ast_validation` into `rustc_passes` * note that this requires a bit of additional refactoring, especially of extern items which currently parse attributes (and thus are a part of the AST) but do not possess attributes in their HIR representation. * Rename `Target` to `AttributeTarget` * Refactor attribute validation completely to go through `Visitor::visit_attribute`. * This would require at a minimum passing `Target` into this method which might be too big of a refactoring to be worth it. * It's also likely not possible to do all the validation this way as some validation requires knowing what other attributes a target has. r? `@davidtwco`
2 parents 29ed864 + 9f0e1d4 commit ac1d26b

File tree

6 files changed

+50
-17
lines changed

6 files changed

+50
-17
lines changed
 

‎compiler/rustc_hir/src/target.rs

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub enum Target {
5454
ForeignTy,
5555
GenericParam(GenericParamKind),
5656
MacroDef,
57+
Param,
5758
}
5859

5960
impl Display for Target {
@@ -96,6 +97,7 @@ impl Display for Target {
9697
GenericParamKind::Const => "const parameter",
9798
},
9899
Target::MacroDef => "macro def",
100+
Target::Param => "function param",
99101
}
100102
)
101103
}

‎compiler/rustc_passes/src/check_attr.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -1101,17 +1101,6 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
11011101
intravisit::walk_arm(self, arm);
11021102
}
11031103

1104-
fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef<'tcx>) {
1105-
self.check_attributes(
1106-
macro_def.hir_id,
1107-
&macro_def.attrs,
1108-
&macro_def.span,
1109-
Target::MacroDef,
1110-
None,
1111-
);
1112-
intravisit::walk_macro_def(self, macro_def);
1113-
}
1114-
11151104
fn visit_foreign_item(&mut self, f_item: &'tcx ForeignItem<'tcx>) {
11161105
let target = Target::from_foreign_item(f_item);
11171106
self.check_attributes(
@@ -1157,6 +1146,23 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
11571146
self.check_attributes(variant.id, variant.attrs, &variant.span, Target::Variant, None);
11581147
intravisit::walk_variant(self, variant, generics, item_id)
11591148
}
1149+
1150+
fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef<'tcx>) {
1151+
self.check_attributes(
1152+
macro_def.hir_id,
1153+
macro_def.attrs,
1154+
&macro_def.span,
1155+
Target::MacroDef,
1156+
None,
1157+
);
1158+
intravisit::walk_macro_def(self, macro_def);
1159+
}
1160+
1161+
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
1162+
self.check_attributes(param.hir_id, param.attrs, &param.span, Target::Param, None);
1163+
1164+
intravisit::walk_param(self, param);
1165+
}
11601166
}
11611167

11621168
fn is_c_like_enum(item: &Item<'_>) -> bool {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This checks that incorrect params on function parameters are caught
2+
3+
fn function(#[inline] param: u32) {
4+
//~^ ERROR attribute should be applied to function or closure
5+
//~| ERROR allow, cfg, cfg_attr, deny, forbid, and warn are the only allowed built-in attributes
6+
}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: allow, cfg, cfg_attr, deny, forbid, and warn are the only allowed built-in attributes in function parameters
2+
--> $DIR/attrs-on-params.rs:3:13
3+
|
4+
LL | fn function(#[inline] param: u32) {
5+
| ^^^^^^^^^
6+
7+
error[E0518]: attribute should be applied to function or closure
8+
--> $DIR/attrs-on-params.rs:3:13
9+
|
10+
LL | fn function(#[inline] param: u32) {
11+
| ^^^^^^^^^-----------
12+
| |
13+
| not a function or closure
14+
15+
error: aborting due to 2 previous errors
16+
17+
For more information about this error, try `rustc --explain E0518`.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#![feature(decl_macro)] //~ ERROR `feature` is ambiguous
44

55
extern crate builtin_attrs;
6-
use builtin_attrs::{test, bench};
76
use builtin_attrs::*;
7+
use builtin_attrs::{bench, test};
88

99
#[repr(C)] //~ ERROR `repr` is ambiguous
1010
struct S;

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | #[repr(C)]
1212
|
1313
= note: `repr` could refer to a built-in attribute
1414
note: `repr` could also refer to the attribute macro imported here
15-
--> $DIR/ambiguous-builtin-attrs.rs:7:5
15+
--> $DIR/ambiguous-builtin-attrs.rs:6:5
1616
|
1717
LL | use builtin_attrs::*;
1818
| ^^^^^^^^^^^^^^^^
@@ -26,7 +26,7 @@ LL | #[cfg_attr(all(), repr(C))]
2626
|
2727
= note: `repr` could refer to a built-in attribute
2828
note: `repr` could also refer to the attribute macro imported here
29-
--> $DIR/ambiguous-builtin-attrs.rs:7:5
29+
--> $DIR/ambiguous-builtin-attrs.rs:6:5
3030
|
3131
LL | use builtin_attrs::*;
3232
| ^^^^^^^^^^^^^^^^
@@ -40,7 +40,7 @@ LL | fn non_macro_expanded_location<#[repr(C)] T>() {
4040
|
4141
= note: `repr` could refer to a built-in attribute
4242
note: `repr` could also refer to the attribute macro imported here
43-
--> $DIR/ambiguous-builtin-attrs.rs:7:5
43+
--> $DIR/ambiguous-builtin-attrs.rs:6:5
4444
|
4545
LL | use builtin_attrs::*;
4646
| ^^^^^^^^^^^^^^^^
@@ -54,7 +54,7 @@ LL | #[repr(C)]
5454
|
5555
= note: `repr` could refer to a built-in attribute
5656
note: `repr` could also refer to the attribute macro imported here
57-
--> $DIR/ambiguous-builtin-attrs.rs:7:5
57+
--> $DIR/ambiguous-builtin-attrs.rs:6:5
5858
|
5959
LL | use builtin_attrs::*;
6060
| ^^^^^^^^^^^^^^^^
@@ -82,7 +82,7 @@ LL | #![feature(decl_macro)]
8282
|
8383
= note: `feature` could refer to a built-in attribute
8484
note: `feature` could also refer to the attribute macro imported here
85-
--> $DIR/ambiguous-builtin-attrs.rs:7:5
85+
--> $DIR/ambiguous-builtin-attrs.rs:6:5
8686
|
8787
LL | use builtin_attrs::*;
8888
| ^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)
Please sign in to comment.