@@ -62,15 +62,23 @@ use crate::attributes::{AttributeParser as _, Combine, Single, WithoutArgs};
62
62
use crate :: parser:: { ArgParser , MetaItemParser , PathParser } ;
63
63
use crate :: session_diagnostics:: { AttributeParseError , AttributeParseErrorReason , UnknownMetaItem } ;
64
64
65
- macro_rules! group_type {
66
- ( $stage: ty) => {
67
- LazyLock <(
68
- BTreeMap <& ' static [ Symbol ] , Vec <( AttributeTemplate , Box <dyn for <' sess, ' a> Fn ( & mut AcceptContext <' _, ' sess, $stage>, & ArgParser <' a>) + Send + Sync >) >>,
69
- Vec <Box <dyn Send + Sync + Fn ( & mut FinalizeContext <' _, ' _, $stage>) -> Option <AttributeKind >>>
70
- ) >
71
- } ;
65
+ type GroupType < S > = LazyLock < GroupTypeInner < S > > ;
66
+
67
+ struct GroupTypeInner < S : Stage > {
68
+ accepters : BTreeMap < & ' static [ Symbol ] , Vec < GroupTypeInnerAccept < S > > > ,
69
+ finalizers : Vec < FinalizeFn < S > > ,
70
+ }
71
+
72
+ struct GroupTypeInnerAccept < S : Stage > {
73
+ template : AttributeTemplate ,
74
+ accept_fn : AcceptFn < S > ,
72
75
}
73
76
77
+ type AcceptFn < S > =
78
+ Box < dyn for <' sess , ' a > Fn ( & mut AcceptContext < ' _ , ' sess , S > , & ArgParser < ' a > ) + Send + Sync > ;
79
+ type FinalizeFn < S > =
80
+ Box < dyn Send + Sync + Fn ( & mut FinalizeContext < ' _ , ' _ , S > ) -> Option < AttributeKind > > ;
81
+
74
82
macro_rules! attribute_parsers {
75
83
(
76
84
pub ( crate ) static $name: ident = [ $( $names: ty) ,* $( , ) ?] ;
@@ -93,23 +101,26 @@ macro_rules! attribute_parsers {
93
101
}
94
102
} ;
95
103
(
96
- @[ $ty : ty] pub ( crate ) static $name: ident = [ $( $names: ty) ,* $( , ) ?] ;
104
+ @[ $stage : ty] pub ( crate ) static $name: ident = [ $( $names: ty) ,* $( , ) ?] ;
97
105
) => {
98
- pub ( crate ) static $name: group_type! ( $ty ) = LazyLock :: new( || {
99
- let mut accepts = BTreeMap :: <_, Vec <( AttributeTemplate , Box <dyn for < ' sess , ' a> Fn ( & mut AcceptContext < ' _ , ' sess , $ty> , & ArgParser < ' a> ) + Send + Sync > ) >>:: new( ) ;
100
- let mut finalizes = Vec :: <Box <dyn Send + Sync + Fn ( & mut FinalizeContext < ' _ , ' _ , $ty> ) -> Option < AttributeKind > >>:: new( ) ;
106
+ pub ( crate ) static $name: GroupType <$stage> = LazyLock :: new( || {
107
+ let mut accepts = BTreeMap :: <_, Vec <GroupTypeInnerAccept <$stage> >>:: new( ) ;
108
+ let mut finalizes = Vec :: <FinalizeFn <$stage >>:: new( ) ;
101
109
$(
102
110
{
103
111
thread_local! {
104
112
static STATE_OBJECT : RefCell <$names> = RefCell :: new( <$names>:: default ( ) ) ;
105
113
} ;
106
114
107
115
for ( path, template, accept_fn) in <$names>:: ATTRIBUTES {
108
- accepts. entry( * path) . or_default( ) . push( ( * template, Box :: new( |cx, args| {
109
- STATE_OBJECT . with_borrow_mut( |s| {
110
- accept_fn( s, cx, args)
116
+ accepts. entry( * path) . or_default( ) . push( GroupTypeInnerAccept {
117
+ template: * template,
118
+ accept_fn: Box :: new( |cx, args| {
119
+ STATE_OBJECT . with_borrow_mut( |s| {
120
+ accept_fn( s, cx, args)
121
+ } )
111
122
} )
112
- } ) ) ) ;
123
+ } ) ;
113
124
}
114
125
115
126
finalizes. push( Box :: new( |cx| {
@@ -119,7 +130,7 @@ macro_rules! attribute_parsers {
119
130
}
120
131
) *
121
132
122
- ( accepts, finalizes)
133
+ GroupTypeInner { accepters : accepts, finalizers : finalizes }
123
134
} ) ;
124
135
} ;
125
136
}
@@ -215,7 +226,7 @@ pub trait Stage: Sized + 'static + Sealed {
215
226
type Id : Copy ;
216
227
const SHOULD_EMIT_LINTS : bool ;
217
228
218
- fn parsers ( ) -> & ' static group_type ! ( Self ) ;
229
+ fn parsers ( ) -> & ' static GroupType < Self > ;
219
230
220
231
fn emit_err < ' sess > (
221
232
& self ,
@@ -230,7 +241,7 @@ impl Stage for Early {
230
241
type Id = NodeId ;
231
242
const SHOULD_EMIT_LINTS : bool = false ;
232
243
233
- fn parsers ( ) -> & ' static group_type ! ( Self ) {
244
+ fn parsers ( ) -> & ' static GroupType < Self > {
234
245
& early:: ATTRIBUTE_PARSERS
235
246
}
236
247
fn emit_err < ' sess > (
@@ -252,7 +263,7 @@ impl Stage for Late {
252
263
type Id = HirId ;
253
264
const SHOULD_EMIT_LINTS : bool = true ;
254
265
255
- fn parsers ( ) -> & ' static group_type ! ( Self ) {
266
+ fn parsers ( ) -> & ' static GroupType < Self > {
256
267
& late:: ATTRIBUTE_PARSERS
257
268
}
258
269
fn emit_err < ' sess > (
@@ -811,8 +822,8 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
811
822
let args = parser. args ( ) ;
812
823
let parts = path. segments ( ) . map ( |i| i. name ) . collect :: < Vec < _ > > ( ) ;
813
824
814
- if let Some ( accepts) = S :: parsers ( ) . 0 . get ( parts. as_slice ( ) ) {
815
- for ( template , accept) in accepts {
825
+ if let Some ( accepts) = S :: parsers ( ) . accepters . get ( parts. as_slice ( ) ) {
826
+ for accept in accepts {
816
827
let mut cx: AcceptContext < ' _ , ' sess , S > = AcceptContext {
817
828
shared : SharedContext {
818
829
cx : self ,
@@ -821,11 +832,11 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
821
832
emit_lint : & mut emit_lint,
822
833
} ,
823
834
attr_span : lower_span ( attr. span ) ,
824
- template,
835
+ template : & accept . template ,
825
836
attr_path : path. get_attribute_path ( ) ,
826
837
} ;
827
838
828
- accept ( & mut cx, args)
839
+ ( accept. accept_fn ) ( & mut cx, args)
829
840
}
830
841
} else {
831
842
// If we're here, we must be compiling a tool attribute... Or someone
@@ -856,7 +867,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
856
867
}
857
868
858
869
let mut parsed_attributes = Vec :: new ( ) ;
859
- for f in & S :: parsers ( ) . 1 {
870
+ for f in & S :: parsers ( ) . finalizers {
860
871
if let Some ( attr) = f ( & mut FinalizeContext {
861
872
shared : SharedContext {
862
873
cx : self ,
@@ -877,7 +888,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
877
888
878
889
/// Returns whether there is a parser for an attribute with this name
879
890
pub fn is_parsed_attribute ( path : & [ Symbol ] ) -> bool {
880
- Late :: parsers ( ) . 0 . contains_key ( path)
891
+ Late :: parsers ( ) . accepters . contains_key ( path)
881
892
}
882
893
883
894
fn lower_attr_args ( & self , args : & ast:: AttrArgs , lower_span : impl Fn ( Span ) -> Span ) -> AttrArgs {
0 commit comments