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
//! > pattern type for a param.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo(x: (felt252, felt252)) {}

//! > function_name
foo

//! > expected_diagnostics

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

//! > Valid destructures of a struct.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo(s2: Struct2) {
    let Struct2 { member1: _, member2: _, member3: _ } = s2;
    let Struct2 { member1: _, member2: _, .. } = s2;
    let Struct2 { member1: _, member2: _, member3: Struct1 { member1: _a, .. } } = s2;
}

//! > function_name
foo

//! > module_code
struct Struct1 {
    member1: felt252,
    member2: (),
}
struct Struct2 {
    member1: felt252,
    member2: (),
    member3: Struct1,
}

//! > expected_diagnostics

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

//! > Test struct pattern with missing members.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(s: MyStruct) {
    let MyStruct { a: _ } = s;
}

//! > function_name
foo

//! > module_code
struct MyStruct {
    a: usize,
    b: usize,
}

//! > expected_diagnostics
error[E0003]: Missing member "b".
 --> lib.cairo:6:9
    let MyStruct { a: _ } = s;
        ^^^^^^^^^^^^^^^^^

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

//! > Test struct pattern with non existing members.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(s: MyStruct) {
    let MyStruct { a, b } = s;
}

//! > function_name
foo

//! > module_code
struct MyStruct {}

//! > expected_diagnostics
error[E2058]: Struct "MyStruct" has no member "a"
 --> lib.cairo:3:20
    let MyStruct { a, b } = s;
                   ^

error[E2058]: Struct "MyStruct" has no member "b"
 --> lib.cairo:3:23
    let MyStruct { a, b } = s;
                      ^

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

//! > Test struct pattern with members redefinition.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(s: MyStruct) {
    let MyStruct { a: _, b: _, a: _, b: _ } = s;
}

//! > function_name
foo

//! > module_code
struct MyStruct {
    a: usize,
    b: usize,
}

//! > expected_diagnostics
error[E2050]: Redefinition of member "a" on struct "MyStruct".
 --> lib.cairo:6:32
    let MyStruct { a: _, b: _, a: _, b: _ } = s;
                               ^

error[E2050]: Redefinition of member "b" on struct "MyStruct".
 --> lib.cairo:6:38
    let MyStruct { a: _, b: _, a: _, b: _ } = s;
                                     ^

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

//! > Test struct pattern with non existing members, members redefinition and missing members.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(s: MyStruct) {
    let MyStruct { a: _, c: _, a: _ } = s;
}

//! > function_name
foo

//! > module_code
struct MyStruct {
    a: usize,
    b: usize,
}

//! > expected_diagnostics
error[E2058]: Struct "MyStruct" has no member "c"
 --> lib.cairo:6:26
    let MyStruct { a: _, c: _, a: _ } = s;
                         ^

error[E2050]: Redefinition of member "a" on struct "MyStruct".
 --> lib.cairo:6:32
    let MyStruct { a: _, c: _, a: _ } = s;
                               ^

error[E0003]: Missing member "b".
 --> lib.cairo:6:9
    let MyStruct { a: _, c: _, a: _ } = s;
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

//! > Test fixed size array pattern incompatible number of elements.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(s: [felt252; 3]) {
    let [_a, _b] = s;
}

//! > function_name
foo

//! > expected_diagnostics
error[E2108]: Wrong number of fixed size array elements in pattern. Expected: 3. Got: 2.
 --> lib.cairo:2:9
    let [_a, _b] = s;
        ^^^^^^^^

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

//! > Test tuple assigned to a fixed size array pattern.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(s: (felt252, felt252, felt252)) {
    let [_a, _b, _c] = s;
}

//! > function_name
foo

//! > expected_diagnostics
error[E2103]: Mismatched types: pattern cannot match against type "(core::felt252, core::felt252, core::felt252)".
 --> lib.cairo:2:9
    let [_a, _b, _c] = s;
        ^^^^^^^^^^^^

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

//! > Test fixed size array assigned to a tuple pattern.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(s: [felt252; 3]) {
    let (_a, _b, _c) = s;
}

//! > function_name
foo

//! > expected_diagnostics
error[E2103]: Mismatched types: pattern cannot match against type "[core::felt252; 3]".
 --> lib.cairo:2:9
    let (_a, _b, _c) = s;
        ^^^^^^^^^^^^

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

//! > Avoid crash on unknown patterns.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(s: [felt252; 3]) {
    let (unresolved,) = if true {} else {
        (array![(1, 2, 3)],)
    };
    for (_x, _y, _z) in unresolved.span() {}
}

//! > function_name
foo

//! > expected_diagnostics
error[E2056]: If blocks have incompatible types: "()" and "(core::array::Array::<(?3, ?4, ?5)>,)"
 --> lib.cairo:2:25-4:5
      let (unresolved,) = if true {} else {
 _________________________^
|         (array![(1, 2, 3)],)
|     };
|_____^

error[E2107]: Wrong number of tuple elements in pattern. Expected: 0. Got: 1.
 --> lib.cairo:2:9
    let (unresolved,) = if true {} else {
        ^^^^^^^^^^^^^

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

//! > Test duplicate variables in tuple pattern let.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    let (_a, _a) = (1, 2);
}

//! > function_name
foo

//! > expected_diagnostics
error[E2049]: Redefinition of variable name "_a" in pattern.
 --> lib.cairo:2:14
    let (_a, _a) = (1, 2);
             ^^

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

//! > Test duplicate variables in match patterns.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    match (1, 2) {
        (_a, _a) => (),
    }

    match ((1, 2), 3) {
        ((_b, _), _b) => (),
    };
}

//! > function_name
foo

//! > expected_diagnostics
error[E2049]: Redefinition of variable name "_a" in pattern.
 --> lib.cairo:3:14
        (_a, _a) => (),
             ^^

error[E2049]: Redefinition of variable name "_b" in pattern.
 --> lib.cairo:7:19
        ((_b, _), _b) => (),
                  ^^

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

//! > Test span into fixed size array pattern.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo(s: Span<u32>) -> u32 {
    let [a, b] = s else {
        return 0;
    };
    *a + *b
}

//! > function_name
foo

//! > module_code

//! > expected_diagnostics

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

//! > Test type mismatch in span pattern arm surfaces as confusing ToSpanTrait error.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    let a: Array<u8> = array![1, 2, 3];
    let c: u16 = 0;
    match a.span() {
        [x, y, z] => c + *x + *y + *z,
        _ => 0,
    };
}

//! > function_name
foo

//! > module_code

//! > expected_diagnostics
error[E2311]: Trait has no implementation in context: core::array::ToSpanTrait::<core::array::Array::<core::integer::u8>, core::integer::u16>.
 --> lib.cairo:4:13
    match a.span() {
            ^^^^

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

//! > Test array pattern on non-Span generic struct.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(s: MyStruct<u8>) {
    let [_x, _y] = s;
}

//! > function_name
foo

//! > module_code
struct MyStruct<T> {
    value: T,
}

//! > expected_diagnostics
error[E2103]: Mismatched types: pattern cannot match against type "test::MyStruct::<core::integer::u8>".
 --> lib.cairo:5:9
    let [_x, _y] = s;
        ^^^^^^^^

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

//! > Test array pattern on Array instead of Span.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    let a: Array<u8> = array![1, 2, 3];
    match a {
        [_x, _y, _z] => {},
        _ => {},
    };
}

//! > function_name
foo

//! > module_code

//! > expected_diagnostics
error[E2103]: Mismatched types: pattern cannot match against type "core::array::Array::<core::integer::u8>".
 --> lib.cairo:4:9
        [_x, _y, _z] => {},
        ^^^^^^^^^^^^

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

//! > Test array pattern on fixed size array value.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() {
    let a: [u8; 3] = [1, 2, 3];
    match a {
        [_x, _y, _z] => {},
        _ => {},
    };
}

//! > function_name
foo

//! > module_code

//! > expected_diagnostics

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

//! > Test invalid patterns on Span value.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(s: Span<u32>) -> u32 {
    match s {
        [a, b] => *a + *b,
        5 => 0,
        (a, b) => *a + *b,
        Some(a) => *a,
    }
}

//! > function_name
foo

//! > module_code

//! > expected_diagnostics
error[E2103]: Mismatched types: pattern cannot match against type "core::array::Span::<core::integer::u32>".
 --> lib.cairo:5:9
        (a, b) => *a + *b,
        ^^^^^^

error[E2135]: Type `<missing>` cannot be dereferenced
 --> lib.cairo:5:19
        (a, b) => *a + *b,
                  ^

error[E2135]: Type `<missing>` cannot be dereferenced
 --> lib.cairo:5:24
        (a, b) => *a + *b,
                       ^

error[E2103]: Mismatched types: pattern cannot match against type "core::array::Span::<core::integer::u32>".
 --> lib.cairo:6:9
        Some(a) => *a,
        ^^^^^^^

error[E0006]: Identifier not found.
 --> lib.cairo:6:21
        Some(a) => *a,
                    ^

error[E2135]: Type `<missing>` cannot be dereferenced
 --> lib.cairo:6:20
        Some(a) => *a,
                   ^

error[E2311]: Mismatched types. The type `core::array::Span::<core::integer::u32>` cannot be created from a numeric literal.
 --> lib.cairo:4:9
        5 => 0,
        ^