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