]> git.lizzy.rs Git - rust.git/blob - src/test/ui/self/self-impl-2.rs
Rollup merge of #101388 - compiler-errors:issue-101376, r=fee1-dead
[rust.git] / src / test / ui / self / self-impl-2.rs
1 // run-pass
2 #![allow(dead_code)]
3 #![allow(unused_variables)]
4 // Test that we can use `Self` types in impls in the expected way.
5
6 // pretty-expanded FIXME #23616
7
8 struct Foo;
9
10 // Test uses on inherent impl.
11 impl Foo {
12     fn foo(_x: Self, _y: &Self, _z: Box<Self>) -> Self {
13         Foo
14     }
15
16     fn baz() {
17         // Test that Self cannot be shadowed.
18         type Foo = i32;
19         // There is no empty method on i32.
20         Self::empty();
21
22         let _: Self = Foo;
23     }
24
25     fn empty() {}
26 }
27
28 // Test uses when implementing a trait and with a type parameter.
29 pub struct Baz<X> {
30     pub f: X,
31 }
32
33 trait SuperBar {
34     type SuperQux;
35 }
36
37 trait Bar<X>: SuperBar {
38     type Qux;
39
40     fn bar(x: Self, y: &Self, z: Box<Self>, _: Self::SuperQux) -> Self;
41     fn dummy(&self, x: X) { }
42 }
43
44 impl SuperBar for Box<Baz<isize>> {
45     type SuperQux = bool;
46 }
47
48 impl Bar<isize> for Box<Baz<isize>> {
49     type Qux = i32;
50
51     fn bar(_x: Self, _y: &Self, _z: Box<Self>, _: Self::SuperQux) -> Self {
52         let _: Self::Qux = 42;
53         let _: <Self as Bar<isize>>::Qux = 42;
54
55         let _: Self::SuperQux = true;
56         let _: <Self as SuperBar>::SuperQux = true;
57
58         Box::new(Baz { f: 42 })
59     }
60 }
61
62 fn main() {
63     let _: Foo = Foo::foo(Foo, &Foo, Box::new(Foo));
64     let _: Box<Baz<isize>> = Bar::bar(Box::new(Baz { f: 42 }),
65                                       &Box::new(Baz { f: 42 }),
66                                       Box::new(Box::new(Baz { f: 42 })),
67                                       true);
68 }