@@ -547,8 +547,14 @@ impl<'tcx> Instance<'tcx> {
547
547
param_env : ty:: ParamEnv < ' tcx > ,
548
548
def_id : DefId ,
549
549
args : GenericArgsRef < ' tcx > ,
550
- span : Option < Span > ,
550
+ span : Span ,
551
551
) -> Instance < ' tcx > {
552
+ // We compute the span lazily, to avoid unnecessary query calls.
553
+ // If `span` is a DUMMY_SP, and the def id is local, then use the
554
+ // def span of the def id.
555
+ let span_or_local_def_span =
556
+ || if span. is_dummy ( ) && def_id. is_local ( ) { tcx. def_span ( def_id) } else { span } ;
557
+
552
558
match ty:: Instance :: resolve ( tcx, param_env, def_id, args) {
553
559
Ok ( Some ( instance) ) => instance,
554
560
Ok ( None ) => {
@@ -567,22 +573,22 @@ impl<'tcx> Instance<'tcx> {
567
573
// We don't use `def_span(def_id)` so that diagnostics point
568
574
// to the crate root during mono instead of to foreign items.
569
575
// This is arguably better.
570
- span : span . unwrap_or ( DUMMY_SP ) ,
576
+ span : span_or_local_def_span ( ) ,
571
577
shrunk,
572
578
was_written,
573
579
path,
574
580
type_length,
575
581
} ) ;
576
582
} else {
577
583
span_bug ! (
578
- span . unwrap_or ( tcx . def_span ( def_id ) ) ,
584
+ span_or_local_def_span ( ) ,
579
585
"failed to resolve instance for {}" ,
580
586
tcx. def_path_str_with_args( def_id, args)
581
587
)
582
588
}
583
589
}
584
590
instance => span_bug ! (
585
- span . unwrap_or ( tcx . def_span ( def_id ) ) ,
591
+ span_or_local_def_span ( ) ,
586
592
"failed to resolve instance for {}: {instance:#?}" ,
587
593
tcx. def_path_str_with_args( def_id, args)
588
594
) ,
@@ -642,6 +648,7 @@ impl<'tcx> Instance<'tcx> {
642
648
param_env : ty:: ParamEnv < ' tcx > ,
643
649
def_id : DefId ,
644
650
args : GenericArgsRef < ' tcx > ,
651
+ span : Span ,
645
652
) -> Instance < ' tcx > {
646
653
debug ! ( "resolve_for_vtable(def_id={:?}, args={:?})" , def_id, args) ;
647
654
let fn_sig = tcx. fn_sig ( def_id) . instantiate_identity ( ) ;
@@ -654,7 +661,7 @@ impl<'tcx> Instance<'tcx> {
654
661
return Instance { def : InstanceKind :: VTableShim ( def_id) , args } ;
655
662
}
656
663
657
- let mut resolved = Instance :: expect_resolve ( tcx, param_env, def_id, args, None ) ;
664
+ let mut resolved = Instance :: expect_resolve ( tcx, param_env, def_id, args, span ) ;
658
665
659
666
let reason = tcx. sess . is_sanitizer_kcfi_enabled ( ) . then_some ( ReifyReason :: Vtable ) ;
660
667
match resolved. def {
@@ -731,13 +738,13 @@ impl<'tcx> Instance<'tcx> {
731
738
pub fn resolve_drop_in_place ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> ty:: Instance < ' tcx > {
732
739
let def_id = tcx. require_lang_item ( LangItem :: DropInPlace , None ) ;
733
740
let args = tcx. mk_args ( & [ ty. into ( ) ] ) ;
734
- Instance :: expect_resolve ( tcx, ty:: ParamEnv :: reveal_all ( ) , def_id, args, None )
741
+ Instance :: expect_resolve ( tcx, ty:: ParamEnv :: reveal_all ( ) , def_id, args, DUMMY_SP )
735
742
}
736
743
737
744
pub fn resolve_async_drop_in_place ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> ty:: Instance < ' tcx > {
738
745
let def_id = tcx. require_lang_item ( LangItem :: AsyncDropInPlace , None ) ;
739
746
let args = tcx. mk_args ( & [ ty. into ( ) ] ) ;
740
- Instance :: expect_resolve ( tcx, ty:: ParamEnv :: reveal_all ( ) , def_id, args, None )
747
+ Instance :: expect_resolve ( tcx, ty:: ParamEnv :: reveal_all ( ) , def_id, args, DUMMY_SP )
741
748
}
742
749
743
750
#[ instrument( level = "debug" , skip( tcx) , ret) ]
0 commit comments