1
- //! [`AttributeGroup`]s are groups of attributes (groups can be size 1) that are parsed together.
2
- //! An [`AttributeGroup`] implementation defines its parser.
3
- //!
4
- //! You can find more docs on what groups are on [`AttributeGroup`] itself.
5
- //! However, for many types of attributes, implementing [`AttributeGroup`] is not necessary.
1
+ //! You can find more docs on what groups are on [`AttributeParser`] itself.
2
+ //! However, for many types of attributes, implementing [`AttributeParser`] is not necessary.
6
3
//! It allows for a lot of flexibility you might not want.
7
4
//!
8
- //! Specifically, you care about managing the state of your [AttributeGroup] state machine
9
- //! yourself. In this case you can choose to implement:
5
+ //! Specifically, you might not care about managing the state of your [`AttributeParser`]
6
+ //! state machine yourself. In this case you can choose to implement:
10
7
//!
11
- //! - [`SingleAttributeGroup `]: makes it easy to implement an attribute which should error if it
8
+ //! - [`SingleAttributeParser `]: makes it easy to implement an attribute which should error if it
12
9
//! appears more than once in a list of attributes
13
- //! - [`CombineAttributeGroup `]: makes it easy to implement an attribute which should combine the
10
+ //! - [`CombineAttributeParser `]: makes it easy to implement an attribute which should combine the
14
11
//! contents of attributes, if an attribute appear multiple times in a list
15
12
//!
16
13
//! Attributes should be added to [`ATTRIBUTE_GROUP_MAPPING`](crate::context::ATTRIBUTE_GROUP_MAPPING) to be parsed.
@@ -21,7 +18,7 @@ use rustc_attr_data_structures::AttributeKind;
21
18
use rustc_span:: Span ;
22
19
use thin_vec:: ThinVec ;
23
20
24
- use crate :: context:: { AttributeAcceptContext , AttributeGroupContext } ;
21
+ use crate :: context:: { AcceptContext , FinalizeContext } ;
25
22
use crate :: parser:: ArgParser ;
26
23
27
24
pub ( crate ) mod allow_unstable;
@@ -33,60 +30,68 @@ pub(crate) mod stability;
33
30
pub ( crate ) mod transparency;
34
31
pub ( crate ) mod util;
35
32
36
- type AttributeHandler < T > = fn ( & mut T , & AttributeAcceptContext < ' _ > , & ArgParser < ' _ > ) ;
37
- type AttributeMapping < T > = & ' static [ ( & ' static [ rustc_span:: Symbol ] , AttributeHandler < T > ) ] ;
33
+ type AcceptFn < T > = fn ( & mut T , & AcceptContext < ' _ > , & ArgParser < ' _ > ) ;
34
+ type AcceptMapping < T > = & ' static [ ( & ' static [ rustc_span:: Symbol ] , AcceptFn < T > ) ] ;
38
35
39
- /// An [`AttributeGroup `] is a type which searches for syntactic attributes.
36
+ /// An [`AttributeParser `] is a type which searches for syntactic attributes.
40
37
///
41
- /// Groups are often tiny state machines. [`Default::default`]
42
- /// creates a new instance that sits in some kind of initial state, usually that the
38
+ /// Parsers are often tiny state machines that gets to see all syntactical attributes on an item.
39
+ /// [`Default::default`] creates a fresh instance that sits in some kind of initial state, usually that the
43
40
/// attribute it is looking for was not yet seen.
44
41
///
45
- /// Then, it defines what paths this group will accept in [`AttributeGroup ::ATTRIBUTES`].
42
+ /// Then, it defines what paths this group will accept in [`AttributeParser ::ATTRIBUTES`].
46
43
/// These are listed as pairs, of symbols and function pointers. The function pointer will
47
- /// be called when that attribute is found on an item, which can influence the state.
44
+ /// be called when that attribute is found on an item, which can influence the state of the little
45
+ /// state machine.
46
+ ///
47
+ /// Finally, after all attributes on an item have been seen, and possibly been accepted,
48
+ /// the [`finalize`](AttributeParser::finalize) functions for all attribute parsers are called. Each can then report
49
+ /// whether it has seen the attribute it has been looking for.
48
50
///
49
- /// Finally, all `finalize` functions are called, for each piece of state,
50
- pub ( crate ) trait AttributeGroup : Default + ' static {
51
- /// The symbols for the attributes that this extractor can extract .
51
+ /// The state machine is automatically reset to parse attributes on the next item.
52
+ pub ( crate ) trait AttributeParser : Default + ' static {
53
+ /// The symbols for the attributes that this parser is interested in .
52
54
///
53
55
/// If an attribute has this symbol, the `accept` function will be called on it.
54
- const ATTRIBUTES : AttributeMapping < Self > ;
56
+ const ATTRIBUTES : AcceptMapping < Self > ;
55
57
56
- /// The extractor has gotten a chance to accept the attributes on an item,
57
- /// now produce an attribute.
58
- fn finalize ( self , cx : & AttributeGroupContext < ' _ > ) -> Option < AttributeKind > ;
58
+ /// The parser has gotten a chance to accept the attributes on an item,
59
+ /// here it can produce an attribute.
60
+ fn finalize ( self , cx : & FinalizeContext < ' _ > ) -> Option < AttributeKind > ;
59
61
}
60
62
61
- /// A slightly simpler and more restricted way to convert attributes which you can implement for
62
- /// unit types. Assumes that a single attribute can only appear a single time on an item
63
- /// [`SingleGroup<T> where T: SingleAttributeGroup`](Single) creates an [`AttributeGroup`] from any [`SingleAttributeGroup`].
63
+ /// Alternative to [`AttributeParser`] that automatically handles state management.
64
+ /// A slightly simpler and more restricted way to convert attributes.
65
+ /// Assumes that an attribute can only appear a single time on an item,
66
+ /// and errors when it sees more.
67
+ ///
68
+ /// [`Single<T> where T: SingleAttributeParser`](Single) implements [`AttributeParser`].
64
69
///
65
- /// [`SingleGroup `] can only convert attributes one-to-one, and cannot combine multiple
70
+ /// [`SingleAttributeParser `] can only convert attributes one-to-one, and cannot combine multiple
66
71
/// attributes together like is necessary for `#[stable()]` and `#[unstable()]` for example.
67
- pub ( crate ) trait SingleAttributeGroup : ' static {
72
+ pub ( crate ) trait SingleAttributeParser : ' static {
68
73
const PATH : & ' static [ rustc_span:: Symbol ] ;
69
74
70
75
/// Caled when a duplicate attribute is found.
71
76
///
72
77
/// `first_span` is the span of the first occurrence of this attribute.
73
- fn on_duplicate ( cx : & AttributeAcceptContext < ' _ > , first_span : Span ) ;
78
+ // FIXME(jdonszelmann): default error
79
+ fn on_duplicate ( cx : & AcceptContext < ' _ > , first_span : Span ) ;
74
80
75
- /// The extractor has gotten a chance to accept the attributes on an item,
76
- /// now produce an attribute.
77
- fn convert ( cx : & AttributeAcceptContext < ' _ > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > ;
81
+ /// Converts a single syntactical attribute to a single semantic attribute, or [`AttributeKind`]
82
+ fn convert ( cx : & AcceptContext < ' _ > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > ;
78
83
}
79
84
80
- pub ( crate ) struct Single < T : SingleAttributeGroup > ( PhantomData < T > , Option < ( AttributeKind , Span ) > ) ;
85
+ pub ( crate ) struct Single < T : SingleAttributeParser > ( PhantomData < T > , Option < ( AttributeKind , Span ) > ) ;
81
86
82
- impl < T : SingleAttributeGroup > Default for Single < T > {
87
+ impl < T : SingleAttributeParser > Default for Single < T > {
83
88
fn default ( ) -> Self {
84
89
Self ( Default :: default ( ) , Default :: default ( ) )
85
90
}
86
91
}
87
92
88
- impl < T : SingleAttributeGroup > AttributeGroup for Single < T > {
89
- const ATTRIBUTES : AttributeMapping < Self > = & [ ( T :: PATH , |group : & mut Single < T > , cx, args| {
93
+ impl < T : SingleAttributeParser > AttributeParser for Single < T > {
94
+ const ATTRIBUTES : AcceptMapping < Self > = & [ ( T :: PATH , |group : & mut Single < T > , cx, args| {
90
95
if let Some ( ( _, s) ) = group. 1 {
91
96
T :: on_duplicate ( cx, s) ;
92
97
return ;
@@ -97,50 +102,49 @@ impl<T: SingleAttributeGroup> AttributeGroup for Single<T> {
97
102
}
98
103
} ) ] ;
99
104
100
- fn finalize ( self , _cx : & AttributeGroupContext < ' _ > ) -> Option < AttributeKind > {
105
+ fn finalize ( self , _cx : & FinalizeContext < ' _ > ) -> Option < AttributeKind > {
101
106
Some ( self . 1 ?. 0 )
102
107
}
103
108
}
104
109
105
110
type ConvertFn < E > = fn ( ThinVec < E > ) -> AttributeKind ;
106
111
107
- /// A slightly simpler and more restricted way to convert attributes which you can implement for
108
- /// unit types. If multiple attributes appear on an element, combines the values of each into a
112
+ /// Alternative to [`AttributeParser`] that automatically handles state management.
113
+ /// If multiple attributes appear on an element, combines the values of each into a
109
114
/// [`ThinVec`].
110
- /// [`CombineGroup <T> where T: CombineAttributeGroup `](Combine) creates an [`AttributeGroup`] from any [`CombineAttributeGroup `].
115
+ /// [`Combine <T> where T: CombineAttributeParser `](Combine) implements [`AttributeParser `].
111
116
///
112
- /// [`CombineAttributeGroup `] can only convert a single kind of attribute, and cannot combine multiple
117
+ /// [`CombineAttributeParser `] can only convert a single kind of attribute, and cannot combine multiple
113
118
/// attributes together like is necessary for `#[stable()]` and `#[unstable()]` for example.
114
- pub ( crate ) trait CombineAttributeGroup : ' static {
119
+ pub ( crate ) trait CombineAttributeParser : ' static {
115
120
const PATH : & ' static [ rustc_span:: Symbol ] ;
116
121
117
122
type Item ;
118
123
const CONVERT : ConvertFn < Self :: Item > ;
119
124
120
- /// The extractor has gotten a chance to accept the attributes on an item,
121
- /// now produce an attribute.
125
+ /// Converts a single syntactical attribute to a number of elements of the semantic attribute, or [`AttributeKind`]
122
126
fn extend < ' a > (
123
- cx : & ' a AttributeAcceptContext < ' a > ,
127
+ cx : & ' a AcceptContext < ' a > ,
124
128
args : & ' a ArgParser < ' a > ,
125
129
) -> impl IntoIterator < Item = Self :: Item > + ' a ;
126
130
}
127
131
128
- pub ( crate ) struct Combine < T : CombineAttributeGroup > (
132
+ pub ( crate ) struct Combine < T : CombineAttributeParser > (
129
133
PhantomData < T > ,
130
- ThinVec < <T as CombineAttributeGroup >:: Item > ,
134
+ ThinVec < <T as CombineAttributeParser >:: Item > ,
131
135
) ;
132
136
133
- impl < T : CombineAttributeGroup > Default for Combine < T > {
137
+ impl < T : CombineAttributeParser > Default for Combine < T > {
134
138
fn default ( ) -> Self {
135
139
Self ( Default :: default ( ) , Default :: default ( ) )
136
140
}
137
141
}
138
142
139
- impl < T : CombineAttributeGroup > AttributeGroup for Combine < T > {
140
- const ATTRIBUTES : AttributeMapping < Self > =
143
+ impl < T : CombineAttributeParser > AttributeParser for Combine < T > {
144
+ const ATTRIBUTES : AcceptMapping < Self > =
141
145
& [ ( T :: PATH , |group : & mut Combine < T > , cx, args| group. 1 . extend ( T :: extend ( cx, args) ) ) ] ;
142
146
143
- fn finalize ( self , _cx : & AttributeGroupContext < ' _ > ) -> Option < AttributeKind > {
147
+ fn finalize ( self , _cx : & FinalizeContext < ' _ > ) -> Option < AttributeKind > {
144
148
if self . 1 . is_empty ( ) { None } else { Some ( T :: CONVERT ( self . 1 ) ) }
145
149
}
146
150
}
0 commit comments