Skip to content

Commit

Permalink
Remove the AssocSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Dec 30, 2014
1 parent 42e645c commit de8e0ae
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 96 deletions.
3 changes: 1 addition & 2 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,8 @@ impl<'tcx, 'a> vtable_decoder_helpers<'tcx> for reader::Decoder<'a> {
{
let types = self.read_to_vec(|this| Ok(f(this))).unwrap();
let selfs = self.read_to_vec(|this| Ok(f(this))).unwrap();
let assocs = self.read_to_vec(|this| Ok(f(this))).unwrap();
let fns = self.read_to_vec(|this| Ok(f(this))).unwrap();
VecPerParamSpace::new(types, selfs, assocs, fns)
VecPerParamSpace::new(types, selfs, fns)
}

fn read_vtable_res_with_key(&mut self,
Expand Down
5 changes: 1 addition & 4 deletions src/librustc/middle/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,10 +864,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let region_param_defs = generics.regions.get_slice(subst::TypeSpace);
let regions = self.region_vars_for_defs(span, region_param_defs);

let assoc_type_parameter_count = generics.types.len(subst::AssocSpace);
let assoc_type_parameters = self.next_ty_vars(assoc_type_parameter_count);

subst::Substs::new_trait(type_parameters, regions, assoc_type_parameters, self_ty)
subst::Substs::new_trait(type_parameters, regions, self_ty)
}

pub fn fresh_bound_region(&self, debruijn: ty::DebruijnIndex) -> ty::Region {
Expand Down
65 changes: 19 additions & 46 deletions src/librustc/middle/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,17 @@ impl<'tcx> Substs<'tcx> {
r: Vec<ty::Region>)
-> Substs<'tcx>
{
Substs::new(VecPerParamSpace::new(t, Vec::new(), Vec::new(), Vec::new()),
VecPerParamSpace::new(r, Vec::new(), Vec::new(), Vec::new()))
Substs::new(VecPerParamSpace::new(t, Vec::new(), Vec::new()),
VecPerParamSpace::new(r, Vec::new(), Vec::new()))
}

pub fn new_trait(t: Vec<Ty<'tcx>>,
r: Vec<ty::Region>,
a: Vec<Ty<'tcx>>,
s: Ty<'tcx>)
-> Substs<'tcx>
{
Substs::new(VecPerParamSpace::new(t, vec!(s), a, Vec::new()),
VecPerParamSpace::new(r, Vec::new(), Vec::new(), Vec::new()))
Substs::new(VecPerParamSpace::new(t, vec!(s), Vec::new()),
VecPerParamSpace::new(r, Vec::new(), Vec::new()))
}

pub fn erased(t: VecPerParamSpace<Ty<'tcx>>) -> Substs<'tcx>
Expand Down Expand Up @@ -123,13 +122,6 @@ impl<'tcx> Substs<'tcx> {
s
}

pub fn with_assoc_tys(&self, assoc_tys: Vec<Ty<'tcx>>) -> Substs<'tcx> {
assert!(self.types.is_empty_in(AssocSpace));
let mut s = (*self).clone();
s.types.replace(AssocSpace, assoc_tys);
s
}

pub fn erase_regions(self) -> Substs<'tcx> {
let Substs { types, regions: _ } = self;
Substs { types: types, regions: ErasedRegions }
Expand Down Expand Up @@ -192,30 +184,27 @@ impl RegionSubsts {
pub enum ParamSpace {
TypeSpace, // Type parameters attached to a type definition, trait, or impl
SelfSpace, // Self parameter on a trait
AssocSpace, // Assoc types defined in a trait/impl
FnSpace, // Type parameters attached to a method or fn
}

impl ParamSpace {
pub fn all() -> [ParamSpace, ..4] {
[TypeSpace, SelfSpace, AssocSpace, FnSpace]
pub fn all() -> [ParamSpace, ..3] {
[TypeSpace, SelfSpace, FnSpace]
}

pub fn to_uint(self) -> uint {
match self {
TypeSpace => 0,
SelfSpace => 1,
AssocSpace => 2,
FnSpace => 3,
FnSpace => 2,
}
}

pub fn from_uint(u: uint) -> ParamSpace {
match u {
0 => TypeSpace,
1 => SelfSpace,
2 => AssocSpace,
3 => FnSpace,
2 => FnSpace,
_ => panic!("Invalid ParamSpace: {}", u)
}
}
Expand All @@ -235,11 +224,9 @@ pub struct VecPerParamSpace<T> {
//
// AF(self) = (self.content[..self.type_limit],
// self.content[self.type_limit..self.self_limit],
// self.content[self.self_limit..self.assoc_limit],
// self.content[self.assoc_limit..])
// self.content[self.self_limit..])
type_limit: uint,
self_limit: uint,
assoc_limit: uint,
content: Vec<T>,
}

Expand All @@ -248,7 +235,6 @@ pub struct VecPerParamSpace<T> {
pub struct SeparateVecsPerParamSpace<T> {
pub types: Vec<T>,
pub selfs: Vec<T>,
pub assocs: Vec<T>,
pub fns: Vec<T>,
}

Expand All @@ -268,16 +254,14 @@ impl<T> VecPerParamSpace<T> {
match space {
TypeSpace => (0, self.type_limit),
SelfSpace => (self.type_limit, self.self_limit),
AssocSpace => (self.self_limit, self.assoc_limit),
FnSpace => (self.assoc_limit, self.content.len()),
FnSpace => (self.self_limit, self.content.len()),
}
}

pub fn empty() -> VecPerParamSpace<T> {
VecPerParamSpace {
type_limit: 0,
self_limit: 0,
assoc_limit: 0,
content: Vec::new()
}
}
Expand All @@ -290,31 +274,27 @@ impl<T> VecPerParamSpace<T> {
/// `s` is the self space.
/// `a` is the assoc space.
/// `f` is the fn space.
pub fn new(t: Vec<T>, s: Vec<T>, a: Vec<T>, f: Vec<T>) -> VecPerParamSpace<T> {
pub fn new(t: Vec<T>, s: Vec<T>, f: Vec<T>) -> VecPerParamSpace<T> {
let type_limit = t.len();
let self_limit = type_limit + s.len();
let assoc_limit = self_limit + a.len();

let mut content = t;
content.extend(s.into_iter());
content.extend(a.into_iter());
content.extend(f.into_iter());

VecPerParamSpace {
type_limit: type_limit,
self_limit: self_limit,
assoc_limit: assoc_limit,
content: content,
}
}

fn new_internal(content: Vec<T>, type_limit: uint, self_limit: uint, assoc_limit: uint)
fn new_internal(content: Vec<T>, type_limit: uint, self_limit: uint)
-> VecPerParamSpace<T>
{
VecPerParamSpace {
type_limit: type_limit,
self_limit: self_limit,
assoc_limit: assoc_limit,
content: content,
}
}
Expand All @@ -326,9 +306,8 @@ impl<T> VecPerParamSpace<T> {
pub fn push(&mut self, space: ParamSpace, value: T) {
let (_, limit) = self.limits(space);
match space {
TypeSpace => { self.type_limit += 1; self.self_limit += 1; self.assoc_limit += 1; }
SelfSpace => { self.self_limit += 1; self.assoc_limit += 1; }
AssocSpace => { self.assoc_limit += 1; }
TypeSpace => { self.type_limit += 1; self.self_limit += 1; }
SelfSpace => { self.self_limit += 1; }
FnSpace => { }
}
self.content.insert(limit, value);
Expand All @@ -340,9 +319,8 @@ impl<T> VecPerParamSpace<T> {
None
} else {
match space {
TypeSpace => { self.type_limit -= 1; self.self_limit -= 1; self.assoc_limit -= 1; }
SelfSpace => { self.self_limit -= 1; self.assoc_limit -= 1; }
AssocSpace => { self.assoc_limit -= 1; }
TypeSpace => { self.type_limit -= 1; self.self_limit -= 1; }
SelfSpace => { self.self_limit -= 1; }
FnSpace => {}
}
self.content.remove(limit - 1)
Expand Down Expand Up @@ -439,8 +417,7 @@ impl<T> VecPerParamSpace<T> {
let result = self.iter().map(pred).collect();
VecPerParamSpace::new_internal(result,
self.type_limit,
self.self_limit,
self.assoc_limit)
self.self_limit)
}

pub fn map_enumerated<U, P>(&self, pred: P) -> VecPerParamSpace<U> where
Expand All @@ -449,8 +426,7 @@ impl<T> VecPerParamSpace<T> {
let result = self.iter_enumerated().map(pred).collect();
VecPerParamSpace::new_internal(result,
self.type_limit,
self.self_limit,
self.assoc_limit)
self.self_limit)
}

pub fn map_move<U, F>(self, mut pred: F) -> VecPerParamSpace<U> where
Expand All @@ -459,25 +435,22 @@ impl<T> VecPerParamSpace<T> {
let SeparateVecsPerParamSpace {
types: t,
selfs: s,
assocs: a,
fns: f
} = self.split();

VecPerParamSpace::new(t.into_iter().map(|p| pred(p)).collect(),
s.into_iter().map(|p| pred(p)).collect(),
a.into_iter().map(|p| pred(p)).collect(),
f.into_iter().map(|p| pred(p)).collect())
}

pub fn split(self) -> SeparateVecsPerParamSpace<T> {
let VecPerParamSpace { type_limit, self_limit, assoc_limit, content } = self;
let VecPerParamSpace { type_limit, self_limit, content } = self;

let mut content_iter = content.into_iter();

SeparateVecsPerParamSpace {
types: content_iter.by_ref().take(type_limit).collect(),
selfs: content_iter.by_ref().take(self_limit - type_limit).collect(),
assocs: content_iter.by_ref().take(assoc_limit - self_limit).collect(),
fns: content_iter.collect()
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/librustc/middle/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1674,8 +1674,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
});
}

let obligations = VecPerParamSpace::new(obligations, Vec::new(),
Vec::new(), Vec::new());
let obligations = VecPerParamSpace::new(obligations, Vec::new(), Vec::new());

debug!("vtable_builtin_data: obligations={}",
obligations.repr(self.tcx()));
Expand Down Expand Up @@ -1769,7 +1768,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
Substs::new_trait(
vec![arguments_tuple, output_type],
vec![],
vec![],
self_ty);
let trait_ref = ty::Binder(Rc::new(ty::TraitRef {
def_id: obligation.predicate.def_id(),
Expand Down Expand Up @@ -1810,7 +1808,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
vec![arguments_tuple.subst(self.tcx(), substs),
closure_sig.0.output.unwrap().subst(self.tcx(), substs)],
vec![],
vec![],
obligation.self_ty());
let trait_ref = ty::Binder(Rc::new(ty::TraitRef {
def_id: obligation.predicate.def_id(),
Expand Down
15 changes: 2 additions & 13 deletions src/librustc/middle/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use middle::subst::{Subst, Substs, VecPerParamSpace};
use middle::subst::{Substs, VecPerParamSpace};
use middle::infer::InferCtxt;
use middle::ty::{mod, Ty, AsPredicate, ToPolyTraitRef};
use std::collections::HashSet;
Expand Down Expand Up @@ -229,18 +229,7 @@ pub fn fresh_substs_for_impl<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
{
let tcx = infcx.tcx;
let impl_generics = ty::lookup_item_type(tcx, impl_def_id).generics;
let input_substs = infcx.fresh_substs_for_generics(span, &impl_generics);

// Add substs for the associated types bound in the impl.
let ref items = tcx.impl_items.borrow()[impl_def_id];
let mut assoc_tys = Vec::new();
for item in items.iter() {
if let &ty::ImplOrTraitItemId::TypeTraitItemId(id) = item {
assoc_tys.push(tcx.tcache.borrow()[id].ty.subst(tcx, &input_substs));
}
}

input_substs.with_assoc_tys(assoc_tys)
infcx.fresh_substs_for_generics(span, &impl_generics)
}

impl<'tcx, N> fmt::Show for VtableImplData<'tcx, N> {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/util/ppaux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,9 @@ impl<'tcx> Repr<'tcx> for subst::Substs<'tcx> {

impl<'tcx, T:Repr<'tcx>> Repr<'tcx> for subst::VecPerParamSpace<T> {
fn repr(&self, tcx: &ctxt<'tcx>) -> String {
format!("[{};{};{};{}]",
format!("[{};{};{}]",
self.get_slice(subst::TypeSpace).repr(tcx),
self.get_slice(subst::SelfSpace).repr(tcx),
self.get_slice(subst::AssocSpace).repr(tcx),
self.get_slice(subst::FnSpace).repr(tcx))
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/librustc_trans/trans/meth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ pub fn trans_static_method_callee(bcx: Block,
let subst::SeparateVecsPerParamSpace {
types: rcvr_type,
selfs: rcvr_self,
assocs: rcvr_assoc,
fns: rcvr_method
} = rcvr_substs.types.split();

Expand Down Expand Up @@ -238,7 +237,6 @@ pub fn trans_static_method_callee(bcx: Block,
let trait_substs =
Substs::erased(VecPerParamSpace::new(rcvr_type,
rcvr_self,
rcvr_assoc,
Vec::new()));
let trait_substs = bcx.tcx().mk_substs(trait_substs);
debug!("trait_substs={}", trait_substs.repr(bcx.tcx()));
Expand Down Expand Up @@ -276,13 +274,11 @@ pub fn trans_static_method_callee(bcx: Block,
let subst::SeparateVecsPerParamSpace {
types: impl_type,
selfs: impl_self,
assocs: impl_assoc,
fns: _
} = impl_substs.types.split();
let callee_substs =
Substs::erased(VecPerParamSpace::new(impl_type,
impl_self,
impl_assoc,
rcvr_method));

let mth_id = method_with_name(ccx, impl_did, mname);
Expand Down Expand Up @@ -411,13 +407,12 @@ fn combine_impl_and_methods_tps<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
let subst::SeparateVecsPerParamSpace {
types: rcvr_type,
selfs: rcvr_self,
assocs: rcvr_assoc,
fns: rcvr_method
} = rcvr_substs.types.clone().split();
assert!(rcvr_method.is_empty());
subst::Substs {
regions: subst::ErasedRegions,
types: subst::VecPerParamSpace::new(rcvr_type, rcvr_self, rcvr_assoc, node_method)
types: subst::VecPerParamSpace::new(rcvr_type, rcvr_self, node_method)
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/librustc_typeck/check/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,11 @@ pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>,
}
};

let number_assoc_types = trait_def.generics.types.len(subst::AssocSpace);
let assoc_types = fcx.inh.infcx.next_ty_vars(number_assoc_types);

assert_eq!(trait_def.generics.types.len(subst::FnSpace), 0);
assert!(trait_def.generics.regions.is_empty());

// Construct a trait-reference `self_ty : Trait<input_tys>`
let substs = subst::Substs::new_trait(input_types, Vec::new(), assoc_types, self_ty);
let substs = subst::Substs::new_trait(input_types, Vec::new(), self_ty);
let trait_ref = Rc::new(ty::TraitRef::new(trait_def_id, fcx.tcx().mk_substs(substs)));

// Construct an obligation
Expand Down
1 change: 0 additions & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5430,7 +5430,6 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
span_err!(fcx.tcx().sess, data.bindings[0].span, E0182,
"unexpected binding of associated item in expression path \
(only allowed in type paths)");
substs.types.truncate(subst::ParamSpace::AssocSpace, 0);
}

{
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ pub fn trait_def_of_item<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
// ...and also create the `Self` parameter.
let self_ty = ty::mk_self_type(ccx.tcx);

subst::Substs::new_trait(types, regions, Vec::new(), self_ty)
subst::Substs::new_trait(types, regions, self_ty)
}
}

Expand Down
Binary file not shown.

0 comments on commit de8e0ae

Please sign in to comment.