Skip to content

Commit db640d5

Browse files
committed
tests
1 parent d3f51dc commit db640d5

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 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+
// Test method calls with self as an argument cannot subvert type checking.
12+
13+
struct Foo;
14+
15+
impl Foo {
16+
fn bar(&self) {}
17+
}
18+
19+
fn main() {
20+
let x = Foo;
21+
Foo::bar(x); //~ERROR mismatched types: expected `&Foo`, found `Foo`
22+
Foo::bar(&&x); //~ERROR mismatched types: expected `&Foo`, found `&&Foo`
23+
Foo::bar(&42i); //~ERROR mismatched types: expected `&Foo`, found `&int`
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 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+
// Test method calls with self as an argument cannot subvert borrow checking.
12+
13+
struct Foo;
14+
15+
impl Foo {
16+
fn bar(&self) {}
17+
fn baz(&mut self) {}
18+
}
19+
20+
fn main() {
21+
let mut x = Foo;
22+
let y = &mut x;
23+
Foo::bar(&x); //~ERROR cannot borrow `x`
24+
25+
let x = Foo;
26+
Foo::baz(&x); //~ERROR cannot borrow immutable dereference of `&`-pointer as mutable
27+
}

src/test/run-pass/method-self-arg.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 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+
// Test method calls with self as an argument
12+
13+
static mut COUNT: uint = 1;
14+
15+
struct Foo;
16+
17+
impl Foo {
18+
fn foo(self, x: &Foo) {
19+
unsafe { COUNT *= 2; }
20+
// Test internal call.
21+
Foo::bar(&self);
22+
Foo::bar(x);
23+
24+
Foo::baz(self);
25+
Foo::baz(*x);
26+
27+
Foo::qux(box self);
28+
Foo::qux(box *x);
29+
}
30+
31+
fn bar(&self) {
32+
unsafe { COUNT *= 3; }
33+
}
34+
35+
fn baz(self) {
36+
unsafe { COUNT *= 5; }
37+
}
38+
39+
fn qux(self: Box<Foo>) {
40+
unsafe { COUNT *= 7; }
41+
}
42+
}
43+
44+
fn main() {
45+
let x = Foo;
46+
// Test external call.
47+
Foo::bar(&x);
48+
Foo::baz(x);
49+
Foo::qux(box x);
50+
51+
x.foo(&x);
52+
53+
unsafe { assert!(COUNT == 2u*3*3*3*5*5*5*7*7*7); }
54+
}

0 commit comments

Comments
 (0)