Commit 2276bb7
committed
Handle default as middle case
C++ allows:
switch (x) {
default:
...
case 1:
...
}
In rust, default needs to be always on the last position, otherwise,
all values of x, even 1, will hit the default arm. Hence, the above C++
exmaple becomes:
match x {
1 => ...
_ => ...
}
As such, the algorithm for translating the cases becomes:
for (case: GetTopLevelSwitchCases()) {
if (ChainContainsDefault(case)) {
defer the conversion for the end
}
Convert(case)
Convert(GetSwitchArmBody(case))
}
Convert(deferred default)
ChainContainsDefault traverses the stacked case statements starting from
a top level case. For example:
case 2:
case 3:
default:
...
is deferred for the end of the match arms and is translated as:
_ => {}
i.e., drop the case 2, case 3 from the output, only convert as if it was
only default.1 parent 4c601da commit 2276bb7
2 files changed
Lines changed: 88 additions & 36 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2579 | 2579 | | |
2580 | 2580 | | |
2581 | 2581 | | |
2582 | | - | |
2583 | | - | |
2584 | | - | |
| 2582 | + | |
| 2583 | + | |
| 2584 | + | |
| 2585 | + | |
| 2586 | + | |
| 2587 | + | |
| 2588 | + | |
| 2589 | + | |
| 2590 | + | |
| 2591 | + | |
| 2592 | + | |
| 2593 | + | |
| 2594 | + | |
| 2595 | + | |
| 2596 | + | |
| 2597 | + | |
| 2598 | + | |
| 2599 | + | |
| 2600 | + | |
| 2601 | + | |
| 2602 | + | |
| 2603 | + | |
| 2604 | + | |
2585 | 2605 | | |
2586 | | - | |
| 2606 | + | |
| 2607 | + | |
2587 | 2608 | | |
2588 | | - | |
2589 | | - | |
| 2609 | + | |
| 2610 | + | |
| 2611 | + | |
| 2612 | + | |
2590 | 2613 | | |
| 2614 | + | |
| 2615 | + | |
2591 | 2616 | | |
2592 | | - | |
2593 | | - | |
2594 | | - | |
2595 | | - | |
2596 | | - | |
2597 | | - | |
2598 | | - | |
| 2617 | + | |
| 2618 | + | |
| 2619 | + | |
| 2620 | + | |
| 2621 | + | |
| 2622 | + | |
| 2623 | + | |
| 2624 | + | |
| 2625 | + | |
| 2626 | + | |
| 2627 | + | |
| 2628 | + | |
| 2629 | + | |
| 2630 | + | |
| 2631 | + | |
| 2632 | + | |
| 2633 | + | |
| 2634 | + | |
| 2635 | + | |
| 2636 | + | |
| 2637 | + | |
| 2638 | + | |
| 2639 | + | |
| 2640 | + | |
| 2641 | + | |
| 2642 | + | |
| 2643 | + | |
| 2644 | + | |
2599 | 2645 | | |
| 2646 | + | |
| 2647 | + | |
| 2648 | + | |
2600 | 2649 | | |
2601 | 2650 | | |
2602 | | - | |
| 2651 | + | |
| 2652 | + | |
| 2653 | + | |
| 2654 | + | |
| 2655 | + | |
2603 | 2656 | | |
2604 | 2657 | | |
2605 | 2658 | | |
2606 | 2659 | | |
| 2660 | + | |
| 2661 | + | |
| 2662 | + | |
2607 | 2663 | | |
2608 | 2664 | | |
2609 | 2665 | | |
2610 | 2666 | | |
2611 | 2667 | | |
2612 | | - | |
2613 | | - | |
2614 | | - | |
2615 | | - | |
2616 | | - | |
2617 | | - | |
2618 | 2668 | | |
2619 | | - | |
2620 | | - | |
2621 | | - | |
2622 | | - | |
2623 | | - | |
2624 | | - | |
2625 | | - | |
2626 | | - | |
2627 | | - | |
2628 | | - | |
2629 | 2669 | | |
2630 | | - | |
2631 | | - | |
2632 | | - | |
| 2670 | + | |
| 2671 | + | |
| 2672 | + | |
| 2673 | + | |
| 2674 | + | |
| 2675 | + | |
| 2676 | + | |
| 2677 | + | |
| 2678 | + | |
| 2679 | + | |
2633 | 2680 | | |
2634 | | - | |
2635 | 2681 | | |
2636 | 2682 | | |
2637 | 2683 | | |
2638 | | - | |
| 2684 | + | |
| 2685 | + | |
| 2686 | + | |
| 2687 | + | |
| 2688 | + | |
| 2689 | + | |
| 2690 | + | |
2639 | 2691 | | |
2640 | 2692 | | |
| 2693 | + | |
2641 | 2694 | | |
2642 | 2695 | | |
2643 | 2696 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
462 | 462 | | |
463 | 463 | | |
464 | 464 | | |
465 | | - | |
466 | 465 | | |
467 | 466 | | |
468 | 467 | | |
| |||
0 commit comments