forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for trait object coercion.
- Loading branch information
Showing
5 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#[feature(managed_boxes)]; | ||
|
||
struct Struct { | ||
person: &'static str | ||
} | ||
|
||
trait Trait<T> { | ||
fn f(&self, x: T); | ||
} | ||
|
||
impl Trait<&'static str> for Struct { | ||
fn f(&self, x: &'static str) { | ||
println!("Hello, {}!", x); | ||
} | ||
} | ||
|
||
fn main() { | ||
let s: @Trait<int> = @Struct { person: "Fred" }; //~ ERROR expected Trait<int>, but found Trait<&'static str> | ||
//~^ ERROR expected Trait<int>, but found Trait<&'static str> | ||
s.f(1); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#[feature(managed_boxes)]; | ||
|
||
struct Struct { | ||
person: &'static str | ||
} | ||
|
||
trait Trait<T> { | ||
fn f(&self, x: T); | ||
} | ||
|
||
impl Trait<&'static str> for Struct { | ||
fn f(&self, x: &'static str) { | ||
println!("Hello, {}!", x); | ||
} | ||
} | ||
|
||
fn main() { | ||
let person = ~"Fred"; | ||
let person: &str = person; //~ ERROR borrowed value does not live long enough | ||
let s: @Trait<&'static str> = @Struct { person: person }; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#[feature(managed_boxes)]; | ||
|
||
trait Trait<T> { | ||
fn f(&self, x: T); | ||
} | ||
|
||
struct Struct { | ||
x: int, | ||
y: int, | ||
} | ||
|
||
impl Trait<&'static str> for Struct { | ||
fn f(&self, x: &'static str) { | ||
println(~"Hi, " + x + ~"!"); | ||
} | ||
} | ||
fn f(x: @Trait<&'static str>) { | ||
x.f("Sue"); | ||
} | ||
|
||
pub fn main() { | ||
let a = Struct { x: 1, y: 2 }; | ||
let b: @Trait<&'static str> = @a; | ||
b.f("Fred"); | ||
let c: ~Trait<&'static str> = ~a; | ||
c.f("Mary"); | ||
let d: &Trait<&'static str> = &a; | ||
d.f("Joe"); | ||
f(@a); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#[feature(managed_boxes)]; | ||
|
||
trait Trait { | ||
fn f(&self); | ||
} | ||
|
||
struct Struct { | ||
x: int, | ||
y: int, | ||
} | ||
|
||
impl Trait for Struct { | ||
fn f(&self) { | ||
println("Hi!"); | ||
} | ||
} | ||
|
||
fn f(x: @Trait) { | ||
x.f(); | ||
} | ||
|
||
pub fn main() { | ||
let a = Struct { x: 1, y: 2 }; | ||
let b: @Trait = @a; | ||
b.f(); | ||
let c: ~Trait = ~a; | ||
c.f(); | ||
let d: &Trait = &a; | ||
d.f(); | ||
f(@a); | ||
} | ||
|