Auto merge of #23998 - nrc:impl-self, r=nikomatsakis

Closes #23909

r? @nikomatsakis (or anyone else, really)
This commit is contained in:
bors 2015-04-08 09:58:05 +00:00
commit 926f38e588
15 changed files with 396 additions and 250 deletions

View file

@ -0,0 +1,40 @@
// 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.
// Test that unsupported uses of `Self` in impls don't crash
struct Bar;
trait Foo {
type Baz;
}
trait SuperFoo {
type SuperBaz;
}
impl Foo for Bar {
type Baz = bool;
}
impl SuperFoo for Bar {
type SuperBaz = bool;
}
impl Bar {
fn f() {
let _: <Self>::Baz = true;
//~^ERROR: ambiguous associated type; specify the type using the syntax `<Bar as Trait>::Baz`
let _: Self::Baz = true;
//~^ERROR: ambiguous associated type; specify the type using the syntax `<Bar as Trait>::Baz`
}
}
fn main() {}

View file

@ -12,7 +12,7 @@
trait One<A> { fn foo(&self) -> A; }
fn foo(_: &One()) //~ ERROR no associated type `Output` defined in `One<()>`
fn foo(_: &One()) //~ ERROR associated type `Output` not found for `One<()>`
{}
fn main() { }

View file

@ -14,7 +14,7 @@ trait Three<A,B,C> { fn dummy(&self) -> (A,B,C); }
fn foo(_: &Three())
//~^ ERROR wrong number of type arguments
//~| ERROR no associated type `Output`
//~| ERROR associated type `Output` not found
{}
fn main() { }

View file

@ -14,7 +14,7 @@ trait Zero { fn dummy(&self); }
fn foo(_: Zero())
//~^ ERROR wrong number of type arguments
//~| ERROR no associated type `Output` defined in `Zero`
//~| ERROR associated type `Output` not found for `Zero`
{}
fn main() { }

View file

@ -14,6 +14,6 @@ trait Trait {}
fn f<F:Trait(isize) -> isize>(x: F) {}
//~^ ERROR wrong number of type arguments: expected 0, found 1
//~| ERROR no associated type `Output`
//~| ERROR associated type `Output` not found
fn main() {}

View file

@ -22,6 +22,17 @@ impl Foo {
fn foo(_x: Self, _y: &Self, _z: Box<Self>) -> Self {
Foo
}
fn baz() {
// Test that Self cannot be shadowed.
type Foo = i32;
// There is no empty method on i32.
Self::empty();
let _: Self = Foo;
}
fn empty() {}
}
// Test uses when implementing a trait and with a type parameter.
@ -29,13 +40,31 @@ pub struct Baz<X> {
pub f: X,
}
trait Bar<X> {
fn bar(x: Self, y: &Self, z: Box<Self>) -> Self;
trait SuperBar {
type SuperQux;
}
trait Bar<X>: SuperBar {
type Qux;
fn bar(x: Self, y: &Self, z: Box<Self>, _: Self::SuperQux) -> Self;
fn dummy(&self, x: X) { }
}
impl SuperBar for Box<Baz<isize>> {
type SuperQux = bool;
}
impl Bar<isize> for Box<Baz<isize>> {
fn bar(_x: Self, _y: &Self, _z: Box<Self>) -> Self {
type Qux = i32;
fn bar(_x: Self, _y: &Self, _z: Box<Self>, _: Self::SuperQux) -> Self {
let _: Self::Qux = 42;
let _: <Self as Bar<isize>>::Qux = 42;
let _: Self::SuperQux = true;
let _: <Self as SuperBar>::SuperQux = true;
box Baz { f: 42 }
}
}
@ -43,6 +72,7 @@ impl Bar<isize> for Box<Baz<isize>> {
fn main() {
let _: Foo = Foo::foo(Foo, &Foo, box Foo);
let _: Box<Baz<isize>> = Bar::bar(box Baz { f: 42 },
&box Baz { f: 42 },
box box Baz { f: 42 });
&box Baz { f: 42 },
box box Baz { f: 42 },
true);
}