Commit 98bf2558

98bf255871b4Merged

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

Description

fix(codegen): unbox poly elements when emitting an IntArray literal

`compile_array_literal`'s IntArray branch emitted `sp_IntArray_push(arr, compile_expr(elem))` without consulting the element's inferred type. When the element was poly (sp_RbVal, e.g. optcarrot's CPU#add_mappings does `addr = [addr]` after unify-to-poly widened addr to poly), the push got handed a sp_RbVal struct and gcc rejected with "incompatible type for argument 2 of sp_IntArray_push".

Extract the int payload via `.v.i` when the element type is `poly`. The runtime tag is not checked; we trust the surrounding code to keep the array element typed correctly. A real fix would either (a) widen the array literal itself to a poly_array when any element is poly, or (b) emit a runtime type check. This unboxing is the simpler match for optcarrot's add_mappings shape where the caller code knows the addr is integer at runtime.

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

Details

Commit
98bf255871b4
Parent
bee7e89d5b64

spinel_codegen.rbmodifiedView file

@@ -20123,7 +20123,17 @@
2012320123 emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
2012420124 k = 0
2012520125 while k < elems.length
20126- emit(" sp_IntArray_push(" + tmp + ", " + compile_expr(elems[k]) + ");")
20126+ ev = compile_expr(elems[k])
20127+ et = infer_type(elems[k])
20128+ # Unbox poly values when pushing into a (concrete) IntArray.
20129+ # An ArrayNode literal whose elements are poly (e.g.
20130+ # `[addr]` where addr's recorded type is sp_RbVal) needs the
20131+ # int payload extracted via `.v.i`. Without this, the C
20132+ # compiler rejects the push with "incompatible type".
20133+ if et == "poly"
20134+ ev = "(" + ev + ").v.i"
20135+ end
20136+ emit(" sp_IntArray_push(" + tmp + ", " + ev + ");")
2012720137 k = k + 1
2012820138 end
2012920139 tmp