Skip to content

Commit f836cf7

Browse files
Wip
1 parent dd186c0 commit f836cf7

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl<S: Stage> SingleAttributeParser<S> for DeprecationParser {
4747
Target::Union,
4848
Target::Const,
4949
Target::Static,
50+
Target::MacroDef,
5051
];
5152
const TEMPLATE: AttributeTemplate = template!(
5253
Word,

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<S: Stage> AttributeParser<S> for MacroUseParser {
110110
}
111111
},
112112
)];
113-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Mod, Target::ExternCrate];
113+
const ALLOWED_TARGETS: &'static [Target] = &[Target::Mod, Target::ExternCrate, Target::Crate]; //TODO crate?
114114

115115
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
116116
Some(AttributeKind::MacroUse { span: self.first_span?, arguments: self.state })

compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ macro_rules! reject_outside_std {
2626
};
2727
}
2828

29+
const ALLOWED_TARGETS: &[Target] = &[Target::Fn,
30+
Target::Struct,
31+
Target::Method(MethodKind::Inherent),
32+
Target::Impl { of_trait: true },
33+
Target::MacroDef];
34+
2935
#[derive(Default)]
3036
pub(crate) struct StabilityParser {
3137
allowed_through_unstable_modules: Option<Symbol>,
@@ -87,12 +93,7 @@ impl<S: Stage> AttributeParser<S> for StabilityParser {
8793
},
8894
),
8995
];
90-
const ALLOWED_TARGETS: &'static [Target] = &[
91-
Target::Fn,
92-
Target::Struct,
93-
Target::Method(MethodKind::Inherent),
94-
Target::Impl { of_trait: true },
95-
];
96+
const ALLOWED_TARGETS: &'static [Target] = ALLOWED_TARGETS;
9697

9798
fn finalize(mut self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
9899
if let Some(atum) = self.allowed_through_unstable_modules {
@@ -148,7 +149,7 @@ impl<S: Stage> AttributeParser<S> for BodyStabilityParser {
148149
}
149150
},
150151
)];
151-
const ALLOWED_TARGETS: &'static [Target] = &[Target::AssocConst, Target::Fn];
152+
const ALLOWED_TARGETS: &'static [Target] = ALLOWED_TARGETS;
152153

153154
fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
154155
let (stability, span) = self.stability?;
@@ -213,7 +214,7 @@ impl<S: Stage> AttributeParser<S> for ConstStabilityParser {
213214
this.promotable = true;
214215
}),
215216
];
216-
const ALLOWED_TARGETS: &'static [Target] = &[Target::Fn, Target::ForeignFn];
217+
const ALLOWED_TARGETS: &'static [Target] = ALLOWED_TARGETS;
217218

218219
fn finalize(mut self, cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> {
219220
if self.promotable {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ struct GroupTypeInner<S: Stage> {
7272
struct GroupTypeInnerAccept<S: Stage> {
7373
template: AttributeTemplate,
7474
accept_fn: AcceptFn<S>,
75+
allowed_targets: &'static [Target],
7576
}
7677

7778
type AcceptFn<S> =
@@ -119,7 +120,8 @@ macro_rules! attribute_parsers {
119120
STATE_OBJECT.with_borrow_mut(|s| {
120121
accept_fn(s, cx, args)
121122
})
122-
})
123+
}),
124+
allowed_targets: <$names as crate::attributes::AttributeParser<$stage>>::ALLOWED_TARGETS,
123125
}).is_some() {
124126
panic!("Found two attribute parsers for attribute {path:?}");
125127
}
@@ -847,7 +849,11 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
847849
attr_path: path.get_attribute_path(),
848850
};
849851

850-
(accept.accept_fn)(&mut cx, args)
852+
(accept.accept_fn)(&mut cx, args);
853+
854+
if !accept.allowed_targets.contains(&target) {
855+
self.dcx().span_bug(n.item.span(), format!("{:?} target {target:?} not allowed {:?}", parts, accept.allowed_targets));
856+
}
851857
} else {
852858
// If we're here, we must be compiling a tool attribute... Or someone
853859
// forgot to parse their fancy new attribute. Let's warn them in any case.

0 commit comments

Comments
 (0)