Skip to content

Commit ce341f7

Browse files
committed
Add tests of move-out-of-array restriction.
1 parent cc8f35f commit ce341f7

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2015 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+
// Ensure that we cannot move out of a fixed-size array (especially
12+
// when the element type has a destructor).
13+
14+
15+
struct D { _x: u8 }
16+
17+
impl Drop for D { fn drop(&mut self) { } }
18+
19+
fn main() {
20+
fn d() -> D { D { _x: 0 } }
21+
22+
let _d1 = foo([d(), d(), d(), d()], 1);
23+
let _d3 = foo([d(), d(), d(), d()], 3);
24+
}
25+
26+
fn foo(a: [D; 4], i: usize) -> D {
27+
a[i] //~ ERROR cannot move out of type `[D; 4]`
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2015 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+
// Ensure that we can copy out of a fixed-size array.
12+
//
13+
// (Compare with compile-fail/move-out-of-array-1.rs)
14+
15+
struct C { _x: u8 }
16+
17+
impl Copy for C { }
18+
19+
fn main() {
20+
fn d() -> C { C { _x: 0 } }
21+
22+
let _d1 = foo([d(), d(), d(), d()], 1);
23+
let _d3 = foo([d(), d(), d(), d()], 3);
24+
}
25+
26+
fn foo(a: [C; 4], i: usize) -> C {
27+
a[i]
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2015 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+
// Ensure that we can do a destructuring bind of a fixed-size array,
12+
// even when the element type has a destructor.
13+
14+
struct D { x: u8 }
15+
16+
impl Drop for D { fn drop(&mut self) { } }
17+
18+
fn main() {
19+
fn d(x: u8) -> D { D { x: x } }
20+
21+
let d1 = foo([d(1), d(2), d(3), d(4)], 1);
22+
let d3 = foo([d(5), d(6), d(7), d(8)], 3);
23+
assert_eq!(d1.x, 2);
24+
assert_eq!(d3.x, 8);
25+
}
26+
27+
fn foo([a, b, c, d]: [D; 4], i: usize) -> D {
28+
match i {
29+
0 => a,
30+
1 => b,
31+
2 => c,
32+
3 => d,
33+
_ => panic!("unmatched"),
34+
}
35+
}

0 commit comments

Comments
 (0)