fix(compiler): preserve declared type of const references
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 15s
ci / build-and-test (push) Successful in 1m25s
commit-lint / conventional-commits (push) Successful in 1s
docs / build-and-deploy (push) Successful in 22s

A const reference lowered to `val(itoa(g.a.ival), "int")` in emit_call.ludic —
the initializer's raw integer bits, hardcoded as `int`. For a fixed const like
`const X: fixed = 10.0` that yielded the Q16.16 bits (655360) typed as int, so
every fixed comparison/arithmetic against it silently broke (it caused an
infinite loop in runtime/native/numeric.ludic, previously worked around with
inline literals).

Fix: a const reference now emits its initializer expression via emit_expr(g.a),
which carries the initializer's real type (E_FLOAT->fixed, E_BOOL->bool,
E_STR->string) and even handles computed initializers. Every existing const is
an int literal, for which this is byte-identical to the old immediate — the
C-free bootstrap fixpoint and all golden renders are unchanged.

- selfhost/tests/const.ludic + sh_case pin fixed/int/bool const behaviour.
- runtime/native/numeric.ludic restored to named fixed consts (HUGE_TEN etc.),
  which the workaround had inlined; the numeric example (20 assertions) still
  passes, validating the fix under runtime splice.

All suites green: x selfhost-test 31/31 (fixpoint holds, goldens byte-identical),
x test 85/85, x test-tools 30/30.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-01 02:48:03 +03:00
parent 790eda6f73
commit 7c91d24595
6 changed files with 3680 additions and 3641 deletions

View file

@ -0,0 +1,11 @@
bump: patch
type: fix
**`const` of a non-int type is no longer miscompiled.** A `const` reference used
to lower to its initializer's raw integer bits hardcoded as type `int`, so a
`const X: fixed = 10.0` compared and computed as the raw Q16.16 value `655360`
instead of `10.0` — silently corrupting fixed-point math (and, in one case, spinning
an infinite loop). A const reference now emits its initializer expression with that
expression's real type, so `fixed` / `bool` / `string` consts behave correctly (and
computed const initializers work too). Every existing const is an `int` literal, for
which the lowering is byte-identical, so the bootstrap fixpoint and all golden
renders are unchanged. Covered by selfhost/tests/const.ludic.

View file

@ -20,9 +20,8 @@
# out of the deterministic simulation and use BigInt/Decimal when exactness
# matters.
# NOTE: fixed values live as inline `10.0` / `1.0` literals, not `const`s — a
# `const` reference lowers to its raw integer value typed `int` (emit_call.ludic
# N_CONST), which silently breaks fixed-point comparisons.
const HUGE_ONE: fixed = 1.0
const HUGE_TEN: fixed = 10.0
property Huge { m: fixed = 0.0, e: int = 0 }
@ -34,8 +33,8 @@ function huge_make(m: fixed, e: int) -> Huge {
var mm = m
if mm < 0.0 { neg = true; mm = 0.0 - mm }
var ee = e
while mm >= 10.0 { mm = mm / 10.0; ee = ee + 1 }
while mm < 1.0 { mm = mm * 10.0; ee = ee - 1 }
while mm >= HUGE_TEN { mm = mm / HUGE_TEN; ee = ee + 1 }
while mm < HUGE_ONE { mm = mm * HUGE_TEN; ee = ee - 1 }
if neg { mm = 0.0 - mm }
h.m = mm; h.e = ee
return h
@ -79,7 +78,7 @@ function huge_add(a: Huge, b: Huge) -> Huge {
if diff > 8 { return big } # negligible at display precision
var sm = small.m
var k = 0
while k < diff { sm = sm / 10.0; k = k + 1 }
while k < diff { sm = sm / HUGE_TEN; k = k + 1 }
return huge_make(big.m + sm, big.e)
}
@ -119,12 +118,14 @@ function huge_str(a: Huge) -> pointer {
# ---- Angle: an auto-wrapping radian angle ----------------------------------
# wrap any radian value into the half-open range [-pi, pi). pi = 3.14159265,
# tau = 6.28318531 (inline literals — see the note above on fixed consts).
const ANGLE_PI: fixed = 3.14159265
const ANGLE_TAU: fixed = 6.28318531
# wrap any radian value into the half-open range [-pi, pi)
function angle_wrap(a: fixed) -> fixed {
var x = a
while x >= 3.14159265 { x = x - 6.28318531 }
while x < 0.0 - 3.14159265 { x = x + 6.28318531 }
while x >= ANGLE_PI { x = x - ANGLE_TAU }
while x < 0.0 - ANGLE_PI { x = x + ANGLE_TAU }
return x
}
@ -142,10 +143,12 @@ function angle_lerp(a: fixed, b: fixed, t: fixed) -> fixed { return angle_wrap(a
# ---- Percent: a value clamped to [0, 1] ------------------------------------
const PCT_ONE: fixed = 1.0
# clamp any fixed into [0, 1]
function percent_clamp(v: fixed) -> fixed {
if v < 0.0 { return 0.0 }
if v > 1.0 { return 1.0 }
if v > PCT_ONE { return PCT_ONE }
return v
}

View file

@ -925,7 +925,11 @@ function emit_expr(e: Node) -> Val {
if li >= 0 { return emit_load_at(loc_reg[li], loc_ty[li]) }
let g = find_global(e.s)
if (g != null) {
if g.kind == N_CONST { return val(itoa(g.a.ival), "int") }
# a const reference IS its initializer expression, carrying that
# expression's real type — so `const X: fixed = 10.0` yields a `fixed`, not
# the raw Q16.16 bits mislabelled `int`. Every existing const is an int
# literal, for which this is byte-identical to the old immediate.
if g.kind == N_CONST { return emit_expr(g.a) }
let r = emit_bind(`load {llty(g.ty)}, ptr @g_{e.s}`)
return val(r, g.ty)
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,24 @@
program T {
# a const reference carries its initializer's real type — so a `fixed` const
# compares and computes as fixed-point, not as its raw i32 bits (the bug this
# pins: a fixed const used to lower to an `int` and silently corrupt fixed math).
const TEN: fixed = 10.0
const HALF: fixed = 0.5
const LIMIT: int = 7
const STEP: int = 3
const ON: bool = true
const OFF: bool = false
entry {
let x: fixed = 10.0
if x >= TEN { print(1) } else { print(0) } # 1 (equal counts)
if x > TEN { print(1) } else { print(0) } # 0 (not strictly greater)
print(floor(TEN)) # 10
print(floor(TEN * HALF)) # 5 (fixed * fixed)
print(floor(TEN / HALF)) # 20 (fixed / fixed)
print(floor(x - TEN)) # 0
print(LIMIT + STEP) # 10 (int const arithmetic)
print(LIMIT) # 7
if ON { print(1) } else { print(0) } # 1 (bool const true)
if OFF { print(1) } else { print(0) } # 0 (bool const false)
}
}

View file

@ -87,6 +87,7 @@ function cmd_selfhost_test() -> int {
sh_case("control", "55 4 15 1")
sh_case("match_bits", "1 2 9 16 4 15")
sh_case("fixed", "2 3 0 6 1")
sh_case("const", "1 0 10 5 20 0 10 7 1 0")
sh_case("math", "3 7 5 10 -1 0 1 2 3 3 2 5 25 50")
sh_case("math2", "1414 3 999 999 499 5 5 25 3142 179 4 2 7 25 30 500")
sh_case("textlist", "3 10 30 1 0 2 -1 30 10 10 30 30 2 0 11 104 1 0 1 0 1 0 6 -1 42 5 1 4 4")