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