@@ -301,6 +301,8 @@ pub enum InvocationKind {
301
301
} ,
302
302
Attr {
303
303
attr : ast:: Attribute ,
304
+ // Re-insertion position for inert attributes.
305
+ pos : usize ,
304
306
item : Annotatable ,
305
307
// Required for resolving derive helper attributes.
306
308
derives : Vec < Path > ,
@@ -690,7 +692,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
690
692
}
691
693
_ => unreachable ! ( ) ,
692
694
} ,
693
- InvocationKind :: Attr { attr, mut item, derives } => match ext {
695
+ InvocationKind :: Attr { attr, pos , mut item, derives } => match ext {
694
696
SyntaxExtensionKind :: Attr ( expander) => {
695
697
self . gate_proc_macro_input ( & item) ;
696
698
self . gate_proc_macro_attr_item ( span, & item) ;
@@ -721,7 +723,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
721
723
ExpandResult :: Retry ( item) => {
722
724
// Reassemble the original invocation for retrying.
723
725
return ExpandResult :: Retry ( Invocation {
724
- kind : InvocationKind :: Attr { attr, item, derives } ,
726
+ kind : InvocationKind :: Attr { attr, pos , item, derives } ,
725
727
..invoc
726
728
} ) ;
727
729
}
@@ -739,7 +741,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
739
741
if * mark_used {
740
742
self . cx . sess . mark_attr_used ( & attr) ;
741
743
}
742
- item. visit_attrs ( |attrs| attrs. push ( attr) ) ;
744
+ item. visit_attrs ( |attrs| attrs. insert ( pos , attr) ) ;
743
745
fragment_kind. expect_from_annotatables ( iter:: once ( item) )
744
746
}
745
747
_ => unreachable ! ( ) ,
@@ -1000,17 +1002,20 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
1000
1002
1001
1003
fn collect_attr (
1002
1004
& mut self ,
1003
- ( attr, derives) : ( ast:: Attribute , Vec < Path > ) ,
1005
+ ( attr, pos , derives) : ( ast:: Attribute , usize , Vec < Path > ) ,
1004
1006
item : Annotatable ,
1005
1007
kind : AstFragmentKind ,
1006
1008
) -> AstFragment {
1007
- self . collect ( kind, InvocationKind :: Attr { attr, item, derives } )
1009
+ self . collect ( kind, InvocationKind :: Attr { attr, pos , item, derives } )
1008
1010
}
1009
1011
1010
1012
/// If `item` is an attribute invocation, remove the attribute and return it together with
1011
- /// derives following it. We have to collect the derives in order to resolve legacy derive
1012
- /// helpers (helpers written before derives that introduce them).
1013
- fn take_first_attr ( & mut self , item : & mut impl HasAttrs ) -> Option < ( ast:: Attribute , Vec < Path > ) > {
1013
+ /// its position and derives following it. We have to collect the derives in order to resolve
1014
+ /// legacy derive helpers (helpers written before derives that introduce them).
1015
+ fn take_first_attr (
1016
+ & mut self ,
1017
+ item : & mut impl HasAttrs ,
1018
+ ) -> Option < ( ast:: Attribute , usize , Vec < Path > ) > {
1014
1019
let mut attr = None ;
1015
1020
1016
1021
item. visit_attrs ( |attrs| {
@@ -1033,7 +1038,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
1033
1038
} )
1034
1039
. collect ( ) ;
1035
1040
1036
- ( attr, following_derives)
1041
+ ( attr, attr_pos , following_derives)
1037
1042
} )
1038
1043
} ) ;
1039
1044
0 commit comments