Skip to content

Commit 952dded

Browse files
committed
Refactored VecPerParamSpace to hide exposure of Vec representation.
This basically meant changing the interface so that no borrowed `&Vec` is exposed, by hiding `fn get_vec` and `fn get_mut_vec` and revising `fn all_vecs`. Instead, clients should use one of the other methods; `get_slice`, `pop`, `truncate`, `replace`, `push_all`, or `is_empty_in`, which should work for any case currently used in rustc.
1 parent 9f2a43c commit 952dded

File tree

16 files changed

+135
-108
lines changed

16 files changed

+135
-108
lines changed

src/librustc/metadata/tyencode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn enc_vec_per_param_space<T>(w: &mut MemWriter,
100100
op: |&mut MemWriter, &ctxt, &T|) {
101101
for &space in subst::ParamSpace::all().iter() {
102102
mywrite!(w, "[");
103-
for t in v.get_vec(space).iter() {
103+
for t in v.get_slice(space).iter() {
104104
op(w, cx, t);
105105
}
106106
mywrite!(w, "]");

src/librustc/middle/astencode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ fn encode_vec_per_param_space<T>(ebml_w: &mut Encoder,
810810
v: &subst::VecPerParamSpace<T>,
811811
f: |&mut Encoder, &T|) {
812812
for &space in subst::ParamSpace::all().iter() {
813-
ebml_w.emit_from_vec(v.get_vec(space).as_slice(),
813+
ebml_w.emit_from_vec(v.get_slice(space),
814814
|ebml_w, n| Ok(f(ebml_w, n))).unwrap();
815815
}
816816
}

src/librustc/middle/subst.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ pub struct VecPerParamSpace<T> {
264264
vecs: (Vec<T>, Vec<T>, Vec<T>)
265265
}
266266

267+
impl<T:Clone> VecPerParamSpace<T> {
268+
pub fn push_all(&mut self, space: ParamSpace, values: &[T]) {
269+
self.get_mut_vec(space).push_all(values);
270+
}
271+
}
272+
267273
impl<T> VecPerParamSpace<T> {
268274
pub fn empty() -> VecPerParamSpace<T> {
269275
VecPerParamSpace {
@@ -293,6 +299,18 @@ impl<T> VecPerParamSpace<T> {
293299
self.get_mut_vec(space).push(value);
294300
}
295301

302+
pub fn pop(&mut self, space: ParamSpace) -> Option<T> {
303+
self.get_mut_vec(space).pop()
304+
}
305+
306+
pub fn truncate(&mut self, space: ParamSpace, len: uint) {
307+
self.get_mut_vec(space).truncate(len)
308+
}
309+
310+
pub fn replace(&mut self, space: ParamSpace, elems: Vec<T>) {
311+
*self.get_mut_vec(space) = elems;
312+
}
313+
296314
pub fn get_self<'a>(&'a self) -> Option<&'a T> {
297315
let v = self.get_vec(SelfSpace);
298316
assert!(v.len() <= 1);
@@ -303,11 +321,19 @@ impl<T> VecPerParamSpace<T> {
303321
self.get_vec(space).len()
304322
}
305323

306-
pub fn get_vec<'a>(&'a self, space: ParamSpace) -> &'a Vec<T> {
324+
pub fn is_empty_in(&self, space: ParamSpace) -> bool {
325+
self.get_vec(space).len() == 0
326+
}
327+
328+
pub fn get_slice<'a>(&'a self, space: ParamSpace) -> &'a [T] {
329+
self.get_vec(space).as_slice()
330+
}
331+
332+
fn get_vec<'a>(&'a self, space: ParamSpace) -> &'a Vec<T> {
307333
self.vecs.get(space as uint).unwrap()
308334
}
309335

310-
pub fn get_mut_vec<'a>(&'a mut self, space: ParamSpace) -> &'a mut Vec<T> {
336+
fn get_mut_vec<'a>(&'a mut self, space: ParamSpace) -> &'a mut Vec<T> {
311337
self.vecs.get_mut(space as uint).unwrap()
312338
}
313339

@@ -336,8 +362,8 @@ impl<T> VecPerParamSpace<T> {
336362
r.iter().chain(s.iter().chain(f.iter()))
337363
}
338364

339-
pub fn all_vecs(&self, pred: |&Vec<T>| -> bool) -> bool {
340-
self.vecs.iter().all(pred)
365+
pub fn all_vecs(&self, pred: |&[T]| -> bool) -> bool {
366+
self.vecs.iter().map(|v|v.as_slice()).all(pred)
341367
}
342368

343369
pub fn all(&self, pred: |&T| -> bool) -> bool {

src/librustc/middle/trans/callee.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,8 @@ fn resolve_default_method_vtables(bcx: &Block,
216216
bcx.tcx(), &param_substs, &impl_res);
217217

218218
// Now we pull any vtables for parameters on the actual method.
219-
param_vtables
220-
.get_mut_vec(subst::FnSpace)
221-
.push_all(
222-
impl_vtables.get_vec(subst::FnSpace).as_slice());
219+
param_vtables.push_all(subst::FnSpace,
220+
impl_vtables.get_slice(subst::FnSpace));
223221

224222
param_vtables
225223
}

src/librustc/middle/trans/debuginfo.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ impl TypeMap {
554554

555555
// Maybe check that there is no self type here.
556556

557-
let tps = substs.types.get_vec(subst::TypeSpace);
557+
let tps = substs.types.get_slice(subst::TypeSpace);
558558
if tps.len() > 0 {
559559
output.push_char('<');
560560

@@ -1377,9 +1377,9 @@ pub fn create_function_debug_context(cx: &CrateContext,
13771377
}
13781378

13791379
// Handle other generic parameters
1380-
let actual_types = param_substs.substs.types.get_vec(subst::FnSpace);
1380+
let actual_types = param_substs.substs.types.get_slice(subst::FnSpace);
13811381
for (index, &ast::TyParam{ ident: ident, .. }) in generics.ty_params.iter().enumerate() {
1382-
let actual_type = *actual_types.get(index);
1382+
let actual_type = actual_types[index];
13831383
// Add actual type name to <...> clause of function name
13841384
let actual_type_name = compute_debuginfo_type_name(cx,
13851385
actual_type,

src/librustc/middle/trans/type_of.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub fn type_of(cx: &CrateContext, t: ty::t) -> Type {
209209
// avoids creating more than one copy of the enum when one
210210
// of the enum's variants refers to the enum itself.
211211
let repr = adt::represent_type(cx, t);
212-
let tps = substs.types.get_vec(subst::TypeSpace);
212+
let tps = substs.types.get_slice(subst::TypeSpace);
213213
let name = llvm_type_name(cx, an_enum, did, tps);
214214
adt::incomplete_type_of(cx, &*repr, name.as_slice())
215215
}
@@ -266,7 +266,7 @@ pub fn type_of(cx: &CrateContext, t: ty::t) -> Type {
266266
// in *after* placing it into the type cache. This prevents
267267
// infinite recursion with recursive struct types.
268268
let repr = adt::represent_type(cx, t);
269-
let tps = substs.types.get_vec(subst::TypeSpace);
269+
let tps = substs.types.get_slice(subst::TypeSpace);
270270
let name = llvm_type_name(cx, a_struct, did, tps);
271271
adt::incomplete_type_of(cx, &*repr, name.as_slice())
272272
}
@@ -305,7 +305,7 @@ pub enum named_ty { a_struct, an_enum }
305305
pub fn llvm_type_name(cx: &CrateContext,
306306
what: named_ty,
307307
did: ast::DefId,
308-
tps: &Vec<ty::t>)
308+
tps: &[ty::t])
309309
-> String
310310
{
311311
let name = match what {

src/librustc/middle/ty.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ impl Generics {
981981
}
982982

983983
pub fn has_type_params(&self, space: subst::ParamSpace) -> bool {
984-
!self.types.get_vec(space).is_empty()
984+
!self.types.is_empty_in(space)
985985
}
986986
}
987987

@@ -4644,14 +4644,14 @@ pub fn construct_parameter_environment(
46444644
let mut types = VecPerParamSpace::empty();
46454645
for &space in subst::ParamSpace::all().iter() {
46464646
push_types_from_defs(tcx, &mut types, space,
4647-
generics.types.get_vec(space));
4647+
generics.types.get_slice(space));
46484648
}
46494649

46504650
// map bound 'a => free 'a
46514651
let mut regions = VecPerParamSpace::empty();
46524652
for &space in subst::ParamSpace::all().iter() {
46534653
push_region_params(&mut regions, space, free_id,
4654-
generics.regions.get_vec(space));
4654+
generics.regions.get_slice(space));
46554655
}
46564656

46574657
let free_substs = Substs {
@@ -4666,7 +4666,7 @@ pub fn construct_parameter_environment(
46664666
let mut bounds = VecPerParamSpace::empty();
46674667
for &space in subst::ParamSpace::all().iter() {
46684668
push_bounds_from_defs(tcx, &mut bounds, space, &free_substs,
4669-
generics.types.get_vec(space));
4669+
generics.types.get_slice(space));
46704670
}
46714671

46724672
debug!("construct_parameter_environment: free_id={} \
@@ -4684,7 +4684,7 @@ pub fn construct_parameter_environment(
46844684
fn push_region_params(regions: &mut VecPerParamSpace<ty::Region>,
46854685
space: subst::ParamSpace,
46864686
free_id: ast::NodeId,
4687-
region_params: &Vec<RegionParameterDef>)
4687+
region_params: &[RegionParameterDef])
46884688
{
46894689
for r in region_params.iter() {
46904690
regions.push(space, ty::free_region_from_def(free_id, r));
@@ -4694,7 +4694,7 @@ pub fn construct_parameter_environment(
46944694
fn push_types_from_defs(tcx: &ty::ctxt,
46954695
types: &mut subst::VecPerParamSpace<ty::t>,
46964696
space: subst::ParamSpace,
4697-
defs: &Vec<TypeParameterDef>) {
4697+
defs: &[TypeParameterDef]) {
46984698
for (i, def) in defs.iter().enumerate() {
46994699
let ty = ty::mk_param(tcx, space, i, def.def_id);
47004700
types.push(space, ty);
@@ -4705,7 +4705,7 @@ pub fn construct_parameter_environment(
47054705
bounds: &mut subst::VecPerParamSpace<ParamBounds>,
47064706
space: subst::ParamSpace,
47074707
free_substs: &subst::Substs,
4708-
defs: &Vec<TypeParameterDef>) {
4708+
defs: &[TypeParameterDef]) {
47094709
for def in defs.iter() {
47104710
let b = (*def.bounds).subst(tcx, free_substs);
47114711
bounds.push(space, b);

src/librustc/middle/typeck/astconv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn ast_path_substs<AC:AstConv,RS:RegionScope>(
203203
};
204204

205205
// Convert the type parameters supplied by the user.
206-
let ty_param_defs = decl_generics.types.get_vec(TypeSpace);
206+
let ty_param_defs = decl_generics.types.get_slice(TypeSpace);
207207
let supplied_ty_param_count = path.segments.iter().flat_map(|s| s.types.iter()).count();
208208
let formal_ty_param_count = ty_param_defs.len();
209209
let required_ty_param_count = ty_param_defs.iter()

src/librustc/middle/typeck/check/method.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ fn construct_transformed_self_ty_for_object(
264264
// The subst we get in has Err as the "Self" type. For an object
265265
// type, we don't put any type into the Self paramspace, so let's
266266
// make a copy of rcvr_substs that has the Self paramspace empty.
267-
obj_substs.types.get_mut_vec(subst::SelfSpace).pop().unwrap();
267+
obj_substs.types.pop(subst::SelfSpace).unwrap();
268268

269269
match method_ty.explicit_self {
270270
ast::SelfStatic => {
@@ -1133,7 +1133,7 @@ impl<'a> LookupContext<'a> {
11331133
let m_regions =
11341134
self.fcx.infcx().region_vars_for_defs(
11351135
self.span,
1136-
candidate.method_ty.generics.regions.get_vec(subst::FnSpace));
1136+
candidate.method_ty.generics.regions.get_slice(subst::FnSpace));
11371137

11381138
let all_substs = candidate.rcvr_substs.clone().with_method(m_types, m_regions);
11391139

0 commit comments

Comments
 (0)