1
1
use rustc_feature:: { AttributeTemplate , template} ;
2
2
use rustc_hir:: attrs:: { AttributeKind , CoverageAttrKind , OptimizeAttr , UsedBy } ;
3
+ use rustc_hir:: { MethodKind , Target } ;
3
4
use rustc_session:: parse:: feature_err;
4
5
use rustc_span:: { Span , Symbol , sym} ;
5
6
6
7
use super :: {
7
8
AcceptMapping , AttributeOrder , AttributeParser , CombineAttributeParser , ConvertFn ,
8
9
NoArgsAttributeParser , OnDuplicate , SingleAttributeParser ,
9
10
} ;
10
- use crate :: context:: { AcceptContext , FinalizeContext , Stage } ;
11
+ use crate :: context:: MaybeWarn :: { Allow , Warn } ;
12
+ use crate :: context:: { AcceptContext , AllowedTargets , FinalizeContext , Stage } ;
11
13
use crate :: parser:: ArgParser ;
12
14
use crate :: session_diagnostics:: { NakedFunctionIncompatibleAttribute , NullOnExport } ;
13
15
@@ -17,6 +19,13 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
17
19
const PATH : & [ Symbol ] = & [ sym:: optimize] ;
18
20
const ATTRIBUTE_ORDER : AttributeOrder = AttributeOrder :: KeepOutermost ;
19
21
const ON_DUPLICATE : OnDuplicate < S > = OnDuplicate :: WarnButFutureError ;
22
+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [
23
+ Allow ( Target :: Fn ) ,
24
+ Allow ( Target :: Closure ) ,
25
+ Allow ( Target :: Method ( MethodKind :: Trait { body : true } ) ) ,
26
+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
27
+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
28
+ ] ) ;
20
29
const TEMPLATE : AttributeTemplate = template ! ( List : "size|speed|none" ) ;
21
30
22
31
fn convert ( cx : & mut AcceptContext < ' _ , ' _ , S > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > {
@@ -49,6 +58,15 @@ pub(crate) struct ColdParser;
49
58
impl < S : Stage > NoArgsAttributeParser < S > for ColdParser {
50
59
const PATH : & [ Symbol ] = & [ sym:: cold] ;
51
60
const ON_DUPLICATE : OnDuplicate < S > = OnDuplicate :: Warn ;
61
+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowListWarnRest ( & [
62
+ Allow ( Target :: Fn ) ,
63
+ Allow ( Target :: Method ( MethodKind :: Trait { body : true } ) ) ,
64
+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
65
+ Allow ( Target :: Method ( MethodKind :: Trait { body : false } ) ) ,
66
+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
67
+ Allow ( Target :: ForeignFn ) ,
68
+ Allow ( Target :: Closure ) ,
69
+ ] ) ;
52
70
const CREATE : fn ( Span ) -> AttributeKind = AttributeKind :: Cold ;
53
71
}
54
72
@@ -58,6 +76,16 @@ impl<S: Stage> SingleAttributeParser<S> for CoverageParser {
58
76
const PATH : & [ Symbol ] = & [ sym:: coverage] ;
59
77
const ATTRIBUTE_ORDER : AttributeOrder = AttributeOrder :: KeepOutermost ;
60
78
const ON_DUPLICATE : OnDuplicate < S > = OnDuplicate :: Error ;
79
+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [
80
+ Allow ( Target :: Fn ) ,
81
+ Allow ( Target :: Closure ) ,
82
+ Allow ( Target :: Method ( MethodKind :: Trait { body : true } ) ) ,
83
+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
84
+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
85
+ Allow ( Target :: Impl { of_trait : true } ) ,
86
+ Allow ( Target :: Impl { of_trait : false } ) ,
87
+ Allow ( Target :: Mod ) ,
88
+ ] ) ;
61
89
const TEMPLATE : AttributeTemplate = template ! ( OneOf : & [ sym:: off, sym:: on] ) ;
62
90
63
91
fn convert ( cx : & mut AcceptContext < ' _ , ' _ , S > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > {
@@ -97,6 +125,16 @@ impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {
97
125
const PATH : & [ rustc_span:: Symbol ] = & [ sym:: export_name] ;
98
126
const ATTRIBUTE_ORDER : AttributeOrder = AttributeOrder :: KeepInnermost ;
99
127
const ON_DUPLICATE : OnDuplicate < S > = OnDuplicate :: WarnButFutureError ;
128
+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [
129
+ Allow ( Target :: Static ) ,
130
+ Allow ( Target :: Fn ) ,
131
+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
132
+ Allow ( Target :: Method ( MethodKind :: Trait { body : true } ) ) ,
133
+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
134
+ Warn ( Target :: Field ) ,
135
+ Warn ( Target :: Arm ) ,
136
+ Warn ( Target :: MacroDef ) ,
137
+ ] ) ;
100
138
const TEMPLATE : AttributeTemplate = template ! ( NameValueStr : "name" ) ;
101
139
102
140
fn convert ( cx : & mut AcceptContext < ' _ , ' _ , S > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > {
@@ -138,6 +176,12 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
138
176
this. span = Some ( cx. attr_span ) ;
139
177
}
140
178
} ) ] ;
179
+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [
180
+ Allow ( Target :: Fn ) ,
181
+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
182
+ Allow ( Target :: Method ( MethodKind :: Trait { body : true } ) ) ,
183
+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
184
+ ] ) ;
141
185
142
186
fn finalize ( self , cx : & FinalizeContext < ' _ , ' _ , S > ) -> Option < AttributeKind > {
143
187
// FIXME(jdonszelmann): upgrade this list to *parsed* attributes
@@ -230,13 +274,31 @@ pub(crate) struct TrackCallerParser;
230
274
impl < S : Stage > NoArgsAttributeParser < S > for TrackCallerParser {
231
275
const PATH : & [ Symbol ] = & [ sym:: track_caller] ;
232
276
const ON_DUPLICATE : OnDuplicate < S > = OnDuplicate :: Warn ;
277
+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [
278
+ Allow ( Target :: Fn ) ,
279
+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
280
+ Allow ( Target :: Method ( MethodKind :: Trait { body : true } ) ) ,
281
+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
282
+ Allow ( Target :: Method ( MethodKind :: Trait { body : false } ) ) ,
283
+ Allow ( Target :: ForeignFn ) ,
284
+ Allow ( Target :: Closure ) ,
285
+ Warn ( Target :: MacroDef ) ,
286
+ Warn ( Target :: Arm ) ,
287
+ Warn ( Target :: Field ) ,
288
+ ] ) ;
233
289
const CREATE : fn ( Span ) -> AttributeKind = AttributeKind :: TrackCaller ;
234
290
}
235
291
236
292
pub ( crate ) struct NoMangleParser ;
237
293
impl < S : Stage > NoArgsAttributeParser < S > for NoMangleParser {
238
294
const PATH : & [ Symbol ] = & [ sym:: no_mangle] ;
239
295
const ON_DUPLICATE : OnDuplicate < S > = OnDuplicate :: Warn ;
296
+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowListWarnRest ( & [
297
+ Allow ( Target :: Fn ) ,
298
+ Allow ( Target :: Static ) ,
299
+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
300
+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
301
+ ] ) ;
240
302
const CREATE : fn ( Span ) -> AttributeKind = AttributeKind :: NoMangle ;
241
303
}
242
304
@@ -310,6 +372,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
310
372
}
311
373
} ,
312
374
) ] ;
375
+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [ Allow ( Target :: Static ) ] ) ;
313
376
314
377
fn finalize ( self , _cx : & FinalizeContext < ' _ , ' _ , S > ) -> Option < AttributeKind > {
315
378
// Ratcheting behaviour, if both `linker` and `compiler` are specified, use `linker`
@@ -373,4 +436,15 @@ impl<S: Stage> CombineAttributeParser<S> for TargetFeatureParser {
373
436
}
374
437
features
375
438
}
439
+
440
+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [
441
+ Allow ( Target :: Fn ) ,
442
+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
443
+ Allow ( Target :: Method ( MethodKind :: Trait { body : true } ) ) ,
444
+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
445
+ Warn ( Target :: Statement ) ,
446
+ Warn ( Target :: Field ) ,
447
+ Warn ( Target :: Arm ) ,
448
+ Warn ( Target :: MacroDef ) ,
449
+ ] ) ;
376
450
}
0 commit comments