Add tests for specialization on associated types

This commit is contained in:
Aaron Turon 2015-12-29 14:01:30 -08:00
parent b7e5112e88
commit a3825827b7
6 changed files with 199 additions and 0 deletions

View File

@ -0,0 +1,32 @@
// Copyright 2015 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.
// Make sure we can't project defaulted associated types
trait Foo {
type Assoc;
}
impl<T> Foo for T {
default type Assoc = ();
}
impl Foo for u8 {
type Assoc = String;
}
fn generic<T>() -> <T as Foo>::Assoc {
() //~ ERROR
}
fn main() {
let s: String = generic::<u8>();
println!("{}", s); // bad news
}

View File

@ -0,0 +1,43 @@
// Copyright 2015 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.
// It should not be possible to use the concrete value of a defaulted
// associated type in the impl defining it -- otherwise, what happens
// if it's overridden?
trait Example {
type Output;
fn generate(self) -> Self::Output;
}
impl<T> Example for T {
default type Output = Box<T>;
default fn generate(self) -> Self::Output {
Box::new(self) //~ ERROR
}
}
impl Example for bool {
type Output = bool;
fn generate(self) -> bool { self }
}
fn trouble<T>(t: T) -> Box<T> {
Example::generate(t) //~ ERROR
}
fn weaponize() -> bool {
let b: Box<bool> = trouble(true);
*b
}
fn main() {
weaponize();
}

View File

@ -0,0 +1,27 @@
// Copyright 2015 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.
trait Foo<T> {}
trait Assoc {
type Item;
}
impl<T: Assoc> Foo<T::Item> for T {}
struct Struct;
impl Assoc for Struct {
type Item = u8;
}
impl Foo<u8> for Struct {}
fn main() {}

View File

@ -0,0 +1,27 @@
// Copyright 2015 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.
// Make sure we *can* project non-defaulted associated types
// cf compile-fail/specialization-default-projection.rs
trait Foo {
type Assoc;
}
impl<T> Foo for T {
type Assoc = ();
}
fn generic<T>() -> <T as Foo>::Assoc {
()
}
fn main() {
}

View File

@ -0,0 +1,34 @@
// Copyright 2015 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.
trait Trait<T> {
fn convert(&self) -> T;
}
trait WithAssoc {
type Item;
fn as_item(&self) -> &Self::Item;
}
impl<T, U> Trait<U> for T where T: WithAssoc<Item=U>, U: Clone {
fn convert(&self) -> U {
self.as_item().clone()
}
}
impl WithAssoc for u8 {
type Item = u8;
fn as_item(&self) -> &u8 { self }
}
impl Trait<u8> for u8 {}
fn main() {
assert!(3u8.convert() == 3u8);
}

View File

@ -0,0 +1,36 @@
// Copyright 2015 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.
use std::convert::Into;
trait Trait {
fn to_u8(&self) -> u8;
}
trait WithAssoc {
type Item;
fn to_item(&self) -> Self::Item;
}
impl<T, U> Trait for T where T: WithAssoc<Item=U>, U: Into<u8> {
fn to_u8(&self) -> u8 {
self.to_item().into()
}
}
impl WithAssoc for u8 {
type Item = u8;
fn to_item(&self) -> u8 { *self }
}
impl Trait for u8 {}
fn main() {
assert!(3u8.to_u8() == 3u8);
}