@@ -234,6 +234,11 @@ fn vtable_entries<'tcx>(
234234 "vtable trait ref should be normalized"
235235 ) ;
236236
237+ // We're monomorphizing a dyn trait object that can never be constructed.
238+ if tcx. instantiate_and_check_impossible_predicates ( ( trait_ref. def_id , trait_ref. args ) ) {
239+ return TyCtxt :: COMMON_VTABLE_ENTRIES ;
240+ }
241+
237242 debug ! ( "vtable_entries({:?})" , trait_ref) ;
238243
239244 let mut entries = vec ! [ ] ;
@@ -320,9 +325,17 @@ pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRe
320325 bug ! ( ) ;
321326 } ;
322327 let source_principal = tcx. instantiate_bound_regions_with_erased (
323- source. principal ( ) . unwrap ( ) . with_self_ty ( tcx, tcx . types . trait_object_dummy_self ) ,
328+ source. principal ( ) . unwrap ( ) . with_self_ty ( tcx, key . self_ty ( ) ) ,
324329 ) ;
325330
331+ // We're monomorphizing a dyn trait object that can never be constructed.
332+ if tcx. instantiate_and_check_impossible_predicates ( (
333+ source_principal. def_id ,
334+ source_principal. args ,
335+ ) ) {
336+ return 0 ;
337+ }
338+
326339 let target_principal = ty:: ExistentialTraitRef :: erase_self_ty ( tcx, key) ;
327340
328341 let vtable_segment_callback = {
@@ -375,19 +388,27 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
375388 let ( source, target) = key;
376389
377390 // If the target principal is `None`, we can just return `None`.
378- let ty:: Dynamic ( target , _, _) = * target. kind ( ) else {
391+ let ty:: Dynamic ( target_data , _, _) = * target. kind ( ) else {
379392 bug ! ( ) ;
380393 } ;
381- let target_principal = tcx. instantiate_bound_regions_with_erased ( target . principal ( ) ?) ;
394+ let target_principal = tcx. instantiate_bound_regions_with_erased ( target_data . principal ( ) ?) ;
382395
383396 // Given that we have a target principal, it is a bug for there not to be a source principal.
384- let ty:: Dynamic ( source , _, _) = * source. kind ( ) else {
397+ let ty:: Dynamic ( source_data , _, _) = * source. kind ( ) else {
385398 bug ! ( ) ;
386399 } ;
387400 let source_principal = tcx. instantiate_bound_regions_with_erased (
388- source . principal ( ) . unwrap ( ) . with_self_ty ( tcx, tcx . types . trait_object_dummy_self ) ,
401+ source_data . principal ( ) . unwrap ( ) . with_self_ty ( tcx, source ) ,
389402 ) ;
390403
404+ // We're monomorphizing a dyn trait object that can never be constructed.
405+ if tcx. instantiate_and_check_impossible_predicates ( (
406+ source_principal. def_id ,
407+ source_principal. args ,
408+ ) ) {
409+ return None ;
410+ }
411+
391412 let vtable_segment_callback = {
392413 let mut vptr_offset = 0 ;
393414 move |segment| {
0 commit comments