@@ -51,7 +51,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
51
51
use rustc_data_structures:: tagged_ptr:: TaggedRef ;
52
52
use rustc_errors:: { DiagArgFromDisplay , DiagCtxtHandle , StashKey } ;
53
53
use 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 } ;
55
55
use rustc_hir:: {
56
56
self as hir, ConstArg , GenericArg , HirId , ItemLocalMap , LangItem , ParamName , TraitCandidate ,
57
57
} ;
@@ -188,7 +188,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
188
188
// interact with `gen`/`async gen` blocks
189
189
allow_async_iterator : [ sym:: gen_future, sym:: async_iterator] . into ( ) ,
190
190
191
- attribute_parser : AttributeParser :: new ( tcx. sess , tcx. features ( ) , registered_tools) ,
191
+ attribute_parser : AttributeParser :: new ( tcx, tcx. features ( ) , registered_tools) ,
192
192
}
193
193
}
194
194
@@ -197,6 +197,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
197
197
}
198
198
}
199
199
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
+
200
216
#[ extension( trait ResolverAstLoweringExt ) ]
201
217
impl ResolverAstLowering {
202
218
fn legacy_const_generic_args ( & self , expr : & Expr ) -> Option < Vec < usize > > {
@@ -740,15 +756,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
740
756
} )
741
757
}
742
758
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
+
743
766
/// Intercept all spans entering HIR.
744
767
/// Mark a span as relative to the current owning item.
745
768
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)
752
770
}
753
771
754
772
fn lower_ident ( & self , ident : Ident ) -> Ident {
@@ -871,7 +889,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
871
889
if attrs. is_empty ( ) {
872
890
& [ ]
873
891
} 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 ) ;
875
893
876
894
debug_assert_eq ! ( id. owner, self . current_hir_id_owner) ;
877
895
let ret = self . arena . alloc_from_iter ( lowered_attrs) ;
@@ -891,9 +909,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
891
909
}
892
910
}
893
911
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
+ )
897
926
}
898
927
899
928
fn alias_attrs ( & mut self , id : HirId , target_id : HirId ) {
0 commit comments