@@ -52,7 +52,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5252use rustc_data_structures:: tagged_ptr:: TaggedRef ;
5353use rustc_errors:: { DiagArgFromDisplay , DiagCtxtHandle , StashKey } ;
5454use rustc_hir:: def:: { DefKind , LifetimeRes , Namespace , PartialRes , PerNS , Res } ;
55- use rustc_hir:: def_id:: { CRATE_DEF_ID , LOCAL_CRATE , LocalDefId } ;
55+ use rustc_hir:: def_id:: { LocalDefId , CRATE_DEF_ID , LOCAL_CRATE } ;
5656use rustc_hir:: {
5757 self as hir, ConstArg , GenericArg , HirId , ItemLocalMap , LangItem , ParamName , TraitCandidate ,
5858} ;
@@ -189,7 +189,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
189189 // interact with `gen`/`async gen` blocks
190190 allow_async_iterator : [ sym:: gen_future, sym:: async_iterator] . into ( ) ,
191191
192- attribute_parser : AttributeParser :: new ( tcx. sess , tcx. features ( ) , registered_tools) ,
192+ attribute_parser : AttributeParser :: new ( tcx, tcx. features ( ) , registered_tools) ,
193193 }
194194 }
195195
@@ -198,6 +198,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
198198 }
199199}
200200
201+ struct SpanLowerer {
202+ is_incremental : bool ,
203+ defid : LocalDefId ,
204+ }
205+
206+ impl SpanLowerer {
207+ fn lower ( & self , span : Span ) -> Span {
208+ if self . is_incremental {
209+ span. with_parent ( Some ( self . defid ) )
210+ } else {
211+ // Do not make spans relative when not using incremental compilation.
212+ span
213+ }
214+ }
215+ }
216+
201217#[ extension( trait ResolverAstLoweringExt ) ]
202218impl ResolverAstLowering {
203219 fn legacy_const_generic_args ( & self , expr : & Expr ) -> Option < Vec < usize > > {
@@ -741,15 +757,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
741757 } )
742758 }
743759
760+ fn span_lowerer ( & self ) -> SpanLowerer {
761+ SpanLowerer {
762+ is_incremental : self . tcx . sess . opts . incremental . is_some ( ) ,
763+ defid : self . current_hir_id_owner . def_id ,
764+ }
765+ }
766+
744767 /// Intercept all spans entering HIR.
745768 /// Mark a span as relative to the current owning item.
746769 fn lower_span ( & self , span : Span ) -> Span {
747- if self . tcx . sess . opts . incremental . is_some ( ) {
748- span. with_parent ( Some ( self . current_hir_id_owner . def_id ) )
749- } else {
750- // Do not make spans relative when not using incremental compilation.
751- span
752- }
770+ self . span_lowerer ( ) . lower ( span)
753771 }
754772
755773 fn lower_ident ( & self , ident : Ident ) -> Ident {
@@ -872,7 +890,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
872890 if attrs. is_empty ( ) {
873891 & [ ]
874892 } else {
875- let lowered_attrs = self . lower_attrs_vec ( attrs, self . lower_span ( target_span) ) ;
893+ let lowered_attrs = self . lower_attrs_vec ( attrs, self . lower_span ( target_span) , id ) ;
876894
877895 debug_assert_eq ! ( id. owner, self . current_hir_id_owner) ;
878896 let ret = self . arena . alloc_from_iter ( lowered_attrs) ;
@@ -892,9 +910,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
892910 }
893911 }
894912
895- fn lower_attrs_vec ( & self , attrs : & [ Attribute ] , target_span : Span ) -> Vec < hir:: Attribute > {
896- self . attribute_parser
897- . parse_attribute_list ( attrs, target_span, OmitDoc :: Lower , |s| self . lower_span ( s) )
913+ fn lower_attrs_vec (
914+ & mut self ,
915+ attrs : & [ Attribute ] ,
916+ target_span : Span ,
917+ target_hir_id : HirId ,
918+ ) -> Vec < hir:: Attribute > {
919+ let l = self . span_lowerer ( ) ;
920+ self . attribute_parser . parse_attribute_list (
921+ attrs,
922+ target_span,
923+ target_hir_id,
924+ OmitDoc :: Lower ,
925+ |s| l. lower ( s) ,
926+ )
898927 }
899928
900929 fn alias_attrs ( & mut self , id : HirId , target_id : HirId ) {
0 commit comments