Skip to content

Commit bd01b49

Browse files
Don't allow duplicate attribute parsers
1 parent ca77504 commit bd01b49

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use crate::session_diagnostics::{AttributeParseError, AttributeParseErrorReason,
6565
type GroupType<S> = LazyLock<GroupTypeInner<S>>;
6666

6767
struct GroupTypeInner<S: Stage> {
68-
accepters: BTreeMap<&'static [Symbol], Vec<GroupTypeInnerAccept<S>>>,
68+
accepters: BTreeMap<&'static [Symbol], GroupTypeInnerAccept<S>>,
6969
finalizers: Vec<FinalizeFn<S>>,
7070
}
7171

@@ -104,7 +104,7 @@ macro_rules! attribute_parsers {
104104
@[$stage: ty] pub(crate) static $name: ident = [$($names: ty),* $(,)?];
105105
) => {
106106
pub(crate) static $name: GroupType<$stage> = LazyLock::new(|| {
107-
let mut accepts = BTreeMap::<_, Vec<GroupTypeInnerAccept<$stage>>>::new();
107+
let mut accepts = BTreeMap::<_, GroupTypeInnerAccept<$stage>>::new();
108108
let mut finalizes = Vec::<FinalizeFn<$stage>>::new();
109109
$(
110110
{
@@ -113,14 +113,16 @@ macro_rules! attribute_parsers {
113113
};
114114

115115
for (path, template, accept_fn) in <$names>::ATTRIBUTES {
116-
accepts.entry(*path).or_default().push(GroupTypeInnerAccept {
116+
if accepts.insert(*path, GroupTypeInnerAccept {
117117
template: *template,
118118
accept_fn: Box::new(|cx, args| {
119119
STATE_OBJECT.with_borrow_mut(|s| {
120120
accept_fn(s, cx, args)
121121
})
122122
})
123-
});
123+
}).is_some() {
124+
panic!("Found two attribute parsers for attribute {path:?}");
125+
}
124126
}
125127

126128
finalizes.push(Box::new(|cx| {
@@ -830,22 +832,20 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
830832
let args = parser.args();
831833
let parts = path.segments().map(|i| i.name).collect::<Vec<_>>();
832834

833-
if let Some(accepts) = S::parsers().accepters.get(parts.as_slice()) {
834-
for accept in accepts {
835-
let mut cx: AcceptContext<'_, 'sess, S> = AcceptContext {
836-
shared: SharedContext {
837-
cx: self,
838-
target_span,
839-
target_id,
840-
emit_lint: &mut emit_lint,
841-
},
842-
attr_span: lower_span(attr.span),
843-
template: &accept.template,
844-
attr_path: path.get_attribute_path(),
845-
};
846-
847-
(accept.accept_fn)(&mut cx, args)
848-
}
835+
if let Some(accept) = S::parsers().accepters.get(parts.as_slice()) {
836+
let mut cx: AcceptContext<'_, 'sess, S> = AcceptContext {
837+
shared: SharedContext {
838+
cx: self,
839+
target_span,
840+
target_id,
841+
emit_lint: &mut emit_lint,
842+
},
843+
attr_span: lower_span(attr.span),
844+
template: &accept.template,
845+
attr_path: path.get_attribute_path(),
846+
};
847+
848+
(accept.accept_fn)(&mut cx, args)
849849
} else {
850850
// If we're here, we must be compiling a tool attribute... Or someone
851851
// forgot to parse their fancy new attribute. Let's warn them in any case.

0 commit comments

Comments
 (0)