summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorAriel Ben-Yehuda <ariel.byd@gmail.com>2018-01-16 23:05:13 +0200
committerMalo Jaffré <jaffre.malo@gmail.com>2018-01-23 16:57:40 +0100
commit7973dc9d128bc61b4151ff4ff50ae8b531bc7db7 (patch)
tree118cf6460852197f7123d52f9aedf42ed78b3803 /src/test
parentdc957614090897b4899b7585e5e6a8e0c709871c (diff)
downloadrust-7973dc9d128bc61b4151ff4ff50ae8b531bc7db7.tar.gz
rust-7973dc9d128bc61b4151ff4ff50ae8b531bc7db7.zip
avoid double-unsizing arrays in bytestring match lowering
The match lowering code, when lowering matches against bytestrings,
works by coercing both the scrutinee and the pattern to `&[u8]` and
then comparing them using `<[u8] as Eq>::eq`.

If the scrutinee is already of type `&[u8]`, then unsizing it is both
unneccessary and a trait error caught by the new and updated MIR typeck,
so this PR changes lowering to avoid doing that (match lowering tried to
avoid that before, but that attempt was quite broken).

Fixes #46920.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/run-pass/issue-46920-byte-array-patterns.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/test/run-pass/issue-46920-byte-array-patterns.rs b/src/test/run-pass/issue-46920-byte-array-patterns.rs
new file mode 100644
index 00000000000..236f6995c51
--- /dev/null
+++ b/src/test/run-pass/issue-46920-byte-array-patterns.rs
@@ -0,0 +1,37 @@
+// Copyright 2017 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.
+
+const CURSOR_PARTITION_LABEL: &'static [u8] = b"partition";
+const CURSOR_EVENT_TYPE_LABEL: &'static [u8] = b"event_type";
+const BYTE_PATTERN: &'static [u8; 5] = b"hello";
+
+fn match_slice(x: &[u8]) -> u32 {
+    match x {
+        CURSOR_PARTITION_LABEL => 0,
+        CURSOR_EVENT_TYPE_LABEL => 1,
+        _ => 2,
+    }
+}
+
+fn match_array(x: &[u8; 5]) -> bool {
+    match x {
+        BYTE_PATTERN => true,
+        _ => false
+    }
+}
+
+fn main() {
+    assert_eq!(match_slice(b"abcde"), 2);
+    assert_eq!(match_slice(b"event_type"), 1);
+    assert_eq!(match_slice(b"partition"), 0);
+
+    assert_eq!(match_array(b"hello"), true);
+    assert_eq!(match_array(b"hella"), false);
+}