Commit 00a23f98

00a23f9868f7Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): is_a? narrow on poly LV unboxes for typed-obj args

compile_expr_for_expected_type's poly-source unbox arm only fired when at == "poly". For a LV-read whose declared storage is poly but the surrounding if v.is_a?(C) guard narrowed at to the concrete obj_<C>, the arm fell through and the raw sp_RbVal slot flowed into a sp_<C> *-typed param at the call site. C compile failed with "expected 'sp_X *' but argument is of type 'sp_RbVal'".

Surfaced in bm_micro_lisp's eval_form recursion:

if form.is_a?(PairV)
  cond = eval_form(list_get(form, 1), env)   # list_get(sp_PairV *)

Also closes box_expr_to_poly's mirror — when the boxed-poly LV is returned from a poly-method, the previous code re-wrapped the already-sp_RbVal value as sp_box_obj(lv, cls_id) (void* type mismatch).

Detect the shape on the LV's declared type and treat it as poly- source for the obj/hash/array narrows; the primitive narrows (int / string / float / bool / sym) are already unboxed inline by compile_expr's LV-read arm so don't need a second pass.

Benchmark: bm_micro_lisp ERR -> PASS. Default raise: 627 / 0 / 0. Optcarrot checksum 59662 unchanged.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
00a23f9868f7
Parent
1bbd268b3780

spinel_codegen.rbmodifiedView file

@@ -5765,7 +5765,21 @@
57655765 # the same trust extends from the #438 unbox-on-IntStrHash-key
57665766 # pattern. Skip the unbox when the expected slot is itself poly
57675767 # — that path already widens via box_value_to_poly below.
5768- if at == "poly" && expected_base != "poly"
5768+ # When the source is a LV-read whose declared storage is poly
5769+ # and `at` is an obj_<C> / hash / array narrow (these primitives
5770+ # — int/string/float/bool/sym — get unboxed inline by compile
5771+ # _expr's LV-read arm, so they reach here already unboxed and
5772+ # mustn't be unboxed twice). For the pointer-shaped narrows
5773+ # compile_expr returns the raw sp_RbVal slot, so the unbox
5774+ # below must still fire.
5775+ src_storage_poly_pft = 0
5776+ if nid >= 0 && @nd_type[nid] == "LocalVariableReadNode"
5777+ if find_var_declared_type(@nd_name[nid]) == "poly" &&
5778+ (is_obj_type(at) == 1 || is_hash_type(at) == 1 || is_array_type(at) == 1)
5779+ src_storage_poly_pft = 1
5780+ end
5781+ end
5782+ if (at == "poly" || src_storage_poly_pft == 1) && expected_base != "poly"
57695783 if expected_base == "string" || expected_base == "mutable_str"
57705784 @needs_rb_value = 1
57715785 return "(" + val + ").v.s"
@@ -23320,6 +23334,20 @@
2332023334 # wrapper picks the right `sp_box_nullable_*` variant before
2332123335 # falling back to base-type dispatch.
2332223336 at = infer_type(nid)
23337+ # `is_a?(<C>)`-narrowed LV read where declared storage is poly:
23338+ # `compile_expr` returns the raw sp_RbVal slot (`lv_x`), so we
23339+ # already have a boxed value. `box_value_to_poly("obj_<C>",
23340+ # "lv_x")` would emit `sp_box_obj(lv_x, <cls_id>)` — type-clash
23341+ # at the void* arg. Short-circuit: declared-poly LV with an
23342+ # obj-narrow stays as the existing slot value (already sp_RbVal,
23343+ # the runtime tag/cls_id match the narrow).
23344+ if nid >= 0 && @nd_type[nid] == "LocalVariableReadNode"
23345+ lvname_bep = @nd_name[nid]
23346+ declared_bep = find_var_declared_type(lvname_bep)
23347+ if declared_bep == "poly" && is_obj_type(at) == 1
23348+ return compile_expr(nid)
23349+ end
23350+ end
2332323351 val = compile_expr(nid)
2332423352 box_value_to_poly(at, val)
2332523353 end