11use rustc_feature:: { AttributeTemplate , template} ;
22use rustc_hir:: attrs:: { AttributeKind , CoverageAttrKind , OptimizeAttr , UsedBy } ;
3+ use rustc_hir:: { MethodKind , Target } ;
34use rustc_session:: parse:: feature_err;
45use rustc_span:: { Span , Symbol , sym} ;
56
67use super :: {
78 AcceptMapping , AttributeOrder , AttributeParser , CombineAttributeParser , ConvertFn ,
89 NoArgsAttributeParser , OnDuplicate , SingleAttributeParser ,
910} ;
10- use crate :: context:: { AcceptContext , FinalizeContext , Stage } ;
11+ use crate :: context:: MaybeWarn :: { Allow , Warn } ;
12+ use crate :: context:: { AcceptContext , AllowedTargets , FinalizeContext , Stage } ;
1113use crate :: parser:: ArgParser ;
1214use crate :: session_diagnostics:: { NakedFunctionIncompatibleAttribute , NullOnExport } ;
1315
@@ -17,6 +19,13 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
1719 const PATH : & [ Symbol ] = & [ sym:: optimize] ;
1820 const ATTRIBUTE_ORDER : AttributeOrder = AttributeOrder :: KeepOutermost ;
1921 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+ ] ) ;
2029 const TEMPLATE : AttributeTemplate = template ! ( List : "size|speed|none" ) ;
2130
2231 fn convert ( cx : & mut AcceptContext < ' _ , ' _ , S > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > {
@@ -49,6 +58,15 @@ pub(crate) struct ColdParser;
4958impl < S : Stage > NoArgsAttributeParser < S > for ColdParser {
5059 const PATH : & [ Symbol ] = & [ sym:: cold] ;
5160 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+ ] ) ;
5270 const CREATE : fn ( Span ) -> AttributeKind = AttributeKind :: Cold ;
5371}
5472
@@ -58,6 +76,16 @@ impl<S: Stage> SingleAttributeParser<S> for CoverageParser {
5876 const PATH : & [ Symbol ] = & [ sym:: coverage] ;
5977 const ATTRIBUTE_ORDER : AttributeOrder = AttributeOrder :: KeepOutermost ;
6078 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+ ] ) ;
6189 const TEMPLATE : AttributeTemplate = template ! ( OneOf : & [ sym:: off, sym:: on] ) ;
6290
6391 fn convert ( cx : & mut AcceptContext < ' _ , ' _ , S > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > {
@@ -97,6 +125,16 @@ impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {
97125 const PATH : & [ rustc_span:: Symbol ] = & [ sym:: export_name] ;
98126 const ATTRIBUTE_ORDER : AttributeOrder = AttributeOrder :: KeepInnermost ;
99127 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+ ] ) ;
100138 const TEMPLATE : AttributeTemplate = template ! ( NameValueStr : "name" ) ;
101139
102140 fn convert ( cx : & mut AcceptContext < ' _ , ' _ , S > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > {
@@ -138,6 +176,12 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
138176 this. span = Some ( cx. attr_span ) ;
139177 }
140178 } ) ] ;
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+ ] ) ;
141185
142186 fn finalize ( self , cx : & FinalizeContext < ' _ , ' _ , S > ) -> Option < AttributeKind > {
143187 // FIXME(jdonszelmann): upgrade this list to *parsed* attributes
@@ -230,13 +274,31 @@ pub(crate) struct TrackCallerParser;
230274impl < S : Stage > NoArgsAttributeParser < S > for TrackCallerParser {
231275 const PATH : & [ Symbol ] = & [ sym:: track_caller] ;
232276 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+ ] ) ;
233289 const CREATE : fn ( Span ) -> AttributeKind = AttributeKind :: TrackCaller ;
234290}
235291
236292pub ( crate ) struct NoMangleParser ;
237293impl < S : Stage > NoArgsAttributeParser < S > for NoMangleParser {
238294 const PATH : & [ Symbol ] = & [ sym:: no_mangle] ;
239295 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+ ] ) ;
240302 const CREATE : fn ( Span ) -> AttributeKind = AttributeKind :: NoMangle ;
241303}
242304
@@ -310,6 +372,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
310372 }
311373 } ,
312374 ) ] ;
375+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [ Allow ( Target :: Static ) ] ) ;
313376
314377 fn finalize ( self , _cx : & FinalizeContext < ' _ , ' _ , S > ) -> Option < AttributeKind > {
315378 // Ratcheting behaviour, if both `linker` and `compiler` are specified, use `linker`
@@ -373,4 +436,15 @@ impl<S: Stage> CombineAttributeParser<S> for TargetFeatureParser {
373436 }
374437 features
375438 }
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+ ] ) ;
376450}
0 commit comments