|
| 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 | +use NC = std::util::NonCopyable; |
| 15 | + |
| 16 | +struct ncint { nc: NC, v: int } |
| 17 | +fn ncint(v: int) -> ncint { ncint { nc: NC, v: v } } |
| 18 | + |
| 19 | +struct NoFoo { copied: int, noncopy: ncint, } |
| 20 | +impl NoFoo { |
| 21 | + fn new(x:int,y:int) -> NoFoo { NoFoo { copied: x, noncopy: ncint(y) } } |
| 22 | +} |
| 23 | + |
| 24 | +struct MoveFoo { copied: int, moved: ~int, } |
| 25 | +impl MoveFoo { |
| 26 | + fn new(x:int,y:int) -> MoveFoo { MoveFoo { copied: x, moved: ~y } } |
| 27 | +} |
| 28 | + |
| 29 | +struct DropNoFoo { inner: NoFoo } |
| 30 | +impl DropNoFoo { |
| 31 | + fn new(x:int,y:int) -> DropNoFoo { DropNoFoo { inner: NoFoo::new(x,y) } } |
| 32 | +} |
| 33 | +impl Drop for DropNoFoo { fn drop(&mut self) { } } |
| 34 | + |
| 35 | +struct DropMoveFoo { inner: MoveFoo } |
| 36 | +impl DropMoveFoo { |
| 37 | + fn new(x:int,y:int) -> DropMoveFoo { DropMoveFoo { inner: MoveFoo::new(x,y) } } |
| 38 | +} |
| 39 | +impl Drop for DropMoveFoo { fn drop(&mut self) { } } |
| 40 | + |
| 41 | + |
| 42 | +fn test0() { |
| 43 | + // just copy implicitly copyable fields from `f`, no moves |
| 44 | + // (and thus it is okay that these are Drop; compare against |
| 45 | + // compile-fail test: borrowck-struct-update-with-dtor.rs). |
| 46 | + |
| 47 | + // Case 1: NonCopyable |
| 48 | + let f = DropNoFoo::new(1, 2); |
| 49 | + let b = DropNoFoo { inner: NoFoo { noncopy: ncint(3), ..f.inner }}; |
| 50 | + let c = DropNoFoo { inner: NoFoo { noncopy: ncint(4), ..f.inner }}; |
| 51 | + assert_eq!(f.inner.copied, 1); |
| 52 | + assert_eq!(f.inner.noncopy.v, 2); |
| 53 | + |
| 54 | + assert_eq!(b.inner.copied, 1); |
| 55 | + assert_eq!(b.inner.noncopy.v, 3); |
| 56 | + |
| 57 | + assert_eq!(c.inner.copied, 1); |
| 58 | + assert_eq!(c.inner.noncopy.v, 4); |
| 59 | + |
| 60 | + // Case 2: Owned |
| 61 | + let f = DropMoveFoo::new(5, 6); |
| 62 | + let b = DropMoveFoo { inner: MoveFoo { moved: ~7, ..f.inner }}; |
| 63 | + let c = DropMoveFoo { inner: MoveFoo { moved: ~8, ..f.inner }}; |
| 64 | + assert_eq!(f.inner.copied, 5); |
| 65 | + assert_eq!(*f.inner.moved, 6); |
| 66 | + |
| 67 | + assert_eq!(b.inner.copied, 5); |
| 68 | + assert_eq!(*b.inner.moved, 7); |
| 69 | + |
| 70 | + assert_eq!(c.inner.copied, 5); |
| 71 | + assert_eq!(*c.inner.moved, 8); |
| 72 | +} |
| 73 | + |
| 74 | +fn test1() { |
| 75 | + // copying move-by-default fields from `f`, so it moves: |
| 76 | + let f = MoveFoo::new(11, 12); |
| 77 | + |
| 78 | + let b = MoveFoo {moved: ~13, ..f}; |
| 79 | + let c = MoveFoo {copied: 14, ..f}; |
| 80 | + assert_eq!(b.copied, 11); |
| 81 | + assert_eq!(*b.moved, 13); |
| 82 | + assert_eq!(c.copied, 14); |
| 83 | + assert_eq!(*c.moved, 12); |
| 84 | +} |
| 85 | + |
| 86 | +fn test2() { |
| 87 | + // move non-copyable field |
| 88 | + let f = NoFoo::new(21, 22); |
| 89 | + let b = NoFoo {noncopy: ncint(23), ..f}; |
| 90 | + let c = NoFoo {copied: 24, ..f}; |
| 91 | + assert_eq!(b.copied, 21); |
| 92 | + assert_eq!(b.noncopy.v, 23); |
| 93 | + assert_eq!(c.copied, 24); |
| 94 | + assert_eq!(c.noncopy.v, 22); |
| 95 | +} |
| 96 | + |
| 97 | +fn main() { |
| 98 | + test0(); |
| 99 | + test1(); |
| 100 | + test2(); |
| 101 | +} |
0 commit comments