summary refs log tree commit diff
path: root/src/test/run-pass/fsu-moves-and-copies.rs
blob: e6ac8b52c51855c6f3e43826b402ab33f444cce1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Issue 4691: Ensure that functional-struct-updates operates
// correctly and moves rather than copy when appropriate.


#![allow(unknown_features)]
#![feature(box_syntax, core)]

struct ncint { v: isize }
fn ncint(v: isize) -> ncint { ncint { v: v } }

struct NoFoo { copied: isize, nocopy: ncint, }
impl NoFoo {
    fn new(x:isize,y:isize) -> NoFoo { NoFoo { copied: x, nocopy: ncint(y) } }
}

struct MoveFoo { copied: isize, moved: Box<isize>, }
impl MoveFoo {
    fn new(x:isize,y:isize) -> MoveFoo { MoveFoo { copied: x, moved: box y } }
}

struct DropNoFoo { inner: NoFoo }
impl DropNoFoo {
    fn new(x:isize,y:isize) -> DropNoFoo { DropNoFoo { inner: NoFoo::new(x,y) } }
}
impl Drop for DropNoFoo { fn drop(&mut self) { } }

struct DropMoveFoo { inner: MoveFoo }
impl DropMoveFoo {
    fn new(x:isize,y:isize) -> DropMoveFoo { DropMoveFoo { inner: MoveFoo::new(x,y) } }
}
impl Drop for DropMoveFoo { fn drop(&mut self) { } }


fn test0() {
    // just copy implicitly copyable fields from `f`, no moves
    // (and thus it is okay that these are Drop; compare against
    // compile-fail test: borrowck-struct-update-with-dtor.rs).

    // Case 1: Nocopyable
    let f = DropNoFoo::new(1, 2);
    let b = DropNoFoo { inner: NoFoo { nocopy: ncint(3), ..f.inner }};
    let c = DropNoFoo { inner: NoFoo { nocopy: ncint(4), ..f.inner }};
    assert_eq!(f.inner.copied,    1);
    assert_eq!(f.inner.nocopy.v, 2);

    assert_eq!(b.inner.copied,    1);
    assert_eq!(b.inner.nocopy.v, 3);

    assert_eq!(c.inner.copied,    1);
    assert_eq!(c.inner.nocopy.v, 4);

    // Case 2: Owned
    let f = DropMoveFoo::new(5, 6);
    let b = DropMoveFoo { inner: MoveFoo { moved: box 7, ..f.inner }};
    let c = DropMoveFoo { inner: MoveFoo { moved: box 8, ..f.inner }};
    assert_eq!(f.inner.copied,    5);
    assert_eq!(*f.inner.moved,    6);

    assert_eq!(b.inner.copied,    5);
    assert_eq!(*b.inner.moved,    7);

    assert_eq!(c.inner.copied,    5);
    assert_eq!(*c.inner.moved,    8);
}

fn test1() {
    // copying move-by-default fields from `f`, so it moves:
    let f = MoveFoo::new(11, 12);

    let b = MoveFoo {moved: box 13, ..f};
    let c = MoveFoo {copied: 14, ..f};
    assert_eq!(b.copied,    11);
    assert_eq!(*b.moved,    13);
    assert_eq!(c.copied,    14);
    assert_eq!(*c.moved,    12);
}

fn test2() {
    // move non-copyable field
    let f = NoFoo::new(21, 22);
    let b = NoFoo {nocopy: ncint(23), ..f};
    let c = NoFoo {copied: 24, ..f};
    assert_eq!(b.copied,    21);
    assert_eq!(b.nocopy.v, 23);
    assert_eq!(c.copied,    24);
    assert_eq!(c.nocopy.v, 22);
}

pub fn main() {
    test0();
    test1();
    test2();
}