]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/fsu-moves-and-copies.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / fsu-moves-and-copies.rs
1 // Copyright 2012-2013 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 // Issue 4691: Ensure that functional-struct-updates operates
12 // correctly and moves rather than copy when appropriate.
13
14
15 #![allow(unknown_features)]
16 #![feature(box_syntax, core)]
17
18 struct ncint { v: isize }
19 fn ncint(v: isize) -> ncint { ncint { v: v } }
20
21 struct NoFoo { copied: isize, nocopy: ncint, }
22 impl NoFoo {
23     fn new(x:isize,y:isize) -> NoFoo { NoFoo { copied: x, nocopy: ncint(y) } }
24 }
25
26 struct MoveFoo { copied: isize, moved: Box<isize>, }
27 impl MoveFoo {
28     fn new(x:isize,y:isize) -> MoveFoo { MoveFoo { copied: x, moved: box y } }
29 }
30
31 struct DropNoFoo { inner: NoFoo }
32 impl DropNoFoo {
33     fn new(x:isize,y:isize) -> DropNoFoo { DropNoFoo { inner: NoFoo::new(x,y) } }
34 }
35 impl Drop for DropNoFoo { fn drop(&mut self) { } }
36
37 struct DropMoveFoo { inner: MoveFoo }
38 impl DropMoveFoo {
39     fn new(x:isize,y:isize) -> DropMoveFoo { DropMoveFoo { inner: MoveFoo::new(x,y) } }
40 }
41 impl Drop for DropMoveFoo { fn drop(&mut self) { } }
42
43
44 fn test0() {
45     // just copy implicitly copyable fields from `f`, no moves
46     // (and thus it is okay that these are Drop; compare against
47     // compile-fail test: borrowck-struct-update-with-dtor.rs).
48
49     // Case 1: Nocopyable
50     let f = DropNoFoo::new(1, 2);
51     let b = DropNoFoo { inner: NoFoo { nocopy: ncint(3), ..f.inner }};
52     let c = DropNoFoo { inner: NoFoo { nocopy: ncint(4), ..f.inner }};
53     assert_eq!(f.inner.copied,    1);
54     assert_eq!(f.inner.nocopy.v, 2);
55
56     assert_eq!(b.inner.copied,    1);
57     assert_eq!(b.inner.nocopy.v, 3);
58
59     assert_eq!(c.inner.copied,    1);
60     assert_eq!(c.inner.nocopy.v, 4);
61
62     // Case 2: Owned
63     let f = DropMoveFoo::new(5, 6);
64     let b = DropMoveFoo { inner: MoveFoo { moved: box 7, ..f.inner }};
65     let c = DropMoveFoo { inner: MoveFoo { moved: box 8, ..f.inner }};
66     assert_eq!(f.inner.copied,    5);
67     assert_eq!(*f.inner.moved,    6);
68
69     assert_eq!(b.inner.copied,    5);
70     assert_eq!(*b.inner.moved,    7);
71
72     assert_eq!(c.inner.copied,    5);
73     assert_eq!(*c.inner.moved,    8);
74 }
75
76 fn test1() {
77     // copying move-by-default fields from `f`, so it moves:
78     let f = MoveFoo::new(11, 12);
79
80     let b = MoveFoo {moved: box 13, ..f};
81     let c = MoveFoo {copied: 14, ..f};
82     assert_eq!(b.copied,    11);
83     assert_eq!(*b.moved,    13);
84     assert_eq!(c.copied,    14);
85     assert_eq!(*c.moved,    12);
86 }
87
88 fn test2() {
89     // move non-copyable field
90     let f = NoFoo::new(21, 22);
91     let b = NoFoo {nocopy: ncint(23), ..f};
92     let c = NoFoo {copied: 24, ..f};
93     assert_eq!(b.copied,    21);
94     assert_eq!(b.nocopy.v, 23);
95     assert_eq!(c.copied,    24);
96     assert_eq!(c.nocopy.v, 22);
97 }
98
99 pub fn main() {
100     test0();
101     test1();
102     test2();
103 }