Commit 0d4cf0af

0d4cf0af7711Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): SplatNode inside Array literal — [*range] / [*arr]

compile_array_literal had no SplatNode branch — compile_expr on the splat returned the inner expression's default value (e.g. the lower bound for a Range), so [*0..4096] lowered to a single-element [0] and any arr[i] for i > 0 read garbage.

Detect SplatNode in array-literal elements and expand it inline:

  • RangeNode source: emit a C for loop bounded by the inclusive / exclusive endpoints, push each i into the accumulator.
  • int_array source: walk with sp_IntArray_get and push.

Optcarrot hit this in @palette = [*0..4096] — the palette ended up as just [0], breaking every palette[i] lookup later in the rendering path.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
0d4cf0af7711
Parent
d920667f4cc7

spinel_codegen.rbmodifiedView file

@@ -20442,8 +20442,35 @@
2044220442 emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
2044320443 k = 0
2044420444 while k < elems.length
20445- ev = compile_expr(elems[k])
20446- et = infer_type(elems[k])
20445+ eid = elems[k]
20446+ # SplatNode in an Array literal — `[*range]` or `[*arr]` —
20447+ # expands the inner expression's elements into the array.
20448+ # Without this branch, compile_expr lowered the splat to a
20449+ # default value (e.g., the lower bound of a Range), so
20450+ # `[*0..4096]` ended up as just `[0]`.
20451+ if @nd_type[eid] == "SplatNode"
20452+ inner = @nd_expression[eid]
20453+ it = infer_type(inner)
20454+ if @nd_type[inner] == "RangeNode"
20455+ lo = compile_expr(@nd_left[inner])
20456+ hi = compile_expr(@nd_right[inner])
20457+ cmp = range_excl_end(inner) == 1 ? "<" : "<="
20458+ ii = new_temp
20459+ emit(" for (mrb_int " + ii + " = " + lo + "; " + ii + " " + cmp + " " + hi + "; " + ii + "++) {")
20460+ emit(" sp_IntArray_push(" + tmp + ", " + ii + ");")
20461+ emit(" }")
20462+ elsif it == "int_array"
20463+ src = compile_expr(inner)
20464+ ii = new_temp
20465+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < sp_IntArray_length(" + src + "); " + ii + "++) {")
20466+ emit(" sp_IntArray_push(" + tmp + ", sp_IntArray_get(" + src + ", " + ii + "));")
20467+ emit(" }")
20468+ end
20469+ k = k + 1
20470+ next
20471+ end
20472+ ev = compile_expr(eid)
20473+ et = infer_type(eid)
2044720474 # Unbox poly values when pushing into a (concrete) IntArray.
2044820475 # An ArrayNode literal whose elements are poly (e.g.
2044920476 # `[addr]` where addr's recorded type is sp_RbVal) needs the