11use std:: convert:: TryFrom ;
2+ use std:: fmt;
23
34use crate :: mir:: interpret:: { alloc_range, AllocId , Allocation , Pointer , Scalar , ScalarMaybeUninit } ;
4- use crate :: ty:: fold:: TypeFoldable ;
5- use crate :: ty:: { self , DefId , PolyExistentialTraitRef , SubstsRef , Ty , TyCtxt } ;
5+ use crate :: ty:: { self , Instance , PolyTraitRef , Ty , TyCtxt } ;
66use rustc_ast:: Mutability ;
77
8- #[ derive( Clone , Copy , Debug , PartialEq , HashStable ) ]
8+ #[ derive( Clone , Copy , PartialEq , HashStable ) ]
99pub enum VtblEntry < ' tcx > {
1010 MetadataDropInPlace ,
1111 MetadataSize ,
1212 MetadataAlign ,
1313 Vacant ,
14- Method ( DefId , SubstsRef < ' tcx > ) ,
15- TraitVPtr ( PolyExistentialTraitRef < ' tcx > ) ,
14+ Method ( Instance < ' tcx > ) ,
15+ TraitVPtr ( PolyTraitRef < ' tcx > ) ,
16+ }
17+
18+ impl < ' tcx > fmt:: Debug for VtblEntry < ' tcx > {
19+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
20+ match self {
21+ VtblEntry :: MetadataDropInPlace => write ! ( f, "MetadataDropInPlace" ) ?,
22+ VtblEntry :: MetadataSize => write ! ( f, "MetadataSize" ) ?,
23+ VtblEntry :: MetadataAlign => write ! ( f, "MetadataAlign" ) ?,
24+ VtblEntry :: Vacant => write ! ( f, "Vacant" ) ?,
25+ VtblEntry :: Method ( instance) => write ! ( f, "Method: {}" , instance) ?,
26+ VtblEntry :: TraitVPtr ( trait_ref) => write ! ( f, "TraitVPtr: {}" , trait_ref) ?,
27+ }
28+ Ok ( ( ) )
29+ }
1630}
1731
1832pub const COMMON_VTABLE_ENTRIES : & [ VtblEntry < ' _ > ] =
@@ -37,11 +51,6 @@ impl<'tcx> TyCtxt<'tcx> {
3751 }
3852 drop ( vtables_cache) ;
3953
40- // See https://github.com/rust-lang/rust/pull/86475#discussion_r655162674
41- assert ! (
42- !ty. needs_subst( ) && !poly_trait_ref. map_or( false , |trait_ref| trait_ref. needs_subst( ) )
43- ) ;
44- let param_env = ty:: ParamEnv :: reveal_all ( ) ;
4554 let vtable_entries = if let Some ( poly_trait_ref) = poly_trait_ref {
4655 let trait_ref = poly_trait_ref. with_self_ty ( tcx, ty) ;
4756 let trait_ref = tcx. erase_regions ( trait_ref) ;
@@ -51,8 +60,9 @@ impl<'tcx> TyCtxt<'tcx> {
5160 COMMON_VTABLE_ENTRIES
5261 } ;
5362
54- let layout =
55- tcx. layout_of ( param_env. and ( ty) ) . expect ( "failed to build vtable representation" ) ;
63+ let layout = tcx
64+ . layout_of ( ty:: ParamEnv :: reveal_all ( ) . and ( ty) )
65+ . expect ( "failed to build vtable representation" ) ;
5666 assert ! ( !layout. is_unsized( ) , "can't create a vtable for an unsized type" ) ;
5767 let size = layout. size . bytes ( ) ;
5868 let align = layout. align . abi . bytes ( ) ;
@@ -80,21 +90,18 @@ impl<'tcx> TyCtxt<'tcx> {
8090 VtblEntry :: MetadataSize => Scalar :: from_uint ( size, ptr_size) . into ( ) ,
8191 VtblEntry :: MetadataAlign => Scalar :: from_uint ( align, ptr_size) . into ( ) ,
8292 VtblEntry :: Vacant => continue ,
83- VtblEntry :: Method ( def_id, substs) => {
84- // See https://github.com/rust-lang/rust/pull/86475#discussion_r655162674
85- assert ! ( !substs. needs_subst( ) ) ;
86-
93+ VtblEntry :: Method ( instance) => {
8794 // Prepare the fn ptr we write into the vtable.
88- let instance =
89- ty:: Instance :: resolve_for_vtable ( tcx, param_env, * def_id, substs)
90- . expect ( "resolution failed during building vtable representation" )
91- . polymorphize ( tcx) ;
95+ let instance = instance. polymorphize ( tcx) ;
9296 let fn_alloc_id = tcx. create_fn_alloc ( instance) ;
9397 let fn_ptr = Pointer :: from ( fn_alloc_id) ;
9498 ScalarMaybeUninit :: from_pointer ( fn_ptr, & tcx)
9599 }
96100 VtblEntry :: TraitVPtr ( trait_ref) => {
97- let supertrait_alloc_id = self . vtable_allocation ( ty, Some ( * trait_ref) ) ;
101+ let super_trait_ref = trait_ref. map_bound ( |trait_ref| {
102+ ty:: ExistentialTraitRef :: erase_self_ty ( tcx, trait_ref)
103+ } ) ;
104+ let supertrait_alloc_id = self . vtable_allocation ( ty, Some ( super_trait_ref) ) ;
98105 let vptr = Pointer :: from ( supertrait_alloc_id) ;
99106 ScalarMaybeUninit :: from_pointer ( vptr, & tcx)
100107 }
0 commit comments