2024-02-16 20:02:50 +00:00
|
|
|
//@ run-pass
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_variables)]
|
2014-12-14 02:42:41 +00:00
|
|
|
// Test that we can use `Self` types in impls in the expected way.
|
|
|
|
|
2024-02-16 20:02:50 +00:00
|
|
|
//@ pretty-expanded FIXME #23616
|
2015-03-22 20:13:15 +00:00
|
|
|
|
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;
|
|
|
|
|
2021-08-25 00:39:40 +00:00
|
|
|
Box::new(Baz { f: 42 })
|
2014-12-14 02:42:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-08-25 00:39:40 +00: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 05:59:10 +00:00
|
|
|
true);
|
2014-12-14 02:42:41 +00:00
|
|
|
}
|