@@ -232,6 +232,11 @@ fn vtable_entries<'tcx>(
232232 "vtable trait ref should be normalized"
233233 ) ;
234234
235+ // We're monomorphizing a dyn trait object that can never be constructed.
236+ if tcx. instantiate_and_check_impossible_predicates ( ( trait_ref. def_id , trait_ref. args ) ) {
237+ return TyCtxt :: COMMON_VTABLE_ENTRIES ;
238+ }
239+
235240 debug ! ( "vtable_entries({:?})" , trait_ref) ;
236241
237242 let mut entries = vec ! [ ] ;
@@ -318,9 +323,17 @@ pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRe
318323 bug ! ( ) ;
319324 } ;
320325 let source_principal = tcx. instantiate_bound_regions_with_erased (
321- source. principal ( ) . unwrap ( ) . with_self_ty ( tcx, tcx . types . trait_object_dummy_self ) ,
326+ source. principal ( ) . unwrap ( ) . with_self_ty ( tcx, key . self_ty ( ) ) ,
322327 ) ;
323328
329+ // We're monomorphizing a dyn trait object that can never be constructed.
330+ if tcx. instantiate_and_check_impossible_predicates ( (
331+ source_principal. def_id ,
332+ source_principal. args ,
333+ ) ) {
334+ return 0 ;
335+ }
336+
324337 let target_principal = ty:: ExistentialTraitRef :: erase_self_ty ( tcx, key) ;
325338
326339 let vtable_segment_callback = {
@@ -373,19 +386,27 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
373386 let ( source, target) = key;
374387
375388 // If the target principal is `None`, we can just return `None`.
376- let ty:: Dynamic ( target , _, _) = * target. kind ( ) else {
389+ let ty:: Dynamic ( target_data , _, _) = * target. kind ( ) else {
377390 bug ! ( ) ;
378391 } ;
379- let target_principal = tcx. instantiate_bound_regions_with_erased ( target . principal ( ) ?) ;
392+ let target_principal = tcx. instantiate_bound_regions_with_erased ( target_data . principal ( ) ?) ;
380393
381394 // 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 {
395+ let ty:: Dynamic ( source_data , _, _) = * source. kind ( ) else {
383396 bug ! ( ) ;
384397 } ;
385398 let source_principal = tcx. instantiate_bound_regions_with_erased (
386- source . principal ( ) . unwrap ( ) . with_self_ty ( tcx, tcx . types . trait_object_dummy_self ) ,
399+ source_data . principal ( ) . unwrap ( ) . with_self_ty ( tcx, source ) ,
387400 ) ;
388401
402+ // We're monomorphizing a dyn trait object that can never be constructed.
403+ if tcx. instantiate_and_check_impossible_predicates ( (
404+ source_principal. def_id ,
405+ source_principal. args ,
406+ ) ) {
407+ return None ;
408+ }
409+
389410 let vtable_segment_callback = {
390411 let mut vptr_offset = 0 ;
391412 move |segment| {
0 commit comments