]> git.lizzy.rs Git - rust.git/blob - src/test/ui/impl-trait/where-allowed.rs
35fb42d6213226918f687979944f6812db7d6278
[rust.git] / src / test / ui / impl-trait / where-allowed.rs
1 //! A simple test for testing many permutations of allowedness of
2 //! impl Trait
3 use std::fmt::Debug;
4
5 // Allowed
6 fn in_parameters(_: impl Debug) { panic!() }
7
8 // Allowed
9 fn in_return() -> impl Debug { panic!() }
10
11 // Allowed
12 fn in_adt_in_parameters(_: Vec<impl Debug>) { panic!() }
13
14 // Disallowed
15 fn in_fn_parameter_in_parameters(_: fn(impl Debug)) { panic!() }
16 //~^ ERROR `impl Trait` not allowed outside of function and method return types
17
18 // Disallowed
19 fn in_fn_return_in_parameters(_: fn() -> impl Debug) { panic!() }
20 //~^ ERROR `impl Trait` not allowed outside of function and method return types
21
22 // Disallowed
23 fn in_fn_parameter_in_return() -> fn(impl Debug) { panic!() }
24 //~^ ERROR `impl Trait` not allowed outside of function and method return types
25
26 // Disallowed
27 fn in_fn_return_in_return() -> fn() -> impl Debug { panic!() }
28 //~^ ERROR `impl Trait` not allowed outside of function and method return types
29
30 // Disallowed
31 fn in_dyn_Fn_parameter_in_parameters(_: &dyn Fn(impl Debug)) { panic!() }
32 //~^ ERROR `impl Trait` not allowed outside of function and method return types
33
34 // Disallowed
35 fn in_dyn_Fn_return_in_parameters(_: &dyn Fn() -> impl Debug) { panic!() }
36 //~^ ERROR `impl Trait` not allowed outside of function and method return types
37
38 // Disallowed
39 fn in_dyn_Fn_parameter_in_return() -> &'static dyn Fn(impl Debug) { panic!() }
40 //~^ ERROR `impl Trait` not allowed outside of function and method return types
41
42 // Disallowed
43 fn in_dyn_Fn_return_in_return() -> &'static dyn Fn() -> impl Debug { panic!() }
44 //~^ ERROR `impl Trait` not allowed outside of function and method return types
45
46 // Disallowed
47 fn in_impl_Fn_parameter_in_parameters(_: &impl Fn(impl Debug)) { panic!() }
48 //~^ ERROR `impl Trait` not allowed outside of function and method return types
49 //~^^ ERROR nested `impl Trait` is not allowed
50
51 // Disallowed
52 fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() }
53 //~^ ERROR `impl Trait` not allowed outside of function and method return types
54
55 // Disallowed
56 fn in_impl_Fn_parameter_in_return() -> &'static impl Fn(impl Debug) { panic!() }
57 //~^ ERROR `impl Trait` not allowed outside of function and method return types
58 //~| ERROR nested `impl Trait` is not allowed
59
60 // Disallowed
61 fn in_impl_Fn_return_in_return() -> &'static impl Fn() -> impl Debug { panic!() }
62 //~^ ERROR `impl Trait` not allowed outside of function and method return types
63
64 // Disallowed
65 fn in_Fn_parameter_in_generics<F: Fn(impl Debug)> (_: F) { panic!() }
66 //~^ ERROR `impl Trait` not allowed outside of function and method return types
67
68 // Disallowed
69 fn in_Fn_return_in_generics<F: Fn() -> impl Debug> (_: F) { panic!() }
70 //~^ ERROR `impl Trait` not allowed outside of function and method return types
71
72
73 // Allowed
74 fn in_impl_Trait_in_parameters(_: impl Iterator<Item = impl Iterator>) { panic!() }
75
76 // Allowed
77 fn in_impl_Trait_in_return() -> impl IntoIterator<Item = impl IntoIterator> {
78     vec![vec![0; 10], vec![12; 7], vec![8; 3]]
79 }
80
81 // Disallowed
82 struct InBraceStructField { x: impl Debug }
83 //~^ ERROR `impl Trait` not allowed outside of function and method return types
84
85 // Disallowed
86 struct InAdtInBraceStructField { x: Vec<impl Debug> }
87 //~^ ERROR `impl Trait` not allowed outside of function and method return types
88
89 // Disallowed
90 struct InTupleStructField(impl Debug);
91 //~^ ERROR `impl Trait` not allowed outside of function and method return types
92
93 // Disallowed
94 enum InEnum {
95     InBraceVariant { x: impl Debug },
96     //~^ ERROR `impl Trait` not allowed outside of function and method return types
97     InTupleVariant(impl Debug),
98     //~^ ERROR `impl Trait` not allowed outside of function and method return types
99 }
100
101 // Allowed
102 trait InTraitDefnParameters {
103     fn in_parameters(_: impl Debug);
104 }
105
106 // Disallowed
107 trait InTraitDefnReturn {
108     fn in_return() -> impl Debug;
109     //~^ ERROR `impl Trait` not allowed outside of function and method return types
110 }
111
112 // Allowed and disallowed in trait impls
113 trait DummyTrait {
114     type Out;
115     fn in_trait_impl_parameter(_: impl Debug);
116     fn in_trait_impl_return() -> Self::Out;
117 }
118 impl DummyTrait for () {
119     type Out = impl Debug;
120     //~^ ERROR `impl Trait` in type aliases is unstable
121
122     fn in_trait_impl_parameter(_: impl Debug) { }
123     // Allowed
124
125     fn in_trait_impl_return() -> impl Debug { () }
126     //~^ ERROR `impl Trait` not allowed outside of function and method return types
127 }
128
129 // Allowed
130 struct DummyType;
131 impl DummyType {
132     fn in_inherent_impl_parameters(_: impl Debug) { }
133     fn in_inherent_impl_return() -> impl Debug { () }
134 }
135
136 // Disallowed
137 extern "C" {
138     fn in_foreign_parameters(_: impl Debug);
139     //~^ ERROR `impl Trait` not allowed outside of function and method return types
140
141     fn in_foreign_return() -> impl Debug;
142     //~^ ERROR `impl Trait` not allowed outside of function and method return types
143 }
144
145 // Allowed
146 extern "C" fn in_extern_fn_parameters(_: impl Debug) {
147 }
148
149 // Allowed
150 extern "C" fn in_extern_fn_return() -> impl Debug {
151     22
152 }
153
154 type InTypeAlias<R> = impl Debug;
155 //~^ ERROR `impl Trait` in type aliases is unstable
156
157 type InReturnInTypeAlias<R> = fn() -> impl Debug;
158 //~^ ERROR `impl Trait` not allowed outside of function and method return types
159 //~| ERROR `impl Trait` in type aliases is unstable
160
161 // Disallowed in impl headers
162 impl PartialEq<impl Debug> for () {
163     //~^ ERROR `impl Trait` not allowed outside of function and method return types
164 }
165
166 // Disallowed in impl headers
167 impl PartialEq<()> for impl Debug {
168     //~^ ERROR `impl Trait` not allowed outside of function and method return types
169 }
170
171 // Disallowed in inherent impls
172 impl impl Debug {
173     //~^ ERROR `impl Trait` not allowed outside of function and method return types
174 }
175
176 // Disallowed in inherent impls
177 struct InInherentImplAdt<T> { t: T }
178 impl InInherentImplAdt<impl Debug> {
179     //~^ ERROR `impl Trait` not allowed outside of function and method return types
180 }
181
182 // Disallowed in where clauses
183 fn in_fn_where_clause()
184     where impl Debug: Debug
185 //~^ ERROR `impl Trait` not allowed outside of function and method return types
186 {
187 }
188
189 // Disallowed in where clauses
190 fn in_adt_in_fn_where_clause()
191     where Vec<impl Debug>: Debug
192 //~^ ERROR `impl Trait` not allowed outside of function and method return types
193 {
194 }
195
196 // Disallowed
197 fn in_trait_parameter_in_fn_where_clause<T>()
198     where T: PartialEq<impl Debug>
199 //~^ ERROR `impl Trait` not allowed outside of function and method return types
200 {
201 }
202
203 // Disallowed
204 fn in_Fn_parameter_in_fn_where_clause<T>()
205     where T: Fn(impl Debug)
206 //~^ ERROR `impl Trait` not allowed outside of function and method return types
207 {
208 }
209
210 // Disallowed
211 fn in_Fn_return_in_fn_where_clause<T>()
212     where T: Fn() -> impl Debug
213 //~^ ERROR `impl Trait` not allowed outside of function and method return types
214 {
215 }
216
217 // Disallowed
218 struct InStructGenericParamDefault<T = impl Debug>(T);
219 //~^ ERROR `impl Trait` not allowed outside of function and method return types
220
221 // Disallowed
222 enum InEnumGenericParamDefault<T = impl Debug> { Variant(T) }
223 //~^ ERROR `impl Trait` not allowed outside of function and method return types
224
225 // Disallowed
226 trait InTraitGenericParamDefault<T = impl Debug> {}
227 //~^ ERROR `impl Trait` not allowed outside of function and method return types
228
229 // Disallowed
230 type InTypeAliasGenericParamDefault<T = impl Debug> = T;
231 //~^ ERROR `impl Trait` not allowed outside of function and method return types
232
233 // Disallowed
234 impl <T = impl Debug> T {}
235 //~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
236 //~| WARNING this was previously accepted by the compiler but is being phased out
237 //~| ERROR `impl Trait` not allowed outside of function and method return types
238 //~| ERROR no nominal type found
239
240 // Disallowed
241 fn in_method_generic_param_default<T = impl Debug>(_: T) {}
242 //~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
243 //~| WARNING this was previously accepted by the compiler but is being phased out
244 //~| ERROR `impl Trait` not allowed outside of function and method return types
245
246 fn main() {
247     let _in_local_variable: impl Fn() = || {};
248     //~^ ERROR `impl Trait` not allowed outside of function and method return types
249     let _in_return_in_local_variable = || -> impl Fn() { || {} };
250     //~^ ERROR `impl Trait` not allowed outside of function and method return types
251 }