Skip to content

Commit 1670a8c

Browse files
committed
Add new tests for irrefutable patterns used in various tricky ways
1 parent 17b3712 commit 1670a8c

9 files changed

+156
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn with(f: &fn(&~str)) {}
2+
3+
fn arg_item(&_x: &~str) {}
4+
//~^ ERROR cannot move out of dereference of & pointer
5+
6+
fn arg_closure() {
7+
with(|&_x| ())
8+
//~^ ERROR cannot move out of dereference of & pointer
9+
}
10+
11+
fn let_pat() {
12+
let &_x = &~"hi";
13+
//~^ ERROR cannot move out of dereference of & pointer
14+
}
15+
16+
pub fn main() {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct S {f:~str}
2+
impl Drop for S {
3+
fn finalize(&self) { println(self.f); }
4+
}
5+
6+
fn move_in_match() {
7+
match S {f:~"foo"} {
8+
S {f:_s} => {}
9+
//~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
10+
}
11+
}
12+
13+
fn move_in_let() {
14+
let S {f:_s} = S {f:~"foo"};
15+
//~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
16+
}
17+
18+
fn move_in_fn_arg(S {f:_s}: S) {
19+
//~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
20+
}
21+
22+
fn main() {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct S(~str);
2+
impl Drop for S {
3+
fn finalize(&self) { println(**self); }
4+
}
5+
6+
fn move_in_match() {
7+
match S(~"foo") {
8+
S(_s) => {}
9+
//~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
10+
}
11+
}
12+
13+
fn move_in_let() {
14+
let S(_s) = S(~"foo");
15+
//~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
16+
}
17+
18+
fn move_in_fn_arg(S(_s): S) {
19+
//~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
20+
}
21+
22+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn arg_item(~ref x: ~int) -> &'static int {
2+
x //~^ ERROR borrowed value does not live long enough
3+
}
4+
5+
fn with<R>(f: &fn(~int) -> R) -> R { f(~3) }
6+
7+
fn arg_closure() -> &'static int {
8+
with(|~ref x| x) //~ ERROR borrowed value does not live long enough
9+
}
10+
11+
fn main() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Test that we do not leak when the arg pattern must drop part of the
2+
// argument (in this case, the `y` field).
3+
4+
struct Foo {
5+
x: ~uint,
6+
y: ~uint,
7+
}
8+
9+
fn foo(Foo {x, _}: Foo) -> *uint {
10+
let addr: *uint = &*x;
11+
addr
12+
}
13+
14+
fn main() {
15+
let obj = ~1;
16+
let objptr: *uint = &*obj;
17+
let f = Foo {x: obj, y: ~2};
18+
let xptr = foo(f);
19+
assert_eq!(objptr, xptr);
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// exec-env:RUST_POISON_ON_FREE=1
2+
3+
// Test argument patterns where we create refs to the inside of `~`
4+
// boxes. Make sure that we don't free the box as we match the
5+
// pattern.
6+
7+
fn getaddr(~ref x: ~uint) -> *uint {
8+
let addr: *uint = &*x;
9+
addr
10+
}
11+
12+
fn checkval(~ref x: ~uint) -> uint {
13+
*x
14+
}
15+
16+
fn main() {
17+
let obj = ~1;
18+
let objptr: *uint = &*obj;
19+
let xptr = getaddr(obj);
20+
assert_eq!(objptr, xptr);
21+
22+
let obj = ~22;
23+
assert_eq!(checkval(obj), 22);
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test that we can compile code that uses a `_` in function argument
2+
// patterns.
3+
4+
fn foo((x, _): (int, int)) -> int {
5+
x
6+
}
7+
8+
fn main() {
9+
assert_eq!(foo((22, 23)), 22);
10+
}

src/test/run-pass/let-destruct-ref.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let x = ~"hello";
3+
let ref y = x;
4+
assert_eq!(x.slice(0, x.len()), y.slice(0, y.len()));
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Tests a tricky scenario involving string matching,
2+
// copying, and moving to ensure that we don't segfault
3+
// or double-free, as we were wont to do in the past.
4+
5+
use std::io;
6+
7+
fn parse_args() -> ~str {
8+
let args = std::os::args();
9+
let mut n = 0;
10+
11+
while n < args.len() {
12+
match copy args[n] {
13+
~"-v" => (),
14+
s => {
15+
return s;
16+
}
17+
}
18+
n += 1;
19+
}
20+
21+
return ~""
22+
}
23+
24+
fn main() {
25+
io::println(parse_args());
26+
}

0 commit comments

Comments
 (0)