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>
This commit is contained in:
parent
790eda6f73
commit
7c91d24595
6 changed files with 3680 additions and 3641 deletions
|
|
@ -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
24
selfhost/tests/const.ludic
Normal file
24
selfhost/tests/const.ludic
Normal 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue