1
1
use rustc_middle:: bug;
2
- use rustc_middle:: ty:: { self , GenericArgKind , Ty } ;
2
+ use rustc_middle:: ty:: { self , GenericArgKind , Ty , TyCtxt } ;
3
3
use rustc_session:: config:: Lto ;
4
4
use rustc_symbol_mangling:: typeid_for_trait_ref;
5
5
use rustc_target:: callconv:: FnAbi ;
@@ -72,12 +72,17 @@ impl<'a, 'tcx> VirtualIndex {
72
72
73
73
/// This takes a valid `self` receiver type and extracts the principal trait
74
74
/// ref of the type. Return `None` if there is no principal trait.
75
- fn dyn_trait_in_self ( ty : Ty < ' _ > ) -> Option < ty:: PolyExistentialTraitRef < ' _ > > {
75
+ fn dyn_trait_in_self < ' tcx > (
76
+ tcx : TyCtxt < ' tcx > ,
77
+ ty : Ty < ' tcx > ,
78
+ ) -> Option < ty:: ExistentialTraitRef < ' tcx > > {
76
79
for arg in ty. peel_refs ( ) . walk ( ) {
77
80
if let GenericArgKind :: Type ( ty) = arg. unpack ( )
78
81
&& let ty:: Dynamic ( data, _, _) = ty. kind ( )
79
82
{
80
- return data. principal ( ) ;
83
+ return data
84
+ . principal ( )
85
+ . map ( |principal| tcx. instantiate_bound_regions_with_erased ( principal) ) ;
81
86
}
82
87
}
83
88
@@ -96,28 +101,24 @@ fn dyn_trait_in_self(ty: Ty<'_>) -> Option<ty::PolyExistentialTraitRef<'_>> {
96
101
pub ( crate ) fn get_vtable < ' tcx , Cx : CodegenMethods < ' tcx > > (
97
102
cx : & Cx ,
98
103
ty : Ty < ' tcx > ,
99
- poly_trait_ref : Option < ty:: PolyExistentialTraitRef < ' tcx > > ,
104
+ trait_ref : Option < ty:: ExistentialTraitRef < ' tcx > > ,
100
105
) -> Cx :: Value {
101
106
let tcx = cx. tcx ( ) ;
102
107
103
108
// Check the cache.
104
- if let Some ( & val) = cx. vtables ( ) . borrow ( ) . get ( & ( ty, poly_trait_ref ) ) {
109
+ if let Some ( & val) = cx. vtables ( ) . borrow ( ) . get ( & ( ty, trait_ref ) ) {
105
110
return val;
106
111
}
107
112
108
- // FIXME(trait_upcasting): Take a non-higher-ranked vtable as arg.
109
- let trait_ref =
110
- poly_trait_ref. map ( |trait_ref| tcx. instantiate_bound_regions_with_erased ( trait_ref) ) ;
111
-
112
113
let vtable_alloc_id = tcx. vtable_allocation ( ( ty, trait_ref) ) ;
113
114
let vtable_allocation = tcx. global_alloc ( vtable_alloc_id) . unwrap_memory ( ) ;
114
115
let vtable_const = cx. const_data_from_alloc ( vtable_allocation) ;
115
116
let align = cx. data_layout ( ) . pointer_align . abi ;
116
117
let vtable = cx. static_addr_of ( vtable_const, align, Some ( "vtable" ) ) ;
117
118
118
- cx. apply_vcall_visibility_metadata ( ty, poly_trait_ref , vtable) ;
119
- cx. create_vtable_debuginfo ( ty, poly_trait_ref , vtable) ;
120
- cx. vtables ( ) . borrow_mut ( ) . insert ( ( ty, poly_trait_ref ) , vtable) ;
119
+ cx. apply_vcall_visibility_metadata ( ty, trait_ref , vtable) ;
120
+ cx. create_vtable_debuginfo ( ty, trait_ref , vtable) ;
121
+ cx. vtables ( ) . borrow_mut ( ) . insert ( ( ty, trait_ref ) , vtable) ;
121
122
vtable
122
123
}
123
124
@@ -135,7 +136,7 @@ pub(crate) fn load_vtable<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
135
136
if bx. cx ( ) . sess ( ) . opts . unstable_opts . virtual_function_elimination
136
137
&& bx. cx ( ) . sess ( ) . lto ( ) == Lto :: Fat
137
138
{
138
- if let Some ( trait_ref) = dyn_trait_in_self ( ty) {
139
+ if let Some ( trait_ref) = dyn_trait_in_self ( bx . tcx ( ) , ty) {
139
140
let typeid = bx. typeid_metadata ( typeid_for_trait_ref ( bx. tcx ( ) , trait_ref) ) . unwrap ( ) ;
140
141
let func = bx. type_checked_load ( llvtable, vtable_byte_offset, typeid) ;
141
142
return func;
0 commit comments