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,17 @@ 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+ Allow ( Target :: Crate ) ,
89+ ] ) ;
6190 const TEMPLATE : AttributeTemplate = template ! ( OneOf : & [ sym:: off, sym:: on] ) ;
6291
6392 fn convert ( cx : & mut AcceptContext < ' _ , ' _ , S > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > {
@@ -97,6 +126,16 @@ impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {
97126 const PATH : & [ rustc_span:: Symbol ] = & [ sym:: export_name] ;
98127 const ATTRIBUTE_ORDER : AttributeOrder = AttributeOrder :: KeepInnermost ;
99128 const ON_DUPLICATE : OnDuplicate < S > = OnDuplicate :: WarnButFutureError ;
129+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [
130+ Allow ( Target :: Static ) ,
131+ Allow ( Target :: Fn ) ,
132+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
133+ Allow ( Target :: Method ( MethodKind :: Trait { body : true } ) ) ,
134+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
135+ Warn ( Target :: Field ) ,
136+ Warn ( Target :: Arm ) ,
137+ Warn ( Target :: MacroDef ) ,
138+ ] ) ;
100139 const TEMPLATE : AttributeTemplate = template ! ( NameValueStr : "name" ) ;
101140
102141 fn convert ( cx : & mut AcceptContext < ' _ , ' _ , S > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > {
@@ -138,6 +177,12 @@ impl<S: Stage> AttributeParser<S> for NakedParser {
138177 this. span = Some ( cx. attr_span ) ;
139178 }
140179 } ) ] ;
180+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [
181+ Allow ( Target :: Fn ) ,
182+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
183+ Allow ( Target :: Method ( MethodKind :: Trait { body : true } ) ) ,
184+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
185+ ] ) ;
141186
142187 fn finalize ( self , cx : & FinalizeContext < ' _ , ' _ , S > ) -> Option < AttributeKind > {
143188 // FIXME(jdonszelmann): upgrade this list to *parsed* attributes
@@ -230,13 +275,31 @@ pub(crate) struct TrackCallerParser;
230275impl < S : Stage > NoArgsAttributeParser < S > for TrackCallerParser {
231276 const PATH : & [ Symbol ] = & [ sym:: track_caller] ;
232277 const ON_DUPLICATE : OnDuplicate < S > = OnDuplicate :: Warn ;
278+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [
279+ Allow ( Target :: Fn ) ,
280+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
281+ Allow ( Target :: Method ( MethodKind :: Trait { body : true } ) ) ,
282+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
283+ Allow ( Target :: Method ( MethodKind :: Trait { body : false } ) ) ,
284+ Allow ( Target :: ForeignFn ) ,
285+ Allow ( Target :: Closure ) ,
286+ Warn ( Target :: MacroDef ) ,
287+ Warn ( Target :: Arm ) ,
288+ Warn ( Target :: Field ) ,
289+ ] ) ;
233290 const CREATE : fn ( Span ) -> AttributeKind = AttributeKind :: TrackCaller ;
234291}
235292
236293pub ( crate ) struct NoMangleParser ;
237294impl < S : Stage > NoArgsAttributeParser < S > for NoMangleParser {
238295 const PATH : & [ Symbol ] = & [ sym:: no_mangle] ;
239296 const ON_DUPLICATE : OnDuplicate < S > = OnDuplicate :: Warn ;
297+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowListWarnRest ( & [
298+ Allow ( Target :: Fn ) ,
299+ Allow ( Target :: Static ) ,
300+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
301+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
302+ ] ) ;
240303 const CREATE : fn ( Span ) -> AttributeKind = AttributeKind :: NoMangle ;
241304}
242305
@@ -310,6 +373,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
310373 }
311374 } ,
312375 ) ] ;
376+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [ Allow ( Target :: Static ) ] ) ;
313377
314378 fn finalize ( self , _cx : & FinalizeContext < ' _ , ' _ , S > ) -> Option < AttributeKind > {
315379 // Ratcheting behaviour, if both `linker` and `compiler` are specified, use `linker`
@@ -373,4 +437,15 @@ impl<S: Stage> CombineAttributeParser<S> for TargetFeatureParser {
373437 }
374438 features
375439 }
440+
441+ const ALLOWED_TARGETS : AllowedTargets = AllowedTargets :: AllowList ( & [
442+ Allow ( Target :: Fn ) ,
443+ Allow ( Target :: Method ( MethodKind :: Inherent ) ) ,
444+ Allow ( Target :: Method ( MethodKind :: Trait { body : true } ) ) ,
445+ Allow ( Target :: Method ( MethodKind :: TraitImpl ) ) ,
446+ Warn ( Target :: Statement ) ,
447+ Warn ( Target :: Field ) ,
448+ Warn ( Target :: Arm ) ,
449+ Warn ( Target :: MacroDef ) ,
450+ ] ) ;
376451}
0 commit comments