@@ -1140,8 +1140,17 @@ pub fn typeid_for_instance<'tcx>(
11401140 let predicates = tcx. mk_poly_existential_predicates ( & [ ty:: Binder :: dummy ( predicate) ] ) ;
11411141 let self_ty = Ty :: new_dynamic ( tcx, predicates, tcx. lifetimes . re_erased , ty:: Dyn ) ;
11421142 instance. args = tcx. mk_args_trait ( self_ty, List :: empty ( ) ) ;
1143- } else if matches ! ( instance. def, ty:: InstanceDef :: Virtual ( ..) ) {
1144- instance. args = strip_receiver_auto ( tcx, instance. args ) ;
1143+ } else if let ty:: InstanceDef :: Virtual ( def_id, _) = instance. def {
1144+ let upcast_ty = match tcx. trait_of_item ( def_id) {
1145+ Some ( trait_id) => trait_object_ty (
1146+ tcx,
1147+ ty:: Binder :: dummy ( ty:: TraitRef :: from_method ( tcx, trait_id, instance. args ) ) ,
1148+ ) ,
1149+ // drop_in_place won't have a defining trait, skip the upcast
1150+ None => instance. args . type_at ( 0 ) ,
1151+ } ;
1152+ let stripped_ty = strip_receiver_auto ( tcx, upcast_ty) ;
1153+ instance. args = tcx. mk_args_trait ( stripped_ty, instance. args . into_iter ( ) . skip ( 1 ) ) ;
11451154 }
11461155
11471156 if let Some ( impl_id) = tcx. impl_of_method ( instance. def_id ( ) )
@@ -1190,15 +1199,11 @@ pub fn typeid_for_instance<'tcx>(
11901199 typeid_for_fnabi ( tcx, fn_abi, options)
11911200}
11921201
1193- fn strip_receiver_auto < ' tcx > (
1194- tcx : TyCtxt < ' tcx > ,
1195- args : ty:: GenericArgsRef < ' tcx > ,
1196- ) -> ty:: GenericArgsRef < ' tcx > {
1197- let ty = args. type_at ( 0 ) ;
1202+ fn strip_receiver_auto < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
11981203 let ty:: Dynamic ( preds, lifetime, kind) = ty. kind ( ) else {
11991204 bug ! ( "Tried to strip auto traits from non-dynamic type {ty}" ) ;
12001205 } ;
1201- let new_rcvr = if preds. principal ( ) . is_some ( ) {
1206+ if preds. principal ( ) . is_some ( ) {
12021207 let filtered_preds =
12031208 tcx. mk_poly_existential_predicates_from_iter ( preds. into_iter ( ) . filter ( |pred| {
12041209 !matches ! ( pred. skip_binder( ) , ty:: ExistentialPredicate :: AutoTrait ( ..) )
@@ -1209,8 +1214,7 @@ fn strip_receiver_auto<'tcx>(
12091214 // about it. This technically discards the knowledge that it was a type that was made
12101215 // into a trait object at some point, but that's not a lot.
12111216 tcx. types . unit
1212- } ;
1213- tcx. mk_args_trait ( new_rcvr, args. into_iter ( ) . skip ( 1 ) )
1217+ }
12141218}
12151219
12161220#[ instrument( skip( tcx) , ret) ]
0 commit comments