ludic/selfhost/tests/const.ludic
Orkuncakilkaya 7c91d24595
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
fix(compiler): preserve declared type of const references
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>
2026-09-01 02:48:03 +03:00

24 lines
1.1 KiB
Text

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)
}
}