]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-assign-comp.rs
Update compile fail tests to use isize.
[rust.git] / src / test / compile-fail / borrowck-assign-comp.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 struct point { x: isize, y: isize }
12
13 fn a() {
14     let mut p = point {x: 3, y: 4};
15     let q = &p;
16
17     // This assignment is illegal because the field x is not
18     // inherently mutable; since `p` was made immutable, `p.x` is now
19     // immutable.  Otherwise the type of &_q.x (&isize) would be wrong.
20     p.x = 5; //~ ERROR cannot assign to `p.x`
21     q.x;
22 }
23
24 fn c() {
25     // this is sort of the opposite.  We take a loan to the interior of `p`
26     // and then try to overwrite `p` as a whole.
27
28     let mut p = point {x: 3, y: 4};
29     let q = &p.y;
30     p = point {x: 5, y: 7};//~ ERROR cannot assign to `p`
31     p.x; // silence warning
32     *q; // stretch loan
33 }
34
35 fn d() {
36     // just for completeness's sake, the easy case, where we take the
37     // address of a subcomponent and then modify that subcomponent:
38
39     let mut p = point {x: 3, y: 4};
40     let q = &p.y;
41     p.y = 5; //~ ERROR cannot assign to `p.y`
42     *q;
43 }
44
45 fn main() {
46 }