Skip to content

i128/u128 end to end: wide literals, formatting, boundary tests, spec §4.1 #914

Closed

Description

@ehartford

Make i128/u128 first-class end to end. Plan of record: docs/i128.md — summary below.

Correction from the first filing

The original body claimed wide literals silently wrap. False — deeper probing (>> 100 discriminators, which bypass the print path) shows the wide-constant pipeline is complete and correct end to end: exact digit storage in the AST (set_int_literal_exact), CK_INT_EXACT MIR consts, wl_const_int_words(lo, hi, 2) codegen. let x: i128 = <i128 max> printing -1 was the FORMATTING defect masquerading as a literal wrap (max mod 2^64 read as i64 is -1). The too-big diagnostic also already fires correctly for 2^128.

The two real defects (verified)

  1. Formatting truncates to 64 bits. Values are correct in the register; std.fmt bottoms out in with_fmt_i64 and the f-string fmt_to_str dispatch has no width-128 arm. f"{i128max}" prints -1, f"{2^100}" prints 0.
  2. Negated wide literals lose the expected-type context. let neg: i128 = -<literal above i64 range> errors "does not fit default type (expected_ty=0)" despite the annotation — the unary-minus path checks against the default type. Consequence: INT128_MIN and every negative literal of magnitude > i64 max are unspellable.

Campaign (details in docs/i128.md)

  • D2: thread the expected type through NK_UNARY(NEGATE, NK_INT_LIT); INT128_MIN special case
  • D1: with_fmt_i128/with_fmt_u128 runtime renderers + width-128 dispatch arm
  • parse_i128/parse_u128
  • acceptance: boundary literals, >> 100 layer discriminators, sign matrix, checked-overflow edges, format/parse round-trip, __int128 c_import round-trip, --dump-abi
  • fixpoint battery if compiler source gains u128
  • spec §4.1 primitive-list sentence (Eric's blessing)

Non-goals

No emulation struct, no BigInt, no ABI special-casing, no rebuild of the exact-int machinery — it exists and is proven.

Activity

  1. changed the title [-]Complete i128/u128: literals beyond 64 bits silently wrap, formatting truncates to 64 bits[/-] [+]i128/u128 end to end: wide literals, formatting, boundary tests, spec §4.1[/+] on Aug 31, 2026
  2. added 3 commits that reference this issue on Sep 4, 2026
  3. migueldecimal128 commented on Sep 4, 2026

    @migueldecimal128
    Contributor

    D2 is done

    Fixed on branch issue943, commit ff201949. Signed MIN is now spellable in
    suffixed form at every width, at runtime and at comptime:

    i8::MIN   -128i8                     -128
    i16::MIN  -32768i16                  -32768
    i32::MIN  -2147483648i32             -2147483648
    i64::MIN  -9223372036854775808i64    -9223372036854775808
    

    i64::MIN could not be written anywhere in the language before this —
    not just in wide-literal contexts:

    var b: i64 = -9223372036854775808
    error: integer literal does not fit default type ('9223372036854775808', suffix_ty=0, expected_ty=0)
    

    It needed five pieces, not one

    The plan scopes D2 at ½–1 day for a UOP_NEGATE arm in check_unary. That arm
    is necessary but not sufficient — every consumer of a negated literal had
    independently re-derived the interpretation, and each got it wrong the same way,
    by narrowing the operand before measuring it.

    file change
    Sema.w in_negated_literal_context, mirroring in_bitwise_literal_context
    Sema.w int_literal_fits_type_negated — magnitude-aware range check via exact_int_fits_signed_negative_bits
    SemaCheck.w the UOP_NEGATE arm, threading a signed integer expectation
    SemaCheck.w the NK_INT_LIT suffix branch consulting the negation context
    MirLower.w try_eval_const folds the whole negation
    MirLower.w lower_un_op emits CK_INT_EXACT instead of a runtime RK_UN_OP

    The plan's guidance to call int_literal_expr_bits on the unary node is
    correct and is what all of these use.

    The trap worth recording

    The sema fix alone is a regression, not a fix. With sema rejecting the
    expression, lowering never saw it; once accepted, lower_un_op emitted a
    runtime negate that trapped on exactly the value the source asked for:

    var a: i32 = -2147483648i32
    panic: integer overflow: negating i32 minimum value
    

    A clean compile error became a crash. Anyone cherry-picking the check_unary
    arm without the MirLower half will ship that. CK_INT_EXACT was already the
    right vehicle — codegen resolves it through int_literal_expr_bits, which keeps
    magnitude and sign apart until the width applies and already handles both words
    for the 128-bit case.

    The guarded regression the plan names — -1 in an unsigned context must still
    say cannot negate an unsigned value — holds. Two guards: check_unary threads
    only signed expectations, and int_literal_fits_type_negated defers to the
    ordinary check for unsigned types.

    Gates

    :fixpoint FIXPOINT (stage2 byte-identical to stage3, with the compiler's own
    negated literals routed through the new path) and the full suite green, every
    target re-run rather than cached — 977 behavior, 749 compile-error, 210 spec,
    51 phase, 16 codegen, 6 comptime-diff, 14 internals, 7 lexer, 9 parser.

    The 749 compile-error fixtures pin diagnostics and none moved: the
    negation-aware path is additive, accepting what was wrongly rejected without
    loosening anything that should still be rejected.

    Two notes for the rest of the campaign

    D4 needs re-reading. See #943. The evaluator is not "i64-only" — it did
    arithmetic at 32 bits, and separately cannot represent anything above 64
    bits at all: ComptimeEval.w keeps exact.lo and discards exact.hi, so
    18446744073709551616 + 1 folds to 1. The 32-bit half is fixed on
    issue943; the ceiling is not, and it is exactly the data0 widening D4
    contemplates. The proposed "refuse 128-bit operands" mitigation would not have
    fired, since the evaluator's own width for those expressions was 32.

    S2's Overflow.w work has a free rider. All six arithmetic helpers truncate
    their operands before the overflow check, so an out-of-range operand is
    silently normalized rather than diagnosed — that ordering is what made the #943
    corruption silent. Fixing it standalone is poor value now that widths resolve
    correctly (it changes no observable behavior), but S2 opens Overflow.w anyway
    for the word-pair helpers, and the reordering is nearly free while the file is
    in play. Detail in plans/comptime-int-width.md → "S1 reassessed".


    Investigated and drafted jointly by @migueldecimal128 and Claude (Claude Code).

  4. added 3 commits that reference this issue on Sep 5, 2026
  5. added 2 commits that reference this issue on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions