Commit a7f1d9b6

a7f1d9b68dcaMerged

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

Description

fix(codegen): box pointer-typed values to poly via sp_box_obj

box_val_to_poly and box_expr_to_poly had a final fallback that emitted sp_box_int(val) for any type they didn't explicitly know about. For pointer-typed expressions (hashes, mutable strings, arrays of object types, user objects, etc.) this silently passed the pointer to a function expecting mrb_int, producing -Wint-conversion errors and (if compiled) truncating the pointer to a 64-bit int.

Add explicit boxing for the array-of-X variants and obj_X (matching what box_expr_to_poly already did for those), then a generic type_is_pointer fallback that routes through sp_box_obj(p, 0). The cls_id of 0 means dispatch on the resulting poly value can't recover the original concrete type, but that's the caller's problem — this just makes the boxing produce a valid sp_RbVal instead of a silently-corrupted int, which is the right floor for a fallback.

In optcarrot, this drops the C-compile error count by ~30 — every lv_opt = sp_box_int(<some_hash>) style assignment generated by the unify-to-poly widening was failing.

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

Details

Commit
a7f1d9b68dca
Parent
bee7e89d5b64

spinel_codegen.rbmodifiedView file

@@ -19179,6 +19179,11 @@
1917919179 if at == "proc" || at == "lambda"
1918019180 return "sp_box_proc(" + val + ")"
1918119181 end
19182+ # Other pointer types (hashes, mutable strings, etc.) — box with a
19183+ # neutral cls_id of 0 rather than truncating the pointer to int.
19184+ if type_is_pointer(at) == 1
19185+ return "sp_box_obj((void *)(" + val + "), 0)"
19186+ end
1918219187 "sp_box_int(" + val + ")"
1918319188 end
1918419189
@@ -19204,6 +19209,33 @@
1920419209 if at == "symbol"
1920519210 return "sp_box_sym(" + val + ")"
1920619211 end
19212+ if at == "int_array"
19213+ return "sp_box_int_array(" + val + ")"
19214+ end
19215+ if at == "float_array"
19216+ return "sp_box_float_array(" + val + ")"
19217+ end
19218+ if at == "str_array"
19219+ return "sp_box_str_array(" + val + ")"
19220+ end
19221+ if at == "sym_array"
19222+ return "sp_box_sym_array(" + val + ")"
19223+ end
19224+ if is_ptr_array_type(at) == 1
19225+ return "sp_box_ptr_array(" + val + ")"
19226+ end
19227+ if is_obj_type(at) == 1
19228+ cname = at[4, at.length - 4]
19229+ ci = find_class_idx(cname)
19230+ return "sp_box_obj(" + val + ", " + ci.to_s + ")"
19231+ end
19232+ # Other pointer types (hashes, mutable strings, etc.) — box with a
19233+ # neutral cls_id of 0. Round-tripping back to the original concrete
19234+ # type is the caller's problem; this just makes the assignment
19235+ # type-check rather than silently truncating a pointer to mrb_int.
19236+ if type_is_pointer(at) == 1
19237+ return "sp_box_obj((void *)(" + val + "), 0)"
19238+ end
1920719239 "sp_box_int(" + val + ")"
1920819240 end
1920919241