]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/deriving-associated-types.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / deriving-associated-types.rs
1 // Copyright 2015 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 #![feature(core)]
12
13 pub trait DeclaredTrait {
14     type Type;
15 }
16
17 impl DeclaredTrait for i32 {
18     type Type = i32;
19 }
20
21 pub trait WhereTrait {
22     type Type;
23 }
24
25 impl WhereTrait for i32 {
26     type Type = i32;
27 }
28
29 // Make sure we don't add a bound that just shares a name with an associated
30 // type.
31 pub mod module {
32     pub type Type = i32;
33 }
34
35 #[derive(PartialEq, Debug)]
36 struct PrivateStruct<T>(T);
37
38 #[derive(PartialEq, Debug)]
39 struct TupleStruct<A, B: DeclaredTrait, C>(
40     module::Type,
41     Option<module::Type>,
42     A,
43     PrivateStruct<A>,
44     B,
45     B::Type,
46     Option<B::Type>,
47     <B as DeclaredTrait>::Type,
48     Option<<B as DeclaredTrait>::Type>,
49     C,
50     C::Type,
51     Option<C::Type>,
52     <C as WhereTrait>::Type,
53     Option<<C as WhereTrait>::Type>,
54     <i32 as DeclaredTrait>::Type,
55 ) where C: WhereTrait;
56
57 #[derive(PartialEq, Debug)]
58 pub struct Struct<A, B: DeclaredTrait, C> where C: WhereTrait {
59     m1: module::Type,
60     m2: Option<module::Type>,
61     a1: A,
62     a2: PrivateStruct<A>,
63     b: B,
64     b1: B::Type,
65     b2: Option<B::Type>,
66     b3: <B as DeclaredTrait>::Type,
67     b4: Option<<B as DeclaredTrait>::Type>,
68     c: C,
69     c1: C::Type,
70     c2: Option<C::Type>,
71     c3: <C as WhereTrait>::Type,
72     c4: Option<<C as WhereTrait>::Type>,
73     d: <i32 as DeclaredTrait>::Type,
74 }
75
76 #[derive(PartialEq, Debug)]
77 enum Enum<A, B: DeclaredTrait, C> where C: WhereTrait {
78     Unit,
79     Seq(
80         module::Type,
81         Option<module::Type>,
82         A,
83         PrivateStruct<A>,
84         B,
85         B::Type,
86         Option<B::Type>,
87         <B as DeclaredTrait>::Type,
88         Option<<B as DeclaredTrait>::Type>,
89         C,
90         C::Type,
91         Option<C::Type>,
92         <C as WhereTrait>::Type,
93         Option<<C as WhereTrait>::Type>,
94         <i32 as DeclaredTrait>::Type,
95     ),
96     Map {
97         m1: module::Type,
98         m2: Option<module::Type>,
99         a1: A,
100         a2: PrivateStruct<A>,
101         b: B,
102         b1: B::Type,
103         b2: Option<B::Type>,
104         b3: <B as DeclaredTrait>::Type,
105         b4: Option<<B as DeclaredTrait>::Type>,
106         c: C,
107         c1: C::Type,
108         c2: Option<C::Type>,
109         c3: <C as WhereTrait>::Type,
110         c4: Option<<C as WhereTrait>::Type>,
111         d: <i32 as DeclaredTrait>::Type,
112     },
113 }
114
115 fn main() {
116     let e: TupleStruct<
117         i32,
118         i32,
119         i32,
120     > = TupleStruct(
121         0,
122         None,
123         0,
124         PrivateStruct(0),
125         0,
126         0,
127         None,
128         0,
129         None,
130         0,
131         0,
132         None,
133         0,
134         None,
135         0,
136     );
137     assert_eq!(e, e);
138
139     let e: Struct<
140         i32,
141         i32,
142         i32,
143     > = Struct {
144         m1: 0,
145         m2: None,
146         a1: 0,
147         a2: PrivateStruct(0),
148         b: 0,
149         b1: 0,
150         b2: None,
151         b3: 0,
152         b4: None,
153         c: 0,
154         c1: 0,
155         c2: None,
156         c3: 0,
157         c4: None,
158         d: 0,
159     };
160     assert_eq!(e, e);
161
162     let e = Enum::Unit::<i32, i32, i32>;
163     assert_eq!(e, e);
164
165     let e: Enum<
166         i32,
167         i32,
168         i32,
169     > = Enum::Seq(
170         0,
171         None,
172         0,
173         PrivateStruct(0),
174         0,
175         0,
176         None,
177         0,
178         None,
179         0,
180         0,
181         None,
182         0,
183         None,
184         0,
185     );
186     assert_eq!(e, e);
187
188     let e: Enum<
189         i32,
190         i32,
191         i32,
192     > = Enum::Map {
193         m1: 0,
194         m2: None,
195         a1: 0,
196         a2: PrivateStruct(0),
197         b: 0,
198         b1: 0,
199         b2: None,
200         b3: 0,
201         b4: None,
202         c: 0,
203         c1: 0,
204         c2: None,
205         c3: 0,
206         c4: None,
207         d: 0,
208     };
209     assert_eq!(e, e);
210 }