Skip to content
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

Vector destructuring from tail, take 2 #5122

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 12 additions & 12 deletions src/librustc/middle/borrowck/gather_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,17 +617,17 @@ pub impl GatherLoanCtxt {
}
}

ast::pat_vec(_, Some(tail_pat)) => {
// The `tail_pat` here creates a slice into the
ast::pat_vec(_, Some(slice_pat), _) => {
// The `slice_pat` here creates a slice into the
// original vector. This is effectively a borrow of
// the elements of the vector being matched.

let tail_ty = self.tcx().ty(tail_pat);
let (tail_mutbl, tail_r) =
self.vec_slice_info(tail_pat, tail_ty);
let slice_ty = self.tcx().ty(slice_pat);
let (slice_mutbl, slice_r) =
self.vec_slice_info(slice_pat, slice_ty);
let mcx = self.bccx.mc_ctxt();
let cmt_index = mcx.cat_index(tail_pat, cmt);
self.guarantee_valid(cmt_index, tail_mutbl, tail_r);
let cmt_index = mcx.cat_index(slice_pat, cmt);
self.guarantee_valid(cmt_index, slice_mutbl, slice_r);
}

_ => {}
Expand All @@ -637,7 +637,7 @@ pub impl GatherLoanCtxt {

fn vec_slice_info(@mut self,
pat: @ast::pat,
tail_ty: ty::t) -> (ast::mutability, ty::Region) {
slice_ty: ty::t) -> (ast::mutability, ty::Region) {
/*!
*
* In a pattern like [a, b, ..c], normally `c` has slice type,
Expand All @@ -646,9 +646,9 @@ pub impl GatherLoanCtxt {
* to recurse through rptrs.
*/

match ty::get(tail_ty).sty {
ty::ty_evec(tail_mt, ty::vstore_slice(tail_r)) => {
(tail_mt.mutbl, tail_r)
match ty::get(slice_ty).sty {
ty::ty_evec(slice_mt, ty::vstore_slice(slice_r)) => {
(slice_mt.mutbl, slice_r)
}

ty::ty_rptr(_, ref mt) => {
Expand All @@ -658,7 +658,7 @@ pub impl GatherLoanCtxt {
_ => {
self.tcx().sess.span_bug(
pat.span,
fmt!("Type of tail pattern is not a slice"));
fmt!("Type of slice pattern is not a slice"));
}
}
}
Expand Down
56 changes: 32 additions & 24 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ pub fn is_useful(cx: @MatchCheckCtxt, m: &matrix, v: &[@pat]) -> useful {
ty::ty_unboxed_vec(*) | ty::ty_evec(*) => {
let max_len = do m.foldr(0) |r, max_len| {
match /*bad*/copy r[0].node {
pat_vec(elems, _) => uint::max(elems.len(), max_len),
pat_vec(before, _, after) => {
uint::max(before.len() + after.len(), max_len)
}
_ => max_len
}
};
Expand Down Expand Up @@ -322,10 +324,10 @@ pub fn pat_ctor_id(cx: @MatchCheckCtxt, p: @pat) -> Option<ctor> {
pat_box(_) | pat_uniq(_) | pat_tup(_) | pat_region(*) => {
Some(single)
}
pat_vec(elems, tail) => {
match tail {
pat_vec(before, slice, after) => {
match slice {
Some(_) => None,
None => Some(vec(elems.len()))
None => Some(vec(before.len() + after.len()))
}
}
}
Expand Down Expand Up @@ -393,47 +395,47 @@ pub fn missing_ctor(cx: @MatchCheckCtxt,
}
ty::ty_unboxed_vec(*) | ty::ty_evec(*) => {

// Find the lengths and tails of all vector patterns.
// Find the lengths and slices of all vector patterns.
let vec_pat_lens = do m.filter_mapped |r| {
match r[0].node {
pat_vec(ref elems, ref tail) => {
Some((elems.len(), tail.is_some()))
pat_vec(ref before, ref slice, ref after) => {
Some((before.len() + after.len(), slice.is_some()))
}
_ => None
}
};

// Sort them by length such that for patterns of the same length,
// those with a destructured tail come first.
// those with a destructured slice come first.
let mut sorted_vec_lens = sort::merge_sort(vec_pat_lens,
|&(len1, tail1), &(len2, tail2)| {
|&(len1, slice1), &(len2, slice2)| {
if len1 == len2 {
tail1 > tail2
slice1 > slice2
} else {
len1 <= len2
}
}
);
vec::dedup(&mut sorted_vec_lens);

let mut found_tail = false;
let mut found_slice = false;
let mut next = 0;
let mut missing = None;
for sorted_vec_lens.each |&(length, tail)| {
for sorted_vec_lens.each |&(length, slice)| {
if length != next {
missing = Some(next);
break;
}
if tail {
found_tail = true;
if slice {
found_slice = true;
break;
}
next += 1;
}

// We found patterns of all lengths within <0, next), yet there was no
// pattern with a tail - therefore, we report vec(next) as missing.
if !found_tail {
// pattern with a slice - therefore, we report vec(next) as missing.
if !found_slice {
missing = Some(next);
}
match missing {
Expand Down Expand Up @@ -621,19 +623,25 @@ pub fn specialize(cx: @MatchCheckCtxt,
compare_const_vals(c_hi, v_hi) <= 0;
if match_ { Some(vec::from_slice(r.tail())) } else { None }
}
pat_vec(elems, tail) => {
pat_vec(before, slice, after) => {
match ctor_id {
vec(_) => {
let num_elements = elems.len();
if num_elements < arity && tail.is_some() {
let num_elements = before.len() + after.len();
if num_elements < arity && slice.is_some() {
Some(vec::append(
vec::append(elems, vec::from_elem(
arity - num_elements, wild()
)),
vec::from_slice(r.tail())
vec::concat(&[
before,
vec::from_elem(
arity - num_elements, wild()),
after
]),
r.tail()
))
} else if num_elements == arity {
Some(vec::append(elems, r.tail()))
Some(vec::append(
vec::append(before, after),
r.tail()
))
} else {
None
}
Expand Down
17 changes: 10 additions & 7 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,16 +963,19 @@ pub impl mem_categorization_ctxt {
self.cat_pattern(subcmt, subpat, op);
}

ast::pat_vec(ref pats, opt_tail_pat) => {
for pats.each |pat| {
ast::pat_vec(ref before, slice, ref after) => {
for before.each |pat| {
let elt_cmt = self.cat_index(*pat, cmt);
self.cat_pattern(elt_cmt, *pat, op);
}

for opt_tail_pat.each |tail_pat| {
let tail_ty = self.tcx.ty(*tail_pat);
let tail_cmt = self.cat_rvalue(*tail_pat, tail_ty);
self.cat_pattern(tail_cmt, *tail_pat, op);
for slice.each |slice_pat| {
let slice_ty = self.tcx.ty(*slice_pat);
let slice_cmt = self.cat_rvalue(*slice_pat, slice_ty);
self.cat_pattern(slice_cmt, *slice_pat, op);
}
for after.each |pat| {
let elt_cmt = self.cat_index(*pat, cmt);
self.cat_pattern(elt_cmt, *pat, op);
}
}

Expand Down
88 changes: 56 additions & 32 deletions src/librustc/middle/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub enum Opt {
var(/* disr val */int, @adt::Repr),
range(@ast::expr, @ast::expr),
vec_len_eq(uint),
vec_len_ge(uint)
vec_len_ge(uint, /* slice */uint)
}

pub fn opt_eq(tcx: ty::ctxt, a: &Opt, b: &Opt) -> bool {
Expand Down Expand Up @@ -235,7 +235,7 @@ pub fn opt_eq(tcx: ty::ctxt, a: &Opt, b: &Opt) -> bool {
}
(&var(a, _), &var(b, _)) => a == b,
(&vec_len_eq(a), &vec_len_eq(b)) => a == b,
(&vec_len_ge(a), &vec_len_ge(b)) => a == b,
(&vec_len_ge(a, _), &vec_len_ge(b, _)) => a == b,
_ => false
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ pub fn trans_opt(bcx: block, o: &Opt) -> opt_result {
vec_len_eq(n) => {
return single_result(rslt(bcx, C_int(ccx, n as int)));
}
vec_len_ge(n) => {
vec_len_ge(n, _) => {
return lower_bound(rslt(bcx, C_int(ccx, n as int)));
}
}
Expand Down Expand Up @@ -565,18 +565,22 @@ pub fn enter_opt(bcx: block, m: &[@Match/&r], opt: &Opt, col: uint,
None
}
}
ast::pat_vec(elems, tail) => {
match tail {
ast::pat_vec(before, slice, after) => {
match slice {
Some(_) => {
if opt_eq(tcx, &vec_len_ge(elems.len()), opt) {
Some(vec::append_one(elems, tail.get()))
let n = before.len() + after.len();
let i = before.len();
if opt_eq(tcx, &vec_len_ge(n, i), opt) {
Some(vec::concat(
&[before, ~[slice.get()], after]))
} else {
None
}
}
None => {
if opt_eq(tcx, &vec_len_eq(elems.len()), opt) {
Some(copy elems)
let n = before.len();
if opt_eq(tcx, &vec_len_eq(n), opt) {
Some(copy before)
} else {
None
}
Expand Down Expand Up @@ -807,10 +811,11 @@ pub fn get_options(bcx: block, m: &[@Match], col: uint) -> ~[Opt] {
ast::pat_range(l1, l2) => {
add_to_set(ccx.tcx, &mut found, range(l1, l2));
}
ast::pat_vec(elems, tail) => {
let opt = match tail {
None => vec_len_eq(elems.len()),
Some(_) => vec_len_ge(elems.len())
ast::pat_vec(before, slice, after) => {
let opt = match slice {
None => vec_len_eq(before.len()),
Some(_) => vec_len_ge(before.len() + after.len(),
before.len())
};
add_to_set(ccx.tcx, &mut found, opt);
}
Expand Down Expand Up @@ -841,35 +846,49 @@ pub fn extract_variant_args(bcx: block,
pub fn extract_vec_elems(bcx: block,
pat_id: ast::node_id,
elem_count: uint,
tail: bool,
val: ValueRef)
slice: Option<uint>,
val: ValueRef,
count: ValueRef)
-> ExtractedBlock {
let _icx = bcx.insn_ctxt("match::extract_vec_elems");
let vt = tvec::vec_types(bcx, node_id_type(bcx, pat_id));
let unboxed = load_if_immediate(bcx, val, vt.vec_ty);
let (base, len) = tvec::get_base_and_len(bcx, unboxed, vt.vec_ty);

let mut elems = do vec::from_fn(elem_count) |i| {
GEPi(bcx, base, ~[i])
match slice {
None => GEPi(bcx, base, ~[i]),
Some(n) if i < n => GEPi(bcx, base, ~[i]),
Some(n) if i > n => {
InBoundsGEP(bcx, base, ~[
Sub(bcx, count,
C_int(bcx.ccx(), (elem_count - i) as int))])
}
_ => unsafe { llvm::LLVMGetUndef(vt.llunit_ty) }
}
};
if tail {
let tail_offset = Mul(bcx, vt.llunit_size,
C_int(bcx.ccx(), elem_count as int)
if slice.is_some() {
let n = slice.get();
let slice_offset = Mul(bcx, vt.llunit_size,
C_int(bcx.ccx(), n as int)
);
let slice_begin = tvec::pointer_add(bcx, base, slice_offset);
let slice_len_offset = Mul(bcx, vt.llunit_size,
C_int(bcx.ccx(), (elem_count - 1u) as int)
);
let tail_begin = tvec::pointer_add(bcx, base, tail_offset);
let tail_len = Sub(bcx, len, tail_offset);
let tail_ty = ty::mk_evec(bcx.tcx(),
let slice_len = Sub(bcx, len, slice_len_offset);
let slice_ty = ty::mk_evec(bcx.tcx(),
ty::mt {ty: vt.unit_ty, mutbl: ast::m_imm},
ty::vstore_slice(ty::re_static)
);
let scratch = scratch_datum(bcx, tail_ty, false);
Store(bcx, tail_begin,
let scratch = scratch_datum(bcx, slice_ty, false);
Store(bcx, slice_begin,
GEPi(bcx, scratch.val, [0u, abi::slice_elt_base])
);
Store(bcx, tail_len,
Store(bcx, slice_len,
GEPi(bcx, scratch.val, [0u, abi::slice_elt_len])
);
elems.push(scratch.val);
elems[n] = scratch.val;
scratch.add_clean(bcx);
}

Expand Down Expand Up @@ -1367,7 +1386,7 @@ pub fn compile_submatch(bcx: block,
test_val = Load(bcx, val);
kind = compare;
},
vec_len_eq(_) | vec_len_ge(_) => {
vec_len_eq(*) | vec_len_ge(*) => {
let vt = tvec::vec_types(bcx, node_id_type(bcx, pat_id));
let unboxed = load_if_immediate(bcx, val, vt.vec_ty);
let (_, len) = tvec::get_base_and_len(
Expand Down Expand Up @@ -1511,12 +1530,17 @@ pub fn compile_submatch(bcx: block,
unpacked = argvals;
opt_cx = new_bcx;
}
vec_len_eq(n) | vec_len_ge(n) => {
let tail = match *opt {
vec_len_ge(_) => true,
_ => false
vec_len_eq(n) | vec_len_ge(n, _) => {
let n = match *opt {
vec_len_ge(*) => n + 1u,
_ => n
};
let slice = match *opt {
vec_len_ge(_, i) => Some(i),
_ => None
};
let args = extract_vec_elems(opt_cx, pat_id, n, tail, val);
let args = extract_vec_elems(opt_cx, pat_id, n, slice,
val, test_val);
size = args.vals.len();
unpacked = /*bad*/copy args.vals;
opt_cx = args.bcx;
Expand Down
Loading