@@ -42,6 +42,7 @@ use middle::trans::type_of;
4242use middle:: ty;
4343use middle:: subst:: Subst ;
4444use middle:: typeck;
45+ use middle:: typeck:: coherence:: make_substs_for_receiver_types;
4546use util:: ppaux:: Repr ;
4647
4748use core:: vec;
@@ -253,50 +254,24 @@ pub fn trans_fn_ref_with_vtables(
253254 // So, what we need to do is find this substitution and
254255 // compose it with the one we already have.
255256
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 ) ;
290261
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
292267 let new_substs = first_subst. subst ( tcx, & substs) ;
293268 debug ! ( "trans_fn_with_vtables - default method: \
294- substs = %s, id_subst = %s, trait_subst = %s, \
269+ substs = %s, trait_subst = %s, \
295270 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) ,
298272 first_subst. repr( tcx) , new_substs. repr( tcx) ) ;
299273
274+
300275 ( source. method_id , Some ( source. impl_id ) , new_substs)
301276 }
302277 } ;
0 commit comments