2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2018-09-25 16:51:35 -05:00
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_variables)]
|
2014-12-13 20:42:41 -06:00
|
|
|
// Test that we can use `Self` types in impls in the expected way.
|
|
|
|
|
2015-03-22 15:13:15 -05:00
|
|
|
// pretty-expanded FIXME #23616
|
|
|
|
|
2014-12-13 20:42:41 -06:00
|
|
|
struct Foo;
|
|
|
|
|
2015-01-06 19:54:54 -06:00
|
|
|
// Test uses on inherent impl.
|
2014-12-13 20:42:41 -06:00
|
|
|
impl Foo {
|
|
|
|
fn foo(_x: Self, _y: &Self, _z: Box<Self>) -> Self {
|
|
|
|
Foo
|
|
|
|
}
|
2015-04-02 23:13:52 -05: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-13 20:42:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test uses when implementing a trait and with a type parameter.
|
|
|
|
pub struct Baz<X> {
|
|
|
|
pub f: X,
|
|
|
|
}
|
|
|
|
|
2015-04-07 00:59:10 -05:00
|
|
|
trait SuperBar {
|
|
|
|
type SuperQux;
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Bar<X>: SuperBar {
|
2015-04-02 23:13:52 -05:00
|
|
|
type Qux;
|
|
|
|
|
2015-04-07 00:59:10 -05:00
|
|
|
fn bar(x: Self, y: &Self, z: Box<Self>, _: Self::SuperQux) -> Self;
|
2015-02-12 09:29:52 -06:00
|
|
|
fn dummy(&self, x: X) { }
|
2014-12-13 20:42:41 -06:00
|
|
|
}
|
|
|
|
|
2015-04-07 00:59:10 -05:00
|
|
|
impl SuperBar for Box<Baz<isize>> {
|
|
|
|
type SuperQux = bool;
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
impl Bar<isize> for Box<Baz<isize>> {
|
2015-04-02 23:13:52 -05:00
|
|
|
type Qux = i32;
|
|
|
|
|
2015-04-07 00:59:10 -05:00
|
|
|
fn bar(_x: Self, _y: &Self, _z: Box<Self>, _: Self::SuperQux) -> Self {
|
2015-04-02 23:13:52 -05:00
|
|
|
let _: Self::Qux = 42;
|
|
|
|
let _: <Self as Bar<isize>>::Qux = 42;
|
2015-04-07 00:59:10 -05:00
|
|
|
|
|
|
|
let _: Self::SuperQux = true;
|
|
|
|
let _: <Self as SuperBar>::SuperQux = true;
|
|
|
|
|
2021-08-24 19:39:40 -05:00
|
|
|
Box::new(Baz { f: 42 })
|
2014-12-13 20:42:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let _: Foo = Foo::foo(Foo, &Foo, Box::new(Foo));
|
|
|
|
let _: Box<Baz<isize>> = Bar::bar(Box::new(Baz { f: 42 }),
|
|
|
|
&Box::new(Baz { f: 42 }),
|
|
|
|
Box::new(Box::new(Baz { f: 42 })),
|
2015-04-07 00:59:10 -05:00
|
|
|
true);
|
2014-12-13 20:42:41 -06:00
|
|
|
}
|