diff options
| author | Gianni Ciccarelli <gianni.ciccarelli@gmail.com> | 2017-10-12 17:38:44 +0000 |
|---|---|---|
| committer | Gianni Ciccarelli <gianni.ciccarelli@gmail.com> | 2018-02-07 17:42:29 +0000 |
| commit | f5c55ff379913b7acca2ae032120e74aaa4dd822 (patch) | |
| tree | 2e9a56c461b7aab73a1c6a6e371dbeb3c25660df /src/test/compile-fail/specialization/defaultimpl | |
| parent | 29c8276cee4a0eab7e0634ff25c6b47bd9f87c6c (diff) | |
support `default impl` for specialization
a `default impl` need not include all items from the trait
a `default impl` alone does not mean that a type implements the trait
Diffstat (limited to 'src/test/compile-fail/specialization/defaultimpl')
11 files changed, 362 insertions, 0 deletions
diff --git a/src/test/compile-fail/specialization/defaultimpl/auxiliary/foo_trait.rs b/src/test/compile-fail/specialization/defaultimpl/auxiliary/foo_trait.rs new file mode 100644 index 00000000000..263f316f3c8 --- /dev/null +++ b/src/test/compile-fail/specialization/defaultimpl/auxiliary/foo_trait.rs @@ -0,0 +1,16 @@ +// Copyright 2014 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(specialization)] + +pub trait Foo { + fn foo_one(&self) -> &'static str; + fn foo_two(&self) -> &'static str; +} diff --git a/src/test/compile-fail/specialization/defaultimpl/auxiliary/foo_trait_default_impl.rs b/src/test/compile-fail/specialization/defaultimpl/auxiliary/foo_trait_default_impl.rs new file mode 100644 index 00000000000..cee6fcf7d9a --- /dev/null +++ b/src/test/compile-fail/specialization/defaultimpl/auxiliary/foo_trait_default_impl.rs @@ -0,0 +1,22 @@ +// Copyright 2014 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(specialization)] + +pub trait Foo { + fn foo_one(&self) -> &'static str; + fn foo_two(&self) -> &'static str; +} + +default impl<T> Foo for T { + fn foo_one(&self) -> &'static str { + "generic" + } +} diff --git a/src/test/compile-fail/specialization/defaultimpl/specialization-method-not-implemented-cross-crate-impl.rs b/src/test/compile-fail/specialization/defaultimpl/specialization-method-not-implemented-cross-crate-impl.rs new file mode 100644 index 00000000000..54f6690aa9f --- /dev/null +++ b/src/test/compile-fail/specialization/defaultimpl/specialization-method-not-implemented-cross-crate-impl.rs @@ -0,0 +1,23 @@ +// Copyright 2014 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. + +// aux-build:foo_trait_default_impl.rs + +#![feature(specialization)] + +extern crate foo_trait_default_impl; + +use foo_trait_default_impl::*; + +struct MyStruct; + +fn main() { + MyStruct.foo_two(); //~ NOTE the function call is here +} diff --git a/src/test/compile-fail/specialization/defaultimpl/specialization-method-not-implemented-cross-crate.rs b/src/test/compile-fail/specialization/defaultimpl/specialization-method-not-implemented-cross-crate.rs new file mode 100644 index 00000000000..8e2de42a099 --- /dev/null +++ b/src/test/compile-fail/specialization/defaultimpl/specialization-method-not-implemented-cross-crate.rs @@ -0,0 +1,30 @@ +// Copyright 2014 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. + +// aux-build:foo_trait.rs + +#![feature(specialization)] + +extern crate foo_trait; + +use foo_trait::{Foo}; + +struct MyStruct; + +default impl Foo for MyStruct { + fn foo_one(&self) -> &'static str { + "generic" + } +} +//~^^^^^ HELP implement it inside this `default impl` + +fn main() { + MyStruct.foo_two(); //~ NOTE the function call is here +} diff --git a/src/test/compile-fail/specialization/defaultimpl/specialization-method-not-implemented.rs b/src/test/compile-fail/specialization/defaultimpl/specialization-method-not-implemented.rs new file mode 100644 index 00000000000..d0db6e996d8 --- /dev/null +++ b/src/test/compile-fail/specialization/defaultimpl/specialization-method-not-implemented.rs @@ -0,0 +1,30 @@ +// Copyright 2014 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(specialization)] + +trait Foo { + fn foo_one(&self) -> &'static str; + fn foo_two(&self) -> &'static str; +} +//~^^^^ HELP provide a default method implementation inside this `trait` + +default impl<T> Foo for T { + fn foo_one(&self) -> &'static str { + "generic" + } +} +//~^^^^^ HELP implement it inside this `default impl` + +struct MyStruct; + +fn main() { + MyStruct.foo_two(); //~ NOTE the function call is here +} diff --git a/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-casting.rs b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-casting.rs new file mode 100644 index 00000000000..81b85f58998 --- /dev/null +++ b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-casting.rs @@ -0,0 +1,47 @@ +// Copyright 2014 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. + +// error-pattern: the trait bound `MyStruct: Draw` is not satisfied + +#![feature(specialization)] + +trait Draw { + fn draw(&self); + fn draw2(&self); +} + +struct Screen { + pub components: Vec<Box<Draw>>, +} + +impl Screen { + pub fn run(&self) { + for component in self.components.iter() { + component.draw(); + } + } +} + +default impl<T> Draw for T { + fn draw(&self) { + println!("draw"); + } +} + +struct MyStruct; + +fn main() { + let screen = Screen { + components: vec![ + Box::new(MyStruct) + ] + }; + screen.run(); +} diff --git a/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-fn.rs b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-fn.rs new file mode 100644 index 00000000000..00cceeb4db3 --- /dev/null +++ b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-fn.rs @@ -0,0 +1,34 @@ +// Copyright 2014 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. + +// error-pattern: the trait bound `MyStruct: Foo` is not satisfied + +#![feature(specialization)] + +trait Foo { + fn foo_one(&self) -> &'static str; + fn foo_two(&self) -> &'static str; +} + +default impl<T> Foo for T { + fn foo_one(&self) -> &'static str { + "generic" + } +} + +fn foo<T: Foo>(x: T) -> &'static str { + x.foo_one() +} + +struct MyStruct; + +fn main() { + println!("{:?}", foo(MyStruct)); +} diff --git a/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-impl-item.rs b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-impl-item.rs new file mode 100644 index 00000000000..51a6a9e2c6b --- /dev/null +++ b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-impl-item.rs @@ -0,0 +1,38 @@ +// Copyright 2014 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. + +// error-pattern: the trait bound `MyStruct: Foo` is not satisfied + +#![feature(specialization)] + +trait Foo { + fn foo_one(&self) -> &'static str; + fn foo_two(&self) -> &'static str; +} + +default impl<T> Foo for T { + fn foo_one(&self) -> &'static str { + "generic" + } +} + +struct FooS; + +impl FooS{ + fn foo<T: Foo>(&self, x: T) -> &'static str{ + x.foo_one() + } +} + +struct MyStruct; + +fn main() { + println!("{:?}", FooS.foo(MyStruct)); +} \ No newline at end of file diff --git a/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-super-trait.rs b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-super-trait.rs new file mode 100644 index 00000000000..3444dea39c2 --- /dev/null +++ b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-super-trait.rs @@ -0,0 +1,40 @@ +// Copyright 2014 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. + +// error-pattern: the trait bound `MyStruct: SuperFoo` is not satisfied + +#![feature(specialization)] + +trait SuperFoo { + fn super_foo_one(&self) -> &'static str; + fn super_foo_two(&self) -> &'static str; +} + +trait Foo: SuperFoo { + fn foo(&self) -> &'static str; +} + +default impl<T> SuperFoo for T { + fn super_foo_one(&self) -> &'static str { + "generic" + } +} + +struct MyStruct; + +impl Foo for MyStruct { + fn foo(&self) -> &'static str { + "foo" + } +} + +fn main() { + println!("{:?}", MyStruct.foo()); +} \ No newline at end of file diff --git a/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-trait-item.rs b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-trait-item.rs new file mode 100644 index 00000000000..6af69e89316 --- /dev/null +++ b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-trait-item.rs @@ -0,0 +1,34 @@ +// Copyright 2014 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(specialization)] + +trait Foo<T> { + fn dummy(&self, t: T); +} + +trait Bar<A> { + fn method<B>(&self) where A: Foo<B>; +} + +struct S; +struct X; + +default impl Foo<X> for X {} + +impl Bar<X> for isize { + fn method<U>(&self) where X: Foo<U> { + } +} + +fn main() { + 1.method::<X>(); + //~^ ERROR the trait bound `X: Foo<X>` is not satisfied +} diff --git a/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-vec.rs b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-vec.rs new file mode 100644 index 00000000000..a2ea087220f --- /dev/null +++ b/src/test/compile-fail/specialization/defaultimpl/specialization-trait-bounds-vec.rs @@ -0,0 +1,48 @@ +// Copyright 2014 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. + +// error-pattern: the trait bound `MyStruct: Draw` is not satisfied + +#![feature(specialization)] + +trait Draw { + fn draw(&self); + fn draw2(&self); +} + +struct Screen<T: Draw> { + pub components: Vec<T>, +} + +impl<T> Screen<T> + where T: Draw { + pub fn run(&self) { + for component in self.components.iter() { + component.draw(); + } + } +} + +default impl Draw for MyStruct { + fn draw(&self) { + println!("draw"); + } +} + +struct MyStruct; + +fn main() { + let screen = Screen { + components: vec![ + MyStruct + ] + }; + screen.run(); +} |
