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