Skip to content

Refactor ty_vec - represent &[T] as &([T]) #13636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/librustc/metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ pub fn parse_substs_data(data: &[u8], crate_num: ast::CrateNum, pos: uint, tcx:
parse_substs(&mut st, conv)
}

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

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

match next(st) {
'~' => ty::VstoreUniq,
'&' => ty::VstoreSlice(parse_region(st, conv), parse_mut(st)),
'&' => ty::VstoreSlice(parse_region(st, conv)),
c => st.tcx.sess.bug(format!("parse_vstore(): bad input '{}'", c))
}
}

fn parse_size(st: &mut PState) -> Option<uint> {
assert_eq!(next(st), '/');

if peek(st) == '|' {
assert_eq!(next(st), '|');
None
} else {
let n = parse_uint(st);
assert_eq!(next(st), '|');
Some(n)
}
}

fn parse_trait_store(st: &mut PState, conv: conv_did) -> ty::TraitStore {
match next(st) {
'~' => ty::UniqTraitStore,
Expand Down Expand Up @@ -342,12 +354,12 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
return ty::mk_rptr(st.tcx, r, mt);
}
'V' => {
let ty = parse_ty(st, |x,y| conv(x,y));
let v = parse_vstore(st, |x,y| conv(x,y), parse_mutability);
return ty::mk_vec(st.tcx, ty, v);
let mt = parse_mt(st, |x,y| conv(x,y));
let sz = parse_size(st);
return ty::mk_vec(st.tcx, mt, sz);
}
'v' => {
let v = parse_vstore(st, |x,y| conv(x,y), |_| ());
let v = parse_vstore(st, |x,y| conv(x,y));
return ty::mk_str(st.tcx, v);
}
'T' => {
Expand Down Expand Up @@ -396,7 +408,7 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
assert_eq!(next(st), ']');
return ty::mk_struct(st.tcx, did, substs);
}
c => { error!("unexpected char in type string: {}", c); fail!();}
c => { fail!("unexpected char in type string: {}", c);}
}
}

Expand Down
22 changes: 13 additions & 9 deletions src/librustc/metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,17 @@ fn enc_bound_region(w: &mut MemWriter, cx: &ctxt, br: ty::BoundRegion) {
}
}

pub fn enc_vstore<M>(w: &mut MemWriter, cx: &ctxt,
v: ty::Vstore<M>,
enc_mut: |&mut MemWriter, M|) {
pub fn enc_vstore(w: &mut MemWriter, cx: &ctxt,
v: ty::Vstore,
enc_mut: |&mut MemWriter|) {
mywrite!(w, "/");
match v {
ty::VstoreFixed(u) => mywrite!(w, "{}|", u),
ty::VstoreUniq => mywrite!(w, "~"),
ty::VstoreSlice(r, m) => {
ty::VstoreSlice(r) => {
mywrite!(w, "&");
enc_region(w, cx, r);
enc_mut(w, m);
enc_mut(w);
}
}
}
Expand Down Expand Up @@ -292,14 +292,18 @@ fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
enc_region(w, cx, r);
enc_mt(w, cx, mt);
}
ty::ty_vec(ty, v) => {
ty::ty_vec(mt, sz) => {
mywrite!(w, "V");
enc_ty(w, cx, ty);
enc_vstore(w, cx, v, enc_mutability);
enc_mt(w, cx, mt);
mywrite!(w, "/");
match sz {
Some(n) => mywrite!(w, "{}|", n),
None => mywrite!(w, "|"),
}
}
ty::ty_str(v) => {
mywrite!(w, "v");
enc_vstore(w, cx, v, |_, ()| {});
enc_vstore(w, cx, v, |_| {});
}
ty::ty_closure(ref f) => {
mywrite!(w, "f");
Expand Down
161 changes: 92 additions & 69 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, pats: Vec<@Pat> ) {
}
}
}
ty::ty_vec(..) => {
ty::ty_vec(..) | ty::ty_rptr(..) => {
match *ctor {
vec(n) => Some(format!("vectors of length {}", n)),
_ => None
Expand Down Expand Up @@ -258,50 +258,57 @@ fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[@Pat]) -> useful {
None => {
match ty::get(left_ty).sty {
ty::ty_bool => {
match is_useful_specialized(cx, m, v,
val(const_bool(true)),
0u, left_ty){
not_useful => {
is_useful_specialized(cx, m, v,
val(const_bool(false)),
0u, left_ty)
match is_useful_specialized(cx, m, v,
val(const_bool(true)),
0u, left_ty){
not_useful => {
is_useful_specialized(cx, m, v,
val(const_bool(false)),
0u, left_ty)
}
ref u => (*u).clone(),
}
ref u => (*u).clone(),
}
}
ty::ty_enum(eid, _) => {
for va in (*ty::enum_variants(cx.tcx, eid)).iter() {
match is_useful_specialized(cx, m, v, variant(va.id),
va.args.len(), left_ty) {
not_useful => (),
ref u => return (*u).clone(),
}
}
not_useful
for va in (*ty::enum_variants(cx.tcx, eid)).iter() {
match is_useful_specialized(cx, m, v, variant(va.id),
va.args.len(), left_ty) {
not_useful => (),
ref u => return (*u).clone(),
}
}
not_useful
}
ty::ty_vec(_, ty::VstoreFixed(n)) => {
is_useful_specialized(cx, m, v, vec(n), n, left_ty)
ty::ty_vec(_, Some(n)) => {
is_useful_specialized(cx, m, v, vec(n), n, left_ty)
}
ty::ty_vec(..) => {
let max_len = m.iter().rev().fold(0, |max_len, r| {
match r.get(0).node {
PatVec(ref before, _, ref after) => {
cmp::max(before.len() + after.len(), max_len)
}
_ => max_len
ty::ty_vec(..) => fail!("impossible case"),
ty::ty_rptr(_, ty::mt{ty: ty, ..}) | ty::ty_uniq(ty) => match ty::get(ty).sty {
ty::ty_vec(_, None) => {
let max_len = m.iter().rev().fold(0, |max_len, r| {
match r.get(0).node {
PatVec(ref before, _, ref after) => {
cmp::max(before.len() + after.len(), max_len)
}
_ => max_len
}
});
for n in iter::range(0u, max_len + 1) {
match is_useful_specialized(cx, m, v, vec(n), n, left_ty) {
not_useful => (),
ref u => return (*u).clone(),
}
}
not_useful
}
});
for n in iter::range(0u, max_len + 1) {
match is_useful_specialized(cx, m, v, vec(n), n, left_ty) {
not_useful => (),
ref u => return (*u).clone(),
_ => {
let arity = ctor_arity(cx, &single, left_ty);
is_useful_specialized(cx, m, v, single, arity, left_ty)
}
}
not_useful
}
},
_ => {
let arity = ctor_arity(cx, &single, left_ty);
is_useful_specialized(cx, m, v, single, arity, left_ty)
let arity = ctor_arity(cx, &single, left_ty);
is_useful_specialized(cx, m, v, single, arity, left_ty)
}
}
}
Expand Down Expand Up @@ -394,17 +401,16 @@ fn is_wild(cx: &MatchCheckCtxt, p: @Pat) -> bool {
}

fn missing_ctor(cx: &MatchCheckCtxt,
m: &matrix,
left_ty: ty::t)
-> Option<ctor> {
match ty::get(left_ty).sty {
ty::ty_box(_) | ty::ty_uniq(_) | ty::ty_rptr(..) | ty::ty_tup(_) |
ty::ty_struct(..) => {
for r in m.iter() {
if !is_wild(cx, *r.get(0)) { return None; }
}
return Some(single);
}
m: &matrix,
left_ty: ty::t)
-> Option<ctor> {
return match ty::get(left_ty).sty {
ty::ty_box(_) | ty::ty_tup(_) |
ty::ty_struct(..) => check_matrix_for_wild(cx, m),
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty: ty, ..}) => match ty::get(ty).sty {
ty::ty_vec(_, None) => ctor_for_slice(m),
_ => check_matrix_for_wild(cx, m),
},
ty::ty_enum(eid, _) => {
let mut found = Vec::new();
for r in m.iter() {
Expand Down Expand Up @@ -441,7 +447,7 @@ fn missing_ctor(cx: &MatchCheckCtxt,
else if true_found { Some(val(const_bool(false))) }
else { Some(val(const_bool(true))) }
}
ty::ty_vec(_, ty::VstoreFixed(n)) => {
ty::ty_vec(_, Some(n)) => {
let mut missing = true;
let mut wrong = false;
for r in m.iter() {
Expand All @@ -464,8 +470,19 @@ fn missing_ctor(cx: &MatchCheckCtxt,
_ => None
}
}
ty::ty_vec(..) => {
ty::ty_vec(..) => fail!("impossible case"),
_ => Some(single)
};

fn check_matrix_for_wild(cx: &MatchCheckCtxt, m: &matrix) -> Option<ctor> {
for r in m.iter() {
if !is_wild(cx, *r.get(0)) { return None; }
}
return Some(single);
}

// For slice and ~[T].
fn ctor_for_slice(m: &matrix) -> Option<ctor> {
// Find the lengths and slices of all vector patterns.
let mut vec_pat_lens = m.iter().filter_map(|r| {
match r.get(0).node {
Expand Down Expand Up @@ -511,31 +528,37 @@ fn missing_ctor(cx: &MatchCheckCtxt,
Some(k) => Some(vec(k)),
None => None
}
}
_ => Some(single)
}
}

fn ctor_arity(cx: &MatchCheckCtxt, ctor: &ctor, ty: ty::t) -> uint {
match ty::get(ty).sty {
ty::ty_tup(ref fs) => fs.len(),
ty::ty_box(_) | ty::ty_uniq(_) | ty::ty_rptr(..) => 1u,
ty::ty_enum(eid, _) => {
let id = match *ctor { variant(id) => id,
_ => fail!("impossible case") };
match ty::enum_variants(cx.tcx, eid).iter().find(|v| v.id == id ) {
Some(v) => v.args.len(),
None => fail!("impossible case")
}
}
ty::ty_struct(cid, _) => ty::lookup_struct_fields(cx.tcx, cid).len(),
ty::ty_vec(..) => {
fn vec_ctor_arity(ctor: &ctor) -> uint {
match *ctor {
vec(n) => n,
_ => 0u
vec(n) => n,
_ => 0u
}
}
_ => 0u
}

match ty::get(ty).sty {
ty::ty_tup(ref fs) => fs.len(),
ty::ty_box(_) => 1u,
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty: ty, ..}) => match ty::get(ty).sty {
ty::ty_vec(_, None) => vec_ctor_arity(ctor),
_ => 1u,
},
ty::ty_enum(eid, _) => {
let id = match *ctor {
variant(id) => id,
_ => fail!("impossible case")
};
match ty::enum_variants(cx.tcx, eid).iter().find(|v| v.id == id ) {
Some(v) => v.args.len(),
None => fail!("impossible case")
}
}
ty::ty_struct(cid, _) => ty::lookup_struct_fields(cx.tcx, cid).len(),
ty::ty_vec(_, Some(_)) => vec_ctor_arity(ctor),
_ => 0u
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,6 @@ fn check_heap_type(cx: &Context, span: Span, ty: ty::t) {
n_box += 1;
}
ty::ty_uniq(_) | ty::ty_str(ty::VstoreUniq) |
ty::ty_vec(_, ty::VstoreUniq) |
ty::ty_trait(~ty::TyTrait { store: ty::UniqTraitStore, .. }) |
ty::ty_closure(~ty::ClosureTy { store: ty::UniqTraitStore, .. }) => {
n_uniq += 1;
Expand Down Expand Up @@ -1156,10 +1155,13 @@ fn check_unused_result(cx: &Context, s: &ast::Stmt) {
fn check_deprecated_owned_vector(cx: &Context, e: &ast::Expr) {
let t = ty::expr_ty(cx.tcx, e);
match ty::get(t).sty {
ty::ty_vec(_, ty::VstoreUniq) => {
cx.span_lint(DeprecatedOwnedVector, e.span,
"use of deprecated `~[]` vector; replaced by `std::vec::Vec`")
}
ty::ty_uniq(t) => match ty::get(t).sty {
ty::ty_vec(_, None) => {
cx.span_lint(DeprecatedOwnedVector, e.span,
"use of deprecated `~[]` vector; replaced by `std::vec::Vec`")
}
_ => {}
},
_ => {}
}
}
Expand Down
Loading