@@ -318,9 +318,17 @@ pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRe
318318 bug ! ( ) ;
319319 } ;
320320 let source_principal = tcx. instantiate_bound_regions_with_erased (
321- source. principal ( ) . unwrap ( ) . with_self_ty ( tcx, tcx . types . trait_object_dummy_self ) ,
321+ source. principal ( ) . unwrap ( ) . with_self_ty ( tcx, key . self_ty ( ) ) ,
322322 ) ;
323323
324+ // We're monomorphizing a call to a dyn trait object that can never be constructed.
325+ if tcx. instantiate_and_check_impossible_predicates ( (
326+ source_principal. def_id ,
327+ source_principal. args ,
328+ ) ) {
329+ return 0 ;
330+ }
331+
324332 let target_principal = ty:: ExistentialTraitRef :: erase_self_ty ( tcx, key) ;
325333
326334 let vtable_segment_callback = {
@@ -373,19 +381,27 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
373381 let ( source, target) = key;
374382
375383 // If the target principal is `None`, we can just return `None`.
376- let ty:: Dynamic ( target , _, _) = * target. kind ( ) else {
384+ let ty:: Dynamic ( target_data , _, _) = * target. kind ( ) else {
377385 bug ! ( ) ;
378386 } ;
379- let target_principal = tcx. instantiate_bound_regions_with_erased ( target . principal ( ) ?) ;
387+ let target_principal = tcx. instantiate_bound_regions_with_erased ( target_data . principal ( ) ?) ;
380388
381389 // Given that we have a target principal, it is a bug for there not to be a source principal.
382- let ty:: Dynamic ( source , _, _) = * source. kind ( ) else {
390+ let ty:: Dynamic ( source_data , _, _) = * source. kind ( ) else {
383391 bug ! ( ) ;
384392 } ;
385393 let source_principal = tcx. instantiate_bound_regions_with_erased (
386- source . principal ( ) . unwrap ( ) . with_self_ty ( tcx, tcx . types . trait_object_dummy_self ) ,
394+ source_data . principal ( ) . unwrap ( ) . with_self_ty ( tcx, source ) ,
387395 ) ;
388396
397+ // We're monomorphizing a dyn trait object upcast that can never be constructed.
398+ if tcx. instantiate_and_check_impossible_predicates ( (
399+ source_principal. def_id ,
400+ source_principal. args ,
401+ ) ) {
402+ return None ;
403+ }
404+
389405 let vtable_segment_callback = {
390406 let mut vptr_offset = 0 ;
391407 move |segment| {
0 commit comments