cli: canonicalize integer match patterns

Emit decimal AIR patterns without source suffixes, radix spellings or
separators. Validate range before formatting and preserve duplicate-value
detection, including negative zero and 128-bit boundaries.

Add source run/reject regressions for canonical spelling, duplicates and
out-of-range patterns; clarify the existing integer pattern contract.
This commit is contained in:
dev committed 2026-10-07 08:31:27 +03:00
1 parent 1eaf968f4f
commit fc23f1b23a
6 files changed
+51 -2

No files matched your search

+1 -1
View File
@@ -239,7 +239,7 @@ match io.stdin_read(input, mut buf) {
- **Rebinding.** A value that comes back on an edge is rebound by the name in its arm: `closed(v)`, `pending(rx)`, `cancelled(t)`. If that name is a variable (`var`) of the value's type, the value becomes the variable's new value. A receiver kept in `var r` is closed with `match channel.close_receiver(move r) { ok => …, pending(r) => … }` and received from again.
- **`or trap`.** `op(args) or trap` is the expression of the `ok` value, or of `void`. Every other outcome traps (`T0001`). It is the only shorthand.
- **Enums.** A `match` over a value of an enum type has exactly one arm per variant, each written `Enum::Variant(names…)` (`Enum::Variant` without payload). It must be exhaustive (`E7004` lists the missing variants), and `_` is not allowed (`E7006`), so a new variant cannot slip through silently. Payload names are bound only inside their arm (they are the arm block's parameters in AIR). It lowers to `enum.switch`.
- **Integers.** A `match` over an integer has literal arms and exactly one `_` arm, which is required (`E7004`). It lowers to `switch`.
- **Integers.** A `match` over an integer has literal arms and exactly one `_` arm, which is required (`E7004`). Pattern values MUST fit the scrutinee type (`E7005`); different spellings of the same value are duplicate arms (`E7006`). It lowers to `switch` with canonical decimal literals, without source suffixes or separators.
- **Booleans** use `if`.
- **As expressions.** `match` and `if` are expressions when their arms yield values (§4). Every arm must have the type of the others, or diverge.
- **Source map.** The switch maps to the `match`; each arm's code and its branch to the join map to that arm.
+9 -1
View File
@@ -1911,11 +1911,19 @@ const Lowerer = struct {
.int => |text| {
const neg = text[0] == '-';
const mag = parse_mod.parseIntText(if (neg) text[1..] else text) orelse return l.fail("E7001", arm.pattern.span, "malformed integer pattern", &.{});
const width = intBits(st.int);
const negative = neg and mag != 0;
const signed = isSigned(st.int);
const limit: u128 = if (signed) (@as(u128, 1) << @intCast(width - 1)) - @as(u128, if (negative) 0 else 1) else if (width == 128) std.math.maxInt(u128) else (@as(u128, 1) << @intCast(width)) - 1;
if ((negative and !signed) or mag > limit) return l.fail("E7005", arm.pattern.span, "integer pattern out of range for its scrutinee type", &.{ .{ .name = "type", .value = st.int }, .{ .name = "literal", .value = text } });
const bits: u128 = if (neg) (0 -% mag) else mag;
for (seen.items) |prev| if (prev == bits) return l.fail("E7006", arm.pattern.span, "integer matched twice", &.{.{ .name = "value", .value = text }});
try seen.append(l.arena, bits);
blk.* = try l.newBlock("case");
try head.appendSlice(l.arena, try l.fmt("{s} -> @{s}(), ", .{ text, l.blocks.items[blk.*].name }));
// AIR uses canonical decimal literals without source
// suffixes/separators. Validate range before formatting
// rather than truncating a pattern's bit representation.
try head.appendSlice(l.arena, try l.fmt("{s}{d} -> @{s}(), ", .{ if (neg and mag != 0) "-" else "", mag, l.blocks.items[blk.*].name }));
},
.ctor => return l.fail("E7005", arm.pattern.span, "an arm of a `match` over an integer is an integer literal or `_`", &.{}),
}
@@ -0,0 +1,7 @@
// expect: reject E7006
// phase: 6
// spec: docs/spec/15-source-language.md §6
module test.integer_patterns
fn main() -> u64 effects [] {
return match 42u64 { 42u64 => { 42u64 }, 0x2_au64 => { 0u64 }, _ => { 99u64 } };
}
@@ -0,0 +1,7 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/07a-air-instructions.md switch.I; docs/spec/15-source-language.md §6
module test.integer_patterns
fn main() -> u64 effects [] {
return match 0u8 { 256 => { 42u64 }, _ => { 0u64 } };
}
@@ -0,0 +1,20 @@
// expect: run
// result: 42
// phase: 6
// spec: docs/spec/15-source-language.md §6; docs/spec/07-air.md §8.5
module test.integer_patterns
fn unsigned(value: u64) -> u64 effects [] {
return match value { 0x2_au64 => { 42u64 }, 0u64 => { 0u64 }, _ => { 99u64 } };
}
fn signed(value: i64) -> u64 effects [] {
return match value { -0x2ai64 => { 42u64 }, -0i64 => { 0u64 }, _ => { 99u64 } };
}
fn main() -> u64 effects [] {
assert(unsigned(42u64) == 42u64); assert(unsigned(0u64) == 0u64);
assert(signed(-42i64) == 42u64); assert(signed(0i64) == 0u64);
let maximum = 340282366920938463463374607431768211455u128;
match maximum { 0xffffffffffffffffffffffffffffffffu128 => {}, _ => { trap; } }
let minimum = -170141183460469231731687303715884105728i128;
match minimum { -170141183460469231731687303715884105728i128 => {}, _ => { trap; } }
return 42;
}
@@ -0,0 +1,7 @@
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md §6
module test.integer_patterns
fn main() -> u64 effects [] {
return match 0u8 { -1 => { 42u64 }, _ => { 0u64 } };
}