register DerefMut bounds for implicit mutable derefs

This commit is contained in:
dianne 2025-03-09 00:06:00 -08:00
parent e4b7b3d820
commit 91d0b579f0
6 changed files with 100 additions and 18 deletions

View file

@ -51,6 +51,7 @@ fn nested_vec(vecvec: Vec<Vec<u32>>) -> u32 {
}
}
#[cfg(explicit)]
fn ref_mut(val: u32) -> u32 {
let mut b = Box::new(0u32);
match &mut b {
@ -64,6 +65,21 @@ fn ref_mut(val: u32) -> u32 {
*x
}
#[cfg(implicit)]
fn ref_mut(val: u32) -> u32 {
let mut b = Box::new((0u32,));
match &mut b {
(_x,) if false => unreachable!(),
(x,) => {
*x = val;
}
_ => unreachable!(),
}
let (x,) = &b else { unreachable!() };
*x
}
#[cfg(explicit)]
#[rustfmt::skip]
fn or_and_guard(tuple: (u32, u32)) -> u32 {
let mut sum = 0;
@ -75,6 +91,18 @@ fn or_and_guard(tuple: (u32, u32)) -> u32 {
sum
}
#[cfg(implicit)]
#[rustfmt::skip]
fn or_and_guard(tuple: (u32, u32)) -> u32 {
let mut sum = 0;
let b = Box::new(tuple);
match b {
(x, _) | (_, x) if { sum += x; false } => {},
_ => {},
}
sum
}
fn main() {
assert_eq!(simple_vec(vec![1]), 1);
assert_eq!(simple_vec(vec![1, 2]), 202);

View file

@ -11,4 +11,11 @@ fn main() {
deref!(false) => {}
_ => {},
}
match b {
true => {}
_ if { *b = true; false } => {}
//~^ ERROR cannot assign `*b` in match guard
false => {}
_ => {},
}
}

View file

@ -7,6 +7,15 @@ LL | deref!(true) => {}
LL | _ if { *b = true; false } => {}
| ^^^^^^^^^ cannot assign
error: aborting due to 1 previous error
error[E0510]: cannot assign `*b` in match guard
--> $DIR/fake_borrows.rs:16:16
|
LL | match b {
| - value is immutable in match guard
LL | true => {}
LL | _ if { *b = true; false } => {}
| ^^^^^^^^^ cannot assign
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0510`.

View file

@ -8,10 +8,19 @@ fn main() {
deref!(x) => {}
_ => {}
}
match &mut vec![1] {
[x] => {}
_ => {}
}
match &mut Rc::new(1) {
deref!(x) => {}
//~^ ERROR the trait bound `Rc<{integer}>: DerefMut` is not satisfied
_ => {}
}
match &mut Rc::new((1,)) {
(x,) => {}
//~^ ERROR the trait bound `Rc<({integer},)>: DerefMut` is not satisfied
_ => {}
}
}

View file

@ -8,13 +8,19 @@ LL | #![feature(deref_patterns)]
= note: `#[warn(incomplete_features)]` on by default
error[E0277]: the trait bound `Rc<{integer}>: DerefMut` is not satisfied
--> $DIR/ref-mut.rs:13:9
--> $DIR/ref-mut.rs:17:9
|
LL | deref!(x) => {}
| ^^^^^^^^^ the trait `DerefMut` is not implemented for `Rc<{integer}>`
|
= note: this error originates in the macro `deref` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 1 previous error; 1 warning emitted
error[E0277]: the trait bound `Rc<({integer},)>: DerefMut` is not satisfied
--> $DIR/ref-mut.rs:22:9
|
LL | (x,) => {}
| ^^^^ the trait `DerefMut` is not implemented for `Rc<({integer},)>`
error: aborting due to 2 previous errors; 1 warning emitted
For more information about this error, try `rustc --explain E0277`.