Skip to content

Commit 373b0fc

Browse files
committed
some extra test cases to cover in the borrow checker.
1 parent 95fdbee commit 373b0fc

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2012-2014 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+
struct S;
12+
// Ensure S is moved, not copied, on assignment.
13+
impl Drop for S { fn drop(&mut self) { } }
14+
15+
// user-defined function "returning" bottom (i.e. no return at all).
16+
fn my_fail() -> ! { loop {} }
17+
18+
pub fn step(f: bool) {
19+
let mut g = S;
20+
let mut i = 0;
21+
loop
22+
{
23+
if i > 10 { break; } else { i += 1; }
24+
25+
let _g = g;
26+
27+
if f {
28+
// re-initialize g, but only before restarting loop.
29+
g = S;
30+
continue;
31+
}
32+
33+
my_fail();
34+
35+
// we never get here, so we do not need to re-initialize g.
36+
}
37+
}
38+
39+
pub fn main() {
40+
step(true);
41+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2012 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+
#[deriving(PartialEq, Show)]
12+
struct Partial<T> { x: T, y: T }
13+
14+
#[deriving(PartialEq, Show)]
15+
struct S { val: int }
16+
impl S { fn new(v: int) -> S { S { val: v } } }
17+
impl Drop for S { fn drop(&mut self) { } }
18+
19+
pub fn f<T>((b1, b2): (T, T), f: |T| -> T) -> Partial<T> {
20+
let p = Partial { x: b1, y: b2 };
21+
22+
// Move of `p` is legal even though we are also moving `p.y`; the
23+
// `..p` moves all fields *except* `p.y` in this context.
24+
Partial { y: f(p.y), ..p }
25+
}
26+
27+
pub fn main() {
28+
let p = f((S::new(3), S::new(4)), |S { val: z }| S::new(z+1));
29+
assert_eq!(p, Partial { x: S::new(3), y: S::new(5) });
30+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2012 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+
#[deriving(PartialEq, Show)]
12+
struct Partial<T> { x: T, y: T }
13+
14+
#[deriving(PartialEq, Show)]
15+
struct S { val: int }
16+
impl S { fn new(v: int) -> S { S { val: v } } }
17+
impl Drop for S { fn drop(&mut self) { } }
18+
19+
type Two<T> = (Partial<T>, Partial<T>);
20+
21+
pub fn f<T>((b1, b2): (T, T), (b3, b4): (T, T), f: |T| -> T) -> Two<T> {
22+
let p = Partial { x: b1, y: b2 };
23+
let q = Partial { x: b3, y: b4 };
24+
25+
// Move of `q` is legal even though we have already moved `q.y`;
26+
// the `..q` moves all fields *except* `q.y` in this context.
27+
// Likewise, the move of `p.x` is legal for similar reasons.
28+
(Partial { x: f(q.y), ..p }, Partial { y: f(p.x), ..q })
29+
}
30+
31+
pub fn main() {
32+
let two = f((S::new(1), S::new(3)),
33+
(S::new(5), S::new(7)),
34+
|S { val: z }| S::new(z+1));
35+
assert_eq!(two, (Partial { x: S::new(8), y: S::new(3) },
36+
Partial { x: S::new(5), y: S::new(2) }));
37+
}

0 commit comments

Comments
 (0)