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.
This commit is contained in:
dianne 2025-03-16 18:21:00 -07:00
parent 3c877f6a47
commit 64c8d5d7f5
7 changed files with 96 additions and 50 deletions

View File

@ -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 } => {},
_ => {},

View File

@ -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

View File

@ -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`.

View File

@ -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 = || {

View File

@ -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) => {}

View File

@ -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`.

View File

@ -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!(),