about summary refs log tree commit diff
diff options
context:
space:
mode:
authordianne <diannes.gm@gmail.com>2025-03-16 18:21:00 -0700
committerdianne <diannes.gm@gmail.com>2025-04-24 14:25:27 -0700
commit64c8d5d7f53d7ca053335eeb3ef5756c1bbdb5f2 (patch)
treec6b90096c2eb84775c95ba7fd556e949bb162b20
parent3c877f6a477380ed61155d3bf816df09c9e05b9e (diff)
downloadrust-64c8d5d7f53d7ca053335eeb3ef5756c1bbdb5f2.tar.gz
rust-64c8d5d7f53d7ca053335eeb3ef5756c1bbdb5f2.zip
move existing tests away from using boxes
Since deref patterns on boxes will be lowered differently, I'll be
making a separate test file for them. This makes sure we're still
testing the generic `Deref(Mut)::deref(_mut)`-based lowering.
-rw-r--r--tests/ui/pattern/deref-patterns/bindings.rs22
-rw-r--r--tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.rs20
-rw-r--r--tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.stderr41
-rw-r--r--tests/ui/pattern/deref-patterns/closure_capture.rs10
-rw-r--r--tests/ui/pattern/deref-patterns/fake_borrows.rs17
-rw-r--r--tests/ui/pattern/deref-patterns/fake_borrows.stderr31
-rw-r--r--tests/ui/pattern/deref-patterns/implicit-cow-deref.rs5
7 files changed, 96 insertions, 50 deletions
diff --git a/tests/ui/pattern/deref-patterns/bindings.rs b/tests/ui/pattern/deref-patterns/bindings.rs
index c14d57f3f24..ac48e3ffefc 100644
--- a/tests/ui/pattern/deref-patterns/bindings.rs
+++ b/tests/ui/pattern/deref-patterns/bindings.rs
@@ -3,6 +3,8 @@
 #![feature(deref_patterns)]
 #![allow(incomplete_features)]
 
+use std::rc::Rc;
+
 #[cfg(explicit)]
 fn simple_vec(vec: Vec<u32>) -> u32 {
     match vec {
@@ -53,29 +55,29 @@ fn nested_vec(vecvec: Vec<Vec<u32>>) -> u32 {
 
 #[cfg(explicit)]
 fn ref_mut(val: u32) -> u32 {
-    let mut b = Box::new(0u32);
+    let mut b = vec![0u32];
     match &mut b {
-        deref!(_x) if false => unreachable!(),
-        deref!(x) => {
+        deref!([_x]) if false => unreachable!(),
+        deref!([x]) => {
             *x = val;
         }
         _ => unreachable!(),
     }
-    let deref!(x) = &b else { unreachable!() };
+    let deref!([x]) = &b else { unreachable!() };
     *x
 }
 
 #[cfg(implicit)]
 fn ref_mut(val: u32) -> u32 {
-    let mut b = Box::new((0u32,));
+    let mut b = vec![0u32];
     match &mut b {
-        (_x,) if false => unreachable!(),
-        (x,) => {
+        [_x] if false => unreachable!(),
+        [x] => {
             *x = val;
         }
         _ => unreachable!(),
     }
-    let (x,) = &b else { unreachable!() };
+    let [x] = &b else { unreachable!() };
     *x
 }
 
@@ -83,7 +85,7 @@ fn ref_mut(val: u32) -> u32 {
 #[rustfmt::skip]
 fn or_and_guard(tuple: (u32, u32)) -> u32 {
     let mut sum = 0;
-    let b = Box::new(tuple);
+    let b = Rc::new(tuple);
     match b {
         deref!((x, _) | (_, x)) if { sum += x; false } => {},
         _ => {},
@@ -95,7 +97,7 @@ fn or_and_guard(tuple: (u32, u32)) -> u32 {
 #[rustfmt::skip]
 fn or_and_guard(tuple: (u32, u32)) -> u32 {
     let mut sum = 0;
-    let b = Box::new(tuple);
+    let b = Rc::new(tuple);
     match b {
         (x, _) | (_, x) if { sum += x; false } => {},
         _ => {},
diff --git a/tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.rs b/tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.rs
index 791776be5ac..2b4746e33e6 100644
--- a/tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.rs
+++ b/tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.rs
@@ -5,11 +5,11 @@ use std::rc::Rc;
 
 struct Struct;
 
-fn cant_move_out_box(b: Box<Struct>) -> Struct {
+fn cant_move_out_vec(b: Vec<Struct>) -> Struct {
     match b {
-        //~^ ERROR: cannot move out of a shared reference
-        deref!(x) => x,
-        _ => unreachable!(),
+        //~^ ERROR: cannot move out of type `[Struct]`, a non-copy slice
+        deref!([x]) => x,
+        _ => panic!(),
     }
 }
 
@@ -21,16 +21,16 @@ fn cant_move_out_rc(rc: Rc<Struct>) -> Struct {
     }
 }
 
-struct Container(Struct);
-
-fn cant_move_out_box_implicit(b: Box<Container>) -> Struct {
+fn cant_move_out_vec_implicit(b: Vec<Struct>) -> Struct {
     match b {
-        //~^ ERROR: cannot move out of a shared reference
-        Container(x) => x,
-        _ => unreachable!(),
+        //~^ ERROR: cannot move out of type `[Struct]`, a non-copy slice
+        [x] => x,
+        _ => panic!(),
     }
 }
 
+struct Container(Struct);
+
 fn cant_move_out_rc_implicit(rc: Rc<Container>) -> Struct {
     match rc {
         //~^ ERROR: cannot move out of a shared reference
diff --git a/tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.stderr b/tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.stderr
index 1887800fc38..a548ac5909a 100644
--- a/tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.stderr
+++ b/tests/ui/pattern/deref-patterns/cant_move_out_of_pattern.stderr
@@ -1,19 +1,19 @@
-error[E0507]: cannot move out of a shared reference
+error[E0508]: cannot move out of type `[Struct]`, a non-copy slice
   --> $DIR/cant_move_out_of_pattern.rs:9:11
    |
 LL |     match b {
-   |           ^
+   |           ^ cannot move out of here
 LL |
-LL |         deref!(x) => x,
-   |                -
-   |                |
-   |                data moved here
-   |                move occurs because `x` has type `Struct`, which does not implement the `Copy` trait
+LL |         deref!([x]) => x,
+   |                 -
+   |                 |
+   |                 data moved here
+   |                 move occurs because `x` has type `Struct`, which does not implement the `Copy` trait
    |
 help: consider borrowing the pattern binding
    |
-LL |         deref!(ref x) => x,
-   |                +++
+LL |         deref!([ref x]) => x,
+   |                 +++
 
 error[E0507]: cannot move out of a shared reference
   --> $DIR/cant_move_out_of_pattern.rs:17:11
@@ -32,22 +32,22 @@ help: consider borrowing the pattern binding
 LL |         deref!(ref x) => x,
    |                +++
 
-error[E0507]: cannot move out of a shared reference
-  --> $DIR/cant_move_out_of_pattern.rs:27:11
+error[E0508]: cannot move out of type `[Struct]`, a non-copy slice
+  --> $DIR/cant_move_out_of_pattern.rs:25:11
    |
 LL |     match b {
-   |           ^
+   |           ^ cannot move out of here
 LL |
-LL |         Container(x) => x,
-   |                   -
-   |                   |
-   |                   data moved here
-   |                   move occurs because `x` has type `Struct`, which does not implement the `Copy` trait
+LL |         [x] => x,
+   |          -
+   |          |
+   |          data moved here
+   |          move occurs because `x` has type `Struct`, which does not implement the `Copy` trait
    |
 help: consider borrowing the pattern binding
    |
-LL |         Container(ref x) => x,
-   |                   +++
+LL |         [ref x] => x,
+   |          +++
 
 error[E0507]: cannot move out of a shared reference
   --> $DIR/cant_move_out_of_pattern.rs:35:11
@@ -68,4 +68,5 @@ LL |         Container(ref x) => x,
 
 error: aborting due to 4 previous errors
 
-For more information about this error, try `rustc --explain E0507`.
+Some errors have detailed explanations: E0507, E0508.
+For more information about an error, try `rustc --explain E0507`.
diff --git a/tests/ui/pattern/deref-patterns/closure_capture.rs b/tests/ui/pattern/deref-patterns/closure_capture.rs
index 08586b6c7ab..5d1aa371eee 100644
--- a/tests/ui/pattern/deref-patterns/closure_capture.rs
+++ b/tests/ui/pattern/deref-patterns/closure_capture.rs
@@ -2,8 +2,10 @@
 #![feature(deref_patterns)]
 #![allow(incomplete_features)]
 
+use std::rc::Rc;
+
 fn main() {
-    let b = Box::new("aaa".to_string());
+    let b = Rc::new("aaa".to_string());
     let f = || {
         let deref!(ref s) = b else { unreachable!() };
         assert_eq!(s.len(), 3);
@@ -20,13 +22,13 @@ fn main() {
     assert_eq!(v, [1, 2, 3]);
     f();
 
-    let mut b = Box::new("aaa".to_string());
+    let mut b = "aaa".to_string();
     let mut f = || {
         let deref!(ref mut s) = b else { unreachable!() };
-        s.push_str("aa");
+        s.make_ascii_uppercase();
     };
     f();
-    assert_eq!(b.len(), 5);
+    assert_eq!(b, "AAA");
 
     let mut v = vec![1, 2, 3];
     let mut f = || {
diff --git a/tests/ui/pattern/deref-patterns/fake_borrows.rs b/tests/ui/pattern/deref-patterns/fake_borrows.rs
index bf614d7d66f..fba2873fd02 100644
--- a/tests/ui/pattern/deref-patterns/fake_borrows.rs
+++ b/tests/ui/pattern/deref-patterns/fake_borrows.rs
@@ -3,6 +3,23 @@
 
 #[rustfmt::skip]
 fn main() {
+    let mut v = vec![false];
+    match v {
+        deref!([true]) => {}
+        _ if { v[0] = true; false } => {}
+        //~^ ERROR cannot borrow `v` as mutable because it is also borrowed as immutable
+        deref!([false]) => {}
+        _ => {},
+    }
+    match v {
+        [true] => {}
+        _ if { v[0] = true; false } => {}
+        //~^ ERROR cannot borrow `v` as mutable because it is also borrowed as immutable
+        [false] => {}
+        _ => {},
+    }
+
+    // deref patterns on boxes are lowered specially; test them separately.
     let mut b = Box::new(false);
     match b {
         deref!(true) => {}
diff --git a/tests/ui/pattern/deref-patterns/fake_borrows.stderr b/tests/ui/pattern/deref-patterns/fake_borrows.stderr
index 8c060236d0d..7dc3001739e 100644
--- a/tests/ui/pattern/deref-patterns/fake_borrows.stderr
+++ b/tests/ui/pattern/deref-patterns/fake_borrows.stderr
@@ -1,6 +1,28 @@
-error[E0510]: cannot assign `*b` in match guard
+error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
   --> $DIR/fake_borrows.rs:9:16
    |
+LL |     match v {
+   |           - immutable borrow occurs here
+LL |         deref!([true]) => {}
+LL |         _ if { v[0] = true; false } => {}
+   |                ^                  - immutable borrow later used here
+   |                |
+   |                mutable borrow occurs here
+
+error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
+  --> $DIR/fake_borrows.rs:16:16
+   |
+LL |     match v {
+   |           - immutable borrow occurs here
+LL |         [true] => {}
+LL |         _ if { v[0] = true; false } => {}
+   |                ^                  - immutable borrow later used here
+   |                |
+   |                mutable borrow occurs here
+
+error[E0510]: cannot assign `*b` in match guard
+  --> $DIR/fake_borrows.rs:26:16
+   |
 LL |     match b {
    |           - value is immutable in match guard
 LL |         deref!(true) => {}
@@ -8,7 +30,7 @@ LL |         _ if { *b = true; false } => {}
    |                ^^^^^^^^^ cannot assign
 
 error[E0510]: cannot assign `*b` in match guard
-  --> $DIR/fake_borrows.rs:16:16
+  --> $DIR/fake_borrows.rs:33:16
    |
 LL |     match b {
    |           - value is immutable in match guard
@@ -16,6 +38,7 @@ LL |         true => {}
 LL |         _ if { *b = true; false } => {}
    |                ^^^^^^^^^ cannot assign
 
-error: aborting due to 2 previous errors
+error: aborting due to 4 previous errors
 
-For more information about this error, try `rustc --explain E0510`.
+Some errors have detailed explanations: E0502, E0510.
+For more information about an error, try `rustc --explain E0502`.
diff --git a/tests/ui/pattern/deref-patterns/implicit-cow-deref.rs b/tests/ui/pattern/deref-patterns/implicit-cow-deref.rs
index a9b8de86010..04c83d4c33f 100644
--- a/tests/ui/pattern/deref-patterns/implicit-cow-deref.rs
+++ b/tests/ui/pattern/deref-patterns/implicit-cow-deref.rs
@@ -4,6 +4,7 @@
 #![allow(incomplete_features)]
 
 use std::borrow::Cow;
+use std::rc::Rc;
 
 fn main() {
     let cow: Cow<'static, [u8]> = Cow::Borrowed(&[1, 2, 3]);
@@ -18,7 +19,7 @@ fn main() {
         Cow::Owned(_) => unreachable!(),
     }
 
-    match Box::new(&cow) {
+    match Rc::new(&cow) {
         Cow::Borrowed { 0: _ } => {}
         Cow::Owned { 0: _ } => unreachable!(),
         _ => unreachable!(),
@@ -37,7 +38,7 @@ fn main() {
         Cow::Owned(_) => {}
     }
 
-    match Box::new(&cow_of_cow) {
+    match Rc::new(&cow_of_cow) {
         Cow::Borrowed { 0: _ } => unreachable!(),
         Cow::Owned { 0: _ } => {}
         _ => unreachable!(),