]> git.proxmox.com Git - rustc.git/blob - src/test/ui/issues/issue-24535-allow-mutable-borrow-in-match-guard.rs
New upstream version 1.54.0+dfsg1
[rustc.git] / src / test / ui / issues / issue-24535-allow-mutable-borrow-in-match-guard.rs
1 // run-pass
2 // This test illustrates that under NLL, we can remove our overly
3 // conservative approach for disallowing mutations of match inputs.
4
5 // See further discussion on rust-lang/rust#24535,
6 // rust-lang/rfcs#1006, and rust-lang/rfcs#107
7
8 fn main() {
9 rust_issue_24535();
10 rfcs_issue_1006_1();
11 rfcs_issue_1006_2();
12 }
13
14 fn rust_issue_24535() {
15 fn compare(a: &u8, b: &mut u8) -> bool {
16 a == b
17 }
18
19 let a = 3u8;
20
21 match a {
22 0 => panic!("nope"),
23 3 if compare(&a, &mut 3) => (),
24 _ => panic!("nope"),
25 }
26 }
27
28 fn rfcs_issue_1006_1() {
29 let v = vec!["1".to_string(), "2".to_string(), "3".to_string()];
30 match Some(&v) {
31 Some(iv) if iv.iter().any(|x| &x[..]=="2") => true,
32 _ => panic!("nope"),
33 };
34 }
35
36 fn rfcs_issue_1006_2() {
37 #[inline(always)]
38 fn check<'a, I: Iterator<Item=&'a i32>>(mut i: I) -> bool {
39 i.any(|&x| x == 2)
40 }
41
42 let slice = [1, 2, 3];
43
44 match 42 {
45 _ if slice.iter().any(|&x| x == 2) => { true },
46 _ => { panic!("nope"); }
47 };
48
49 // (This match is just illustrating how easy it was to circumvent
50 // the checking performed for the previous `match`.)
51 match 42 {
52 _ if check(slice.iter()) => { true },
53 _ => { panic!("nope"); }
54 };
55 }