Skip to content

Commit 02081e7

Browse files
committed
auto merge of #13636 : nick29581/rust/ty_vec, r=pcwalton
Refactors all uses of ty_vec and associated things to remove the vstore abstraction (still used for strings, for now). Pointers to vectors are stored as ty_rptr or ty_uniq wrapped around a ty_vec. There are no user-facing changes. Existing behaviour is preserved by special-casing many instances of pointers containing vectors. Hopefully with DST most of these hacks will go away. For now it is useful to leave them hanging around rather than abstracting them into a method or something. Closes #13554.
2 parents 50671dc + 37306c1 commit 02081e7

28 files changed

+755
-506
lines changed

src/librustc/metadata/tydecode.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ pub fn parse_substs_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx:
137137
parse_substs(&mut st, conv)
138138
}
139139

140-
fn parse_vstore<M>(st: &mut PState, conv: conv_did,
141-
parse_mut: |&mut PState| -> M) -> ty::Vstore<M> {
140+
fn parse_vstore(st: &mut PState, conv: conv_did) -> ty::Vstore {
142141
assert_eq!(next(st), '/');
143142

144143
let c = peek(st);
@@ -150,11 +149,24 @@ fn parse_vstore<M>(st: &mut PState, conv: conv_did,
150149

151150
match next(st) {
152151
'~' => ty::VstoreUniq,
153-
'&' => ty::VstoreSlice(parse_region(st, conv), parse_mut(st)),
152+
'&' => ty::VstoreSlice(parse_region(st, conv)),
154153
c => st.tcx.sess.bug(format!("parse_vstore(): bad input '{}'", c))
155154
}
156155
}
157156

157+
fn parse_size(st: &mut PState) -> Option<uint> {
158+
assert_eq!(next(st), '/');
159+
160+
if peek(st) == '|' {
161+
assert_eq!(next(st), '|');
162+
None
163+
} else {
164+
let n = parse_uint(st);
165+
assert_eq!(next(st), '|');
166+
Some(n)
167+
}
168+
}
169+
158170
fn parse_trait_store(st: &mut PState, conv: conv_did) -> ty::TraitStore {
159171
match next(st) {
160172
'~' => ty::UniqTraitStore,
@@ -342,12 +354,12 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
342354
return ty::mk_rptr(st.tcx, r, mt);
343355
}
344356
'V' => {
345-
let ty = parse_ty(st, |x,y| conv(x,y));
346-
let v = parse_vstore(st, |x,y| conv(x,y), parse_mutability);
347-
return ty::mk_vec(st.tcx, ty, v);
357+
let mt = parse_mt(st, |x,y| conv(x,y));
358+
let sz = parse_size(st);
359+
return ty::mk_vec(st.tcx, mt, sz);
348360
}
349361
'v' => {
350-
let v = parse_vstore(st, |x,y| conv(x,y), |_| ());
362+
let v = parse_vstore(st, |x,y| conv(x,y));
351363
return ty::mk_str(st.tcx, v);
352364
}
353365
'T' => {
@@ -396,7 +408,7 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
396408
assert_eq!(next(st), ']');
397409
return ty::mk_struct(st.tcx, did, substs);
398410
}
399-
c => { error!("unexpected char in type string: {}", c); fail!();}
411+
c => { fail!("unexpected char in type string: {}", c);}
400412
}
401413
}
402414

src/librustc/metadata/tyencode.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,17 @@ fn enc_bound_region(w: &mut MemWriter, cx: &ctxt, br: ty::BoundRegion) {
204204
}
205205
}
206206

207-
pub fn enc_vstore<M>(w: &mut MemWriter, cx: &ctxt,
208-
v: ty::Vstore<M>,
209-
enc_mut: |&mut MemWriter, M|) {
207+
pub fn enc_vstore(w: &mut MemWriter, cx: &ctxt,
208+
v: ty::Vstore,
209+
enc_mut: |&mut MemWriter|) {
210210
mywrite!(w, "/");
211211
match v {
212212
ty::VstoreFixed(u) => mywrite!(w, "{}|", u),
213213
ty::VstoreUniq => mywrite!(w, "~"),
214-
ty::VstoreSlice(r, m) => {
214+
ty::VstoreSlice(r) => {
215215
mywrite!(w, "&");
216216
enc_region(w, cx, r);
217-
enc_mut(w, m);
217+
enc_mut(w);
218218
}
219219
}
220220
}
@@ -292,14 +292,18 @@ fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
292292
enc_region(w, cx, r);
293293
enc_mt(w, cx, mt);
294294
}
295-
ty::ty_vec(ty, v) => {
295+
ty::ty_vec(mt, sz) => {
296296
mywrite!(w, "V");
297-
enc_ty(w, cx, ty);
298-
enc_vstore(w, cx, v, enc_mutability);
297+
enc_mt(w, cx, mt);
298+
mywrite!(w, "/");
299+
match sz {
300+
Some(n) => mywrite!(w, "{}|", n),
301+
None => mywrite!(w, "|"),
302+
}
299303
}
300304
ty::ty_str(v) => {
301305
mywrite!(w, "v");
302-
enc_vstore(w, cx, v, |_, ()| {});
306+
enc_vstore(w, cx, v, |_| {});
303307
}
304308
ty::ty_closure(ref f) => {
305309
mywrite!(w, "f");

src/librustc/middle/check_match.rs

+92-69
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, pats: Vec<@Pat> ) {
191191
}
192192
}
193193
}
194-
ty::ty_vec(..) => {
194+
ty::ty_vec(..) | ty::ty_rptr(..) => {
195195
match *ctor {
196196
vec(n) => Some(format!("vectors of length {}", n)),
197197
_ => None
@@ -258,50 +258,57 @@ fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[@Pat]) -> useful {
258258
None => {
259259
match ty::get(left_ty).sty {
260260
ty::ty_bool => {
261-
match is_useful_specialized(cx, m, v,
262-
val(const_bool(true)),
263-
0u, left_ty){
264-
not_useful => {
265-
is_useful_specialized(cx, m, v,
266-
val(const_bool(false)),
267-
0u, left_ty)
261+
match is_useful_specialized(cx, m, v,
262+
val(const_bool(true)),
263+
0u, left_ty){
264+
not_useful => {
265+
is_useful_specialized(cx, m, v,
266+
val(const_bool(false)),
267+
0u, left_ty)
268+
}
269+
ref u => (*u).clone(),
268270
}
269-
ref u => (*u).clone(),
270-
}
271271
}
272272
ty::ty_enum(eid, _) => {
273-
for va in (*ty::enum_variants(cx.tcx, eid)).iter() {
274-
match is_useful_specialized(cx, m, v, variant(va.id),
275-
va.args.len(), left_ty) {
276-
not_useful => (),
277-
ref u => return (*u).clone(),
278-
}
279-
}
280-
not_useful
273+
for va in (*ty::enum_variants(cx.tcx, eid)).iter() {
274+
match is_useful_specialized(cx, m, v, variant(va.id),
275+
va.args.len(), left_ty) {
276+
not_useful => (),
277+
ref u => return (*u).clone(),
278+
}
279+
}
280+
not_useful
281281
}
282-
ty::ty_vec(_, ty::VstoreFixed(n)) => {
283-
is_useful_specialized(cx, m, v, vec(n), n, left_ty)
282+
ty::ty_vec(_, Some(n)) => {
283+
is_useful_specialized(cx, m, v, vec(n), n, left_ty)
284284
}
285-
ty::ty_vec(..) => {
286-
let max_len = m.iter().rev().fold(0, |max_len, r| {
287-
match r.get(0).node {
288-
PatVec(ref before, _, ref after) => {
289-
cmp::max(before.len() + after.len(), max_len)
290-
}
291-
_ => max_len
285+
ty::ty_vec(..) => fail!("impossible case"),
286+
ty::ty_rptr(_, ty::mt{ty: ty, ..}) | ty::ty_uniq(ty) => match ty::get(ty).sty {
287+
ty::ty_vec(_, None) => {
288+
let max_len = m.iter().rev().fold(0, |max_len, r| {
289+
match r.get(0).node {
290+
PatVec(ref before, _, ref after) => {
291+
cmp::max(before.len() + after.len(), max_len)
292+
}
293+
_ => max_len
294+
}
295+
});
296+
for n in iter::range(0u, max_len + 1) {
297+
match is_useful_specialized(cx, m, v, vec(n), n, left_ty) {
298+
not_useful => (),
299+
ref u => return (*u).clone(),
300+
}
301+
}
302+
not_useful
292303
}
293-
});
294-
for n in iter::range(0u, max_len + 1) {
295-
match is_useful_specialized(cx, m, v, vec(n), n, left_ty) {
296-
not_useful => (),
297-
ref u => return (*u).clone(),
304+
_ => {
305+
let arity = ctor_arity(cx, &single, left_ty);
306+
is_useful_specialized(cx, m, v, single, arity, left_ty)
298307
}
299-
}
300-
not_useful
301-
}
308+
},
302309
_ => {
303-
let arity = ctor_arity(cx, &single, left_ty);
304-
is_useful_specialized(cx, m, v, single, arity, left_ty)
310+
let arity = ctor_arity(cx, &single, left_ty);
311+
is_useful_specialized(cx, m, v, single, arity, left_ty)
305312
}
306313
}
307314
}
@@ -394,17 +401,16 @@ fn is_wild(cx: &MatchCheckCtxt, p: @Pat) -> bool {
394401
}
395402

396403
fn missing_ctor(cx: &MatchCheckCtxt,
397-
m: &matrix,
398-
left_ty: ty::t)
399-
-> Option<ctor> {
400-
match ty::get(left_ty).sty {
401-
ty::ty_box(_) | ty::ty_uniq(_) | ty::ty_rptr(..) | ty::ty_tup(_) |
402-
ty::ty_struct(..) => {
403-
for r in m.iter() {
404-
if !is_wild(cx, *r.get(0)) { return None; }
405-
}
406-
return Some(single);
407-
}
404+
m: &matrix,
405+
left_ty: ty::t)
406+
-> Option<ctor> {
407+
return match ty::get(left_ty).sty {
408+
ty::ty_box(_) | ty::ty_tup(_) |
409+
ty::ty_struct(..) => check_matrix_for_wild(cx, m),
410+
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty: ty, ..}) => match ty::get(ty).sty {
411+
ty::ty_vec(_, None) => ctor_for_slice(m),
412+
_ => check_matrix_for_wild(cx, m),
413+
},
408414
ty::ty_enum(eid, _) => {
409415
let mut found = Vec::new();
410416
for r in m.iter() {
@@ -441,7 +447,7 @@ fn missing_ctor(cx: &MatchCheckCtxt,
441447
else if true_found { Some(val(const_bool(false))) }
442448
else { Some(val(const_bool(true))) }
443449
}
444-
ty::ty_vec(_, ty::VstoreFixed(n)) => {
450+
ty::ty_vec(_, Some(n)) => {
445451
let mut missing = true;
446452
let mut wrong = false;
447453
for r in m.iter() {
@@ -464,8 +470,19 @@ fn missing_ctor(cx: &MatchCheckCtxt,
464470
_ => None
465471
}
466472
}
467-
ty::ty_vec(..) => {
473+
ty::ty_vec(..) => fail!("impossible case"),
474+
_ => Some(single)
475+
};
476+
477+
fn check_matrix_for_wild(cx: &MatchCheckCtxt, m: &matrix) -> Option<ctor> {
478+
for r in m.iter() {
479+
if !is_wild(cx, *r.get(0)) { return None; }
480+
}
481+
return Some(single);
482+
}
468483

484+
// For slice and ~[T].
485+
fn ctor_for_slice(m: &matrix) -> Option<ctor> {
469486
// Find the lengths and slices of all vector patterns.
470487
let mut vec_pat_lens = m.iter().filter_map(|r| {
471488
match r.get(0).node {
@@ -511,31 +528,37 @@ fn missing_ctor(cx: &MatchCheckCtxt,
511528
Some(k) => Some(vec(k)),
512529
None => None
513530
}
514-
}
515-
_ => Some(single)
516531
}
517532
}
518533

519534
fn ctor_arity(cx: &MatchCheckCtxt, ctor: &ctor, ty: ty::t) -> uint {
520-
match ty::get(ty).sty {
521-
ty::ty_tup(ref fs) => fs.len(),
522-
ty::ty_box(_) | ty::ty_uniq(_) | ty::ty_rptr(..) => 1u,
523-
ty::ty_enum(eid, _) => {
524-
let id = match *ctor { variant(id) => id,
525-
_ => fail!("impossible case") };
526-
match ty::enum_variants(cx.tcx, eid).iter().find(|v| v.id == id ) {
527-
Some(v) => v.args.len(),
528-
None => fail!("impossible case")
529-
}
530-
}
531-
ty::ty_struct(cid, _) => ty::lookup_struct_fields(cx.tcx, cid).len(),
532-
ty::ty_vec(..) => {
535+
fn vec_ctor_arity(ctor: &ctor) -> uint {
533536
match *ctor {
534-
vec(n) => n,
535-
_ => 0u
537+
vec(n) => n,
538+
_ => 0u
536539
}
537-
}
538-
_ => 0u
540+
}
541+
542+
match ty::get(ty).sty {
543+
ty::ty_tup(ref fs) => fs.len(),
544+
ty::ty_box(_) => 1u,
545+
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty: ty, ..}) => match ty::get(ty).sty {
546+
ty::ty_vec(_, None) => vec_ctor_arity(ctor),
547+
_ => 1u,
548+
},
549+
ty::ty_enum(eid, _) => {
550+
let id = match *ctor {
551+
variant(id) => id,
552+
_ => fail!("impossible case")
553+
};
554+
match ty::enum_variants(cx.tcx, eid).iter().find(|v| v.id == id ) {
555+
Some(v) => v.args.len(),
556+
None => fail!("impossible case")
557+
}
558+
}
559+
ty::ty_struct(cid, _) => ty::lookup_struct_fields(cx.tcx, cid).len(),
560+
ty::ty_vec(_, Some(_)) => vec_ctor_arity(ctor),
561+
_ => 0u
539562
}
540563
}
541564

src/librustc/middle/lint.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,6 @@ fn check_heap_type(cx: &Context, span: Span, ty: ty::t) {
918918
n_box += 1;
919919
}
920920
ty::ty_uniq(_) | ty::ty_str(ty::VstoreUniq) |
921-
ty::ty_vec(_, ty::VstoreUniq) |
922921
ty::ty_trait(~ty::TyTrait { store: ty::UniqTraitStore, .. }) |
923922
ty::ty_closure(~ty::ClosureTy { store: ty::UniqTraitStore, .. }) => {
924923
n_uniq += 1;
@@ -1156,10 +1155,13 @@ fn check_unused_result(cx: &Context, s: &ast::Stmt) {
11561155
fn check_deprecated_owned_vector(cx: &Context, e: &ast::Expr) {
11571156
let t = ty::expr_ty(cx.tcx, e);
11581157
match ty::get(t).sty {
1159-
ty::ty_vec(_, ty::VstoreUniq) => {
1160-
cx.span_lint(DeprecatedOwnedVector, e.span,
1161-
"use of deprecated `~[]` vector; replaced by `std::vec::Vec`")
1162-
}
1158+
ty::ty_uniq(t) => match ty::get(t).sty {
1159+
ty::ty_vec(_, None) => {
1160+
cx.span_lint(DeprecatedOwnedVector, e.span,
1161+
"use of deprecated `~[]` vector; replaced by `std::vec::Vec`")
1162+
}
1163+
_ => {}
1164+
},
11631165
_ => {}
11641166
}
11651167
}

0 commit comments

Comments
 (0)