summaryrefslogtreecommitdiffstats
path: root/tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.rs
blob: 4ac3ea53a08ab552199c5d06ccc48fb0301b6a36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Here we test that `lowering` behaves correctly wrt. `let $pats = $expr` expressions.
//
// We want to make sure that `let` is banned in situations other than:
//
// expr =
//   | ...
//   | "if" expr_with_let block {"else" block}?
//   | {label ":"}? while" expr_with_let block
//   ;
//
// expr_with_let =
//   | "let" top_pats "=" expr
//   | expr_with_let "&&" expr_with_let
//   | "(" expr_with_let ")"
//   | expr
//   ;
//
// To that end, we check some positions which is not part of the language above.

#![feature(let_chains)] // Avoid inflating `.stderr` with overzealous gates in this test.

#![allow(irrefutable_let_patterns)]

use std::ops::Range;

fn main() {}

fn _if() {
    if (let 0 = 1) {}
    //~^ ERROR expected expression, found `let` statement

    if (((let 0 = 1))) {}
    //~^ ERROR expected expression, found `let` statement

    if (let 0 = 1) && true {}
    //~^ ERROR expected expression, found `let` statement

    if true && (let 0 = 1) {}
    //~^ ERROR expected expression, found `let` statement

    if (let 0 = 1) && (let 0 = 1) {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR expected expression, found `let` statement

    if let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR expected expression, found `let` statement
    //~| ERROR expected expression, found `let` statement
}

fn _while() {
    while (let 0 = 1) {}
    //~^ ERROR expected expression, found `let` statement

    while (((let 0 = 1))) {}
    //~^ ERROR expected expression, found `let` statement

    while (let 0 = 1) && true {}
    //~^ ERROR expected expression, found `let` statement

    while true && (let 0 = 1) {}
    //~^ ERROR expected expression, found `let` statement

    while (let 0 = 1) && (let 0 = 1) {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR expected expression, found `let` statement

    while let 0 = 1 && let 1 = 2 && (let 2 = 3 && let 3 = 4 && let 4 = 5) {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR expected expression, found `let` statement
    //~| ERROR expected expression, found `let` statement
}

fn _macros() {
    macro_rules! use_expr {
        ($e:expr) => {
            if $e {}
            while $e {}
        }
    }
    use_expr!((let 0 = 1 && 0 == 0));
    //~^ ERROR expected expression, found `let` statement
    use_expr!((let 0 = 1));
    //~^ ERROR expected expression, found `let` statement
}

fn nested_within_if_expr() {
    if &let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement

    if !let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement
    if *let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement
    if -let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement

    fn _check_try_binds_tighter() -> Result<(), ()> {
        if let 0 = 0? {}
        //~^ ERROR the `?` operator can only be applied to values that implement `Try`
        Ok(())
    }
    if (let 0 = 0)? {}
    //~^ ERROR expected expression, found `let` statement

    if true || let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement
    if (true || let 0 = 0) {}
    //~^ ERROR expected expression, found `let` statement
    if true && (true || let 0 = 0) {}
    //~^ ERROR expected expression, found `let` statement
    if true || (true && let 0 = 0) {}
    //~^ ERROR expected expression, found `let` statement

    let mut x = true;
    if x = let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement

    if true..(let 0 = 0) {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR mismatched types
    if ..(let 0 = 0) {}
    //~^ ERROR expected expression, found `let` statement
    if (let 0 = 0).. {}
    //~^ ERROR expected expression, found `let` statement

    // Binds as `(let ... = true)..true &&/|| false`.
    if let Range { start: _, end: _ } = true..true && false {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR mismatched types
    if let Range { start: _, end: _ } = true..true || false {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR mismatched types

    // Binds as `(let Range { start: F, end } = F)..(|| true)`.
    const F: fn() -> bool = || true;
    if let Range { start: F, end } = F..|| true {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR mismatched types

    // Binds as `(let Range { start: true, end } = t)..(&&false)`.
    let t = &&true;
    if let Range { start: true, end } = t..&&false {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR mismatched types

    if let true = let true = true {}
    //~^ ERROR expected expression, found `let` statement
}

fn nested_within_while_expr() {
    while &let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement

    while !let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement
    while *let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement
    while -let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement

    fn _check_try_binds_tighter() -> Result<(), ()> {
        while let 0 = 0? {}
        //~^ ERROR the `?` operator can only be applied to values that implement `Try`
        Ok(())
    }
    while (let 0 = 0)? {}
    //~^ ERROR expected expression, found `let` statement

    while true || let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement
    while (true || let 0 = 0) {}
    //~^ ERROR expected expression, found `let` statement
    while true && (true || let 0 = 0) {}
    //~^ ERROR expected expression, found `let` statement
    while true || (true && let 0 = 0) {}
    //~^ ERROR expected expression, found `let` statement

    let mut x = true;
    while x = let 0 = 0 {}
    //~^ ERROR expected expression, found `let` statement

    while true..(let 0 = 0) {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR mismatched types
    while ..(let 0 = 0) {}
    //~^ ERROR expected expression, found `let` statement
    while (let 0 = 0).. {}
    //~^ ERROR expected expression, found `let` statement

    // Binds as `(let ... = true)..true &&/|| false`.
    while let Range { start: _, end: _ } = true..true && false {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR mismatched types
    while let Range { start: _, end: _ } = true..true || false {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR mismatched types

    // Binds as `(let Range { start: F, end } = F)..(|| true)`.
    const F: fn() -> bool = || true;
    while let Range { start: F, end } = F..|| true {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR mismatched types

    // Binds as `(let Range { start: true, end } = t)..(&&false)`.
    let t = &&true;
    while let Range { start: true, end } = t..&&false {}
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR mismatched types

    while let true = let true = true {}
    //~^ ERROR expected expression, found `let` statement
}

fn not_error_because_clarified_intent() {
    if let Range { start: _, end: _ } = (true..true || false) { }

    if let Range { start: _, end: _ } = (true..true && false) { }

    while let Range { start: _, end: _ } = (true..true || false) { }

    while let Range { start: _, end: _ } = (true..true && false) { }
}

fn outside_if_and_while_expr() {
    &let 0 = 0;
    //~^ ERROR expected expression, found `let` statement

    !let 0 = 0;
    //~^ ERROR expected expression, found `let` statement
    *let 0 = 0;
    //~^ ERROR expected expression, found `let` statement
    -let 0 = 0;
    //~^ ERROR expected expression, found `let` statement

    fn _check_try_binds_tighter() -> Result<(), ()> {
        let 0 = 0?;
        //~^ ERROR the `?` operator can only be applied to values that implement `Try`
        Ok(())
    }
    (let 0 = 0)?;
    //~^ ERROR expected expression, found `let` statement

    true || let 0 = 0;
    //~^ ERROR expected expression, found `let` statement
    (true || let 0 = 0);
    //~^ ERROR expected expression, found `let` statement
    true && (true || let 0 = 0);
    //~^ ERROR expected expression, found `let` statement

    let mut x = true;
    x = let 0 = 0;
    //~^ ERROR expected expression, found `let` statement

    true..(let 0 = 0);
    //~^ ERROR expected expression, found `let` statement
    ..(let 0 = 0);
    //~^ ERROR expected expression, found `let` statement
    (let 0 = 0)..;
    //~^ ERROR expected expression, found `let` statement

    (let Range { start: _, end: _ } = true..true || false);
    //~^ ERROR mismatched types
    //~| ERROR expected expression, found `let` statement

    (let true = let true = true);
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR expected expression, found `let` statement

    {
        #[cfg(FALSE)]
        let x = true && let y = 1;
        //~^ ERROR expected expression, found `let` statement
    }

    #[cfg(FALSE)]
    {
        [1, 2, 3][let _ = ()]
        //~^ ERROR expected expression, found `let` statement
    }

    // Check function tail position.
    &let 0 = 0
    //~^ ERROR expected expression, found `let` statement
}

// Let's make sure that `let` inside const generic arguments are considered.
fn inside_const_generic_arguments() {
    struct A<const B: bool>;
    impl<const B: bool> A<{B}> { const O: u32 = 5; }

    if let A::<{
        true && let 1 = 1
        //~^ ERROR expected expression, found `let` statement
    }>::O = 5 {}

    while let A::<{
        true && let 1 = 1
        //~^ ERROR expected expression, found `let` statement
    }>::O = 5 {}

    if A::<{
        true && let 1 = 1
        //~^ ERROR expected expression, found `let` statement
    }>::O == 5 {}

    // In the cases above we have `ExprKind::Block` to help us out.
    // Below however, we would not have a block and so an implementation might go
    // from visiting expressions to types without banning `let` expressions down the tree.
    // This tests ensures that we are not caught by surprise should the parser
    // admit non-IDENT expressions in const generic arguments.

    if A::<
        true && let 1 = 1
        //~^ ERROR expressions must be enclosed in braces
        //~| ERROR expected expression, found `let` statement
    >::O == 5 {}
}

fn with_parenthesis() {
    let opt = Some(Some(1i32));

    if (let Some(a) = opt && true) {
    //~^ ERROR expected expression, found `let` statement
    }

    if (let Some(a) = opt) && true {
    //~^ ERROR expected expression, found `let` statement
    }
    if (let Some(a) = opt) && (let Some(b) = a) {
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR expected expression, found `let` statement
    }
    if let Some(a) = opt && (true && true) {
    }

    if (let Some(a) = opt && (let Some(b) = a)) && b == 1 {
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR expected expression, found `let` statement
    }
    if (let Some(a) = opt && (let Some(b) = a)) && true {
    //~^ ERROR expected expression, found `let` statement
    //~| ERROR expected expression, found `let` statement
    }
    if (let Some(a) = opt && (true)) && true {
    //~^ ERROR expected expression, found `let` statement
    }

    if (true && (true)) && let Some(a) = opt {
    }
    if (true) && let Some(a) = opt {
    }
    if true && let Some(a) = opt {
    }

    let fun = || true;
    if let true = (true && fun()) && (true) {
    }

    #[cfg(FALSE)]
    let x = (true && let y = 1);
    //~^ ERROR expected expression, found `let` statement

    #[cfg(FALSE)]
    {
        ([1, 2, 3][let _ = ()])
        //~^ ERROR expected expression, found `let` statement
    }
}