lang: Lower scalar matches with any prong count

9a164eae4a7468c0b450e2b2bd589b203719ecb2d08c373ca52a6369defb945c
Lowering a scalar match with more than 32 prongs wrote past a fixed
block array and crashed the compiler.

The block list is now allocated from the function arena with one entry
per prong, so the number of prongs has no fixed limit.

Assisted-by: Claude:opus-5.5
Alexis Sellier committed ago 1 parent fbd7ece9
lib/std/lang/lower.rad +4 -4
106 106
// happen, since they are potentially truncating values.
107 107
108 108
// TODO: Support constant union lowering.
109 109
// TODO: Void unions should be passed by value.
110 110
111 -
/// Maximum prongs stored while lowering a scalar switch.
112 -
constant MAX_SWITCH_PRONGS: u32 = 32;
113 -
114 111
////////////////////
115 112
// Error Handling //
116 113
////////////////////
117 114
118 115
/// Lowering errors are typically unrecoverable since they indicate bugs in
5260 5257
    self: &mut Lowerer,
5261 5258
    prongs: *[*ast::Node],
5262 5259
    subject: &MatchSubject,
5263 5260
    mergeBlock: &mut ?BlockId
5264 5261
) throws (LowerError) {
5265 -
    let mut blocks: [BlockId; MAX_SWITCH_PRONGS] = undefined;
5262 +
    let mut blocks: *mut [BlockId] = &mut [];
5263 +
    try blocks.resize(prongs.len, BlockId(0), &mut self.fnArena) catch {
5264 +
        throw LowerError::AllocationFailed;
5265 +
    };
5266 5266
    let caseStart = self.caseList.len;
5267 5267
    let mut defaultIdx: u32 = 0;
5268 5268
    let entry = currentBlock(self);
5269 5269
    // A switch compares whole registers, and a narrow subject has undefined high
5270 5270
    // bits. The subject is extended as comparisons extend it: words and signed
test/tests/match.many.rad added +61 -0
1 +
//! returns: 0
2 +
//! A scalar match is not limited in its number of prongs. This match has
3 +
//! 40 cases and an `else` prong.
4 +
5 +
fn pick(x: u32) -> u32 {
6 +
    match x {
7 +
        case 0  => return 1,
8 +
        case 1  => return 4,
9 +
        case 2  => return 7,
10 +
        case 3  => return 10,
11 +
        case 4  => return 13,
12 +
        case 5  => return 16,
13 +
        case 6  => return 19,
14 +
        case 7  => return 22,
15 +
        case 8  => return 25,
16 +
        case 9  => return 28,
17 +
        case 10 => return 31,
18 +
        case 11 => return 34,
19 +
        case 12 => return 37,
20 +
        case 13 => return 40,
21 +
        case 14 => return 43,
22 +
        case 15 => return 46,
23 +
        case 16 => return 49,
24 +
        case 17 => return 52,
25 +
        case 18 => return 55,
26 +
        case 19 => return 58,
27 +
        case 20 => return 61,
28 +
        case 21 => return 64,
29 +
        case 22 => return 67,
30 +
        case 23 => return 70,
31 +
        case 24 => return 73,
32 +
        case 25 => return 76,
33 +
        case 26 => return 79,
34 +
        case 27 => return 82,
35 +
        case 28 => return 85,
36 +
        case 29 => return 88,
37 +
        case 30 => return 91,
38 +
        case 31 => return 94,
39 +
        case 32 => return 97,
40 +
        case 33 => return 100,
41 +
        case 34 => return 103,
42 +
        case 35 => return 106,
43 +
        case 36 => return 109,
44 +
        case 37 => return 112,
45 +
        case 38 => return 115,
46 +
        case 39 => return 118,
47 +
        else    => return 1000,
48 +
    }
49 +
}
50 +
51 +
@default fn main() -> i32 {
52 +
    assert pick(0) == 1;
53 +
    assert pick(1) == 4;
54 +
    assert pick(17) == 52;
55 +
    assert pick(32) == 97;
56 +
    assert pick(33) == 100;
57 +
    assert pick(39) == 118;
58 +
    assert pick(40) == 1000;
59 +
    assert pick(255) == 1000;
60 +
    return 0;
61 +
}