summaryrefslogtreecommitdiffstats
path: root/tests/ui/nll/match-on-borrowed.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
commit64d98f8ee037282c35007b64c2649055c56af1db (patch)
tree5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/nll/match-on-borrowed.rs
parentAdding debian version 1.67.1+dfsg1-1. (diff)
downloadrustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz
rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/nll/match-on-borrowed.rs')
-rw-r--r--tests/ui/nll/match-on-borrowed.rs96
1 files changed, 96 insertions, 0 deletions
diff --git a/tests/ui/nll/match-on-borrowed.rs b/tests/ui/nll/match-on-borrowed.rs
new file mode 100644
index 000000000..447dabeb4
--- /dev/null
+++ b/tests/ui/nll/match-on-borrowed.rs
@@ -0,0 +1,96 @@
+// Test that a (partially) mutably borrowed place can be matched on, so long as
+// we don't have to read any values that are mutably borrowed to determine
+// which arm to take.
+//
+// Test that we don't allow mutating the value being matched on in a way that
+// changes which patterns it matches, until we have chosen an arm.
+
+struct A(i32, i32);
+
+fn struct_example(mut a: A) {
+ let x = &mut a.0;
+ match a { // OK, no access of borrowed data
+ _ if false => (),
+ A(_, r) => (),
+ }
+ x;
+}
+
+fn indirect_struct_example(mut b: &mut A) {
+ let x = &mut b.0;
+ match *b { // OK, no access of borrowed data
+ _ if false => (),
+ A(_, r) => (),
+ }
+ x;
+}
+
+fn underscore_example(mut c: i32) {
+ let r = &mut c;
+ match c { // OK, no access of borrowed data (or any data at all)
+ _ if false => (),
+ _ => (),
+ }
+ r;
+}
+
+enum E {
+ V(i32, i32),
+ W,
+}
+
+fn enum_example(mut e: E) {
+ let x = match e {
+ E::V(ref mut x, _) => x,
+ E::W => panic!(),
+ };
+ match e { // Don't know that E uses a tag for its discriminant
+ //~^ ERROR
+ _ if false => (),
+ E::V(_, r) => (),
+ E::W => (),
+ }
+ x;
+}
+
+fn indirect_enum_example(mut f: &mut E) {
+ let x = match *f {
+ E::V(ref mut x, _) => x,
+ E::W => panic!(),
+ };
+ match f { // Don't know that E uses a tag for its discriminant
+ //~^ ERROR
+ _ if false => (),
+ E::V(_, r) => (),
+ E::W => (),
+ }
+ x;
+}
+
+fn match_on_muatbly_borrowed_ref(mut p: &bool) {
+ let r = &mut p;
+ match *p { // OK, no access at all
+ _ if false => (),
+ _ => (),
+ }
+ r;
+}
+
+fn match_on_borrowed(mut t: bool) {
+ let x = &mut t;
+ match t {
+ //~^ ERROR
+ true => (),
+ false => (),
+ }
+ x;
+}
+
+enum Never {}
+
+fn never_init() {
+ let n: Never;
+ match n {} //~ ERROR
+}
+
+fn main() {}