2024-02-16 20:02:50 +00:00
|
|
|
//@ run-pass
|
2019-07-26 21:54:25 +00:00
|
|
|
|
2018-09-14 10:20:28 +00:00
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
#![allow(stable_features)]
|
2013-09-20 00:59:06 +00:00
|
|
|
// Issue 4691: Ensure that functional-struct-updates operates
|
|
|
|
// correctly and moves rather than copy when appropriate.
|
|
|
|
|
2021-08-25 00:39:40 +00:00
|
|
|
#![feature(core)]
|
2015-01-08 01:25:56 +00:00
|
|
|
|
2015-08-12 00:27:05 +00:00
|
|
|
struct ncint { v: isize }
|
|
|
|
fn ncint(v: isize) -> ncint { ncint { v: v } }
|
2013-09-20 00:59:06 +00:00
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
struct NoFoo { copied: isize, nocopy: ncint, }
|
2013-09-20 00:59:06 +00:00
|
|
|
impl NoFoo {
|
2015-03-26 00:06:52 +00:00
|
|
|
fn new(x:isize,y:isize) -> NoFoo { NoFoo { copied: x, nocopy: ncint(y) } }
|
2013-09-20 00:59:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
struct MoveFoo { copied: isize, moved: Box<isize>, }
|
2013-09-20 00:59:06 +00:00
|
|
|
impl MoveFoo {
|
2021-08-25 00:39:40 +00:00
|
|
|
fn new(x:isize,y:isize) -> MoveFoo { MoveFoo { copied: x, moved: Box::new(y) } }
|
2013-09-20 00:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DropNoFoo { inner: NoFoo }
|
|
|
|
impl DropNoFoo {
|
2015-03-26 00:06:52 +00:00
|
|
|
fn new(x:isize,y:isize) -> DropNoFoo { DropNoFoo { inner: NoFoo::new(x,y) } }
|
2013-09-20 00:59:06 +00:00
|
|
|
}
|
|
|
|
impl Drop for DropNoFoo { fn drop(&mut self) { } }
|
|
|
|
|
|
|
|
struct DropMoveFoo { inner: MoveFoo }
|
|
|
|
impl DropMoveFoo {
|
2015-03-26 00:06:52 +00:00
|
|
|
fn new(x:isize,y:isize) -> DropMoveFoo { DropMoveFoo { inner: MoveFoo::new(x,y) } }
|
2013-09-20 00:59:06 +00:00
|
|
|
}
|
|
|
|
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
|
2020-12-28 17:15:16 +00:00
|
|
|
// test ui/borrowck/borrowck-struct-update-with-dtor.rs).
|
2013-09-20 00:59:06 +00:00
|
|
|
|
2014-03-26 23:01:11 +00:00
|
|
|
// Case 1: Nocopyable
|
2013-09-20 00:59:06 +00:00
|
|
|
let f = DropNoFoo::new(1, 2);
|
2014-03-26 23:01:11 +00:00
|
|
|
let b = DropNoFoo { inner: NoFoo { nocopy: ncint(3), ..f.inner }};
|
|
|
|
let c = DropNoFoo { inner: NoFoo { nocopy: ncint(4), ..f.inner }};
|
2013-09-20 00:59:06 +00:00
|
|
|
assert_eq!(f.inner.copied, 1);
|
2014-03-26 23:01:11 +00:00
|
|
|
assert_eq!(f.inner.nocopy.v, 2);
|
2013-09-20 00:59:06 +00:00
|
|
|
|
|
|
|
assert_eq!(b.inner.copied, 1);
|
2014-03-26 23:01:11 +00:00
|
|
|
assert_eq!(b.inner.nocopy.v, 3);
|
2013-09-20 00:59:06 +00:00
|
|
|
|
|
|
|
assert_eq!(c.inner.copied, 1);
|
2014-03-26 23:01:11 +00:00
|
|
|
assert_eq!(c.inner.nocopy.v, 4);
|
2013-09-20 00:59:06 +00:00
|
|
|
|
|
|
|
// Case 2: Owned
|
|
|
|
let f = DropMoveFoo::new(5, 6);
|
2021-08-25 00:39:40 +00:00
|
|
|
let b = DropMoveFoo { inner: MoveFoo { moved: Box::new(7), ..f.inner }};
|
|
|
|
let c = DropMoveFoo { inner: MoveFoo { moved: Box::new(8), ..f.inner }};
|
2013-09-20 00:59:06 +00:00
|
|
|
assert_eq!(f.inner.copied, 5);
|
|
|
|
assert_eq!(*f.inner.moved, 6);
|
|
|
|
|
2013-09-20 01:09:57 +00:00
|
|
|
assert_eq!(b.inner.copied, 5);
|
2013-09-20 00:59:06 +00:00
|
|
|
assert_eq!(*b.inner.moved, 7);
|
|
|
|
|
2013-09-20 01:09:57 +00:00
|
|
|
assert_eq!(c.inner.copied, 5);
|
2013-09-20 00:59:06 +00:00
|
|
|
assert_eq!(*c.inner.moved, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test1() {
|
|
|
|
// copying move-by-default fields from `f`, so it moves:
|
|
|
|
let f = MoveFoo::new(11, 12);
|
|
|
|
|
2021-08-25 00:39:40 +00:00
|
|
|
let b = MoveFoo {moved: Box::new(13), ..f};
|
2013-09-20 00:59:06 +00:00
|
|
|
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);
|
2014-03-26 23:01:11 +00:00
|
|
|
let b = NoFoo {nocopy: ncint(23), ..f};
|
2013-09-20 00:59:06 +00:00
|
|
|
let c = NoFoo {copied: 24, ..f};
|
|
|
|
assert_eq!(b.copied, 21);
|
2014-03-26 23:01:11 +00:00
|
|
|
assert_eq!(b.nocopy.v, 23);
|
2013-09-20 00:59:06 +00:00
|
|
|
assert_eq!(c.copied, 24);
|
2014-03-26 23:01:11 +00:00
|
|
|
assert_eq!(c.nocopy.v, 22);
|
2013-09-20 00:59:06 +00:00
|
|
|
}
|
|
|
|
|
2013-09-25 07:43:37 +00:00
|
|
|
pub fn main() {
|
2013-09-20 00:59:06 +00:00
|
|
|
test0();
|
|
|
|
test1();
|
|
|
|
test2();
|
|
|
|
}
|