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

+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 `_`", &.{}),
}