2014-12-14 02:42:41 +00:00
|
|
|
// 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 that we can use `Self` types in impls in the expected way.
|
|
|
|
|
2015-03-22 20:13:15 +00:00
|
|
|
// pretty-expanded FIXME #23616
|
|
|
|
|
2015-01-08 01:25:56 +00:00
|
|
|
#![feature(box_syntax)]
|
|
|
|
|
2014-12-14 02:42:41 +00:00
|
|
|
struct Foo;
|
|
|
|
|
2015-01-07 01:54:54 +00:00
|
|
|
// Test uses on inherent impl.
|
2014-12-14 02:42:41 +00:00
|
|
|
impl Foo {
|
|
|
|
fn foo(_x: Self, _y: &Self, _z: Box<Self>) -> Self {
|
|
|
|
Foo
|
|
|
|
}
|
2015-04-03 04:13:52 +00:00
|
|
|
|
|
|
|
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() {}
|
2014-12-14 02:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test uses when implementing a trait and with a type parameter.
|
|
|
|
pub struct Baz<X> {
|
|
|
|
pub f: X,
|
|
|
|
}
|
|
|
|
|
2015-04-07 05:59:10 +00:00
|
|
|
trait SuperBar {
|
|
|
|
type SuperQux;
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Bar<X>: SuperBar {
|
2015-04-03 04:13:52 +00:00
|
|
|
type Qux;
|
|
|
|
|
2015-04-07 05:59:10 +00:00
|
|
|
fn bar(x: Self, y: &Self, z: Box<Self>, _: Self::SuperQux) -> Self;
|
2015-02-12 15:29:52 +00:00
|
|
|
fn dummy(&self, x: X) { }
|
2014-12-14 02:42:41 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 05:59:10 +00:00
|
|
|
impl SuperBar for Box<Baz<isize>> {
|
|
|
|
type SuperQux = bool;
|
|
|
|
}
|
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
impl Bar<isize> for Box<Baz<isize>> {
|
2015-04-03 04:13:52 +00:00
|
|
|
type Qux = i32;
|
|
|
|
|
2015-04-07 05:59:10 +00:00
|
|
|
fn bar(_x: Self, _y: &Self, _z: Box<Self>, _: Self::SuperQux) -> Self {
|
2015-04-03 04:13:52 +00:00
|
|
|
let _: Self::Qux = 42;
|
|
|
|
let _: <Self as Bar<isize>>::Qux = 42;
|
2015-04-07 05:59:10 +00:00
|
|
|
|
|
|
|
let _: Self::SuperQux = true;
|
|
|
|
let _: <Self as SuperBar>::SuperQux = true;
|
|
|
|
|
2014-12-14 02:42:41 +00:00
|
|
|
box Baz { f: 42 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let _: Foo = Foo::foo(Foo, &Foo, box Foo);
|
2015-03-26 00:06:52 +00:00
|
|
|
let _: Box<Baz<isize>> = Bar::bar(box Baz { f: 42 },
|
2015-04-03 04:13:52 +00:00
|
|
|
&box Baz { f: 42 },
|
2015-04-07 05:59:10 +00:00
|
|
|
box box Baz { f: 42 },
|
|
|
|
true);
|
2014-12-14 02:42:41 +00:00
|
|
|
}
|