@@ -51,7 +51,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5151use rustc_data_structures:: tagged_ptr:: TaggedRef ;
5252use rustc_errors:: { DiagArgFromDisplay , DiagCtxtHandle , StashKey } ;
5353use rustc_hir:: def:: { DefKind , LifetimeRes , Namespace , PartialRes , PerNS , Res } ;
54- use rustc_hir:: def_id:: { CRATE_DEF_ID , LOCAL_CRATE , LocalDefId } ;
54+ use rustc_hir:: def_id:: { LocalDefId , CRATE_DEF_ID , LOCAL_CRATE } ;
5555use rustc_hir:: {
5656 self as hir, ConstArg , GenericArg , HirId , ItemLocalMap , LangItem , ParamName , TraitCandidate ,
5757} ;
@@ -188,7 +188,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
188188 // interact with `gen`/`async gen` blocks
189189 allow_async_iterator : [ sym:: gen_future, sym:: async_iterator] . into ( ) ,
190190
191- attribute_parser : AttributeParser :: new ( tcx. sess , tcx. features ( ) , registered_tools) ,
191+ attribute_parser : AttributeParser :: new ( tcx, tcx. features ( ) , registered_tools) ,
192192 }
193193 }
194194
@@ -197,6 +197,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
197197 }
198198}
199199
200+ struct SpanLowerer {
201+ is_incremental : bool ,
202+ defid : LocalDefId ,
203+ }
204+
205+ impl SpanLowerer {
206+ fn lower ( & self , span : Span ) -> Span {
207+ if self . is_incremental {
208+ span. with_parent ( Some ( self . defid ) )
209+ } else {
210+ // Do not make spans relative when not using incremental compilation.
211+ span
212+ }
213+ }
214+ }
215+
200216#[ extension( trait ResolverAstLoweringExt ) ]
201217impl ResolverAstLowering {
202218 fn legacy_const_generic_args ( & self , expr : & Expr ) -> Option < Vec < usize > > {
@@ -740,15 +756,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
740756 } )
741757 }
742758
759+ fn span_lowerer ( & self ) -> SpanLowerer {
760+ SpanLowerer {
761+ is_incremental : self . tcx . sess . opts . incremental . is_some ( ) ,
762+ defid : self . current_hir_id_owner . def_id ,
763+ }
764+ }
765+
743766 /// Intercept all spans entering HIR.
744767 /// Mark a span as relative to the current owning item.
745768 fn lower_span ( & self , span : Span ) -> Span {
746- if self . tcx . sess . opts . incremental . is_some ( ) {
747- span. with_parent ( Some ( self . current_hir_id_owner . def_id ) )
748- } else {
749- // Do not make spans relative when not using incremental compilation.
750- span
751- }
769+ self . span_lowerer ( ) . lower ( span)
752770 }
753771
754772 fn lower_ident ( & self , ident : Ident ) -> Ident {
@@ -871,7 +889,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
871889 if attrs. is_empty ( ) {
872890 & [ ]
873891 } else {
874- let lowered_attrs = self . lower_attrs_vec ( attrs, self . lower_span ( target_span) ) ;
892+ let lowered_attrs = self . lower_attrs_vec ( attrs, self . lower_span ( target_span) , id ) ;
875893
876894 debug_assert_eq ! ( id. owner, self . current_hir_id_owner) ;
877895 let ret = self . arena . alloc_from_iter ( lowered_attrs) ;
@@ -891,9 +909,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
891909 }
892910 }
893911
894- fn lower_attrs_vec ( & self , attrs : & [ Attribute ] , target_span : Span ) -> Vec < hir:: Attribute > {
895- self . attribute_parser
896- . parse_attribute_list ( attrs, target_span, OmitDoc :: Lower , |s| self . lower_span ( s) )
912+ fn lower_attrs_vec (
913+ & mut self ,
914+ attrs : & [ Attribute ] ,
915+ target_span : Span ,
916+ target_hir_id : HirId ,
917+ ) -> Vec < hir:: Attribute > {
918+ let l = self . span_lowerer ( ) ;
919+ self . attribute_parser . parse_attribute_list (
920+ attrs,
921+ target_span,
922+ target_hir_id,
923+ OmitDoc :: Lower ,
924+ |s| l. lower ( s) ,
925+ )
897926 }
898927
899928 fn alias_attrs ( & mut self , id : HirId , target_id : HirId ) {
0 commit comments