Skip to content

Issue 4635 #4748

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

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
2 changes: 1 addition & 1 deletion doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ match crayons[0] {

A vector can be destructured using pattern matching:

~~~~
~~~~ {.xfail-test}
let numbers: [int * 3] = [1, 2, 3];
let score = match numbers {
[] => 0,
Expand Down
26 changes: 13 additions & 13 deletions src/librustc/middle/borrowck/gather_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,17 +592,17 @@ impl GatherLoanCtxt {
}
}

ast::pat_vec(_, Some(tail_pat)) => {
// The `tail_pat` here creates a slice into the
ast::pat_vec(ref pats, Some(rest)) => {
// The `rest_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 rest_pat = pats[rest];
let rest_ty = self.tcx().ty(rest_pat);
let (rest_mutbl, rest_r) =
self.vec_slice_info(rest_pat, rest_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(rest_pat, cmt);
self.guarantee_valid(cmt_index, rest_mutbl, rest_r);
}

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

fn vec_slice_info(@mut self,
pat: @ast::pat,
tail_ty: ty::t) -> (ast::mutability, ty::Region) {
rest_ty: ty::t) -> (ast::mutability, ty::Region) {
/*!
*
* In a pattern like [a, b, ..c], normally `c` has slice type,
Expand All @@ -621,9 +621,9 @@ 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(rest_ty).sty {
ty::ty_evec(rest_mt, ty::vstore_slice(rest_r)) => {
(rest_mt.mutbl, rest_r)
}

ty::ty_rptr(_, ref mt) => {
Expand All @@ -633,7 +633,7 @@ impl GatherLoanCtxt {
_ => {
self.tcx().sess.span_bug(
pat.span,
fmt!("Type of tail pattern is not a slice"));
fmt!("Type of rest pattern is not a slice"));
}
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ pub fn pat_ctor_id(cx: @MatchCheckCtxt, p: @pat) -> Option<ctor> {
pat_region(*) => {
Some(single)
}
pat_vec(elems, tail) => {
match tail {
pat_vec(elems, rest) => {
match rest {
Some(_) => None,
None => Some(vec(elems.len()))
}
Expand Down Expand Up @@ -386,47 +386,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 rests 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 elems, ref rest) => {
Some((elems.len(), rest.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 rest come first.
let mut sorted_vec_lens = sort::merge_sort(vec_pat_lens,
|&(len1, tail1), &(len2, tail2)| {
|&(len1, rest1), &(len2, rest2)| {
if len1 == len2 {
tail1 > tail2
rest1 > rest2
} else {
len1 <= len2
}
}
);
vec::dedup(&mut sorted_vec_lens);

let mut found_tail = false;
let mut found_rest = false;
let mut next = 0;
let mut missing = None;
for sorted_vec_lens.each |&(length, tail)| {
for sorted_vec_lens.each |&(length, rest)| {
if length != next {
missing = Some(next);
break;
}
if tail {
found_tail = true;
if rest {
found_rest = 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 rest - therefore, we report vec(next) as missing.
if !found_rest {
missing = Some(next);
}
match missing {
Expand Down Expand Up @@ -613,11 +613,11 @@ pub fn specialize(cx: @MatchCheckCtxt,
compare_const_vals(c_hi, v_hi) <= 0;
if match_ { Some(vec::tail(r)) } else { None }
}
pat_vec(elems, tail) => {
pat_vec(elems, rest) => {
match ctor_id {
vec(_) => {
let num_elements = elems.len();
if num_elements < arity && tail.is_some() {
if num_elements < arity && rest.is_some() {
Some(vec::append(
vec::append(elems, vec::from_elem(
arity - num_elements, wild()
Expand Down
21 changes: 11 additions & 10 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,16 +882,17 @@ pub impl &mem_categorization_ctxt {
self.cat_pattern(subcmt, subpat, op);
}

ast::pat_vec(ref pats, opt_tail_pat) => {
for pats.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);
ast::pat_vec(ref pats, rest) => {
for pats.eachi |i, pat| {
if Some(i) == rest {
let rest_pat = pat;
let rest_ty = self.tcx.ty(*rest_pat);
let rest_cmt = self.cat_rvalue(*rest_pat, rest_ty);
self.cat_pattern(rest_cmt, *rest_pat, op);
} else {
let elt_cmt = self.cat_index(*pat, cmt);
self.cat_pattern(elt_cmt, *pat, op);
}
}
}

Expand Down
83 changes: 52 additions & 31 deletions src/librustc/middle/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub enum Opt {
var(/* disr val */int, /* variant dids */{enm: def_id, var: def_id}),
range(@ast::expr, @ast::expr),
vec_len_eq(uint),
vec_len_ge(uint)
vec_len_ge(uint, /* rest */uint)
}

pub fn opt_eq(tcx: ty::ctxt, a: &Opt, b: &Opt) -> bool {
Expand Down Expand Up @@ -237,7 +237,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 @@ -275,7 +275,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 @@ -564,17 +564,19 @@ pub fn enter_opt(bcx: block, m: &[@Match/&r], opt: &Opt, col: uint,
None
}
}
ast::pat_vec(elems, tail) => {
match tail {
Some(_) => {
if opt_eq(tcx, &vec_len_ge(elems.len()), opt) {
Some(vec::append_one(elems, tail.get()))
ast::pat_vec(elems, rest) => {
match rest {
Some(i) => {
let n = elems.len() - 1u;
if opt_eq(tcx, &vec_len_ge(n, i), opt) {
Some(copy elems)
} else {
None
}
}
None => {
if opt_eq(tcx, &vec_len_eq(elems.len()), opt) {
let n = elems.len();
if opt_eq(tcx, &vec_len_eq(n), opt) {
Some(copy elems)
} else {
None
Expand Down Expand Up @@ -805,10 +807,10 @@ pub fn get_options(ccx: @crate_ctxt, m: &[@Match], col: uint) -> ~[Opt] {
ast::pat_range(l1, l2) => {
add_to_set(ccx.tcx, &found, range(l1, l2));
}
ast::pat_vec(elems, tail) => {
let opt = match tail {
ast::pat_vec(elems, rest) => {
let opt = match rest {
None => vec_len_eq(elems.len()),
Some(_) => vec_len_ge(elems.len())
Some(i) => vec_len_ge(elems.len() - 1u, i)
};
add_to_set(ccx.tcx, &found, opt);
}
Expand Down Expand Up @@ -853,35 +855,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)
rest: Option<uint>,
val: ValueRef,
count: ValueRef)
-> {vals: ~[ValueRef], bcx: block} {
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 rest {
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 rest.is_some() {
let n = rest.get();
let rest_offset = Mul(bcx, vt.llunit_size,
C_int(bcx.ccx(), n 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 rest_begin = tvec::pointer_add(bcx, base, rest_offset);
let rest_len_offset = Mul(bcx, vt.llunit_size,
C_int(bcx.ccx(), (elem_count - 1u) as int)
);
let rest_len = Sub(bcx, len, rest_len_offset);
let rest_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, rest_ty, false);
Store(bcx, rest_begin,
GEPi(bcx, scratch.val, [0u, abi::slice_elt_base])
);
Store(bcx, tail_len,
Store(bcx, rest_len,
GEPi(bcx, scratch.val, [0u, abi::slice_elt_len])
);
elems.push(scratch.val);
elems[n] = scratch.val;
scratch.add_clean(bcx);
}
return {vals: elems, bcx: bcx};
Expand Down Expand Up @@ -1380,7 +1396,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 @@ -1523,12 +1539,17 @@ pub fn compile_submatch(bcx: block,
unpacked = /*bad*/copy args.vals;
opt_cx = args.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 rest = 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, rest,
val, test_val);
size = args.vals.len();
unpacked = /*bad*/copy args.vals;
opt_cx = args.bcx;
Expand Down
18 changes: 7 additions & 11 deletions src/librustc/middle/typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ pub fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
}
}
}
ast::pat_vec(elts, tail) => {
ast::pat_vec(elts, rest) => {
let default_region_var =
fcx.infcx().next_region_var_with_lb(
pat.span, pcx.block_region
Expand Down Expand Up @@ -565,21 +565,17 @@ pub fn check_pat(pcx: pat_ctxt, pat: @ast::pat, expected: ty::t) {
);
}
};
for elts.each |elt| {
check_pat(pcx, *elt, elt_type.ty);
}
fcx.write_ty(pat.id, expected);

match tail {
Some(tail_pat) => {
let slice_ty = ty::mk_evec(tcx,
for elts.eachi |i, elt| {
let mut t = elt_type.ty;
if Some(i) == rest {
t = ty::mk_evec(tcx,
ty::mt {ty: elt_type.ty, mutbl: elt_type.mutbl},
ty::vstore_slice(region_var)
);
check_pat(pcx, tail_pat, slice_ty);
}
None => ()
check_pat(pcx, *elt, t);
}
fcx.write_ty(pat.id, expected);
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/middle/typeck/check/regionck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ pub mod guarantor {
}
ast::pat_lit(*) => {}
ast::pat_range(*) => {}
ast::pat_vec(ref ps, ref opt_tail_pat) => {
ast::pat_vec(ref ps, _) => {
let vec_ty = rcx.resolve_node_type(pat.id);
if !ty::type_contains_err(vec_ty) {
let vstore = ty::ty_vstore(vec_ty);
Expand All @@ -893,10 +893,6 @@ pub mod guarantor {
};

link_ref_bindings_in_pats(rcx, ps, guarantor1);

for opt_tail_pat.each |p| {
link_ref_bindings_in_pat(rcx, *p, guarantor);
}
}
}
}
Expand Down
Loading