@@ -63,33 +63,30 @@ pub(crate) struct DelegationResults<'hir> {
6363}
6464
6565struct AttributeAdditionInfo {
66- pub factory : fn ( Span ) -> hir:: Attribute ,
6766 pub equals : fn ( & hir:: Attribute ) -> bool ,
6867 pub kind : AttributeAdditionKind ,
6968}
7069
71- struct AttributeAdditionState {
72- pub info : & ' static AttributeAdditionInfo ,
73- pub already_exists : bool ,
74- }
75-
7670enum AttributeAdditionKind {
77- Default ,
78- Inherit { flag : DelegationFnSigAttrs } ,
71+ Default { factory : fn ( Span ) -> hir :: Attribute } ,
72+ Inherit { flag : DelegationFnSigAttrs , factory : fn ( Span ) -> hir :: Attribute } ,
7973}
8074
8175const PARENT_ID : hir:: ItemLocalId = hir:: ItemLocalId :: ZERO ;
8276
83- static ATTRIBUTES_ADDITIONS : & ' static [ AttributeAdditionInfo ] = & [
77+ static ATTRIBUTES_ADDITIONS : & [ AttributeAdditionInfo ] = & [
8478 AttributeAdditionInfo {
85- factory : |span| hir:: Attribute :: Parsed ( AttributeKind :: MustUse { span, reason : None } ) ,
8679 equals : |a| matches ! ( a, hir:: Attribute :: Parsed ( AttributeKind :: MustUse { .. } ) ) ,
87- kind : AttributeAdditionKind :: Inherit { flag : DelegationFnSigAttrs :: MUST_USE } ,
80+ kind : AttributeAdditionKind :: Inherit {
81+ factory : |span| hir:: Attribute :: Parsed ( AttributeKind :: MustUse { span, reason : None } ) ,
82+ flag : DelegationFnSigAttrs :: MUST_USE ,
83+ } ,
8884 } ,
8985 AttributeAdditionInfo {
90- factory : |span| hir:: Attribute :: Parsed ( AttributeKind :: Inline ( InlineAttr :: Hint , span) ) ,
9186 equals : |a| matches ! ( a, hir:: Attribute :: Parsed ( AttributeKind :: Inline ( ..) ) ) ,
92- kind : AttributeAdditionKind :: Default ,
87+ kind : AttributeAdditionKind :: Default {
88+ factory : |span| hir:: Attribute :: Parsed ( AttributeKind :: Inline ( InlineAttr :: Hint , span) ) ,
89+ } ,
9390 } ,
9491] ;
9592
@@ -135,13 +132,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
135132 }
136133
137134 fn add_attributes_if_needed ( & mut self , span : Span , sig_id : DefId ) {
138- let mut candidate_additions = ATTRIBUTES_ADDITIONS
139- . iter ( )
140- . map ( |info| AttributeAdditionState { info, already_exists : false } )
141- . collect :: < Vec < _ > > ( ) ;
142-
143- self . mark_existing_attributes ( & mut candidate_additions) ;
144- let new_attributes = self . create_new_attributes ( & candidate_additions, span, sig_id) ;
135+ let new_attributes = self . create_new_attributes (
136+ ATTRIBUTES_ADDITIONS ,
137+ span,
138+ sig_id,
139+ self . attrs . get ( & PARENT_ID ) ,
140+ ) ;
145141
146142 if new_attributes. is_empty ( ) {
147143 return ;
@@ -157,35 +153,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
157153 self . attrs . insert ( PARENT_ID , new_arena_allocated_attributes) ;
158154 }
159155
160- fn mark_existing_attributes ( & mut self , candidate_additions : & mut Vec < AttributeAdditionState > ) {
161- let Some ( existing_attrs) = self . attrs . get ( & PARENT_ID ) else { return } ;
162-
163- for attr in existing_attrs. iter ( ) {
164- for addition in candidate_additions. iter_mut ( ) {
165- if addition. already_exists {
166- continue ;
167- }
168-
169- if ( addition. info . equals ) ( attr) {
170- addition. already_exists = true ;
171- }
172- }
173- }
174- }
175-
176156 fn create_new_attributes (
177- & mut self ,
178- candidate_additions : & Vec < AttributeAdditionState > ,
157+ & self ,
158+ candidate_additions : & [ AttributeAdditionInfo ] ,
179159 span : Span ,
180160 sig_id : DefId ,
161+ existing_attrs : Option < & & [ hir:: Attribute ] > ,
181162 ) -> Vec < hir:: Attribute > {
182163 candidate_additions
183164 . iter ( )
184- . filter_map ( |a| match a. already_exists {
185- true => None ,
186- false => match a. info . kind {
187- AttributeAdditionKind :: Default => Some ( ( a. info . factory ) ( span) ) ,
188- AttributeAdditionKind :: Inherit { flag } => {
165+ . filter_map ( |addition_info| {
166+ if let Some ( existing_attrs) = existing_attrs
167+ && existing_attrs
168+ . iter ( )
169+ . any ( |existing_attr| ( addition_info. equals ) ( existing_attr) )
170+ {
171+ return None ;
172+ }
173+
174+ match addition_info. kind {
175+ AttributeAdditionKind :: Default { factory } => Some ( factory ( span) ) ,
176+ AttributeAdditionKind :: Inherit { flag, factory } => {
189177 // Check if an attribute present on the target of delegation,
190178 // either in this crate or other
191179 let should_inherit = match sig_id. as_local ( ) {
@@ -200,15 +188,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
200188 . tcx
201189 . get_all_attrs ( sig_id)
202190 . iter ( )
203- . any ( |base_attr| ( a . info . equals ) ( base_attr) ) ,
191+ . any ( |base_attr| ( addition_info . equals ) ( base_attr) ) ,
204192 } ;
205193
206194 match should_inherit {
207- true => Some ( ( a . info . factory ) ( span) ) ,
195+ true => Some ( factory ( span) ) ,
208196 false => None ,
209197 }
210198 }
211- } ,
199+ }
212200 } )
213201 . collect :: < Vec < _ > > ( )
214202 }
0 commit comments