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