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:
8
specs.md
8
specs.md
@@ -277,8 +277,12 @@ n := u64.max; // 18446744073709551615 (all-ones)
|
||||
(`18446744073709551615`), which exceeds the signed `i64` range used for
|
||||
integer constants; it is stored as that exact bit pattern carrying the `u64`
|
||||
type (it reinterprets to `-1` as an `s64`). It cannot be written as a decimal
|
||||
literal, and the default integer formatter (which is `s64`-based) prints it as
|
||||
`-1`; assert it exactly through a bit reinterpret (`union { u: u64; s: s64 }`).
|
||||
literal. The default integer formatter is signedness-aware:
|
||||
`print("{}", u64.max)` renders the full unsigned decimal
|
||||
`18446744073709551615` (and any unsigned value across all 64 bits), while a
|
||||
signed value — including `s64.min` — prints with all its digits. A bit
|
||||
reinterpret (`union { u: u64; s: s64 }`) is still a valid way to inspect the
|
||||
raw bits, but is no longer needed merely to print the value.
|
||||
- **Non-numeric receivers.** `.min` / `.max` on a non-numeric type (`bool`,
|
||||
`string`, a pointer, a `struct`, `void`, an `enum`) is a compile error, never
|
||||
a silent value.
|
||||
|
||||
Reference in New Issue
Block a user