]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-assign-comp.rs
Rollup merge of #107580 - lenko-d:default_value_for_a_lifetime_generic_parameter_prod...
[rust.git] / tests / ui / borrowck / borrowck-assign-comp.rs
1 struct Point { x: isize, y: isize }
2
3 fn a() {
4     let mut p = Point {x: 3, y: 4};
5     let q = &p;
6
7     // This assignment is illegal because the field x is not
8     // inherently mutable; since `p` was made immutable, `p.x` is now
9     // immutable.  Otherwise the type of &_q.x (&isize) would be wrong.
10     p.x = 5; //~ ERROR cannot assign to `p.x` because it is borrowed
11     q.x;
12 }
13
14 fn c() {
15     // this is sort of the opposite.  We take a loan to the interior of `p`
16     // and then try to overwrite `p` as a whole.
17
18     let mut p = Point {x: 3, y: 4};
19     let q = &p.y;
20     p = Point {x: 5, y: 7};//~ ERROR cannot assign to `p` because it is borrowed
21     p.x; // silence warning
22     *q; // stretch loan
23 }
24
25 fn d() {
26     // just for completeness's sake, the easy case, where we take the
27     // address of a subcomponent and then modify that subcomponent:
28
29     let mut p = Point {x: 3, y: 4};
30     let q = &p.y;
31     p.y = 5; //~ ERROR cannot assign to `p.y` because it is borrowed
32     *q;
33 }
34
35 fn main() {
36 }