]> git.lizzy.rs Git - rust.git/commitdiff
Fix panic in subslice patterns of arrays (fixes #276)
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 29 Aug 2017 07:55:40 +0000 (09:55 +0200)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 29 Aug 2017 08:03:00 +0000 (10:03 +0200)
src/librustc_mir/interpret/lvalue.rs
tests/run-pass/subslice_array.rs [new file with mode: 0644]

index 2bb0b88a356f6ca5a7a12ef68218f468cdeb5697..c4ec2aa795e89cd61a96ca5750cdccde612baaf5 100644 (file)
@@ -476,7 +476,12 @@ pub(super) fn eval_lvalue_projection(
                 );
                 assert!(u64::from(from) <= n - u64::from(to));
                 let ptr = base_ptr.offset(u64::from(from) * elem_size, &self)?;
-                let extra = LvalueExtra::Length(n - u64::from(to) - u64::from(from));
+                // sublicing arrays produces arrays
+                let extra = if self.type_is_sized(base_ty) {
+                    LvalueExtra::None
+                } else {
+                    LvalueExtra::Length(n - u64::from(to) - u64::from(from))
+                };
                 (ptr, extra)
             }
         };
diff --git a/tests/run-pass/subslice_array.rs b/tests/run-pass/subslice_array.rs
new file mode 100644 (file)
index 0000000..468cc9f
--- /dev/null
@@ -0,0 +1,14 @@
+#![feature(advanced_slice_patterns)]
+#![feature(slice_patterns)]
+
+fn bar(a: &'static str, b: &'static str) -> [&'static str; 4] {
+    [a, b, b, a]
+}
+
+fn main() {
+    let out = bar("baz", "foo");
+    let [a, xs.., d] = out;
+    assert_eq!(a, "baz");
+    assert_eq!(xs, ["foo", "foo"]);
+    assert_eq!(d, "baz");
+}