Repository navigation
Description
Summary
The comptime evaluator performs integer arithmetic at 32 bits regardless of
the declared type. Values wider than i32 are truncated on the way into the
arithmetic helper, before the overflow check runs — so an out-of-range operand is
silently normalized rather than diagnosed.
The clearest demonstration is adding zero:
comptime fn ident -> i64: 3000000001i64 + 0i64
const I: i64 = comptime ident()
fn main: print(f"{I}")
-1294967295 // expected 3000000001
Both operands carry explicit i64 suffixes, the function returns i64, and the
const is annotated i64. -1294967295 is exactly 3000000001 - 2^32.
This is not a 128-bit issue — plain i64 comptime arithmetic is affected today.
Reproduction
comptime fn lit -> i64: 3000000001i64 // no arithmetic
comptime fn ident -> i64: 3000000001i64 + 0i64 // add zero
comptime fn divbig -> i64: 3000000000i64 / 1000000000i64 // division
comptime fn edge -> i64: 2147483647i64 + 1i64 // i32 MAX + 1
const L: i64 = comptime lit()
const I: i64 = comptime ident()
const D: i64 = comptime divbig()
fn main:
print(f"lit = {L}")
print(f"ident = {I}")
print(f"divbig = {D}")
| Expression | Expected | Actual |
|---|---|---|
3000000001i64 (no arithmetic) |
3000000001 |
3000000001 — correct |
3000000001i64 + 0i64 |
3000000001 |
-1294967295 — silent |
0i64 - 3000000000i64 |
-3000000000 |
1294967296 — silent |
3000000000i64 / 1000000000i64 |
3 |
3 — correct |
2147483647i64 + 1i64 |
2147483648 |
error: integer overflow in comptime |
The same wrap occurs through a typed local (var x: i64 = 2147483647; x + 1),
so annotating the binding does not avoid it. The identical expression evaluated
at runtime rather than comptime is correct, and lit above shows the value
survives comptime → const materialization intact when no arithmetic is applied.
2147483647i64 + 1i64 raising an overflow error is itself the proof that the
arithmetic is being done at 32 bits.
Root cause
Three links, all at 22e534c6:
1. src/ComptimeEval.w:7862 — integer literals default to i32
return comptime_control_value(comptime_value_int(
self.node_type_or(node, self.sema.ty_i32 as i32), fast.value))
A literal that fits in i64 takes this fast path. When typed_expr_types has
no entry for the node, node_type_or falls back to ty_i32. The exact path
one line above (:7861) — for literals too large for i64 — falls back to
ty_i64, so the two paths disagree on their default.
Sema itself has the correct type. with analyze on the ident case reports the
binary node as i64:
sema expression node:6657 node-kind=25 type-name=i64 start=26 end=46
So the type information exists; the evaluator's lookup misses it and takes the
i32 fallback.
2. src/ComptimeEval.w:6742 — the fallback propagates
let result_ty = self.node_type_or(node, if lhs.type_id != 0: lhs.type_id else: rhs.type_id)
lhs.type_id is now i32, so comptime_int_width(result_ty) returns 32.
3. src/Overflow.w:154-155 — operands are truncated before the check
fn int_signed_add(lhs: i64, rhs: i64, width_raw: i32, mode: i32) -> IntArithmeticResult:
let width = int_width_clamp(width_raw)
let lv = int_truncate_to_width(lhs, width, false) // 3000000001 -> -1294967295
let rv = int_truncate_to_width(rhs, width, false)
let min = int_signed_min(width)
let max = int_signed_max(width)
let over_hi = rv > 0 and lv > max - rv // rv == 0 -> false
let over_lo = rv < 0 and lv < min - rv // rv == 0 -> false
let wrapped = int_truncate_to_width(lv +% rv, width, false)
if not over_hi and not over_lo:
return int_arith_ok(wrapped) // returns the corrupted value
The overflow check asks whether the operation overflows, but by then the
operands have already been silently truncated. Adding 0 cannot overflow, so
the corruption is never flagged. int_signed_sub (:171-172) and
int_signed_mul (:203-204) have the same ordering.
This is the link that makes the bug silent. Link 1 alone would produce loud
errors on any out-of-range literal.
Division is unaffected because the OP_DIV arm in ComptimeEval.w computes
lv / rv directly and only consults the width for int_div_overflows — it never
truncates its operands.
Relationship to #914
This corrects the D4 characterization in the i128/u128 plan, which describes
comptime evaluation as "i64-only" and silently narrowing for 128-bit values. The
measured boundary is 32 bits, and the corruption reaches i64 code that has
nothing to do with 128-bit integers.
It also affects the plan's proposed D4 mitigation. Refusing 128-bit operands with
a diagnostic would be keyed on the evaluator's own notion of width — which is 32
here, not 128 — so the refusal would not fire and these expressions would still
be silently corrupted. Fixing the operand-truncation ordering in Overflow.w
converts silent corruption into a loud error across all widths at once, which
matches the plan's stated principle that a loud gap is shippable and a silent
truncation is not.
Filing separately because the i64 corruption is independent of the 128-bit
campaign and is probably worth landing first.
Test coverage
There are no comptime integer overflow tests in test/behavior/, and the only
128-bit test is the 12-line smoke test behav_i128_u128.w (1 + 1, 40 + 2).
Nothing in the suite would catch this.
Provenance
Runtime measurements and the with analyze output were produced with the
released v0.15.1.7 binary (tagged at 04ca1a74). Root cause was read at
22e534c6.
The two are equivalent for this bug. Between 04ca1a74 and 22e534c6:
src/Overflow.w— byte-identicalsrc/ComptimeValue.w— byte-identicalsrc/ComptimeEval.w— five hunks, all part of the Campaign: build performance — full verification under 10 min and 8 GB #679 decl-index caching
optimization (find_decl_index,ComptimeEvaluator.init). None touches
:6742or:7862; both lines are unchanged.
So every code path exercised by the reproduction is identical in the two
revisions, and the behavior above is the behavior at 22e534c6.
I was not able to confirm this by building 22e534c6 directly — with build
on a clean checkout does not produce out/stage/bin/with-stage2 with a
v0.15.1.7 seed, so the bootstrap never reaches a usable stage compiler.
That may well be a seeding mistake on my part rather than a repo issue; I have
not filed anything about it.
Investigated and drafted jointly by @migueldecimal128 and Claude (Claude Code).
Activity
migueldecimal128 commentedon Sep 2, 2026
Correction:
u64is affected too, and more quietlyThe report above uses only signed examples, which makes it read as an
i64issue. It isn't —
u64is affected by the same root cause, and the unsignedmanifestation is harder to notice.
All three identity operations corrupt their operand:
4294967301u64 + 0u64429496730154294967296u64 + 0u64429496729608589934592u64 + 7u64858993459974294967301u64 - 1u64429496730044294967301u64 * 1u64429496730152^32silently evaluating to0is the case I would most want to avoidshipping. Unlike the signed examples, these results are small positive integers
— they look like ordinary data rather than obvious garbage, so they are unlikely
to be spotted by inspection.
Same root cause
int_unsigned_add(src/Overflow.w:109-110),int_unsigned_sub(:125-126),and
int_unsigned_mul(:138-139) each truncate both operands to width beforethe overflow check, exactly as the signed helpers do:
5 + 0cannot overflow at any width, so nothing is flagged. The unsigned addand mul helpers do use the
exact_int_*machinery for the operation itself,which is more careful than the signed path — but that care is spent on operands
that were already corrupted one line earlier.
One accidental guard, and a misleading message
When the truncated value happens to land negative, the unsigned fit check
catches it at materialization:
Note
raw=-1294967295— the identical corruption from the signed report,surfaced only because the result went negative. Two things worth noting:
u64fails loudly or silently depends on which side of zero thetruncated value lands on. That is not a guard anyone should rely on.
3000000001u64fitsu64fine. The diagnostic points away from the actualfault. Worth fixing alongside, since anyone hitting it will start debugging
in the wrong place.
Scope note
Signed and unsigned share the single root cause described in the issue body, so
a fix to the operand-truncation ordering should cover both. Flagging it
explicitly because the issue title and examples name only
i64, and a fixscoped from those alone would leave
2^32 -> 0live.Provenance is as stated in the issue body: measured on
v0.15.1.7(
04ca1a74), andsrc/Overflow.wis byte-identical at22e534c6, so theseresults hold at
main.migueldecimal128 commentedon Sep 4, 2026
Provenance update: reproduced directly on
22e534c6The Provenance section above argues from unchanged diffs that measurements taken
with the released
v0.15.1.7binary hold atmain. That inference is no longerneeded — I built the compiler from
22e534c6and reproduced all three symptomclasses on it directly.
The tree is healthy while this is live
Worth recording, because it sharpens the "Test coverage" section: this was
reproduced on a checkout where every gate passes.
The full battery is green with
2^32 + 0folding to0. Nothing in the suiteobserves comptime integer arithmetic past 2^31, so the bug is invisible to it.
Reproducing this needs a
:depsworkaroundwith build :depsfails on a clean checkout:The asset exists — it is published on
v0.15.1.7and on every nightly, so themessage names the wrong thing whatever the cause. Root cause not established.
I originally wrote here that
seed_release_from_api(build/seed.w:137) was atfault; that was a guess and it does not hold up. Replaying that function's
algorithm against the live
releases?per_page=10response finds the asset andreturns a tag, and
seed_fetch_to_fileemits its own diagnostic on a fetchfailure, which did not appear. The failure was observed once and I could not
reproduce it afterwards, since the marker short-circuits the target once the SDK
is in place. Treat the lookup as unproven rather than as the culprit.
The documented escape hatch works regardless:
Failing that, fetching the asset and extracting it into
.deps/works, sincethe tarball's top-level directory is already
llvm-22.1.6-darwin-arm64asBuildGraphTools.w:58expects. Note the asset name saysaarch64while thedirectory says
arm64.Noting it here only so the next person to reproduce this does not lose an hour
to it. Not filed separately — without a reproduction there is nothing actionable
to file.
Edited to withdraw the
seed_release_from_apiattribution, which I could notsubstantiate.
eas4ai commentedon Sep 4, 2026
Still reproduces at 450733e (seed stage1):
3000000001i64 + 0i64in comptime yields -1294967295, re-verified during read-only audit (probe.audit/probes/comptime/t23_i64_silent.w). Sibling checks all loud: comptime division-by-zero errors, I/O rejected, infinite loop/recursion hit step limits (rc=1 each). So the 32-bit truncation is isolated to value narrowing, not a general limit failure.migueldecimal128 commentedon Sep 4, 2026
Thanks for confirmation. I'm still working on getting it fixed.
#943: resolve comptime integer width from suffix and annotation
#943: make the upper half of u64 usable from comptime
#943: evaluate negated integer literals against the target width
#943: assert comptime integer payloads are canonical in their own type
migueldecimal128 commentedon Sep 5, 2026
The
:depsworkaround note above is now root-caused and filed as #1079My earlier comment described
with build :depsfailing on a clean checkout withand then withdrew the attribution to
seed_release_from_api(
build/seed.w) as unproven, on the grounds that replaying that function'salgorithm against the live
releases?per_page=10response finds the asset andreturns a tag.
That withdrawal was wrong. The original attribution was correct, and the
failure is deterministic rather than intermittent. Filed as #1079.
The replay succeeds only against a pretty-printed copy of the response — what
gh apior a browser renders. The GitHub REST API returns compact JSON, andseed_release_from_apiparses it line by line, so the whole 239,931-byte bodyis a single "line", the scan loop runs exactly once, and
seed_json_line_valuereturns the first
"name"in the entire document — the release's own displayname — instead of ever reaching
assets[].Running the two functions transcribed verbatim from
build/seed.wagainst theraw bytes the fetch helper writes:
It looked intermittent because the
:depsmarker file short-circuits the targetonce the SDK is on disk, so it only surfaces on a checkout that actually needs
to download.
with build :seedhas the identical defect through the same helper, which isthe more damaging half: a checkout whose installed compiler has fallen behind
maincannot bootstrap back, because the command that repairs a stale seed isitself the broken one. The stale seed then fails somewhere else entirely — on
mainat5a21c2e0a v0.15.1.7 seed dies withcomptime index requires an array, tuple, or vec, and on this branch's base it dies withexpression kind 26 is not comptime-evaluable yetduringpcre2-wo-build— neither of whichpoints at the seed.
Workaround unchanged, and it applies to both targets:
Nothing here bears on the comptime width defect this issue tracks; it only
retires the "root cause not established" note above.
#943: failing tests and implementation plan for comptime integer width
#943: resolve comptime integer width from suffix and annotation
#943: make the upper half of u64 usable from comptime
#943: evaluate negated integer literals against the target width
#943: assert comptime integer payloads are canonical in their own type
Merge pull request #1080 from withlang-dev/issue943
Sema/Codegen/migrator: unsuffixed literal arithmetic takes its contex…