librustc: Allow trait bounds on structures and enumerations, and check

them during kind checking.

This implements RFC #11.

Closes #15759.
This commit is contained in:
Patrick Walton 2014-08-11 17:12:01 -07:00
parent cb9c1e0e70
commit 086a5ca7d2
26 changed files with 622 additions and 43 deletions

View file

@ -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.
pub trait Trait {}
pub struct Foo<T:Trait> {
pub x: T,
}
pub enum Bar<T:Trait> {
ABar(int),
BBar(T),
CBar(uint),
}

View file

@ -16,9 +16,11 @@ extern crate rand;
struct Error;
#[deriving(Zero)]
#[deriving(Zero)] //~ ERROR failed to find an implementation
struct Struct {
x: Error //~ ERROR
x: Error //~ ERROR failed to find an implementation
//~^ ERROR failed to find an implementation
//~^^ ERROR type `Error` does not implement any method in scope
}
fn main() {}

View file

@ -16,9 +16,11 @@ extern crate rand;
struct Error;
#[deriving(Zero)]
#[deriving(Zero)] //~ ERROR failed to find an implementation
struct Struct(
Error //~ ERROR
//~^ ERROR failed to find an implementation
//~^^ ERROR type `Error` does not implement any method in scope
);
fn main() {}

View file

@ -18,4 +18,5 @@ fn main() {
//~^ ERROR cannot determine a type for this bounded type parameter: unconstrained type
println!("{}", y + 1);
//~^ ERROR binary operation `+` cannot be applied to type `Gc<int>`
//~^^ ERROR cannot determine a type for this bounded type parameter: unconstrained type
}

View file

@ -19,5 +19,5 @@ fn main() {
let x: Box<Map<int, int>> = x;
let y: Box<Map<uint, int>> = box x;
//~^ ERROR failed to find an implementation of trait collections::Map<uint,int>
// for ~collections::Map<int,int>:Send
//~^^ ERROR failed to find an implementation of trait core::collections::Collection
}

View file

@ -8,6 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// ignore-test
//
// Ignored because of an ICE at the moment.
// Check that non-constant exprs do fail as count in fixed length vec type
fn main() {

View file

@ -15,4 +15,5 @@
trait Foo {}
fn take_foo<F:Foo>(f: F) {}
fn take_object(f: Box<Foo>) { take_foo(f); } //~ ERROR failed to find an implementation of trait
//~^ ERROR failed to find an implementation
fn main() {}

View file

@ -44,6 +44,7 @@ fn main() {
// Can't do this copy
let x = box box box A {y: r(i)};
let _z = x.clone(); //~ ERROR failed to find an implementation
//~^ ERROR failed to find an implementation
println!("{:?}", x);
}
println!("{:?}", *i);

View file

@ -0,0 +1,27 @@
// 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.
trait Trait {}
struct Foo<T:Trait> {
x: T,
}
fn main() {
let foo = Foo {
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
x: 3i
};
let baz: Foo<uint> = fail!();
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
}

View file

@ -0,0 +1,25 @@
// 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.
trait Trait {}
struct Foo<T:Trait> {
x: T,
}
static X: Foo<uint> = Foo {
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
x: 1,
};
fn main() {
}

View file

@ -0,0 +1,36 @@
// 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:trait_bounds_on_structs_and_enums_xc.rs
extern crate trait_bounds_on_structs_and_enums_xc;
use trait_bounds_on_structs_and_enums_xc::{Bar, Foo, Trait};
fn explode(x: Foo<uint>) {}
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
fn kaboom(y: Bar<f32>) {}
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
fn main() {
let foo = Foo {
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
x: 3i
};
let bar: Bar<f64> = return;
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
let _ = bar;
}

View file

@ -0,0 +1,77 @@
// 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.
trait Trait {}
struct Foo<T:Trait> {
x: T,
}
enum Bar<T:Trait> {
ABar(int),
BBar(T),
CBar(uint),
}
fn explode(x: Foo<uint>) {}
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
fn kaboom(y: Bar<f32>) {}
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
impl<T> Foo<T> {
fn uhoh() {}
}
struct Baz {
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
a: Foo<int>,
}
enum Boo {
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
Quux(Bar<uint>),
}
struct Badness<T> {
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
b: Foo<T>,
}
enum MoreBadness<T> {
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
EvenMoreBadness(Bar<T>),
}
trait PolyTrait<T> {
fn whatever() {}
}
struct Struct;
impl PolyTrait<Foo<uint>> for Struct {
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
fn whatever() {}
}
fn main() {
let bar: Bar<f64> = return;
//~^ ERROR failed to find an implementation
//~^^ ERROR instantiating a type parameter with an incompatible type
let _ = bar;
}

View file

@ -21,5 +21,6 @@ impl Drop for r {
fn main() {
let i = box r { b: true };
let _j = i.clone(); //~ ERROR failed to find an implementation
//~^ ERROR failed to find an implementation
println!("{:?}", i);
}

View file

@ -37,6 +37,9 @@ fn main() {
let r2 = vec!(box r { i: i2 });
f(r1.clone(), r2.clone());
//~^ ERROR failed to find an implementation of
//~^^ ERROR failed to find an implementation of
//~^^^ ERROR failed to find an implementation of
//~^^^^ ERROR failed to find an implementation of
println!("{:?}", (r2, i1.get()));
println!("{:?}", (r1, i2.get()));
}

View file

@ -25,6 +25,7 @@ impl TraitB for int {
fn call_it<B:TraitB>(b: B) -> int {
let y = 4u;
b.gimme_an_a(y) //~ ERROR failed to find an implementation of trait TraitA
//~^ ERROR failed to find an implementation of trait TraitA
}
fn main() {

View file

@ -14,7 +14,10 @@ fn equal<T>(_: &T, _: &T) -> bool where T : Eq {
struct Struct;
fn main() {
equal(&Struct, &Struct)
//~^ ERROR failed to find an implementation of trait
drop(equal(&Struct, &Struct))
//~^ ERROR failed to find an implementation of trait core::cmp::Eq
//~^^ ERROR failed to find an implementation of trait core::cmp::PartialEq
//~^^^ ERROR failed to find an implementation of trait core::cmp::Eq
//~^^^^ ERROR failed to find an implementation of trait core::cmp::PartialEq
}

View file

@ -13,7 +13,7 @@
pub fn main() {
static FOO: int = 2;
static FOO: uint = 2;
let _v: [int, ..FOO*3];
}

View file

@ -0,0 +1,26 @@
// 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.
trait U {}
trait T<X: U> {}
trait S2<Y: U> {
fn m(x: Box<T<Y>>) {}
}
struct St<X: U> {
f: Box<T<X>>,
}
impl<X: U> St<X> {
fn blah() {}
}
fn main() {}