From 981c6b12fad24ef28998668e515e62976f72b3bf Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Fri, 27 Dec 2013 00:38:28 -0500 Subject: [PATCH] Add tests for trait object coercion. --- src/test/compile-fail/map-types.rs | 4 +- .../trait-coercion-generic-bad.rs | 32 ++++++++++++++ .../trait-coercion-generic-regions.rs | 32 ++++++++++++++ src/test/run-pass/trait-coercion-generic.rs | 42 +++++++++++++++++++ src/test/run-pass/trait-coercion.rs | 42 +++++++++++++++++++ 5 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 src/test/compile-fail/trait-coercion-generic-bad.rs create mode 100644 src/test/compile-fail/trait-coercion-generic-regions.rs create mode 100644 src/test/run-pass/trait-coercion-generic.rs create mode 100644 src/test/run-pass/trait-coercion.rs diff --git a/src/test/compile-fail/map-types.rs b/src/test/compile-fail/map-types.rs index c2c4a0b6908e6..0a1eab913be19 100644 --- a/src/test/compile-fail/map-types.rs +++ b/src/test/compile-fail/map-types.rs @@ -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 = @x; - //~^ ERROR expected trait std::container::Map but found @-ptr + //~^ ERROR failed to find an implementation of trait std::container::Map for @std::container::Map<~str,~str>:'static } diff --git a/src/test/compile-fail/trait-coercion-generic-bad.rs b/src/test/compile-fail/trait-coercion-generic-bad.rs new file mode 100644 index 0000000000000..2d73158add2e1 --- /dev/null +++ b/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 or the MIT license +// , 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 { + 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 = @Struct { person: "Fred" }; //~ ERROR expected Trait, but found Trait<&'static str> + //~^ ERROR expected Trait, but found Trait<&'static str> + s.f(1); +} + diff --git a/src/test/compile-fail/trait-coercion-generic-regions.rs b/src/test/compile-fail/trait-coercion-generic-regions.rs new file mode 100644 index 0000000000000..1ea18a7c75b4d --- /dev/null +++ b/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 or the MIT license +// , 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 { + 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 }; +} + diff --git a/src/test/run-pass/trait-coercion-generic.rs b/src/test/run-pass/trait-coercion-generic.rs new file mode 100644 index 0000000000000..415f7baf3d9cc --- /dev/null +++ b/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 or the MIT license +// , 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, 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); +} + diff --git a/src/test/run-pass/trait-coercion.rs b/src/test/run-pass/trait-coercion.rs new file mode 100644 index 0000000000000..1c6968266e18d --- /dev/null +++ b/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 or the MIT license +// , 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); +} +