@@ -2,8 +2,6 @@ use std::fmt::Debug;
22use std:: ops:: ControlFlow ;
33
44use rustc_hir:: def_id:: DefId ;
5- use rustc_infer:: infer:: TyCtxtInferExt ;
6- use rustc_infer:: traits:: ObligationCause ;
75use rustc_infer:: traits:: util:: PredicateSet ;
86use rustc_middle:: bug;
97use rustc_middle:: query:: Providers ;
@@ -14,7 +12,7 @@ use rustc_span::DUMMY_SP;
1412use smallvec:: { SmallVec , smallvec} ;
1513use tracing:: debug;
1614
17- use crate :: traits:: { ObligationCtxt , impossible_predicates, is_vtable_safe_method} ;
15+ use crate :: traits:: { impossible_predicates, is_vtable_safe_method} ;
1816
1917#[ derive( Clone , Debug ) ]
2018pub enum VtblSegment < ' tcx > {
@@ -230,6 +228,11 @@ fn vtable_entries<'tcx>(
230228 trait_ref : ty:: TraitRef < ' tcx > ,
231229) -> & ' tcx [ VtblEntry < ' tcx > ] {
232230 debug_assert ! ( !trait_ref. has_non_region_infer( ) && !trait_ref. has_non_region_param( ) ) ;
231+ debug_assert_eq ! (
232+ tcx. normalize_erasing_regions( ty:: TypingEnv :: fully_monomorphized( ) , trait_ref) ,
233+ trait_ref,
234+ "vtable trait ref should be normalized"
235+ ) ;
233236
234237 debug ! ( "vtable_entries({:?})" , trait_ref) ;
235238
@@ -307,6 +310,11 @@ fn vtable_entries<'tcx>(
307310// for `Supertrait`'s methods in the vtable of `Subtrait`.
308311pub ( crate ) fn first_method_vtable_slot < ' tcx > ( tcx : TyCtxt < ' tcx > , key : ty:: TraitRef < ' tcx > ) -> usize {
309312 debug_assert ! ( !key. has_non_region_infer( ) && !key. has_non_region_param( ) ) ;
313+ debug_assert_eq ! (
314+ tcx. normalize_erasing_regions( ty:: TypingEnv :: fully_monomorphized( ) , key) ,
315+ key,
316+ "vtable trait ref should be normalized"
317+ ) ;
310318
311319 let ty:: Dynamic ( source, _, _) = * key. self_ty ( ) . kind ( ) else {
312320 bug ! ( ) ;
@@ -325,11 +333,9 @@ pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRe
325333 vptr_offset += TyCtxt :: COMMON_VTABLE_ENTRIES . len ( ) ;
326334 }
327335 VtblSegment :: TraitOwnEntries { trait_ref : vtable_principal, emit_vptr } => {
328- if trait_refs_are_compatible (
329- tcx,
330- ty:: ExistentialTraitRef :: erase_self_ty ( tcx, vtable_principal) ,
331- target_principal,
332- ) {
336+ if ty:: ExistentialTraitRef :: erase_self_ty ( tcx, vtable_principal)
337+ == target_principal
338+ {
333339 return ControlFlow :: Break ( vptr_offset) ;
334340 }
335341
@@ -360,6 +366,12 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
360366 ) ,
361367) -> Option < usize > {
362368 debug_assert ! ( !key. has_non_region_infer( ) && !key. has_non_region_param( ) ) ;
369+ debug_assert_eq ! (
370+ tcx. normalize_erasing_regions( ty:: TypingEnv :: fully_monomorphized( ) , key) ,
371+ key,
372+ "upcasting trait refs should be normalized"
373+ ) ;
374+
363375 let ( source, target) = key;
364376
365377 // If the target principal is `None`, we can just return `None`.
@@ -386,11 +398,9 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
386398 VtblSegment :: TraitOwnEntries { trait_ref : vtable_principal, emit_vptr } => {
387399 vptr_offset +=
388400 tcx. own_existential_vtable_entries ( vtable_principal. def_id ) . len ( ) ;
389- if trait_refs_are_compatible (
390- tcx,
391- ty:: ExistentialTraitRef :: erase_self_ty ( tcx, vtable_principal) ,
392- target_principal,
393- ) {
401+ if ty:: ExistentialTraitRef :: erase_self_ty ( tcx, vtable_principal)
402+ == target_principal
403+ {
394404 if emit_vptr {
395405 return ControlFlow :: Break ( Some ( vptr_offset) ) ;
396406 } else {
@@ -410,27 +420,6 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
410420 prepare_vtable_segments ( tcx, source_principal, vtable_segment_callback) . unwrap ( )
411421}
412422
413- fn trait_refs_are_compatible < ' tcx > (
414- tcx : TyCtxt < ' tcx > ,
415- vtable_principal : ty:: ExistentialTraitRef < ' tcx > ,
416- target_principal : ty:: ExistentialTraitRef < ' tcx > ,
417- ) -> bool {
418- if vtable_principal. def_id != target_principal. def_id {
419- return false ;
420- }
421-
422- let ( infcx, param_env) =
423- tcx. infer_ctxt ( ) . build_with_typing_env ( ty:: TypingEnv :: fully_monomorphized ( ) ) ;
424- let ocx = ObligationCtxt :: new ( & infcx) ;
425- let source_principal = ocx. normalize ( & ObligationCause :: dummy ( ) , param_env, vtable_principal) ;
426- let target_principal = ocx. normalize ( & ObligationCause :: dummy ( ) , param_env, target_principal) ;
427- let Ok ( ( ) ) = ocx. eq ( & ObligationCause :: dummy ( ) , param_env, target_principal, source_principal)
428- else {
429- return false ;
430- } ;
431- ocx. select_all_or_error ( ) . is_empty ( )
432- }
433-
434423pub ( super ) fn provide ( providers : & mut Providers ) {
435424 * providers = Providers {
436425 own_existential_vtable_entries,
0 commit comments