Skip to content

Commit c1f037e

Browse files
committed
Add tests for once functions (rust-lang#2549).
1 parent 1496216 commit c1f037e

7 files changed

+190
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 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+
// Testing guarantees provided by once functions.
12+
// This program would segfault if it were legal.
13+
14+
extern mod extra;
15+
use extra::arc;
16+
use std::util;
17+
18+
fn foo(blk: ~once fn()) {
19+
blk();
20+
blk(); //~ ERROR use of moved value
21+
}
22+
23+
fn main() {
24+
let x = arc::ARC(true);
25+
do foo {
26+
assert!(*x.get());
27+
util::ignore(x);
28+
}
29+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 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+
// Testing guarantees provided by once functions.
12+
// This program would segfault if it were legal.
13+
14+
extern mod extra;
15+
use extra::arc;
16+
use std::util;
17+
18+
fn foo(blk: &once fn()) {
19+
blk();
20+
blk(); //~ ERROR use of moved value
21+
}
22+
23+
fn main() {
24+
let x = arc::ARC(true);
25+
do foo {
26+
assert!(*x.get());
27+
util::ignore(x);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 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+
// Though it should be legal to copy a heap-allocated "once fn:Copy",
12+
// stack closures are not deep-copied, so (counterintuitively) it should be
13+
// illegal to copy them.
14+
15+
fn foo<'r>(blk: &'r once fn:Copy()) -> (&'r once fn:Copy(), &'r once fn:Copy()) {
16+
(copy blk, blk) //~ ERROR copying a value of non-copyable type
17+
}
18+
19+
fn main() {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 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+
// Testing guarantees provided by once functions.
12+
// This program would segfault if it were legal.
13+
14+
extern mod extra;
15+
use extra::arc;
16+
use std::util;
17+
18+
fn foo(blk: ~fn()) {
19+
blk();
20+
blk();
21+
}
22+
23+
fn main() {
24+
let x = arc::ARC(true);
25+
do foo {
26+
assert!(*x.get());
27+
util::ignore(x); //~ ERROR cannot move out of captured outer variable
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 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+
// Testing guarantees provided by once functions.
12+
// This program would segfault if it were legal.
13+
14+
extern mod extra;
15+
use extra::arc;
16+
use std::util;
17+
18+
fn foo(blk: &fn()) {
19+
blk();
20+
blk();
21+
}
22+
23+
fn main() {
24+
let x = arc::ARC(true);
25+
do foo {
26+
assert!(*x.get());
27+
util::ignore(x); //~ ERROR cannot move out of captured outer variable
28+
}
29+
}

Diff for: src/test/run-pass/once-move-out-on-heap.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 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+
// Testing guarantees provided by once functions.
12+
13+
extern mod extra;
14+
use extra::arc;
15+
use std::util;
16+
17+
fn foo(blk: ~once fn()) {
18+
blk();
19+
}
20+
21+
fn main() {
22+
let x = arc::ARC(true);
23+
do foo {
24+
assert!(*x.get());
25+
util::ignore(x);
26+
}
27+
}

Diff for: src/test/run-pass/once-move-out-on-stack.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 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+
// Testing guarantees provided by once functions.
12+
13+
extern mod extra;
14+
use extra::arc;
15+
use std::util;
16+
17+
fn foo(blk: &once fn()) {
18+
blk();
19+
}
20+
21+
fn main() {
22+
let x = arc::ARC(true);
23+
do foo {
24+
assert!(*x.get());
25+
util::ignore(x);
26+
}
27+
}

0 commit comments

Comments
 (0)