Commit d34ffc5
committed
feat(analyzer): rewrite
This commit introduces a complete rewrite of the `match` expression analyzer to resolve numerous bugs and correctness issues in the previous implementation.
The old analyzer struggled to correctly model the control flow and type narrowing within a `match` block, leading to false positives and missed errors related to unreachable arms and exhaustiveness.
The new implementation correctly simulates `match` semantics by sequentially analyzing each arm, applying its conditions as assertions to narrow the subject's type, and carrying the remaining (unhandled) subject type forward to subsequent arms.
Key capabilities of the new analyzer include:
- **Accurate Exhaustiveness Checking**: Correctly determines if all possible types of the subject are handled and reports `MatchNotExhaustive` if not.
- **Precise Unreachable Code Detection**: Identifies unreachable arms that are shadowed by previous arms or whose conditions can never be met.
- **Redundant Condition Analysis**: Finds arms that are *always* true given the remaining subject type, making subsequent arms unreachable.
- **Correct Type Inference**: Reliably infers the final return type by combining the types of all reachable arms.
This rewrite provides a robust and correct foundation for analyzing `match` expressions, leading to far more accurate and useful diagnostics.
closes #260
Signed-off-by: azjezz <[email protected]>match expression analyzer for correctness1 parent 1ba43ac commit d34ffc5
File tree
13 files changed
+914
-1145
lines changed- crates
- analyzer
- src
- common
- context
- expression
- binary
- statement
- tests
- cases
- span/src
13 files changed
+914
-1145
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
47 | 61 | | |
48 | 62 | | |
49 | 63 | | |
| |||
331 | 345 | | |
332 | 346 | | |
333 | 347 | | |
334 | | - | |
| 348 | + | |
335 | 349 | | |
336 | 350 | | |
337 | 351 | | |
338 | | - | |
339 | | - | |
| 352 | + | |
| 353 | + | |
340 | 354 | | |
341 | 355 | | |
342 | | - | |
| 356 | + | |
343 | 357 | | |
344 | 358 | | |
345 | 359 | | |
| |||
410 | 424 | | |
411 | 425 | | |
412 | 426 | | |
413 | | - | |
| 427 | + | |
414 | 428 | | |
415 | 429 | | |
416 | 430 | | |
417 | | - | |
| 431 | + | |
418 | 432 | | |
419 | 433 | | |
420 | 434 | | |
421 | | - | |
| 435 | + | |
422 | 436 | | |
423 | 437 | | |
424 | 438 | | |
| |||
1142 | 1156 | | |
1143 | 1157 | | |
1144 | 1158 | | |
| 1159 | + | |
1145 | 1160 | | |
1146 | 1161 | | |
1147 | 1162 | | |
| |||
1166 | 1181 | | |
1167 | 1182 | | |
1168 | 1183 | | |
1169 | | - | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
1170 | 1188 | | |
1171 | | - | |
1172 | | - | |
1173 | 1189 | | |
1174 | 1190 | | |
1175 | 1191 | | |
| |||
1473 | 1489 | | |
1474 | 1490 | | |
1475 | 1491 | | |
1476 | | - | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
1477 | 1497 | | |
1478 | 1498 | | |
1479 | 1499 | | |
1480 | 1500 | | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
1481 | 1507 | | |
1482 | 1508 | | |
1483 | 1509 | | |
1484 | 1510 | | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
1485 | 1517 | | |
1486 | 1518 | | |
1487 | 1519 | | |
1488 | 1520 | | |
1489 | | - | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
1490 | 1526 | | |
1491 | 1527 | | |
1492 | 1528 | | |
1493 | 1529 | | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
1494 | 1536 | | |
1495 | 1537 | | |
1496 | 1538 | | |
1497 | 1539 | | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
1498 | 1546 | | |
1499 | 1547 | | |
1500 | 1548 | | |
1501 | 1549 | | |
1502 | | - | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
1503 | 1555 | | |
1504 | 1556 | | |
1505 | 1557 | | |
1506 | 1558 | | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
1507 | 1565 | | |
1508 | 1566 | | |
1509 | 1567 | | |
1510 | 1568 | | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
1511 | 1575 | | |
1512 | 1576 | | |
1513 | 1577 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
117 | | - | |
118 | | - | |
119 | 117 | | |
120 | 118 | | |
121 | 119 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
41 | 54 | | |
42 | 55 | | |
43 | 56 | | |
| |||
49 | 62 | | |
50 | 63 | | |
51 | 64 | | |
52 | | - | |
53 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
54 | 71 | | |
55 | 72 | | |
56 | 73 | | |
57 | | - | |
| 74 | + | |
58 | 75 | | |
59 | 76 | | |
60 | 77 | | |
61 | | - | |
| 78 | + | |
62 | 79 | | |
63 | 80 | | |
64 | 81 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| 16 | + | |
| 17 | + | |
15 | 18 | | |
| 19 | + | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
| |||
24 | 28 | | |
25 | 29 | | |
26 | 30 | | |
27 | | - | |
28 | 31 | | |
29 | 32 | | |
| 33 | + | |
30 | 34 | | |
31 | 35 | | |
32 | 36 | | |
| |||
666 | 670 | | |
667 | 671 | | |
668 | 672 | | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
669 | 736 | | |
670 | 737 | | |
671 | 738 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
348 | 348 | | |
349 | 349 | | |
350 | 350 | | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
351 | 357 | | |
352 | 358 | | |
353 | 359 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
| 115 | + | |
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| |||
473 | 473 | | |
474 | 474 | | |
475 | 475 | | |
476 | | - | |
| 476 | + | |
477 | 477 | | |
478 | 478 | | |
479 | 479 | | |
| |||
642 | 642 | | |
643 | 643 | | |
644 | 644 | | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
645 | 651 | | |
646 | 652 | | |
647 | 653 | | |
| |||
0 commit comments