]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-anon-fields-variant.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-anon-fields-variant.rs
1 enum Foo {
2     X, Y(usize, usize)
3 }
4
5 fn distinct_variant() {
6     let mut y = Foo::Y(1, 2);
7
8     let a = match y {
9       Foo::Y(ref mut a, _) => a,
10       Foo::X => panic!()
11     };
12
13     // While `a` and `b` are disjoint, borrowck doesn't know that `a` is not
14     // also used for the discriminant of `Foo`, which it would be if `a` was a
15     // reference.
16     let b = match y {
17       //~^ ERROR cannot use `y`
18       Foo::Y(_, ref mut b) => b,
19       Foo::X => panic!()
20     };
21
22     *a += 1;
23     *b += 1;
24 }
25
26 fn same_variant() {
27     let mut y = Foo::Y(1, 2);
28
29     let a = match y {
30       Foo::Y(ref mut a, _) => a,
31       Foo::X => panic!()
32     };
33
34     let b = match y {
35       //~^ ERROR cannot use `y`
36       Foo::Y(ref mut b, _) => b,
37       //~^ ERROR cannot borrow `y.0` as mutable
38       Foo::X => panic!()
39     };
40
41     *a += 1;
42     *b += 1;
43 }
44
45 fn main() {
46 }