about summary refs log tree commit diff
diff options
context:
space:
mode:
authordianne <diannes.gm@gmail.com>2025-04-22 13:07:15 -0700
committerdianne <diannes.gm@gmail.com>2025-04-22 13:07:29 -0700
commit08baec4ebfe4e3679f322324c1740e0690289760 (patch)
tree3236b76a97b232b3f91dcae3ab70381db6790730
parent191df20fcad9331d3a948aa8e8556775ec3fe69d (diff)
downloadrust-08baec4ebfe4e3679f322324c1740e0690289760.tar.gz
rust-08baec4ebfe4e3679f322324c1740e0690289760.zip
add a test for byte string literal pattern mutability mismatches
-rw-r--r--tests/ui/pattern/byte-string-mutability-mismatch.rs19
-rw-r--r--tests/ui/pattern/byte-string-mutability-mismatch.stderr25
2 files changed, 44 insertions, 0 deletions
diff --git a/tests/ui/pattern/byte-string-mutability-mismatch.rs b/tests/ui/pattern/byte-string-mutability-mismatch.rs
new file mode 100644
index 00000000000..4ffdb5f9b99
--- /dev/null
+++ b/tests/ui/pattern/byte-string-mutability-mismatch.rs
@@ -0,0 +1,19 @@
+//! Byte string literal patterns use the mutability of the literal, rather than the mutability of
+//! the pattern's scrutinee. Since byte string literals are always shared references, it's a
+//! mismatch to use a byte string literal pattern to match on a mutable array or slice reference.
+
+fn main() {
+    let mut val = [97u8, 10u8];
+    match &mut val {
+        b"a\n" => {},
+        //~^ ERROR mismatched types
+        //~| types differ in mutability
+        _ => {},
+    }
+    match &mut val[..] {
+         b"a\n" => {},
+        //~^ ERROR mismatched types
+        //~| types differ in mutability
+         _ => {},
+    }
+}
diff --git a/tests/ui/pattern/byte-string-mutability-mismatch.stderr b/tests/ui/pattern/byte-string-mutability-mismatch.stderr
new file mode 100644
index 00000000000..ee796278e69
--- /dev/null
+++ b/tests/ui/pattern/byte-string-mutability-mismatch.stderr
@@ -0,0 +1,25 @@
+error[E0308]: mismatched types
+  --> $DIR/byte-string-mutability-mismatch.rs:8:9
+   |
+LL |     match &mut val {
+   |           -------- this expression has type `&mut [u8; 2]`
+LL |         b"a\n" => {},
+   |         ^^^^^^ types differ in mutability
+   |
+   = note: expected mutable reference `&mut _`
+                      found reference `&'static _`
+
+error[E0308]: mismatched types
+  --> $DIR/byte-string-mutability-mismatch.rs:14:10
+   |
+LL |     match &mut val[..] {
+   |           ------------ this expression has type `&mut [u8]`
+LL |          b"a\n" => {},
+   |          ^^^^^^ types differ in mutability
+   |
+   = note: expected mutable reference `&mut _`
+                      found reference `&'static _`
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0308`.