@@ -2,8 +2,6 @@ use std::fmt::Debug;
2
2
use std:: ops:: ControlFlow ;
3
3
4
4
use rustc_hir:: def_id:: DefId ;
5
- use rustc_infer:: infer:: TyCtxtInferExt ;
6
- use rustc_infer:: traits:: ObligationCause ;
7
5
use rustc_infer:: traits:: util:: PredicateSet ;
8
6
use rustc_middle:: bug;
9
7
use rustc_middle:: query:: Providers ;
@@ -14,7 +12,7 @@ use rustc_span::DUMMY_SP;
14
12
use smallvec:: { SmallVec , smallvec} ;
15
13
use tracing:: debug;
16
14
17
- use crate :: traits:: { ObligationCtxt , impossible_predicates, is_vtable_safe_method} ;
15
+ use crate :: traits:: { impossible_predicates, is_vtable_safe_method} ;
18
16
19
17
#[ derive( Clone , Debug ) ]
20
18
pub enum VtblSegment < ' tcx > {
@@ -230,6 +228,11 @@ fn vtable_entries<'tcx>(
230
228
trait_ref : ty:: TraitRef < ' tcx > ,
231
229
) -> & ' tcx [ VtblEntry < ' tcx > ] {
232
230
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
+ ) ;
233
236
234
237
debug ! ( "vtable_entries({:?})" , trait_ref) ;
235
238
@@ -307,6 +310,11 @@ fn vtable_entries<'tcx>(
307
310
// for `Supertrait`'s methods in the vtable of `Subtrait`.
308
311
pub ( crate ) fn first_method_vtable_slot < ' tcx > ( tcx : TyCtxt < ' tcx > , key : ty:: TraitRef < ' tcx > ) -> usize {
309
312
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
+ ) ;
310
318
311
319
let ty:: Dynamic ( source, _, _) = * key. self_ty ( ) . kind ( ) else {
312
320
bug ! ( ) ;
@@ -325,11 +333,9 @@ pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRe
325
333
vptr_offset += TyCtxt :: COMMON_VTABLE_ENTRIES . len ( ) ;
326
334
}
327
335
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
+ {
333
339
return ControlFlow :: Break ( vptr_offset) ;
334
340
}
335
341
@@ -360,6 +366,12 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
360
366
) ,
361
367
) -> Option < usize > {
362
368
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
+
363
375
let ( source, target) = key;
364
376
365
377
// If the target principal is `None`, we can just return `None`.
@@ -386,11 +398,9 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
386
398
VtblSegment :: TraitOwnEntries { trait_ref : vtable_principal, emit_vptr } => {
387
399
vptr_offset +=
388
400
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
+ {
394
404
if emit_vptr {
395
405
return ControlFlow :: Break ( Some ( vptr_offset) ) ;
396
406
} else {
@@ -410,27 +420,6 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
410
420
prepare_vtable_segments ( tcx, source_principal, vtable_segment_callback) . unwrap ( )
411
421
}
412
422
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
-
434
423
pub ( super ) fn provide ( providers : & mut Providers ) {
435
424
* providers = Providers {
436
425
own_existential_vtable_entries,
0 commit comments