2022-12-21 09:29:35 -06:00
|
|
|
#![feature(if_let_guard)]
|
|
|
|
|
2018-04-10 07:33:24 -05:00
|
|
|
// Here is arielb1's basic example from rust-lang/rust#27282
|
|
|
|
// that AST borrowck is flummoxed by:
|
|
|
|
|
|
|
|
fn should_reject_destructive_mutate_in_guard() {
|
|
|
|
match Some(&4) {
|
|
|
|
None => {},
|
|
|
|
ref mut foo if {
|
|
|
|
(|| { let bar = foo; bar.take() })();
|
2019-05-05 06:02:32 -05:00
|
|
|
//~^ ERROR cannot move out of `foo` in pattern guard [E0507]
|
2018-04-10 07:33:24 -05:00
|
|
|
false } => { },
|
|
|
|
Some(s) => std::process::exit(*s),
|
|
|
|
}
|
2022-12-21 09:29:35 -06:00
|
|
|
|
|
|
|
match Some(&4) {
|
|
|
|
None => {},
|
|
|
|
ref mut foo if let Some(()) = {
|
|
|
|
(|| { let bar = foo; bar.take() })();
|
|
|
|
//~^ ERROR cannot move out of `foo` in pattern guard [E0507]
|
|
|
|
None } => { },
|
|
|
|
Some(s) => std::process::exit(*s),
|
|
|
|
}
|
2018-04-10 07:33:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Here below is a case that needs to keep working: we only use the
|
|
|
|
// binding via immutable-borrow in the guard, and we mutate in the arm
|
|
|
|
// body.
|
|
|
|
fn allow_mutate_in_arm_body() {
|
|
|
|
match Some(&4) {
|
|
|
|
None => {},
|
2022-12-21 09:29:35 -06:00
|
|
|
ref mut foo if foo.is_some() => { foo.take(); () }
|
|
|
|
Some(s) => std::process::exit(*s),
|
|
|
|
}
|
|
|
|
|
|
|
|
match Some(&4) {
|
|
|
|
None => {},
|
|
|
|
ref mut foo if let Some(_) = foo => { foo.take(); () }
|
2018-04-10 07:33:24 -05:00
|
|
|
Some(s) => std::process::exit(*s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here below is a case that needs to keep working: we only use the
|
|
|
|
// binding via immutable-borrow in the guard, and we move into the arm
|
|
|
|
// body.
|
|
|
|
fn allow_move_into_arm_body() {
|
|
|
|
match Some(&4) {
|
|
|
|
None => {},
|
2022-12-21 09:29:35 -06:00
|
|
|
mut foo if foo.is_some() => { foo.unwrap(); () }
|
|
|
|
Some(s) => std::process::exit(*s),
|
|
|
|
}
|
|
|
|
|
|
|
|
match Some(&4) {
|
|
|
|
None => {},
|
|
|
|
mut foo if let Some(_) = foo => { foo.unwrap(); () }
|
2018-04-10 07:33:24 -05:00
|
|
|
Some(s) => std::process::exit(*s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 02:40:08 -05:00
|
|
|
fn main() {
|
2018-04-10 07:33:24 -05:00
|
|
|
should_reject_destructive_mutate_in_guard();
|
|
|
|
allow_mutate_in_arm_body();
|
|
|
|
allow_move_into_arm_body();
|
|
|
|
}
|