Add support for equality constraints on associated types

This commit is contained in:
Nick Cameron 2014-11-29 17:08:30 +13:00
parent da83ad8e2c
commit 397dda8aa0
29 changed files with 791 additions and 164 deletions

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.
// Test equality constraints on associated types. Check that unsupported syntax
// does not ICE.
#![feature(associated_types)]
pub trait Foo {
type A;
fn boo(&self) -> <Self as Foo>::A;
}
fn foo2<I: Foo>(x: I) {
let _: A = x.boo(); //~ERROR use of undeclared
let _: I::A = x.boo(); //~ERROR failed to resolve
//~^ERROR use of undeclared type name `I::A`
}
pub fn main() {}

View file

@ -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.
// Test equality constraints on associated types. Check we get an error when an
// equality constraint is used in a qualified path.
#![feature(associated_types)]
pub trait Foo {
type A;
fn boo(&self) -> <Self as Foo>::A;
}
struct Bar;
impl Foo for int {
type A = uint;
fn boo(&self) -> uint { 42 }
}
fn baz<I: Foo>(x: &<I as Foo<A=Bar>>::A) {} //~ERROR equality constraints are not allowed in this
pub fn main() {}

View file

@ -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.
// Test equality constraints on associated types. Check we get type errors
// where we should.
#![feature(associated_types)]
pub trait Foo {
type A;
fn boo(&self) -> <Self as Foo>::A;
}
struct Bar;
impl Foo for int {
type A = uint;
fn boo(&self) -> uint {
42
}
}
fn foo1<I: Foo<A=Bar>>(x: I) {
let _: Bar = x.boo();
}
fn foo2<I: Foo>(x: I) {
let _: Bar = x.boo(); //~ERROR mismatched types
}
pub fn baz(x: &Foo<A=Bar>) {
let _: Bar = x.boo();
}
pub fn main() {
let a = 42i;
foo1(a); //~ERROR the trait `Foo` is not implemented for the type `int`
baz(&a); //~ERROR the trait `Foo` is not implemented for the type `int`
}

View file

@ -31,6 +31,6 @@ impl ToString_ for Point {
fn main() {
let p = Point::new(0.0, 0.0);
//~^ ERROR unresolved name `Point::new`
//~^^ ERROR failed to resolve. Use of undeclared module `Point`
//~^^ ERROR failed to resolve. Use of undeclared type or module `Point`
println!("{}", p.to_string());
}

View file

@ -25,7 +25,7 @@ test!(b,
#[qux]
fn main() {
a::bar();
//~^ ERROR failed to resolve. Use of undeclared module `a`
//~^ ERROR failed to resolve. Use of undeclared type or module `a`
//~^^ ERROR unresolved name `a::bar`
b::bar();
}

View file

@ -0,0 +1,55 @@
// 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.
// Test equality constraints on associated types.
#![feature(associated_types)]
pub trait Foo {
type A;
fn boo(&self) -> <Self as Foo>::A;
}
struct Bar;
impl Foo for int {
type A = uint;
fn boo(&self) -> uint { 42 }
}
impl Foo for Bar {
type A = int;
fn boo(&self) -> int { 43 }
}
impl Foo for char {
type A = Bar;
fn boo(&self) -> Bar { Bar }
}
fn foo1<I: Foo<A=Bar>>(x: I) -> Bar {
x.boo()
}
fn foo2<I: Foo>(x: I) -> <I as Foo>::A {
x.boo()
}
fn baz(x: &Foo<A=Bar>) -> Bar {
x.boo()
}
pub fn main() {
let a = 42i;
assert!(foo2(a) == 42u);
let a = Bar;
assert!(foo2(a) == 43i);
let a = 'a';
foo1(a);
baz(&a);
}