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