Skip to content

Commit

Permalink
auto merge of #13503 : edwardw/rust/lifetime-ice, r=nikomatsakis
Browse files Browse the repository at this point in the history
When instantiating trait default methods for certain implementation,
`typeck` correctly combined type parameters from trait bound with those
from method bound, but didn't do so for lifetime parameters. Applies
the same logic to lifetime parameters.

Closes #13204
  • Loading branch information
bors committed Apr 17, 2014
2 parents 9f3fd93 + daa1f50 commit 1dec477
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
58 changes: 28 additions & 30 deletions src/librustc/middle/typeck/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use syntax::ast_map::NodeItem;
use syntax::ast_map;
use syntax::ast_util::{def_id_of_def, local_def};
use syntax::codemap::Span;
use syntax::owned_slice::OwnedSlice;
use syntax::parse::token;
use syntax::visit;

Expand Down Expand Up @@ -346,7 +347,8 @@ impl<'a> CoherenceChecker<'a> {
Rc::new(Vec::from_slice(impl_poly_type.generics.type_param_defs()).append(
new_method_ty.generics.type_param_defs())),
region_param_defs:
impl_poly_type.generics.region_param_defs.clone()
Rc::new(Vec::from_slice(impl_poly_type.generics.region_param_defs()).append(
new_method_ty.generics.region_param_defs()))
};
let new_polytype = ty::ty_param_bounds_and_ty {
generics: new_generics,
Expand Down Expand Up @@ -741,39 +743,35 @@ pub fn make_substs_for_receiver_types(tcx: &ty::ctxt,
* receiver and method generics.
*/

// determine how many type parameters were declared on the impl
let num_impl_type_parameters = {
let impl_polytype = ty::lookup_item_type(tcx, impl_id);
impl_polytype.generics.type_param_defs().len()
};

// determine how many type parameters appear on the trait
let num_trait_type_parameters = trait_ref.substs.tps.len();

// the current method type has the type parameters from the trait + method
let num_method_type_parameters =
num_trait_type_parameters + method.generics.type_param_defs().len();

// the new method type will have the type parameters from the impl + method
let combined_tps = Vec::from_fn(num_method_type_parameters, |i| {
if i < num_trait_type_parameters {
// replace type parameters that come from trait with new value
*trait_ref.substs.tps.get(i)
} else {
// replace type parameters that belong to method with another
// type parameter, this time with the index adjusted
let method_index = i - num_trait_type_parameters;
let type_param_def = &method.generics.type_param_defs()[method_index];
let new_index = num_impl_type_parameters + method_index;
ty::mk_param(tcx, new_index, type_param_def.def_id)
let impl_polytype = ty::lookup_item_type(tcx, impl_id);
let num_impl_tps = impl_polytype.generics.type_param_defs().len();
let num_impl_regions = impl_polytype.generics.region_param_defs().len();
let meth_tps: Vec<ty::t> =
method.generics.type_param_defs().iter().enumerate()
.map(|(i, t)| ty::mk_param(tcx, i + num_impl_tps, t.def_id))
.collect();
let meth_regions: Vec<ty::Region> =
method.generics.region_param_defs().iter().enumerate()
.map(|(i, l)| ty::ReEarlyBound(l.def_id.node, i + num_impl_regions, l.name))
.collect();
let mut combined_tps = trait_ref.substs.tps.clone();
combined_tps.push_all_move(meth_tps);
let combined_regions = match &trait_ref.substs.regions {
&ty::ErasedRegions =>
fail!("make_substs_for_receiver_types: unexpected ErasedRegions"),

&ty::NonerasedRegions(ref rs) => {
let mut rs = rs.clone().into_vec();
rs.push_all_move(meth_regions);
ty::NonerasedRegions(OwnedSlice::from_vec(rs))
}
});
};

return ty::substs {
regions: trait_ref.substs.regions.clone(),
ty::substs {
regions: combined_regions,
self_ty: trait_ref.substs.self_ty,
tps: combined_tps
};
}
}

fn subst_receiver_types_in_method_ty(tcx: &ty::ctxt,
Expand Down
32 changes: 32 additions & 0 deletions src/test/run-pass/issue-13204.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test that when instantiating trait default methods, typeck handles
// lifetime parameters defined on the method bound correctly.

pub trait Foo {
fn bar<'a, I: Iterator<&'a ()>>(&self, it: I) -> uint {
let mut xs = it.filter(|_| true);
xs.len()
}
}

pub struct Baz;

impl Foo for Baz {
// When instantiating `Foo::bar` for `Baz` here, typeck used to
// ICE due to the lifetime parameter of `bar`.
}

fn main() {
let x = Baz;
let y = vec!((), (), ());
assert_eq!(x.bar(y.iter()), 3);
}

0 comments on commit 1dec477

Please sign in to comment.