Skip to content

Commit b402e34

Browse files
committed
tests: Add new tests for borrowck/objects and update some existing tests
1 parent df016dc commit b402e34

13 files changed

+265
-22
lines changed

src/test/run-pass/unique-object.rs renamed to src/test/compile-fail/borrowck-borrow-mut-object-twice.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,17 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
trait Foo {
12-
fn f(&self) -> int;
13-
}
11+
// Check that `&mut` objects cannot be borrowed twice, just like
12+
// other `&mut` pointers.
1413

15-
struct Bar {
16-
x: int
14+
trait Foo {
15+
fn f1<'a>(&'a mut self) -> &'a ();
16+
fn f2(&mut self);
1717
}
1818

19-
impl Foo for Bar {
20-
fn f(&self) -> int {
21-
self.x
22-
}
19+
fn test(x: &mut Foo) {
20+
let _y = x.f1();
21+
x.f2(); //~ ERROR cannot borrow `*x` as mutable more than once at a time
2322
}
2423

25-
pub fn main() {
26-
let x = ~Bar { x: 10 };
27-
let y = x as ~Foo;
28-
assert_eq!(y.f(), 10);
29-
}
24+
fn main() {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
trait Foo {
12+
fn borrowed<'a>(&'a self) -> &'a ();
13+
}
14+
15+
fn borrowed_receiver<'a>(x: &'a Foo) -> &'a () {
16+
x.borrowed()
17+
}
18+
19+
fn managed_receiver(x: @Foo) -> &() {
20+
x.borrowed() //~ ERROR cannot root managed value long enough
21+
}
22+
23+
fn managed_receiver_1(x: @Foo) {
24+
*x.borrowed()
25+
}
26+
27+
fn owned_receiver(x: ~Foo) -> &() {
28+
x.borrowed() //~ ERROR borrowed value does not live long enough
29+
}
30+
31+
fn mut_owned_receiver(mut x: ~Foo) {
32+
let _y = x.borrowed();
33+
let _z = &mut x; //~ ERROR cannot borrow
34+
}
35+
36+
fn imm_owned_receiver(mut x: ~Foo) {
37+
let _y = x.borrowed();
38+
let _z = &x;
39+
}
40+
41+
fn main() {}
42+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
trait Foo {
12+
fn borrowed(&self);
13+
fn borrowed_mut(&mut self);
14+
}
15+
16+
fn borrowed_receiver(x: &Foo) {
17+
x.borrowed();
18+
x.borrowed_mut(); //~ ERROR cannot borrow
19+
}
20+
21+
fn borrowed_mut_receiver(x: &mut Foo) {
22+
x.borrowed();
23+
x.borrowed_mut();
24+
}
25+
26+
fn managed_receiver(x: @Foo) {
27+
x.borrowed();
28+
x.borrowed_mut(); //~ ERROR cannot borrow
29+
}
30+
31+
fn managed_mut_receiver(x: @mut Foo) {
32+
x.borrowed();
33+
x.borrowed_mut();
34+
}
35+
36+
fn owned_receiver(x: ~Foo) {
37+
x.borrowed();
38+
x.borrowed_mut(); //~ ERROR cannot borrow
39+
}
40+
41+
fn mut_owned_receiver(mut x: ~Foo) {
42+
x.borrowed();
43+
x.borrowed_mut();
44+
}
45+
46+
fn main() {}
47+

src/test/compile-fail/kindck-owned-trait-contains.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,4 @@ fn main() {
3131
//~^ ERROR dereference of reference outside its lifetime
3232
//~^^ ERROR automatically borrowed pointer is not valid at the time of borrow
3333
//~^^^ ERROR lifetime of return value does not outlive the function call
34-
//~^^^^ ERROR cannot infer an appropriate lifetime
3534
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
trait Foo {
12+
fn borrowed(&self);
13+
fn borrowed_mut(&mut self);
14+
15+
fn managed(@self);
16+
fn managed_mut(@mut self);
17+
18+
fn owned(~self);
19+
}
20+
21+
fn borrowed_receiver(x: &Foo) {
22+
x.borrowed();
23+
x.borrowed_mut(); // See [1]
24+
x.managed(); //~ ERROR does not implement any method
25+
x.managed_mut(); //~ ERROR does not implement any method
26+
x.owned(); //~ ERROR does not implement any method
27+
}
28+
29+
fn borrowed_mut_receiver(x: &mut Foo) {
30+
x.borrowed();
31+
x.borrowed_mut();
32+
x.managed(); //~ ERROR does not implement any method
33+
x.managed_mut(); //~ ERROR does not implement any method
34+
x.owned(); //~ ERROR does not implement any method
35+
}
36+
37+
fn managed_receiver(x: @Foo) {
38+
x.borrowed();
39+
x.borrowed_mut(); // See [1]
40+
x.managed();
41+
x.managed_mut(); //~ ERROR does not implement any method
42+
x.owned(); //~ ERROR does not implement any method
43+
}
44+
45+
fn managed_mut_receiver(x: @mut Foo) {
46+
x.borrowed();
47+
x.borrowed_mut();
48+
x.managed(); //~ ERROR does not implement any method
49+
x.managed_mut();
50+
x.owned(); //~ ERROR does not implement any method
51+
}
52+
53+
fn owned_receiver(x: ~Foo) {
54+
x.borrowed();
55+
x.borrowed_mut(); // See [1]
56+
x.managed(); //~ ERROR does not implement any method
57+
x.managed_mut(); //~ ERROR does not implement any method
58+
x.owned();
59+
}
60+
61+
fn main() {}
62+
63+
// [1]: These cases are illegal, but the error is not detected
64+
// until borrowck, so see the test borrowck-object-mutability.rs

src/test/compile-fail/selftype-traittype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait add {
1313
}
1414

1515
fn do_add(x: @add, y: @add) -> @add {
16-
x.plus(y) //~ ERROR cannot call a method whose type contains a self-type through a boxed trait
16+
x.plus(y) //~ ERROR cannot call a method whose type contains a self-type through an object
1717
}
1818

1919
fn main() {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// error-pattern:borrowed
2+
3+
trait Foo {
4+
fn foo(&self, @mut int);
5+
}
6+
7+
impl Foo for int {
8+
fn foo(&self, x: @mut int) {
9+
*x += *self;
10+
}
11+
}
12+
13+
fn main() {
14+
let x = @mut 3_i;
15+
let y = x as @mut Foo;
16+
17+
// The call to `y.foo(...)` should freeze `y` (and thus also `x`,
18+
// since `x === y`). It is thus an error when `foo` tries to
19+
// mutate `x`.
20+
y.foo(x);
21+
}

src/test/run-pass/class-cast-to-trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat {
5656

5757

5858
pub fn main() {
59-
let mut nyan: @noisy = @cat(0u, 2, ~"nyan") as @noisy;
59+
let nyan: @mut noisy = @mut cat(0u, 2, ~"nyan") as @mut noisy;
6060
nyan.speak();
6161
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
// Test invoked `&self` methods on owned objects where the values
12+
// closed over contain managed values. This implies that the ~ boxes
13+
// will have headers that must be skipped over.
14+
15+
trait FooTrait {
16+
fn foo(&self) -> uint;
17+
}
18+
19+
struct BarStruct {
20+
x: @uint
21+
}
22+
23+
impl FooTrait for BarStruct {
24+
fn foo(&self) -> uint {
25+
*self.x
26+
}
27+
}
28+
29+
pub fn main() {
30+
let foos: ~[ ~FooTrait: ] = ~[
31+
~BarStruct{ x: @0 } as ~FooTrait:,
32+
~BarStruct{ x: @1 } as ~FooTrait:,
33+
~BarStruct{ x: @2 } as ~FooTrait:
34+
];
35+
36+
for i in range(0u, foos.len()) {
37+
assert_eq!(i, foos[i].foo());
38+
}
39+
}

src/test/run-pass/owned-trait-objects.rs renamed to src/test/run-pass/objects-owned-object-borrowed-method-headerless.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// Test invoked `&self` methods on owned objects where the values
12+
// closed over do not contain managed values, and thus the ~ boxes do
13+
// not have headers.
14+
1115
trait FooTrait {
1216
fn foo(&self) -> uint;
1317
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
// Test invoked `&self` methods on owned objects where the values
12+
// closed over contain managed values. This implies that the ~ boxes
13+
// will have headers that must be skipped over.
14+
15+
trait FooTrait {
16+
fn foo(~self) -> uint;
17+
}
18+
19+
struct BarStruct {
20+
x: uint
21+
}
22+
23+
impl FooTrait for BarStruct {
24+
fn foo(~self) -> uint {
25+
self.x
26+
}
27+
}
28+
29+
pub fn main() {
30+
let foo = ~BarStruct{ x: 22 } as ~FooTrait;
31+
assert_eq!(22, foo.foo());
32+
}

src/test/run-pass/reflect-visit-data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl my_visitor {
502502
unsafe {
503503
let u = my_visitor(**self);
504504
let v = ptr_visit_adaptor::<my_visitor>(Inner {inner: u});
505-
visit_tydesc(inner, @v as @TyVisitor);
505+
visit_tydesc(inner, &v as &TyVisitor);
506506
true
507507
}
508508
}
@@ -662,7 +662,7 @@ pub fn main() {
662662
let td = get_tydesc_for(r);
663663
error!("tydesc sz: %u, align: %u",
664664
(*td).size, (*td).align);
665-
let v = @v as @TyVisitor;
665+
let v = &v as &TyVisitor;
666666
visit_tydesc(td, v);
667667

668668
let r = u.vals.clone();

src/test/run-pass/reflect-visit-type.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ impl TyVisitor for MyVisitor {
7979
fn visit_evec_uniq(&self, _mtbl: uint, inner: *TyDesc) -> bool {
8080
self.types.push(~"[");
8181
unsafe {
82-
visit_tydesc(inner, (@*self) as @TyVisitor);
82+
visit_tydesc(inner, (&*self) as &TyVisitor);
8383
}
8484
self.types.push(~"]");
8585
true
8686
}
8787
fn visit_evec_uniq_managed(&self, _mtbl: uint, inner: *TyDesc) -> bool {
8888
self.types.push(~"[");
8989
unsafe {
90-
visit_tydesc(inner, (@*self) as @TyVisitor);
90+
visit_tydesc(inner, (&*self) as &TyVisitor);
9191
}
9292
self.types.push(~"]");
9393
true
@@ -154,7 +154,7 @@ impl TyVisitor for MyVisitor {
154154
fn visit_closure_ptr(&self, _ck: uint) -> bool { true }
155155
}
156156

157-
fn visit_ty<T>(v: @TyVisitor) {
157+
fn visit_ty<T>(v: &TyVisitor) {
158158
unsafe {
159159
visit_tydesc(get_tydesc::<T>(), v);
160160
}

0 commit comments

Comments
 (0)