fix(std): render integer formatter extremes — i64::MIN and unsigned all-ones [F0.8]
Resolves issue 0090. The `{}` integer formatter mis-rendered both ends of
the 64-bit range:
- `int_to_string` computed the magnitude as `0 - n`, which overflows for
`s64::MIN` (its magnitude is unrepresentable as a positive s64) — the
value stayed negative, the digit loop ran zero times, so only `-`
printed. It now extracts digits straight from `n` (per-digit
`|n % 10|`, `n` truncating toward zero), never negating MIN.
- `any_to_string`'s `case int:` formatted every integer as s64, so a u64
all-ones value printed as `-1`. There was no `uint` type-category to
distinguish signedness. Added an additive `type_is_unsigned(T)`
reflection builtin (static fold + dynamic interp/LLVM paths, mirroring
`type_name`), backed by the new `TypeTable.isUnsignedInt` predicate, and
a `uint_to_string` formatter (unsigned decimal via long-division over
four 16-bit limbs). `case int:` routes through `type_is_unsigned(type)`.
The 16-bit-limb split is factored into a shared `decompose_u16x4`, now
reused by `int_to_hex_string` (no second unsigned-math routine).
Regression: examples/0046-basic-int-formatter-extremes pins both extremes
plus a width spread; unit tests cover `isUnsignedInt`. Docs (specs.md
representation note, readme std API) updated for unsigned/extreme `{}`
behavior. IR snapshots refreshed for the two new std functions.
This commit is contained in:
@@ -253,3 +253,37 @@ test "errorSetType: tags stored sorted by global id" {
|
||||
try std.testing.expectEqual(@as(usize, 3), stored.len);
|
||||
try std.testing.expect(stored[0] <= stored[1] and stored[1] <= stored[2]);
|
||||
}
|
||||
|
||||
test "isUnsignedInt: builtin signedness classification" {
|
||||
const alloc = std.testing.allocator;
|
||||
var table = TypeTable.init(alloc);
|
||||
defer table.deinit();
|
||||
|
||||
// Unsigned builtins (the formatter must route these to unsigned decimal).
|
||||
inline for (.{ TypeId.u8, TypeId.u16, TypeId.u32, TypeId.u64, TypeId.usize }) |ty| {
|
||||
try std.testing.expect(table.isUnsignedInt(ty));
|
||||
}
|
||||
// Signed / non-integer builtins are not unsigned.
|
||||
inline for (.{
|
||||
TypeId.s8, TypeId.s16, TypeId.s32, TypeId.s64, TypeId.isize,
|
||||
TypeId.bool, TypeId.f32, TypeId.f64, TypeId.string,
|
||||
TypeId.void, TypeId.any, TypeId.unresolved,
|
||||
}) |ty| {
|
||||
try std.testing.expect(!table.isUnsignedInt(ty));
|
||||
}
|
||||
}
|
||||
|
||||
test "isUnsignedInt: user-defined arbitrary-width ints" {
|
||||
const alloc = std.testing.allocator;
|
||||
var table = TypeTable.init(alloc);
|
||||
defer table.deinit();
|
||||
|
||||
const u24_ty = table.intern(.{ .unsigned = 24 });
|
||||
const s24_ty = table.intern(.{ .signed = 24 });
|
||||
try std.testing.expect(table.isUnsignedInt(u24_ty));
|
||||
try std.testing.expect(!table.isUnsignedInt(s24_ty));
|
||||
|
||||
// A non-integer user type is never unsigned.
|
||||
const ptr_ty = table.ptrTo(.u32);
|
||||
try std.testing.expect(!table.isUnsignedInt(ptr_ty));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user