@@ -547,8 +547,14 @@ impl<'tcx> Instance<'tcx> {
547547 param_env : ty:: ParamEnv < ' tcx > ,
548548 def_id : DefId ,
549549 args : GenericArgsRef < ' tcx > ,
550- span : Option < Span > ,
550+ span : Span ,
551551 ) -> 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+
552558 match ty:: Instance :: resolve ( tcx, param_env, def_id, args) {
553559 Ok ( Some ( instance) ) => instance,
554560 Ok ( None ) => {
@@ -567,22 +573,22 @@ impl<'tcx> Instance<'tcx> {
567573 // We don't use `def_span(def_id)` so that diagnostics point
568574 // to the crate root during mono instead of to foreign items.
569575 // This is arguably better.
570- span : span . unwrap_or ( DUMMY_SP ) ,
576+ span : span_or_local_def_span ( ) ,
571577 shrunk,
572578 was_written,
573579 path,
574580 type_length,
575581 } ) ;
576582 } else {
577583 span_bug ! (
578- span . unwrap_or ( tcx . def_span ( def_id ) ) ,
584+ span_or_local_def_span ( ) ,
579585 "failed to resolve instance for {}" ,
580586 tcx. def_path_str_with_args( def_id, args)
581587 )
582588 }
583589 }
584590 instance => span_bug ! (
585- span . unwrap_or ( tcx . def_span ( def_id ) ) ,
591+ span_or_local_def_span ( ) ,
586592 "failed to resolve instance for {}: {instance:#?}" ,
587593 tcx. def_path_str_with_args( def_id, args)
588594 ) ,
@@ -642,6 +648,7 @@ impl<'tcx> Instance<'tcx> {
642648 param_env : ty:: ParamEnv < ' tcx > ,
643649 def_id : DefId ,
644650 args : GenericArgsRef < ' tcx > ,
651+ span : Span ,
645652 ) -> Instance < ' tcx > {
646653 debug ! ( "resolve_for_vtable(def_id={:?}, args={:?})" , def_id, args) ;
647654 let fn_sig = tcx. fn_sig ( def_id) . instantiate_identity ( ) ;
@@ -654,7 +661,7 @@ impl<'tcx> Instance<'tcx> {
654661 return Instance { def : InstanceKind :: VTableShim ( def_id) , args } ;
655662 }
656663
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 ) ;
658665
659666 let reason = tcx. sess . is_sanitizer_kcfi_enabled ( ) . then_some ( ReifyReason :: Vtable ) ;
660667 match resolved. def {
@@ -731,13 +738,13 @@ impl<'tcx> Instance<'tcx> {
731738 pub fn resolve_drop_in_place ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> ty:: Instance < ' tcx > {
732739 let def_id = tcx. require_lang_item ( LangItem :: DropInPlace , None ) ;
733740 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 )
735742 }
736743
737744 pub fn resolve_async_drop_in_place ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> ty:: Instance < ' tcx > {
738745 let def_id = tcx. require_lang_item ( LangItem :: AsyncDropInPlace , None ) ;
739746 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 )
741748 }
742749
743750 #[ instrument( level = "debug" , skip( tcx) , ret) ]
0 commit comments