]> git.lizzy.rs Git - rust.git/commitdiff
Add support for fixed size vectors in let/arg patterns
authorJakub Wieczorek <jakub@jakub.cc>
Sun, 1 Jun 2014 13:46:14 +0000 (15:46 +0200)
committerJakub Wieczorek <jakub@jakub.cc>
Fri, 20 Jun 2014 15:08:57 +0000 (17:08 +0200)
Fixes #7784

src/librustc/middle/trans/_match.rs
src/test/run-pass/issue-7784.rs [new file with mode: 0644]

index ffd29ffeb8fb42797b013d5430aea9bc402d6226..5b9c89f6250931f4f8a8713263bebcaeb776e1e4 100644 (file)
@@ -988,8 +988,7 @@ fn extract_vec_elems<'a>(
                      pat_id: ast::NodeId,
                      elem_count: uint,
                      slice: Option<uint>,
-                     val: ValueRef,
-                     count: ValueRef)
+                     val: ValueRef)
                      -> ExtractedBlock<'a> {
     let _icx = push_ctxt("match::extract_vec_elems");
     let vec_datum = match_datum(bcx, val, pat_id);
@@ -1003,7 +1002,7 @@ fn extract_vec_elems<'a>(
             Some(n) if i < n => GEPi(bcx, base, [i]),
             Some(n) if i > n => {
                 InBoundsGEP(bcx, base, [
-                    Sub(bcx, count,
+                    Sub(bcx, len,
                         C_int(bcx.ccx(), (elem_count - i) as int))])
             }
             _ => unsafe { llvm::LLVMGetUndef(vt.llunit_ty.to_ref()) }
@@ -1765,7 +1764,7 @@ fn compile_submatch_continue<'a, 'b>(
                     vec_len_eq => (n, None)
                 };
                 let args = extract_vec_elems(opt_cx, pat_id, n,
-                                             slice, val, test_val);
+                                             slice, val);
                 size = args.vals.len();
                 unpacked = args.vals.clone();
                 opt_cx = args.bcx;
@@ -2264,9 +2263,21 @@ fn bind_irrefutable_pat<'a>(
             let loaded_val = Load(bcx, val);
             bcx = bind_irrefutable_pat(bcx, inner, loaded_val, binding_mode, cleanup_scope);
         }
-        ast::PatVec(..) => {
-            bcx.sess().span_bug(pat.span,
-                                "vector patterns are never irrefutable!");
+        ast::PatVec(ref before, ref slice, ref after) => {
+            let extracted = extract_vec_elems(
+                bcx, pat.id, before.len() + 1u + after.len(),
+                slice.map(|_| before.len()), val
+            );
+            bcx = before
+                .iter().map(|v| Some(*v))
+                .chain(Some(*slice).move_iter())
+                .chain(after.iter().map(|v| Some(*v)))
+                .zip(extracted.vals.iter())
+                .fold(bcx, |bcx, (inner, elem)| {
+                    inner.map_or(bcx, |inner| {
+                        bind_irrefutable_pat(bcx, inner, *elem, binding_mode, cleanup_scope)
+                    })
+                });
         }
         ast::PatMac(..) => {
             bcx.sess().span_bug(pat.span, "unexpanded macro");
diff --git a/src/test/run-pass/issue-7784.rs b/src/test/run-pass/issue-7784.rs
new file mode 100644 (file)
index 0000000..06d2214
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn foo<T: Add<T, T> + Clone>([x, y, z]: [T, ..3]) -> (T, T, T) {
+    (x.clone(), x.clone() + y.clone(), x + y + z)
+}
+fn bar(a: &'static str, b: &'static str) -> [&'static str, ..4] {
+    [a, b, b, a]
+}
+
+fn main() {
+    assert_eq!(foo([1, 2, 3]), (1, 3, 6));
+
+    let [a, b, c, d] = bar("foo", "bar");
+    assert_eq!(a, "foo");
+    assert_eq!(b, "bar");
+    assert_eq!(c, "bar");
+    assert_eq!(d, "foo");
+
+    let [a, _, _, d] = bar("baz", "foo");
+    assert_eq!(a, "baz");
+    assert_eq!(d, "baz");
+}
\ No newline at end of file