cairo-lang-semantic 2.18.0

Cairo semantic model.
Documentation
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! > Test match

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(b: A) -> felt252 {
    match (b, 4) {
        (A::a(_x), _) => { 1 },
        (A::b(x), 1) => { x },
        (7, 1) => { x },
        (A::b(x), 1, _) => { x },
    }
    let x = (5, true);
    let (y, _) = x;
    y
}

//! > function_name
foo

//! > module_code
enum A {
    a: (),
    b: felt252,
}

//! > expected_diagnostics
error[E0006]: Identifier not found.
 --> lib.cairo:9:21
        (7, 1) => { x },
                    ^

error[E2107]: Wrong number of tuple elements in pattern. Expected: 2. Got: 3.
 --> lib.cairo:10:9
        (A::b(x), 1, _) => { x },
        ^^^^^^^^^^^^^^^

error[E0006]: Identifier not found.
 --> lib.cairo:10:30
        (A::b(x), 1, _) => { x },
                             ^

error[E2311]: Mismatched types. The type `test::A` cannot be created from a numeric literal.
 --> lib.cairo:9:10
        (7, 1) => { x },
         ^

//! > ==========================================================================

//! > Match with missing type

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(a: bool) -> felt252 {
    match a + 1 {
        A::a(_) => 0,
        A::b(_) => 1,
    }
}

//! > function_name
foo

//! > module_code
enum A {
    a: (),
    b: felt252,
}

//! > expected_diagnostics
error[E2109]: Wrong enum in pattern. Expected: "bool". Got: "A".
 --> lib.cairo:7:9
        A::a(_) => 0,
        ^^^^^^^

error[E2109]: Wrong enum in pattern. Expected: "bool". Got: "A".
 --> lib.cairo:8:9
        A::b(_) => 1,
        ^^^^^^^

error[E2311]: Trait has no implementation in context: core::traits::Add::<core::bool>.
 --> lib.cairo:6:11
    match a + 1 {
          ^^^^^

//! > ==========================================================================

//! > Match incompatible arm types

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(a: felt252, b: bool) -> felt252 {
    match a {
        0 => 0_felt252,
        _ => b,
    }
}

//! > function_name
foo

//! > module_code
enum A {
    a: (),
    b: felt252,
}

//! > expected_diagnostics
error[E2056]: Match arms have incompatible types: "core::felt252" and "core::bool"
 --> lib.cairo:8:14
        _ => b,
             ^

//! > ==========================================================================

//! > Match incompatible literal bool patterns

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(a: A) -> felt252 {
    match a {
        true => 2,
        false => 3,
    }
}

//! > function_name
foo

//! > module_code
enum A {
    a: (),
    b: felt252,
}

//! > expected_diagnostics
error[E2109]: Wrong enum in pattern. Expected: "A". Got: "bool".
 --> lib.cairo:7:9
        true => 2,
        ^^^^

error[E2109]: Wrong enum in pattern. Expected: "A". Got: "bool".
 --> lib.cairo:8:9
        false => 3,
        ^^^^^

//! > ==========================================================================

//! > Match arm types incompatible, also with expected type.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(x: Option<()>) {
    match x {
        Some(_) => true,
        None => 0,
    }
}

//! > function_name
foo

//! > expected_diagnostics
error[E2042]: Unexpected return type. Expected: "()", found: "core::bool".
 --> lib.cairo:1:23-6:1
  fn foo(x: Option<()>) {
 _______________________^
| ...
| }
|_^

//! > ==========================================================================

//! > Test match with or pattern inconsistent mutability

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(a: MyEnum) -> felt252 {
    match a {
        MyEnum::A(mut x) | MyEnum::B((x, _)) => x,
        MyEnum::C((x, _, t)) | MyEnum::D(P { x, z: mut t, .. }) => x + t,
    }
}

//! > function_name
foo

//! > module_code
struct P {
    x: felt252,
    y: felt252,
    z: felt252,
    w: felt252,
}
enum MyEnum {
    A: felt252,
    B: (felt252, felt252),
    C: (felt252, felt252, felt252),
    D: P,
}

//! > expected_diagnostics
error[E2040]: variable is bound inconsistently across alternatives separated by `|` bound in different ways
 --> lib.cairo:15:39
        MyEnum::A(mut x) | MyEnum::B((x, _)) => x,
                                      ^

error[E2040]: variable is bound inconsistently across alternatives separated by `|` bound in different ways
 --> lib.cairo:16:52
        MyEnum::C((x, _, t)) | MyEnum::D(P { x, z: mut t, .. }) => x + t,
                                                   ^^^^^

//! > ==========================================================================

//! > Test conform in or pattern

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    let a = MyEnum::A(5);
    match a {
        MyEnum::A(x) | MyEnum::B((x, _)) => x,
    }
    bar(a);
}

//! > function_name
foo

//! > module_code
enum MyEnum<T> {
    A: T,
    B: (felt252, felt252),
}
fn bar(a: MyEnum<u32>) {}

//! > expected_diagnostics
error[E2041]: Unexpected argument type. Expected: "test::MyEnum::<core::integer::u32>", found: "test::MyEnum::<core::felt252>".
 --> lib.cairo:11:9
    bar(a);
        ^

//! > ==========================================================================

//! > Test match on variant without args.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(a: A, b: A) -> felt252 {
    match (a, b) {
        (A::One, _) => 2,
        (_, _) => 6,
    }
}

//! > function_name
foo

//! > module_code
#[derive(Copy, Drop)]
enum A {
    One: felt252,
}

//! > expected_diagnostics
warning[E2192]: Pattern missing subpattern for the payload of variant. Consider using `A::One(_)`
 --> lib.cairo:7:10
        (A::One, _) => 2,
         ^^^^^^

//! > ==========================================================================

//! > Test match on unit variant without args.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo(a: A, b: A) -> felt252 {
    match (a, b) {
        (A::One(_), _) => 2,
        (A::Two, _) => 6,
    }
}

//! > function_name
foo

//! > module_code
#[derive(Copy, Drop)]
enum A {
    One: felt252,
    Two: (),
}

//! > expected_diagnostics

//! > ==========================================================================

//! > Test match on unit variant with args.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo(a: A, b: A) -> felt252 {
    match (a, b) {
        (A::One(_), _) => 2,
        (A::Two(_), _) => 6,
    }
}

//! > function_name
foo

//! > module_code
#[derive(Copy, Drop)]
enum A {
    One: felt252,
    Two: (),
}

//! > expected_diagnostics

//! > ==========================================================================

//! > Test match on Some without args.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo<T>(a: Option<T>) -> felt252 {
    match a {
        Some => 2,
        None => 6,
    }
}
fn bar() -> felt252 {
    let a = Some(());
    foo(a)
}

//! > function_name
bar

//! > expected_diagnostics
warning[E2192]: Pattern missing subpattern for the payload of variant. Consider using `Some(_)`
 --> lib.cairo:3:9
        Some => 2,
        ^^^^

//! > ==========================================================================

//! > Test match on Path no args.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo(a: A, b: A) -> felt252 {
    match (a, b) {
        (A::One(_), _) => 2,
        (Two, _) => 6,
    }
}

//! > function_name
foo

//! > module_code
#[derive(Copy, Drop)]
enum A {
    One: felt252,
    Two: (),
}
use A::Two;

//! > expected_diagnostics

//! > ==========================================================================

//! > Test match on inferred unit Some without args.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn fb() -> Option<()> {
    let x = None;
    match x {
        Some => 2,
        None => 6,
    }
    return x;
}

//! > function_name
fb

//! > expected_diagnostics
warning[E2192]: Pattern missing subpattern for the payload of variant. Consider using `Some(_)`
 --> lib.cairo:4:9
        Some => 2,
        ^^^^

//! > ==========================================================================

//! > Test match on inferred unit Option::Some without args.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn fb() -> Option<()> {
    let x = None;
    match x {
        Option::Some => 2,
        Option::None => 6,
    }
    return x;
}

//! > function_name
fb

//! > expected_diagnostics
warning[E2192]: Pattern missing subpattern for the payload of variant. Consider using `Option::Some(_)`
 --> lib.cairo:4:9
        Option::Some => 2,
        ^^^^^^^^^^^^

//! > ==========================================================================

//! > Conform enum generic args in match patterns.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() -> u32 {
    let x = Some(3);
    match x {
        Some::<u32>(y) => y,
        None::<u8> => 6,
    }
}

//! > function_name
foo

//! > expected_diagnostics
error[E2302]: Type mismatch: `core::option::Option::<core::integer::u32>` and `core::option::Option::<core::integer::u8>`.
 --> lib.cairo:5:9
        None::<u8> => 6,
        ^^^^^^^^^^

//! > ==========================================================================

//! > Test bad type in multi level enum pattern variable binding.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(x: Result<Option<u8>, felt252>) {
    let _y = match x {
        Ok(Some(x)) | Err(x) => x,
        _ => 3,
    };
}

//! > function_name
foo

//! > expected_diagnostics
error[E2039]: Expected type "core::integer::u8", found: "core::felt252".
 --> lib.cairo:3:27
        Ok(Some(x)) | Err(x) => x,
                          ^

//! > ==========================================================================

//! > Test bad inner pattern type in multi level enum pattern.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(x: Result<Option<u8>, felt252>) {
    let _y = match x {
        Ok(Ok(x)) | Err(x) => x,
        _ => 3,
    };
}

//! > function_name
foo

//! > expected_diagnostics
error[E2109]: Wrong enum in pattern. Expected: "Option". Got: "Result".
 --> lib.cairo:3:12
        Ok(Ok(x)) | Err(x) => x,
           ^^^^^

error[E2048]: Missing variable in pattern.
 --> lib.cairo:3:9
        Ok(Ok(x)) | Err(x) => x,
        ^^^^^^^^^

//! > ==========================================================================

//! > Test value match after enum match.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn bar(x: felt252) {
    foo(Ok(Some(x)));
}

//! > function_name
bar

//! > module_code
fn foo<T>(x: Result<Option<T>, felt252>) {
    let _y = match x {
        Ok(Some(x)) | Err(x) => x,
        _ => 3,
    };
}

//! > expected_diagnostics
error[E2039]: Expected type "T", found: "core::felt252".
 --> lib.cairo:3:27
        Ok(Some(x)) | Err(x) => x,
                          ^