]> git.lizzy.rs Git - rust.git/blob - tests/ui/typeck/typeck_type_placeholder_item.rs
Rollup merge of #107355 - JohnTitor:issue-60755, r=compiler-errors
[rust.git] / tests / ui / typeck / typeck_type_placeholder_item.rs
1 // Needed for `type Y = impl Trait<_>` and `type B = _;`
2 #![feature(associated_type_defaults)]
3 #![feature(type_alias_impl_trait)]
4 // This test checks that it is not possible to enable global type
5 // inference by using the `_` type placeholder.
6
7 fn test() -> _ { 5 }
8 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
9
10 fn test2() -> (_, _) { (5, 5) }
11 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
12
13 static TEST3: _ = "test";
14 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for static variables
15
16 static TEST4: _ = 145;
17 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for static variables
18
19 static TEST5: (_, _) = (1, 2);
20 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for static variables
21
22 fn test6(_: _) { }
23 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
24
25 fn test6_b<T>(_: _, _: T) { }
26 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
27
28 fn test6_c<T, K, L, A, B>(_: _, _: (T, K, L, A, B)) { }
29 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
30
31 fn test7(x: _) { let _x: usize = x; }
32 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
33
34 fn test8(_f: fn() -> _) { }
35 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
36 //~^^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
37
38 struct Test9;
39
40 impl Test9 {
41     fn test9(&self) -> _ { () }
42     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
43
44     fn test10(&self, _x : _) { }
45     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
46 }
47
48 fn test11(x: &usize) -> &_ {
49 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
50     &x
51 }
52
53 unsafe fn test12(x: *const usize) -> *const *const _ {
54 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
55     &x
56 }
57
58 impl Clone for Test9 {
59     fn clone(&self) -> _ { Test9 }
60     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
61
62     fn clone_from(&mut self, other: _) { *self = Test9; }
63     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
64 }
65
66 struct Test10 {
67     a: _,
68     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for structs
69     b: (_, _),
70 }
71
72 pub fn main() {
73     static A = 42;
74     //~^ ERROR missing type for `static` item
75     static B: _ = 42;
76     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for static variables
77     static C: Option<_> = Some(42);
78     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for static variables
79     fn fn_test() -> _ { 5 }
80     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
81
82     fn fn_test2() -> (_, _) { (5, 5) }
83     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
84
85     static FN_TEST3: _ = "test";
86     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for static variables
87
88     static FN_TEST4: _ = 145;
89     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for static variables
90
91     static FN_TEST5: (_, _) = (1, 2);
92     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for static variables
93
94     fn fn_test6(_: _) { }
95     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
96
97     fn fn_test7(x: _) { let _x: usize = x; }
98     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
99
100     fn fn_test8(_f: fn() -> _) { }
101     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
102     //~^^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
103
104     struct FnTest9;
105
106     impl FnTest9 {
107         fn fn_test9(&self) -> _ { () }
108         //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
109
110         fn fn_test10(&self, _x : _) { }
111         //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
112     }
113
114     impl Clone for FnTest9 {
115         fn clone(&self) -> _ { FnTest9 }
116         //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
117
118         fn clone_from(&mut self, other: _) { *self = FnTest9; }
119         //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
120     }
121
122     struct FnTest10 {
123         a: _,
124         //~^ ERROR the placeholder `_` is not allowed within types on item signatures for structs
125         b: (_, _),
126     }
127
128     fn fn_test11(_: _) -> (_, _) { panic!() }
129     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
130     //~| ERROR type annotations needed
131
132     fn fn_test12(x: i32) -> (_, _) { (x, x) }
133     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
134
135     fn fn_test13(x: _) -> (i32, _) { (x, x) }
136     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
137 }
138
139 trait T {
140     fn method_test1(&self, x: _);
141     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
142     fn method_test2(&self, x: _) -> _;
143     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
144     fn method_test3(&self) -> _;
145     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
146     fn assoc_fn_test1(x: _);
147     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
148     fn assoc_fn_test2(x: _) -> _;
149     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
150     fn assoc_fn_test3() -> _;
151     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
152 }
153
154 struct BadStruct<_>(_);
155 //~^ ERROR expected identifier, found reserved identifier `_`
156 //~| ERROR the placeholder `_` is not allowed within types on item signatures for structs
157 trait BadTrait<_> {}
158 //~^ ERROR expected identifier, found reserved identifier `_`
159 impl BadTrait<_> for BadStruct<_> {}
160 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for implementations
161
162 fn impl_trait() -> impl BadTrait<_> {
163 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for opaque types
164     unimplemented!()
165 }
166
167 struct BadStruct1<_, _>(_);
168 //~^ ERROR expected identifier, found reserved identifier `_`
169 //~| ERROR expected identifier, found reserved identifier `_`
170 //~| ERROR the name `_` is already used
171 //~| ERROR the placeholder `_` is not allowed within types on item signatures for structs
172 struct BadStruct2<_, T>(_, T);
173 //~^ ERROR expected identifier, found reserved identifier `_`
174 //~| ERROR the placeholder `_` is not allowed within types on item signatures for structs
175
176 type X = Box<_>;
177 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for type aliases
178
179 struct Struct;
180 trait Trait<T> {}
181 impl Trait<usize> for Struct {}
182 type Y = impl Trait<_>;
183 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for opaque types
184 fn foo() -> Y {
185     Struct
186 }
187
188 trait Qux {
189     type A;
190     type B = _;
191     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types
192     const C: _;
193     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
194     const D: _ = 42;
195     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
196     // type E: _; // FIXME: make the parser propagate the existence of `B`
197     type F: std::ops::Fn(_);
198     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types
199 }
200 impl Qux for Struct {
201     type A = _;
202     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types
203     type B = _;
204     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types
205     const C: _;
206     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
207     //~| ERROR associated constant in `impl` without body
208     const D: _ = 42;
209     //~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
210 }
211
212 fn map<T>(_: fn() -> Option<&'static T>) -> Option<T> {
213     None
214 }
215
216 fn value() -> Option<&'static _> {
217 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
218     Option::<&'static u8>::None
219 }
220
221 const _: Option<_> = map(value);
222 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
223
224 fn evens_squared(n: usize) -> _ {
225 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types
226     (1..n).filter(|x| x % 2 == 0).map(|x| x * x)
227 }
228
229 const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
230 //~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants