1
1
use std:: convert:: TryFrom ;
2
+ use std:: fmt;
2
3
3
4
use 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 } ;
6
6
use rustc_ast:: Mutability ;
7
7
8
- #[ derive( Clone , Copy , Debug , PartialEq , HashStable ) ]
8
+ #[ derive( Clone , Copy , PartialEq , HashStable ) ]
9
9
pub enum VtblEntry < ' tcx > {
10
10
MetadataDropInPlace ,
11
11
MetadataSize ,
12
12
MetadataAlign ,
13
13
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
+ }
16
30
}
17
31
18
32
pub const COMMON_VTABLE_ENTRIES : & [ VtblEntry < ' _ > ] =
@@ -37,11 +51,6 @@ impl<'tcx> TyCtxt<'tcx> {
37
51
}
38
52
drop ( vtables_cache) ;
39
53
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 ( ) ;
45
54
let vtable_entries = if let Some ( poly_trait_ref) = poly_trait_ref {
46
55
let trait_ref = poly_trait_ref. with_self_ty ( tcx, ty) ;
47
56
let trait_ref = tcx. erase_regions ( trait_ref) ;
@@ -51,8 +60,9 @@ impl<'tcx> TyCtxt<'tcx> {
51
60
COMMON_VTABLE_ENTRIES
52
61
} ;
53
62
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" ) ;
56
66
assert ! ( !layout. is_unsized( ) , "can't create a vtable for an unsized type" ) ;
57
67
let size = layout. size . bytes ( ) ;
58
68
let align = layout. align . abi . bytes ( ) ;
@@ -80,21 +90,18 @@ impl<'tcx> TyCtxt<'tcx> {
80
90
VtblEntry :: MetadataSize => Scalar :: from_uint ( size, ptr_size) . into ( ) ,
81
91
VtblEntry :: MetadataAlign => Scalar :: from_uint ( align, ptr_size) . into ( ) ,
82
92
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) => {
87
94
// 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) ;
92
96
let fn_alloc_id = tcx. create_fn_alloc ( instance) ;
93
97
let fn_ptr = Pointer :: from ( fn_alloc_id) ;
94
98
ScalarMaybeUninit :: from_pointer ( fn_ptr, & tcx)
95
99
}
96
100
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) ) ;
98
105
let vptr = Pointer :: from ( supertrait_alloc_id) ;
99
106
ScalarMaybeUninit :: from_pointer ( vptr, & tcx)
100
107
}
0 commit comments