aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/tests/patterns.rs
blob: af291092d30c9cfe62298117080697b38f505bd1 (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use insta::assert_snapshot;
use test_utils::covers;

use super::{infer, infer_with_mismatches};

#[test]
fn infer_pattern() {
    assert_snapshot!(
        infer(r#"
fn test(x: &i32) {
    let y = x;
    let &z = x;
    let a = z;
    let (c, d) = (1, "hello");

    for (e, f) in some_iter {
        let g = e;
    }

    if let [val] = opt {
        let h = val;
    }

    let lambda = |a: u64, b, c: i32| { a + b; c };

    let ref ref_to_x = x;
    let mut mut_x = x;
    let ref mut mut_ref_to_x = x;
    let k = mut_ref_to_x;
}
"#),
        @r###"
    9..10 'x': &i32
    18..369 '{     ...o_x; }': ()
    28..29 'y': &i32
    32..33 'x': &i32
    43..45 '&z': &i32
    44..45 'z': i32
    48..49 'x': &i32
    59..60 'a': i32
    63..64 'z': i32
    74..80 '(c, d)': (i32, &str)
    75..76 'c': i32
    78..79 'd': &str
    83..95 '(1, "hello")': (i32, &str)
    84..85 '1': i32
    87..94 '"hello"': &str
    102..152 'for (e...     }': ()
    106..112 '(e, f)': ({unknown}, {unknown})
    107..108 'e': {unknown}
    110..111 'f': {unknown}
    116..125 'some_iter': {unknown}
    126..152 '{     ...     }': ()
    140..141 'g': {unknown}
    144..145 'e': {unknown}
    158..205 'if let...     }': ()
    165..170 '[val]': [{unknown}]
    166..169 'val': {unknown}
    173..176 'opt': [{unknown}]
    177..205 '{     ...     }': ()
    191..192 'h': {unknown}
    195..198 'val': {unknown}
    215..221 'lambda': |u64, u64, i32| -> i32
    224..256 '|a: u6...b; c }': |u64, u64, i32| -> i32
    225..226 'a': u64
    233..234 'b': u64
    236..237 'c': i32
    244..256 '{ a + b; c }': i32
    246..247 'a': u64
    246..251 'a + b': u64
    250..251 'b': u64
    253..254 'c': i32
    267..279 'ref ref_to_x': &&i32
    282..283 'x': &i32
    293..302 'mut mut_x': &i32
    305..306 'x': &i32
    316..336 'ref mu...f_to_x': &mut &i32
    339..340 'x': &i32
    350..351 'k': &mut &i32
    354..366 'mut_ref_to_x': &mut &i32
    "###
    );
}

#[test]
fn infer_literal_pattern() {
    assert_snapshot!(
        infer_with_mismatches(r#"
fn any<T>() -> T { loop {} }
fn test(x: &i32) {
    if let "foo" = any() {}
    if let 1 = any() {}
    if let 1u32 = any() {}
    if let 1f32 = any() {}
    if let 1.0 = any() {}
    if let true = any() {}
}
"#, true),
        @r###"
    18..29 '{ loop {} }': T
    20..27 'loop {}': !
    25..27 '{}': ()
    38..39 'x': &i32
    47..209 '{     ...) {} }': ()
    53..76 'if let...y() {}': ()
    60..65 '"foo"': &str
    60..65 '"foo"': &str
    68..71 'any': fn any<&str>() -> &str
    68..73 'any()': &str
    74..76 '{}': ()
    81..100 'if let...y() {}': ()
    88..89 '1': i32
    88..89 '1': i32
    92..95 'any': fn any<i32>() -> i32
    92..97 'any()': i32
    98..100 '{}': ()
    105..127 'if let...y() {}': ()
    112..116 '1u32': u32
    112..116 '1u32': u32
    119..122 'any': fn any<u32>() -> u32
    119..124 'any()': u32
    125..127 '{}': ()
    132..154 'if let...y() {}': ()
    139..143 '1f32': f32
    139..143 '1f32': f32
    146..149 'any': fn any<f32>() -> f32
    146..151 'any()': f32
    152..154 '{}': ()
    159..180 'if let...y() {}': ()
    166..169 '1.0': f64
    166..169 '1.0': f64
    172..175 'any': fn any<f64>() -> f64
    172..177 'any()': f64
    178..180 '{}': ()
    185..207 'if let...y() {}': ()
    192..196 'true': bool
    192..196 'true': bool
    199..202 'any': fn any<bool>() -> bool
    199..204 'any()': bool
    205..207 '{}': ()
    "###
    );
}

#[test]
fn infer_range_pattern() {
    assert_snapshot!(
        infer_with_mismatches(r#"
fn test(x: &i32) {
    if let 1..76 = 2u32 {}
    if let 1..=76 = 2u32 {}
}
"#, true),
        @r###"
    9..10 'x': &i32
    18..76 '{     ...2 {} }': ()
    24..46 'if let...u32 {}': ()
    31..36 '1..76': u32
    39..43 '2u32': u32
    44..46 '{}': ()
    51..74 'if let...u32 {}': ()
    58..64 '1..=76': u32
    67..71 '2u32': u32
    72..74 '{}': ()
    "###
    );
}

#[test]
fn infer_pattern_match_ergonomics() {
    assert_snapshot!(
        infer(r#"
struct A<T>(T);

fn test() {
    let A(n) = &A(1);
    let A(n) = &mut A(1);
}
"#),
    @r###"
    28..79 '{     ...(1); }': ()
    38..42 'A(n)': A<i32>
    40..41 'n': &i32
    45..50 '&A(1)': &A<i32>
    46..47 'A': A<i32>(i32) -> A<i32>
    46..50 'A(1)': A<i32>
    48..49 '1': i32
    60..64 'A(n)': A<i32>
    62..63 'n': &mut i32
    67..76 '&mut A(1)': &mut A<i32>
    72..73 'A': A<i32>(i32) -> A<i32>
    72..76 'A(1)': A<i32>
    74..75 '1': i32
    "###
    );
}

#[test]
fn infer_pattern_match_ergonomics_ref() {
    covers!(match_ergonomics_ref);
    assert_snapshot!(
        infer(r#"
fn test() {
    let v = &(1, &2);
    let (_, &w) = v;
}
"#),
    @r###"
    11..57 '{     ...= v; }': ()
    21..22 'v': &(i32, &i32)
    25..33 '&(1, &2)': &(i32, &i32)
    26..33 '(1, &2)': (i32, &i32)
    27..28 '1': i32
    30..32 '&2': &i32
    31..32 '2': i32
    43..50 '(_, &w)': (i32, &i32)
    44..45 '_': i32
    47..49 '&w': &i32
    48..49 'w': i32
    53..54 'v': &(i32, &i32)
    "###
    );
}

#[test]
fn infer_pattern_match_slice() {
    assert_snapshot!(
        infer(r#"
fn test() {
    let slice: &[f64] = &[0.0];
    match slice {
        &[] => {},
        &[a] => {
            a;
        },
        &[b, c] => {
            b;
            c;
        }
        _ => {}
    }
}
"#),
    @r###"
    11..210 '{     ...   } }': ()
    21..26 'slice': &[f64]
    37..43 '&[0.0]': &[f64; _]
    38..43 '[0.0]': [f64; _]
    39..42 '0.0': f64
    49..208 'match ...     }': ()
    55..60 'slice': &[f64]
    71..74 '&[]': &[f64]
    72..74 '[]': [f64]
    78..80 '{}': ()
    90..94 '&[a]': &[f64]
    91..94 '[a]': [f64]
    92..93 'a': f64
    98..124 '{     ...     }': ()
    112..113 'a': f64
    134..141 '&[b, c]': &[f64]
    135..141 '[b, c]': [f64]
    136..137 'b': f64
    139..140 'c': f64
    145..186 '{     ...     }': ()
    159..160 'b': f64
    174..175 'c': f64
    195..196 '_': &[f64]
    200..202 '{}': ()
    "###
    );
}

#[test]
fn infer_pattern_match_arr() {
    assert_snapshot!(
        infer(r#"
fn test() {
    let arr: [f64; 2] = [0.0, 1.0];
    match arr {
        [1.0, a] => {
            a;
        },
        [b, c] => {
            b;
            c;
        }
    }
}
"#),
    @r###"
    11..180 '{     ...   } }': ()
    21..24 'arr': [f64; _]
    37..47 '[0.0, 1.0]': [f64; _]
    38..41 '0.0': f64
    43..46 '1.0': f64
    53..178 'match ...     }': ()
    59..62 'arr': [f64; _]
    73..81 '[1.0, a]': [f64; _]
    74..77 '1.0': f64
    74..77 '1.0': f64
    79..80 'a': f64
    85..111 '{     ...     }': ()
    99..100 'a': f64
    121..127 '[b, c]': [f64; _]
    122..123 'b': f64
    125..126 'c': f64
    131..172 '{     ...     }': ()
    145..146 'b': f64
    160..161 'c': f64
    "###
    );
}

#[test]
fn infer_adt_pattern() {
    assert_snapshot!(
        infer(r#"
enum E {
    A { x: usize },
    B
}

struct S(u32, E);

fn test() {
    let e = E::A { x: 3 };

    let S(y, z) = foo;
    let E::A { x: new_var } = e;

    match e {
        E::A { x } => x,
        E::B if foo => 1,
        E::B => 10,
    };

    let ref d @ E::A { .. } = e;
    d;
}
"#),
        @r###"
    68..289 '{     ...  d; }': ()
    78..79 'e': E
    82..95 'E::A { x: 3 }': E
    92..93 '3': usize
    106..113 'S(y, z)': S
    108..109 'y': u32
    111..112 'z': E
    116..119 'foo': S
    129..148 'E::A {..._var }': E
    139..146 'new_var': usize
    151..152 'e': E
    159..245 'match ...     }': usize
    165..166 'e': E
    177..187 'E::A { x }': E
    184..185 'x': usize
    191..192 'x': usize
    202..206 'E::B': E
    210..213 'foo': bool
    217..218 '1': usize
    228..232 'E::B': E
    236..238 '10': usize
    256..275 'ref d ...{ .. }': &E
    264..275 'E::A { .. }': E
    278..279 'e': E
    285..286 'd': &E
    "###
    );
}

#[test]
fn infer_generics_in_patterns() {
    assert_snapshot!(
        infer(r#"
struct A<T> {
    x: T,
}

enum Option<T> {
    Some(T),
    None,
}

fn test(a1: A<u32>, o: Option<u64>) {
    let A { x: x2 } = a1;
    let A::<i64> { x: x3 } = A { x: 1 };
    match o {
        Option::Some(t) => t,
        _ => 1,
    };
}
"#),
        @r###"
    79..81 'a1': A<u32>
    91..92 'o': Option<u64>
    107..244 '{     ...  }; }': ()
    117..128 'A { x: x2 }': A<u32>
    124..126 'x2': u32
    131..133 'a1': A<u32>
    143..161 'A::<i6...: x3 }': A<i64>
    157..159 'x3': i64
    164..174 'A { x: 1 }': A<i64>
    171..172 '1': i64
    180..241 'match ...     }': u64
    186..187 'o': Option<u64>
    198..213 'Option::Some(t)': Option<u64>
    211..212 't': u64
    217..218 't': u64
    228..229 '_': Option<u64>
    233..234 '1': u64
    "###
    );
}

#[test]
fn infer_const_pattern() {
    assert_snapshot!(
        infer_with_mismatches(r#"
enum Option<T> { None }
use Option::None;
struct Foo;
const Bar: usize = 1;

fn test() {
    let a: Option<u32> = None;
    let b: Option<i64> = match a {
        None => None,
    };
    let _: () = match () { Foo => Foo }; // Expected mismatch
    let _: () = match () { Bar => Bar }; // Expected mismatch
}
"#, true),
        @r###"
    74..75 '1': usize
    88..310 '{     ...atch }': ()
    98..99 'a': Option<u32>
    115..119 'None': Option<u32>
    129..130 'b': Option<i64>
    146..183 'match ...     }': Option<i64>
    152..153 'a': Option<u32>
    164..168 'None': Option<u32>
    172..176 'None': Option<i64>
    193..194 '_': ()
    201..224 'match ... Foo }': Foo
    207..209 '()': ()
    212..215 'Foo': Foo
    219..222 'Foo': Foo
    255..256 '_': ()
    263..286 'match ... Bar }': usize
    269..271 '()': ()
    274..277 'Bar': usize
    281..284 'Bar': usize
    201..224: expected (), got Foo
    263..286: expected (), got usize
    "###
    );
}

#[test]
fn infer_guard() {
    assert_snapshot!(
        infer(r#"
struct S;
impl S { fn foo(&self) -> bool { false } }

fn main() {
    match S {
        s if s.foo() => (),
    }
}
    "#), @"
        28..32 'self': &S
        42..51 '{ false }': bool
        44..49 'false': bool
        65..116 '{     ...   } }': ()
        71..114 'match ...     }': ()
        77..78 'S': S
        89..90 's': S
        94..95 's': S
        94..101 's.foo()': bool
        105..107 '()': ()
    ")
}