Skip to content

Commit

Permalink
Add tests for trait object coercion.
Browse files Browse the repository at this point in the history
  • Loading branch information
luqmana committed Dec 27, 2013
1 parent 1265a03 commit 981c6b1
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/test/compile-fail/map-types.rs
Expand Up @@ -17,7 +17,7 @@ use std::hashmap::HashMap;

fn main() {
let x: @HashMap<~str, ~str> = @HashMap::new();
let x: @Map<~str, ~str> = x as @Map<~str, ~str>;
let x: @Map<~str, ~str> = x;
let y: @Map<uint, ~str> = @x;
//~^ ERROR expected trait std::container::Map but found @-ptr
//~^ ERROR failed to find an implementation of trait std::container::Map<uint,~str> for @std::container::Map<~str,~str>:'static
}
32 changes: 32 additions & 0 deletions src/test/compile-fail/trait-coercion-generic-bad.rs
@@ -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);
}

32 changes: 32 additions & 0 deletions src/test/compile-fail/trait-coercion-generic-regions.rs
@@ -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 };
}

42 changes: 42 additions & 0 deletions src/test/run-pass/trait-coercion-generic.rs
@@ -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);
}

42 changes: 42 additions & 0 deletions src/test/run-pass/trait-coercion.rs
@@ -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);
}

13 comments on commit 981c6b1

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at luqmana@981c6b1

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging luqmana/rust/trait-object-coercion = 981c6b1 into auto

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

luqmana/rust/trait-object-coercion = 981c6b1 merged ok, testing candidate = cea4d138

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at luqmana@981c6b1

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging luqmana/rust/trait-object-coercion = 981c6b1 into auto

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

luqmana/rust/trait-object-coercion = 981c6b1 merged ok, testing candidate = c34623d7

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from pcwalton
at luqmana@981c6b1

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging luqmana/rust/trait-object-coercion = 981c6b1 into auto

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

luqmana/rust/trait-object-coercion = 981c6b1 merged ok, testing candidate = 1a9c8cc

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 981c6b1 Dec 27, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 1a9c8cc

Please sign in to comment.