Skip to content

Commit 1e68d57

Browse files
author
Jakub Wieczorek
committed
Add support for fixed size vectors in let/arg patterns
Fixes #7784
1 parent 34407dc commit 1e68d57

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/librustc/middle/trans/_match.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,7 @@ fn extract_vec_elems<'a>(
988988
pat_id: ast::NodeId,
989989
elem_count: uint,
990990
slice: Option<uint>,
991-
val: ValueRef,
992-
count: ValueRef)
991+
val: ValueRef)
993992
-> ExtractedBlock<'a> {
994993
let _icx = push_ctxt("match::extract_vec_elems");
995994
let vec_datum = match_datum(bcx, val, pat_id);
@@ -1003,7 +1002,7 @@ fn extract_vec_elems<'a>(
10031002
Some(n) if i < n => GEPi(bcx, base, [i]),
10041003
Some(n) if i > n => {
10051004
InBoundsGEP(bcx, base, [
1006-
Sub(bcx, count,
1005+
Sub(bcx, len,
10071006
C_int(bcx.ccx(), (elem_count - i) as int))])
10081007
}
10091008
_ => unsafe { llvm::LLVMGetUndef(vt.llunit_ty.to_ref()) }
@@ -1765,7 +1764,7 @@ fn compile_submatch_continue<'a, 'b>(
17651764
vec_len_eq => (n, None)
17661765
};
17671766
let args = extract_vec_elems(opt_cx, pat_id, n,
1768-
slice, val, test_val);
1767+
slice, val);
17691768
size = args.vals.len();
17701769
unpacked = args.vals.clone();
17711770
opt_cx = args.bcx;
@@ -2264,9 +2263,21 @@ fn bind_irrefutable_pat<'a>(
22642263
let loaded_val = Load(bcx, val);
22652264
bcx = bind_irrefutable_pat(bcx, inner, loaded_val, binding_mode, cleanup_scope);
22662265
}
2267-
ast::PatVec(..) => {
2268-
bcx.sess().span_bug(pat.span,
2269-
"vector patterns are never irrefutable!");
2266+
ast::PatVec(ref before, ref slice, ref after) => {
2267+
let extracted = extract_vec_elems(
2268+
bcx, pat.id, before.len() + 1u + after.len(),
2269+
slice.map(|_| before.len()), val
2270+
);
2271+
bcx = before
2272+
.iter().map(|v| Some(*v))
2273+
.chain(Some(*slice).move_iter())
2274+
.chain(after.iter().map(|v| Some(*v)))
2275+
.zip(extracted.vals.iter())
2276+
.fold(bcx, |bcx, (inner, elem)| {
2277+
inner.map_or(bcx, |inner| {
2278+
bind_irrefutable_pat(bcx, inner, *elem, binding_mode, cleanup_scope)
2279+
})
2280+
});
22702281
}
22712282
ast::PatMac(..) => {
22722283
bcx.sess().span_bug(pat.span, "unexpanded macro");

src/test/run-pass/issue-7784.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo<T: Add<T, T> + Clone>([x, y, z]: [T, ..3]) -> (T, T, T) {
12+
(x.clone(), x.clone() + y.clone(), x + y + z)
13+
}
14+
fn bar(a: &'static str, b: &'static str) -> [&'static str, ..4] {
15+
[a, b, b, a]
16+
}
17+
18+
fn main() {
19+
assert_eq!(foo([1, 2, 3]), (1, 3, 6));
20+
21+
let [a, b, c, d] = bar("foo", "bar");
22+
assert_eq!(a, "foo");
23+
assert_eq!(b, "bar");
24+
assert_eq!(c, "bar");
25+
assert_eq!(d, "foo");
26+
27+
let [a, _, _, d] = bar("baz", "foo");
28+
assert_eq!(a, "baz");
29+
assert_eq!(d, "baz");
30+
}

0 commit comments

Comments
 (0)