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:
@@ -10561,6 +10561,24 @@ pub const Lowering = struct {
|
||||
const b = self.resolveTypeArg(c.args[1]);
|
||||
return self.builder.constBool(a == b);
|
||||
}
|
||||
if (std.mem.eql(u8, name, "type_is_unsigned")) {
|
||||
// type_is_unsigned(T) → bool. Static arg (a spelled type or
|
||||
// generic binding) folds to const_bool at lower time. A
|
||||
// dynamic arg — the runtime `type_of(x)` value queried by
|
||||
// `any_to_string` — emits a `callBuiltin`: the interp reads
|
||||
// the boxed TypeId, LLVM GEPs a per-type signedness table.
|
||||
// Mirrors `type_name`'s static/dynamic split; the same split
|
||||
// avoids `resolveTypeArg`'s silent `.s64` default lying about
|
||||
// a runtime Type value.
|
||||
if (c.args.len < 1) return self.builder.constBool(false);
|
||||
if (self.isStaticTypeArg(c.args[0])) {
|
||||
const ty = self.resolveTypeArg(c.args[0]);
|
||||
return self.builder.constBool(self.module.types.isUnsignedInt(ty));
|
||||
}
|
||||
const arg_ref = self.lowerExpr(c.args[0]);
|
||||
const args_owned = self.alloc.dupe(Ref, &.{arg_ref}) catch return self.builder.constBool(false);
|
||||
return self.builder.callBuiltin(.type_is_unsigned, args_owned, .bool);
|
||||
}
|
||||
if (std.mem.eql(u8, name, "has_impl")) {
|
||||
// has_impl(P, T) → const_bool. Returns true when type T has
|
||||
// a reachable impl for protocol P. P is either:
|
||||
|
||||
Reference in New Issue
Block a user