]> git.proxmox.com Git - rustc.git/blame - src/test/ui/borrowck/borrowck-assign-comp.rs
New upstream version 1.33.0+dfsg1
[rustc.git] / src / test / ui / borrowck / borrowck-assign-comp.rs
CommitLineData
ea8adc8c 1// revisions: ast mir
ff7c6d11 2//[mir]compile-flags: -Z borrowck=mir
ea8adc8c 3
0731742a 4struct Point { x: isize, y: isize }
223e47cc
LB
5
6fn a() {
0731742a 7 let mut p = Point {x: 3, y: 4};
970d7e83 8 let q = &p;
223e47cc
LB
9
10 // This assignment is illegal because the field x is not
11 // inherently mutable; since `p` was made immutable, `p.x` is now
1a4d82fc 12 // immutable. Otherwise the type of &_q.x (&isize) would be wrong.
ea8adc8c 13 p.x = 5; //[ast]~ ERROR cannot assign to `p.x`
ff7c6d11 14 //[mir]~^ ERROR cannot assign to `p.x` because it is borrowed
970d7e83 15 q.x;
223e47cc
LB
16}
17
18fn c() {
19 // this is sort of the opposite. We take a loan to the interior of `p`
20 // and then try to overwrite `p` as a whole.
21
0731742a 22 let mut p = Point {x: 3, y: 4};
970d7e83 23 let q = &p.y;
0731742a 24 p = Point {x: 5, y: 7};//[ast]~ ERROR cannot assign to `p`
ff7c6d11 25 //[mir]~^ ERROR cannot assign to `p` because it is borrowed
970d7e83
LB
26 p.x; // silence warning
27 *q; // stretch loan
223e47cc
LB
28}
29
30fn d() {
31 // just for completeness's sake, the easy case, where we take the
32 // address of a subcomponent and then modify that subcomponent:
33
0731742a 34 let mut p = Point {x: 3, y: 4};
970d7e83 35 let q = &p.y;
ea8adc8c 36 p.y = 5; //[ast]~ ERROR cannot assign to `p.y`
ff7c6d11 37 //[mir]~^ ERROR cannot assign to `p.y` because it is borrowed
970d7e83 38 *q;
223e47cc
LB
39}
40
41fn main() {
42}