Skip to content

Bug: unsigned integer f-string interpolation formats as signed (§15.4) #639

Closed

Description

@ehartford

Summary

Bare f-string interpolation of an unsigned integer prints the signed two's-complement reading of the bits, not the unsigned value. Any u8/u16/u32/u64 above its signed max prints negative.

Repro

fn main:
    let a: u32 = 4000000000
    print(f"{a}")            // prints -294967296  (want 4000000000)
    let b: u64 = 18000000000000000000
    print(f"{b}")            // prints -446744073709551616  (want 18000000000000000000)

Verified by execution on HEAD/seed.

Spec

§15.4 f-string formatting; unsigned integer types listed at docs/with-specification.md:953. Display/ToString for unsigned must print the unsigned value. Note x.to_string() is correct (ToString for u32/u64 uses with_fmt_u32/with_fmt_u64, lib/std/builtins.w:114-120) — only bare f-string interpolation is wrong, so the two disagree today.

Root cause (exact lines)

Bare {x} (no format spec) lowers via MirLower.lower_fmt_to_str → MirIntrinsic.FMT_TO_STR. Codegen at src/CodegenDispatch.w:9965-9971 recovers the sema type and calls coerce_typed_val_to_str, but that helper's integer fall-through at src/CodegenDispatch.w:2752 calls self.coerce_val_to_str(val, str_ty), dropping the sema type. coerce_val_to_str then dispatches purely on the LLVM integer width:

  • bit_w <= 32 → with_fmt_i32 (signed), and
  • bit_w > 32 → with_fmt_i64 (signed),

each via coerce_int_ext(val, ..., false) which sign-extends. LLVM iN carries no signedness, so an unsigned value is formatted signed.

Fix direction

In coerce_typed_val_to_str, before the coerce_val_to_str fall-through, detect an unsigned integer sema type (Sema.is_unsigned_int_type, src/Sema.w:3011) and route to the existing unsigned runtime formatters with_fmt_u32/with_fmt_u64 (already declared and used by ToString). The spec-carrying path (FMT_SPEC/FMT_BUF_WRITE_FMT) threads sema_ty and should be checked for the same gap.

Found

Coverage sweep of #511 (numeric intrinsics) — writing the tests surfaced this broader formatting bug. Blocks visible verification of unsigned min/max/abs results (those tests must compare with == until this lands).

Activity

  1. ehartford commented on Jul 4, 2026

    @ehartford
    ContributorAuthor

    Fixed in 7cb60f7. Build + fixpoint (stage2==stage3) + full test suite green (GATES EXIT: 0). Bare f-string interpolation of unsigned integers now prints the unsigned value: coerce_typed_val_to_str threads signedness (mir_sema_type_is_unsigned) and routes to with_fmt_u32/with_fmt_u64 with zero-extension (new coerce_val_to_str_ext). Pinned by test/spec/spec_ss15_4_unsigned_format.w.

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