Skip to content

Commit 7855893

Browse files
committed
Add a bunch of new tests per Alex's suggestion.
1 parent 5fe0ad1 commit 7855893

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
// Simple smoke test that unsafe traits can be compiled etc.
12+
13+
pub unsafe trait Foo {
14+
fn foo(&self) -> int;
15+
}
16+
17+
unsafe impl Foo for int {
18+
fn foo(&self) -> int { *self }
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
// aux-build:trait-safety-lib.rs
12+
13+
// Check that unsafe traits require unsafe impls and that inherent
14+
// impls cannot be unsafe.
15+
16+
extern crate "trait-safety-lib" as lib;
17+
18+
struct Bar;
19+
impl lib::Foo for Bar { //~ ERROR requires an `unsafe impl` declaration
20+
fn foo(&self) -> int {
21+
*self as int
22+
}
23+
}
24+
25+
fn main() { }
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
// aux-build:trait-safety-lib.rs
12+
13+
// Simple smoke test that unsafe traits can be compiled across crates.
14+
15+
extern crate "trait-safety-lib" as lib;
16+
17+
use lib::Foo;
18+
19+
struct Bar { x: int }
20+
unsafe impl Foo for Bar {
21+
fn foo(&self) -> int { self.x }
22+
}
23+
24+
fn take_foo<F:Foo>(f: &F) -> int { f.foo() }
25+
26+
fn main() {
27+
let x: int = 22;
28+
assert_eq!(22, take_foo(&x));
29+
30+
let x: Bar = Bar { x: 23 };
31+
assert_eq!(23, take_foo(&x));
32+
}

src/test/run-pass/trait-safety-ok.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
// Simple smoke test that unsafe traits can be compiled etc.
12+
13+
unsafe trait Foo {
14+
fn foo(&self) -> int;
15+
}
16+
17+
unsafe impl Foo for int {
18+
fn foo(&self) -> int { *self }
19+
}
20+
21+
fn take_foo<F:Foo>(f: &F) -> int { f.foo() }
22+
23+
fn main() {
24+
let x: int = 22;
25+
assert_eq!(22, take_foo(&x));
26+
}

0 commit comments

Comments
 (0)