Stop writing code that is (unnecessarily) generic over any AstConv in collect,

just hard-code the ccx.
This commit is contained in:
Niko Matsakis 2015-01-04 06:37:49 -05:00
parent 94c345b66c
commit 95ee339bd1

View File

@ -636,7 +636,7 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) {
debug!("trait_def: ident={} trait_def={}", debug!("trait_def: ident={} trait_def={}",
it.ident.repr(ccx.tcx), it.ident.repr(ccx.tcx),
trait_def.repr(ccx.tcx())); trait_def.repr(ccx.tcx));
for trait_method in trait_methods.iter() { for trait_method in trait_methods.iter() {
let self_type = ty::mk_self_type(tcx); let self_type = ty::mk_self_type(tcx);
@ -1108,14 +1108,13 @@ fn ty_generics_for_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
} }
} }
fn ty_generics_for_fn_or_method<'tcx,AC>( fn ty_generics_for_fn_or_method<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
this: &AC, generics: &ast::Generics,
generics: &ast::Generics, base_generics: ty::Generics<'tcx>)
base_generics: ty::Generics<'tcx>) -> ty::Generics<'tcx>
-> ty::Generics<'tcx> {
where AC: AstConv<'tcx> {
let early_lifetimes = resolve_lifetime::early_bound_lifetimes(generics); let early_lifetimes = resolve_lifetime::early_bound_lifetimes(generics);
ty_generics(this, ty_generics(ccx,
subst::FnSpace, subst::FnSpace,
early_lifetimes[], early_lifetimes[],
generics.ty_params[], generics.ty_params[],
@ -1124,11 +1123,11 @@ fn ty_generics_for_fn_or_method<'tcx,AC>(
} }
// Add the Sized bound, unless the type parameter is marked as `?Sized`. // Add the Sized bound, unless the type parameter is marked as `?Sized`.
fn add_unsized_bound<'tcx,AC>(this: &AC, fn add_unsized_bound<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
bounds: &mut ty::BuiltinBounds, bounds: &mut ty::BuiltinBounds,
ast_bounds: &[ast::TyParamBound], ast_bounds: &[ast::TyParamBound],
span: Span) span: Span)
where AC: AstConv<'tcx> { {
// Try to find an unbound in bounds. // Try to find an unbound in bounds.
let mut unbound = None; let mut unbound = None;
for ab in ast_bounds.iter() { for ab in ast_bounds.iter() {
@ -1137,24 +1136,24 @@ fn add_unsized_bound<'tcx,AC>(this: &AC,
assert!(ptr.bound_lifetimes.is_empty()); assert!(ptr.bound_lifetimes.is_empty());
unbound = Some(ptr.trait_ref.clone()); unbound = Some(ptr.trait_ref.clone());
} else { } else {
this.tcx().sess.span_err(span, "type parameter has more than one relaxed default \ ccx.tcx.sess.span_err(span, "type parameter has more than one relaxed default \
bound, only one is supported"); bound, only one is supported");
} }
} }
} }
let kind_id = this.tcx().lang_items.require(SizedTraitLangItem); let kind_id = ccx.tcx.lang_items.require(SizedTraitLangItem);
match unbound { match unbound {
Some(ref tpb) => { Some(ref tpb) => {
// FIXME(#8559) currently requires the unbound to be built-in. // FIXME(#8559) currently requires the unbound to be built-in.
let trait_def_id = ty::trait_ref_to_def_id(this.tcx(), tpb); let trait_def_id = ty::trait_ref_to_def_id(ccx.tcx, tpb);
match kind_id { match kind_id {
Ok(kind_id) if trait_def_id != kind_id => { Ok(kind_id) if trait_def_id != kind_id => {
this.tcx().sess.span_warn(span, ccx.tcx.sess.span_warn(span,
"default bound relaxed for a type parameter, but \ "default bound relaxed for a type parameter, but \
this does nothing because the given bound is not \ this does nothing because the given bound is not \
a default. Only `?Sized` is supported"); a default. Only `?Sized` is supported");
ty::try_add_builtin_trait(this.tcx(), ty::try_add_builtin_trait(ccx.tcx,
kind_id, kind_id,
bounds); bounds);
} }
@ -1162,27 +1161,26 @@ fn add_unsized_bound<'tcx,AC>(this: &AC,
} }
} }
_ if kind_id.is_ok() => { _ if kind_id.is_ok() => {
ty::try_add_builtin_trait(this.tcx(), kind_id.unwrap(), bounds); ty::try_add_builtin_trait(ccx.tcx, kind_id.unwrap(), bounds);
} }
// No lang item for Sized, so we can't add it as a bound. // No lang item for Sized, so we can't add it as a bound.
None => {} None => {}
} }
} }
fn ty_generics<'tcx,AC>(this: &AC, fn ty_generics<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
space: subst::ParamSpace, space: subst::ParamSpace,
lifetime_defs: &[ast::LifetimeDef], lifetime_defs: &[ast::LifetimeDef],
types: &[ast::TyParam], types: &[ast::TyParam],
base_generics: ty::Generics<'tcx>, base_generics: ty::Generics<'tcx>,
where_clause: &ast::WhereClause) where_clause: &ast::WhereClause)
-> ty::Generics<'tcx> -> ty::Generics<'tcx>
where AC: AstConv<'tcx>
{ {
let mut result = base_generics; let mut result = base_generics;
for (i, l) in lifetime_defs.iter().enumerate() { for (i, l) in lifetime_defs.iter().enumerate() {
let bounds = l.bounds.iter() let bounds = l.bounds.iter()
.map(|l| ast_region_to_region(this.tcx(), l)) .map(|l| ast_region_to_region(ccx.tcx, l))
.collect(); .collect();
let def = ty::RegionParameterDef { name: l.lifetime.name, let def = ty::RegionParameterDef { name: l.lifetime.name,
space: space, space: space,
@ -1197,25 +1195,25 @@ fn ty_generics<'tcx,AC>(this: &AC,
// Now create the real type parameters. // Now create the real type parameters.
for (i, param) in types.iter().enumerate() { for (i, param) in types.iter().enumerate() {
let def = get_or_create_type_parameter_def(this, let def = get_or_create_type_parameter_def(ccx,
space, space,
param, param,
i as u32); i as u32);
debug!("ty_generics: def for type param: {}, {}", debug!("ty_generics: def for type param: {}, {}",
def.repr(this.tcx()), def.repr(ccx.tcx),
space); space);
result.types.push(space, def); result.types.push(space, def);
} }
// Just for fun, also push the bounds from the type parameters // Just for fun, also push the bounds from the type parameters
// into the predicates list. This is currently kind of non-DRY. // into the predicates list. This is currently kind of non-DRY.
create_predicates(this.tcx(), &mut result, space); create_predicates(ccx.tcx, &mut result, space);
// Add the bounds not associated with a type parameter // Add the bounds not associated with a type parameter
for predicate in where_clause.predicates.iter() { for predicate in where_clause.predicates.iter() {
match predicate { match predicate {
&ast::WherePredicate::BoundPredicate(ref bound_pred) => { &ast::WherePredicate::BoundPredicate(ref bound_pred) => {
let ty = ast_ty_to_ty(this, &ExplicitRscope, &*bound_pred.bounded_ty); let ty = ast_ty_to_ty(ccx, &ExplicitRscope, &*bound_pred.bounded_ty);
for bound in bound_pred.bounds.iter() { for bound in bound_pred.bounds.iter() {
match bound { match bound {
@ -1223,7 +1221,7 @@ fn ty_generics<'tcx,AC>(this: &AC,
let mut projections = Vec::new(); let mut projections = Vec::new();
let trait_ref = astconv::instantiate_poly_trait_ref( let trait_ref = astconv::instantiate_poly_trait_ref(
this, ccx,
&ExplicitRscope, &ExplicitRscope,
poly_trait_ref, poly_trait_ref,
Some(ty), Some(ty),
@ -1238,7 +1236,7 @@ fn ty_generics<'tcx,AC>(this: &AC,
} }
&ast::TyParamBound::RegionTyParamBound(ref lifetime) => { &ast::TyParamBound::RegionTyParamBound(ref lifetime) => {
let region = ast_region_to_region(this.tcx(), lifetime); let region = ast_region_to_region(ccx.tcx, lifetime);
let pred = ty::Binder(ty::OutlivesPredicate(ty, region)); let pred = ty::Binder(ty::OutlivesPredicate(ty, region));
result.predicates.push(space, ty::Predicate::TypeOutlives(pred)) result.predicates.push(space, ty::Predicate::TypeOutlives(pred))
} }
@ -1247,9 +1245,9 @@ fn ty_generics<'tcx,AC>(this: &AC,
} }
&ast::WherePredicate::RegionPredicate(ref region_pred) => { &ast::WherePredicate::RegionPredicate(ref region_pred) => {
let r1 = ast_region_to_region(this.tcx(), &region_pred.lifetime); let r1 = ast_region_to_region(ccx.tcx, &region_pred.lifetime);
for bound in region_pred.bounds.iter() { for bound in region_pred.bounds.iter() {
let r2 = ast_region_to_region(this.tcx(), bound); let r2 = ast_region_to_region(ccx.tcx, bound);
let pred = ty::Binder(ty::OutlivesPredicate(r1, r2)); let pred = ty::Binder(ty::OutlivesPredicate(r1, r2));
result.predicates.push(space, ty::Predicate::RegionOutlives(pred)) result.predicates.push(space, ty::Predicate::RegionOutlives(pred))
} }
@ -1257,7 +1255,7 @@ fn ty_generics<'tcx,AC>(this: &AC,
&ast::WherePredicate::EqPredicate(ref eq_pred) => { &ast::WherePredicate::EqPredicate(ref eq_pred) => {
// FIXME(#20041) // FIXME(#20041)
this.tcx().sess.span_bug(eq_pred.span, ccx.tcx.sess.span_bug(eq_pred.span,
"Equality constraints are not yet \ "Equality constraints are not yet \
implemented (#20041)") implemented (#20041)")
} }
@ -1292,34 +1290,33 @@ fn ty_generics<'tcx,AC>(this: &AC,
} }
} }
fn get_or_create_type_parameter_def<'tcx,AC>(this: &AC, fn get_or_create_type_parameter_def<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
space: subst::ParamSpace, space: subst::ParamSpace,
param: &ast::TyParam, param: &ast::TyParam,
index: u32) index: u32)
-> ty::TypeParameterDef<'tcx> -> ty::TypeParameterDef<'tcx>
where AC: AstConv<'tcx>
{ {
match this.tcx().ty_param_defs.borrow().get(&param.id) { match ccx.tcx.ty_param_defs.borrow().get(&param.id) {
Some(d) => { return (*d).clone(); } Some(d) => { return (*d).clone(); }
None => { } None => { }
} }
let param_ty = ty::ParamTy::new(space, index, param.ident.name); let param_ty = ty::ParamTy::new(space, index, param.ident.name);
let bounds = compute_bounds(this, let bounds = compute_bounds(ccx,
param_ty.to_ty(this.tcx()), param_ty.to_ty(ccx.tcx),
param.bounds[], param.bounds[],
SizedByDefault::Yes, SizedByDefault::Yes,
param.span); param.span);
let default = match param.default { let default = match param.default {
None => None, None => None,
Some(ref path) => { Some(ref path) => {
let ty = ast_ty_to_ty(this, &ExplicitRscope, &**path); let ty = ast_ty_to_ty(ccx, &ExplicitRscope, &**path);
let cur_idx = index; let cur_idx = index;
ty::walk_ty(ty, |t| { ty::walk_ty(ty, |t| {
match t.sty { match t.sty {
ty::ty_param(p) => if p.idx > cur_idx { ty::ty_param(p) => if p.idx > cur_idx {
span_err!(this.tcx().sess, path.span, E0128, span_err!(ccx.tcx.sess, path.span, E0128,
"type parameters with a default cannot use \ "type parameters with a default cannot use \
forward declared identifiers"); forward declared identifiers");
}, },
@ -1340,7 +1337,7 @@ fn get_or_create_type_parameter_def<'tcx,AC>(this: &AC,
default: default default: default
}; };
this.tcx().ty_param_defs.borrow_mut().insert(param.id, def.clone()); ccx.tcx.ty_param_defs.borrow_mut().insert(param.id, def.clone());
def def
} }
@ -1350,26 +1347,25 @@ enum SizedByDefault { Yes, No }
/// Translate the AST's notion of ty param bounds (which are an enum consisting of a newtyped Ty or /// Translate the AST's notion of ty param bounds (which are an enum consisting of a newtyped Ty or
/// a region) to ty's notion of ty param bounds, which can either be user-defined traits, or the /// a region) to ty's notion of ty param bounds, which can either be user-defined traits, or the
/// built-in trait (formerly known as kind): Send. /// built-in trait (formerly known as kind): Send.
fn compute_bounds<'tcx,AC>(this: &AC, fn compute_bounds<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
param_ty: ty::Ty<'tcx>, param_ty: ty::Ty<'tcx>,
ast_bounds: &[ast::TyParamBound], ast_bounds: &[ast::TyParamBound],
sized_by_default: SizedByDefault, sized_by_default: SizedByDefault,
span: Span) span: Span)
-> ty::ParamBounds<'tcx> -> ty::ParamBounds<'tcx>
where AC: AstConv<'tcx>
{ {
let mut param_bounds = conv_param_bounds(this, let mut param_bounds = conv_param_bounds(ccx,
span, span,
param_ty, param_ty,
ast_bounds); ast_bounds);
if let SizedByDefault::Yes = sized_by_default { if let SizedByDefault::Yes = sized_by_default {
add_unsized_bound(this, add_unsized_bound(ccx,
&mut param_bounds.builtin_bounds, &mut param_bounds.builtin_bounds,
ast_bounds, ast_bounds,
span); span);
check_bounds_compatible(this.tcx(), check_bounds_compatible(ccx.tcx,
param_ty, param_ty,
&param_bounds, &param_bounds,
span); span);
@ -1404,24 +1400,23 @@ fn check_bounds_compatible<'tcx>(tcx: &ty::ctxt<'tcx>,
} }
} }
fn conv_param_bounds<'tcx,AC>(this: &AC, fn conv_param_bounds<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
span: Span, span: Span,
param_ty: ty::Ty<'tcx>, param_ty: ty::Ty<'tcx>,
ast_bounds: &[ast::TyParamBound]) ast_bounds: &[ast::TyParamBound])
-> ty::ParamBounds<'tcx> -> ty::ParamBounds<'tcx>
where AC: AstConv<'tcx>
{ {
let astconv::PartitionedBounds { builtin_bounds, let astconv::PartitionedBounds { builtin_bounds,
trait_bounds, trait_bounds,
region_bounds } = region_bounds } =
astconv::partition_bounds(this.tcx(), span, ast_bounds.as_slice()); astconv::partition_bounds(ccx.tcx, span, ast_bounds.as_slice());
let mut projection_bounds = Vec::new(); let mut projection_bounds = Vec::new();
let trait_bounds: Vec<ty::PolyTraitRef> = let trait_bounds: Vec<ty::PolyTraitRef> =
trait_bounds.into_iter() trait_bounds.into_iter()
.map(|bound| { .map(|bound| {
astconv::instantiate_poly_trait_ref(this, astconv::instantiate_poly_trait_ref(ccx,
&ExplicitRscope, &ExplicitRscope,
bound, bound,
Some(param_ty), Some(param_ty),
@ -1430,7 +1425,7 @@ fn conv_param_bounds<'tcx,AC>(this: &AC,
.collect(); .collect();
let region_bounds: Vec<ty::Region> = let region_bounds: Vec<ty::Region> =
region_bounds.into_iter() region_bounds.into_iter()
.map(|r| ast_region_to_region(this.tcx(), r)) .map(|r| ast_region_to_region(ccx.tcx, r))
.collect(); .collect();
ty::ParamBounds { ty::ParamBounds {
region_bounds: region_bounds, region_bounds: region_bounds,