Skip to content

Commit cfe6720

Browse files
committed
Auto merge of #151382 - JonathanBrouwer:attr-perf, r=<try>
Only run finalizers of accepted attributes
2 parents d940e56 + 3544db9 commit cfe6720

4 files changed

Lines changed: 35 additions & 33 deletions

File tree

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,18 @@ type GroupType<S> = LazyLock<GroupTypeInner<S>>;
103103

104104
pub(super) struct GroupTypeInner<S: Stage> {
105105
pub(super) accepters: BTreeMap<&'static [Symbol], Vec<GroupTypeInnerAccept<S>>>,
106-
pub(super) finalizers: Vec<FinalizeFn<S>>,
107106
}
108107

109108
pub(super) struct GroupTypeInnerAccept<S: Stage> {
110109
pub(super) template: AttributeTemplate,
111110
pub(super) accept_fn: AcceptFn<S>,
112111
pub(super) allowed_targets: AllowedTargets,
112+
pub(super) finalizer: FinalizeFn<S>,
113113
}
114114

115-
type AcceptFn<S> =
115+
pub(crate) type AcceptFn<S> =
116116
Box<dyn for<'sess, 'a> Fn(&mut AcceptContext<'_, 'sess, S>, &ArgParser) + Send + Sync>;
117-
type FinalizeFn<S> =
117+
pub(crate) type FinalizeFn<S> =
118118
Box<dyn Send + Sync + Fn(&mut FinalizeContext<'_, '_, S>) -> Option<AttributeKind>>;
119119

120120
macro_rules! attribute_parsers {
@@ -142,34 +142,32 @@ macro_rules! attribute_parsers {
142142
@[$stage: ty] pub(crate) static $name: ident = [$($names: ty),* $(,)?];
143143
) => {
144144
pub(crate) static $name: GroupType<$stage> = LazyLock::new(|| {
145-
let mut accepts = BTreeMap::<_, Vec<GroupTypeInnerAccept<$stage>>>::new();
146-
let mut finalizes = Vec::<FinalizeFn<$stage>>::new();
145+
let mut accepters = BTreeMap::<_, Vec<GroupTypeInnerAccept<$stage>>>::new();
147146
$(
148147
{
149148
thread_local! {
150149
static STATE_OBJECT: RefCell<$names> = RefCell::new(<$names>::default());
151150
};
152151

153152
for (path, template, accept_fn) in <$names>::ATTRIBUTES {
154-
accepts.entry(*path).or_default().push(GroupTypeInnerAccept {
153+
accepters.entry(*path).or_default().push(GroupTypeInnerAccept {
155154
template: *template,
156155
accept_fn: Box::new(|cx, args| {
157156
STATE_OBJECT.with_borrow_mut(|s| {
158157
accept_fn(s, cx, args)
159158
})
160159
}),
161160
allowed_targets: <$names as crate::attributes::AttributeParser<$stage>>::ALLOWED_TARGETS,
161+
finalizer: Box::new(|cx| {
162+
let state = STATE_OBJECT.take();
163+
state.finalize(cx)
164+
}),
162165
});
163166
}
164-
165-
finalizes.push(Box::new(|cx| {
166-
let state = STATE_OBJECT.take();
167-
state.finalize(cx)
168-
}));
169167
}
170168
)*
171169

172-
GroupTypeInner { accepters:accepts, finalizers:finalizes }
170+
GroupTypeInner { accepters }
173171
});
174172
};
175173
}

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_session::Session;
1212
use rustc_session::lint::{BuiltinLintDiag, LintId};
1313
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
1414

15-
use crate::context::{AcceptContext, FinalizeContext, SharedContext, Stage};
15+
use crate::context::{AcceptContext, FinalizeContext, FinalizeFn, SharedContext, Stage};
1616
use crate::early_parsed::{EARLY_PARSED_ATTRIBUTES, EarlyParsedState};
1717
use crate::parser::{ArgParser, PathParser, RefPathParser};
1818
use crate::session_diagnostics::ParsedDescription;
@@ -270,6 +270,8 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
270270
let mut attr_paths: Vec<RefPathParser<'_>> = Vec::new();
271271
let mut early_parsed_state = EarlyParsedState::default();
272272

273+
let mut finalizers: Vec<&FinalizeFn<S>> = Vec::with_capacity(attrs.len());
274+
273275
for attr in attrs {
274276
// If we're only looking for a single attribute, skip all the ones we don't care about.
275277
if let Some(expected) = self.parse_only {
@@ -383,6 +385,8 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
383385
};
384386

385387
(accept.accept_fn)(&mut cx, &args);
388+
finalizers.push(&accept.finalizer);
389+
386390
if !matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
387391
Self::check_target(&accept.allowed_targets, target, &mut cx);
388392
}
@@ -417,7 +421,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
417421
}
418422

419423
early_parsed_state.finalize_early_parsed_attributes(&mut attributes);
420-
for f in &S::parsers().finalizers {
424+
for f in &finalizers {
421425
if let Some(attr) = f(&mut FinalizeContext {
422426
shared: SharedContext { cx: self, target_span, target, emit_lint: &mut emit_lint },
423427
all_attrs: &attr_paths,
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error: can't mark as unstable using an already stable feature
2-
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:7:1
2+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:6:1
33
|
4+
LL | #[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
46
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
67
LL | const fn my_fun() {}
78
| -------------------- the stability attribute annotates this item
89
|
910
help: consider removing the attribute
10-
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:7:1
11+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:6:1
1112
|
12-
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
LL | #[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1415

1516
error: can't mark as unstable using an already stable feature
16-
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:6:1
17+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:7:1
1718
|
18-
LL | #[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
2019
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this feature is already stable
2121
LL | const fn my_fun() {}
2222
| -------------------- the stability attribute annotates this item
2323
|
2424
help: consider removing the attribute
25-
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:6:1
25+
--> $DIR/unstable-attribute-rejects-already-stable-features.rs:7:1
2626
|
27-
LL | #[unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
28-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27+
LL | #[rustc_const_unstable(feature = "arbitrary_enum_discriminant", issue = "42")]
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2929

3030
error: aborting due to 2 previous errors
3131

tests/ui/loop-match/invalid-attribute.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,6 @@ LL | #[const_continue]
9494
|
9595
= help: `#[const_continue]` can be applied to
9696

97-
error: `#[const_continue]` should be applied to a break expression
98-
--> $DIR/invalid-attribute.rs:40:9
99-
|
100-
LL | #[const_continue]
101-
| ^^^^^^^^^^^^^^^^^
102-
LL | 5
103-
| - not a break expression
104-
10597
error: `#[loop_match]` should be applied to a loop
10698
--> $DIR/invalid-attribute.rs:39:9
10799
|
@@ -111,5 +103,13 @@ LL | #[const_continue]
111103
LL | 5
112104
| - not a loop
113105

106+
error: `#[const_continue]` should be applied to a break expression
107+
--> $DIR/invalid-attribute.rs:40:9
108+
|
109+
LL | #[const_continue]
110+
| ^^^^^^^^^^^^^^^^^
111+
LL | 5
112+
| - not a break expression
113+
114114
error: aborting due to 14 previous errors
115115

0 commit comments

Comments
 (0)