@@ -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 > {
@@ -228,6 +226,11 @@ fn vtable_entries<'tcx>(
228226 trait_ref : ty:: TraitRef < ' tcx > ,
229227) -> & ' tcx [ VtblEntry < ' tcx > ] {
230228 debug_assert ! ( !trait_ref. has_non_region_infer( ) && !trait_ref. has_non_region_param( ) ) ;
229+ debug_assert_eq ! (
230+ tcx. normalize_erasing_regions( ty:: TypingEnv :: fully_monomorphized( ) , trait_ref) ,
231+ trait_ref,
232+ "vtable trait ref should be normalized"
233+ ) ;
231234
232235 debug ! ( "vtable_entries({:?})" , trait_ref) ;
233236
@@ -305,6 +308,11 @@ fn vtable_entries<'tcx>(
305308// for `Supertrait`'s methods in the vtable of `Subtrait`.
306309pub ( crate ) fn first_method_vtable_slot < ' tcx > ( tcx : TyCtxt < ' tcx > , key : ty:: TraitRef < ' tcx > ) -> usize {
307310 debug_assert ! ( !key. has_non_region_infer( ) && !key. has_non_region_param( ) ) ;
311+ debug_assert_eq ! (
312+ tcx. normalize_erasing_regions( ty:: TypingEnv :: fully_monomorphized( ) , key) ,
313+ key,
314+ "vtable trait ref should be normalized"
315+ ) ;
308316
309317 let ty:: Dynamic ( source, _, _) = * key. self_ty ( ) . kind ( ) else {
310318 bug ! ( ) ;
@@ -323,11 +331,9 @@ pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRe
323331 vptr_offset += TyCtxt :: COMMON_VTABLE_ENTRIES . len ( ) ;
324332 }
325333 VtblSegment :: TraitOwnEntries { trait_ref : vtable_principal, emit_vptr } => {
326- if trait_refs_are_compatible (
327- tcx,
328- ty:: ExistentialTraitRef :: erase_self_ty ( tcx, vtable_principal) ,
329- target_principal,
330- ) {
334+ if ty:: ExistentialTraitRef :: erase_self_ty ( tcx, vtable_principal)
335+ == target_principal
336+ {
331337 return ControlFlow :: Break ( vptr_offset) ;
332338 }
333339
@@ -358,6 +364,12 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
358364 ) ,
359365) -> Option < usize > {
360366 debug_assert ! ( !key. has_non_region_infer( ) && !key. has_non_region_param( ) ) ;
367+ debug_assert_eq ! (
368+ tcx. normalize_erasing_regions( ty:: TypingEnv :: fully_monomorphized( ) , key) ,
369+ key,
370+ "upcasting trait refs should be normalized"
371+ ) ;
372+
361373 let ( source, target) = key;
362374
363375 // If the target principal is `None`, we can just return `None`.
@@ -384,11 +396,9 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
384396 VtblSegment :: TraitOwnEntries { trait_ref : vtable_principal, emit_vptr } => {
385397 vptr_offset +=
386398 tcx. own_existential_vtable_entries ( vtable_principal. def_id ) . len ( ) ;
387- if trait_refs_are_compatible (
388- tcx,
389- ty:: ExistentialTraitRef :: erase_self_ty ( tcx, vtable_principal) ,
390- target_principal,
391- ) {
399+ if ty:: ExistentialTraitRef :: erase_self_ty ( tcx, vtable_principal)
400+ == target_principal
401+ {
392402 if emit_vptr {
393403 return ControlFlow :: Break ( Some ( vptr_offset) ) ;
394404 } else {
@@ -408,27 +418,6 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
408418 prepare_vtable_segments ( tcx, source_principal, vtable_segment_callback) . unwrap ( )
409419}
410420
411- fn trait_refs_are_compatible < ' tcx > (
412- tcx : TyCtxt < ' tcx > ,
413- vtable_principal : ty:: ExistentialTraitRef < ' tcx > ,
414- target_principal : ty:: ExistentialTraitRef < ' tcx > ,
415- ) -> bool {
416- if vtable_principal. def_id != target_principal. def_id {
417- return false ;
418- }
419-
420- let ( infcx, param_env) =
421- tcx. infer_ctxt ( ) . build_with_typing_env ( ty:: TypingEnv :: fully_monomorphized ( ) ) ;
422- let ocx = ObligationCtxt :: new ( & infcx) ;
423- let source_principal = ocx. normalize ( & ObligationCause :: dummy ( ) , param_env, vtable_principal) ;
424- let target_principal = ocx. normalize ( & ObligationCause :: dummy ( ) , param_env, target_principal) ;
425- let Ok ( ( ) ) = ocx. eq ( & ObligationCause :: dummy ( ) , param_env, target_principal, source_principal)
426- else {
427- return false ;
428- } ;
429- ocx. select_all_or_error ( ) . is_empty ( )
430- }
431-
432421pub ( super ) fn provide ( providers : & mut Providers ) {
433422 * providers = Providers {
434423 own_existential_vtable_entries,
0 commit comments