@@ -42,6 +42,7 @@ use middle::trans::type_of;
42
42
use middle:: ty;
43
43
use middle:: subst:: Subst ;
44
44
use middle:: typeck;
45
+ use middle:: typeck:: coherence:: make_substs_for_receiver_types;
45
46
use util:: ppaux:: Repr ;
46
47
47
48
use core:: vec;
@@ -253,50 +254,24 @@ pub fn trans_fn_ref_with_vtables(
253
254
// So, what we need to do is find this substitution and
254
255
// compose it with the one we already have.
255
256
256
- // In order to find the substitution for the trait params,
257
- // we look up the impl in the ast map, find its trait_ref
258
- // id, then look up its trait ref. I feel like there
259
- // should be a better way.
260
- let map_node = session:: expect (
261
- ccx. sess ,
262
- ccx. tcx . items . find_copy ( & source. impl_id . node ) ,
263
- || fmt ! ( "couldn't find node while monomorphizing \
264
- default method: %?", source. impl_id. node) ) ;
265
- let item = match map_node {
266
- ast_map:: node_item( item, _) => item,
267
- _ => ccx. tcx . sess . bug ( "Not an item" )
268
- } ;
269
- let ast_trait_ref = match copy item. node {
270
- ast:: item_impl( _, Some ( tr) , _, _) => tr,
271
- _ => ccx. tcx . sess . bug ( "Not an impl with trait_ref" )
272
- } ;
273
- let trait_ref = ccx. tcx . trait_refs . get ( & ast_trait_ref. ref_id ) ;
274
-
275
- // The substs from the trait_ref only substitues for the
276
- // trait parameters. Our substitution also needs to be
277
- // able to substitute for the actual method type
278
- // params. To do this, we figure out how many method
279
- // parameters there are and pad out the substitution with
280
- // substitution for the variables.
281
- let item_ty = ty:: lookup_item_type ( tcx, source. method_id ) ;
282
- let num_params = item_ty. generics . type_param_defs . len ( ) -
283
- trait_ref. substs . tps . len ( ) ;
284
- let id_subst = do vec:: from_fn ( num_params) |i| {
285
- ty:: mk_param ( tcx, i, ast:: def_id { crate : 0 , node : 0 } )
286
- } ;
287
- // Merge the two substitions together now.
288
- let first_subst = ty:: substs { tps : trait_ref. substs . tps + id_subst,
289
- .. trait_ref. substs } ;
257
+ let trait_ref = ty:: impl_trait_ref ( tcx, source. impl_id )
258
+ . expect ( "could not find trait_ref for impl with \
259
+ default methods") ;
260
+ let method = ty:: method ( tcx, source. method_id ) ;
290
261
291
- // And compose them.
262
+ // Compute the first substitution
263
+ let first_subst = make_substs_for_receiver_types (
264
+ tcx, source. impl_id , trait_ref, method) ;
265
+
266
+ // And compose them
292
267
let new_substs = first_subst. subst ( tcx, & substs) ;
293
268
debug ! ( "trans_fn_with_vtables - default method: \
294
- substs = %s, id_subst = %s, trait_subst = %s, \
269
+ substs = %s, trait_subst = %s, \
295
270
first_subst = %s, new_subst = %s",
296
- substs. repr( tcx) ,
297
- id_subst. repr( tcx) , trait_ref. substs. repr( tcx) ,
271
+ substs. repr( tcx) , trait_ref. substs. repr( tcx) ,
298
272
first_subst. repr( tcx) , new_substs. repr( tcx) ) ;
299
273
274
+
300
275
( source. method_id , Some ( source. impl_id ) , new_substs)
301
276
}
302
277
} ;
0 commit comments