]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/self-impl.rs
Auto merge of #28369 - ebfull:fix-higher-ranked, r=nikomatsakis
[rust.git] / src / test / run-pass / self-impl.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test that we can use `Self` types in impls in the expected way.
12
13 // pretty-expanded FIXME #23616
14
15 #![allow(unknown_features)]
16 #![feature(box_syntax)]
17
18 struct Foo;
19
20 // Test uses on inherent impl.
21 impl Foo {
22     fn foo(_x: Self, _y: &Self, _z: Box<Self>) -> Self {
23         Foo
24     }
25
26     fn baz() {
27         // Test that Self cannot be shadowed.
28         type Foo = i32;
29         // There is no empty method on i32.
30         Self::empty();
31
32         let _: Self = Foo;
33     }
34
35     fn empty() {}
36 }
37
38 // Test uses when implementing a trait and with a type parameter.
39 pub struct Baz<X> {
40     pub f: X,
41 }
42
43 trait SuperBar {
44     type SuperQux;
45 }
46
47 trait Bar<X>: SuperBar {
48     type Qux;
49
50     fn bar(x: Self, y: &Self, z: Box<Self>, _: Self::SuperQux) -> Self;
51     fn dummy(&self, x: X) { }
52 }
53
54 impl SuperBar for Box<Baz<isize>> {
55     type SuperQux = bool;
56 }
57
58 impl Bar<isize> for Box<Baz<isize>> {
59     type Qux = i32;
60
61     fn bar(_x: Self, _y: &Self, _z: Box<Self>, _: Self::SuperQux) -> Self {
62         let _: Self::Qux = 42;
63         let _: <Self as Bar<isize>>::Qux = 42;
64
65         let _: Self::SuperQux = true;
66         let _: <Self as SuperBar>::SuperQux = true;
67
68         box Baz { f: 42 }
69     }
70 }
71
72 fn main() {
73     let _: Foo = Foo::foo(Foo, &Foo, box Foo);
74     let _: Box<Baz<isize>> = Bar::bar(box Baz { f: 42 },
75                                       &box Baz { f: 42 },
76                                       box box Baz { f: 42 },
77                                       true);
78 }