@@ -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 > {
@@ -228,6 +226,11 @@ fn vtable_entries<'tcx>(
228
226
trait_ref : ty:: TraitRef < ' tcx > ,
229
227
) -> & ' tcx [ VtblEntry < ' tcx > ] {
230
228
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
+ ) ;
231
234
232
235
debug ! ( "vtable_entries({:?})" , trait_ref) ;
233
236
@@ -305,6 +308,11 @@ fn vtable_entries<'tcx>(
305
308
// for `Supertrait`'s methods in the vtable of `Subtrait`.
306
309
pub ( crate ) fn first_method_vtable_slot < ' tcx > ( tcx : TyCtxt < ' tcx > , key : ty:: TraitRef < ' tcx > ) -> usize {
307
310
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
+ ) ;
308
316
309
317
let ty:: Dynamic ( source, _, _) = * key. self_ty ( ) . kind ( ) else {
310
318
bug ! ( ) ;
@@ -323,11 +331,9 @@ pub(crate) fn first_method_vtable_slot<'tcx>(tcx: TyCtxt<'tcx>, key: ty::TraitRe
323
331
vptr_offset += TyCtxt :: COMMON_VTABLE_ENTRIES . len ( ) ;
324
332
}
325
333
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
+ {
331
337
return ControlFlow :: Break ( vptr_offset) ;
332
338
}
333
339
@@ -358,6 +364,12 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
358
364
) ,
359
365
) -> Option < usize > {
360
366
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
+
361
373
let ( source, target) = key;
362
374
363
375
// If the target principal is `None`, we can just return `None`.
@@ -384,11 +396,9 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
384
396
VtblSegment :: TraitOwnEntries { trait_ref : vtable_principal, emit_vptr } => {
385
397
vptr_offset +=
386
398
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
+ {
392
402
if emit_vptr {
393
403
return ControlFlow :: Break ( Some ( vptr_offset) ) ;
394
404
} else {
@@ -408,27 +418,6 @@ pub(crate) fn supertrait_vtable_slot<'tcx>(
408
418
prepare_vtable_segments ( tcx, source_principal, vtable_segment_callback) . unwrap ( )
409
419
}
410
420
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
-
432
421
pub ( super ) fn provide ( providers : & mut Providers ) {
433
422
* providers = Providers {
434
423
own_existential_vtable_entries,
0 commit comments