Commit 0be81c97

0be81c976fbaMerged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): case/when on value-type class and qualified-constant arm

case obj when ClassName had two C-compile-blocking bugs when the predicate's class was a value-type instance (e.g. per-class free-list pool layouts where lv_x is sp_X rather than sp_X *):

  1. compile_case_stmt and the case-as-expression branch in compile_expr always emitted the predicate tmp as sp_<X> *, regardless of whether the predicate's actual storage was a value type. The generated C then failed with "initializing 'sp_X *' with an expression of incompatible type 'sp_X'; take the address with &".

  2. The static-resolution branch in compile_when_conds only matched the bare ConstantReadNode arg shape, so a qualified when Mod::Klass (ConstantPathNode) fell through to the poly-recv path and emitted <undeclared identifier> Mod_Klass against a non-poly predicate.

Fix:

  • Route both tmp declarations through c_type(pred_type) so value-type classes get sp_<X> (no pointer) and standard heap classes get sp_<X> *. The assignment then matches the predicate's actual storage shape in both cases.
  • Extend the const-shape guard in compile_when_conds to also match ConstantPathNode, resolving the qualified name via the existing resolve_const_ref_name helper.

Test: test/case_when_qualified_class.rb exercises both fixes via a value-type module-namespaced class and a value-type plain class.

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

Details

Commit
0be81c976fba
Parent
d5b20a0db9db

spinel_codegen.rbmodifiedView file

@@ -10625,12 +10625,11 @@
1062510625 if pred_type == "string"
1062610626 emit(" const char *" + ptmp + " = " + pred_val + ";")
1062710627 elsif is_obj_type(pred_type) == 1
10628- # See compile_case_stmt — the temp must be the right pointer
10629- # type so compile_when_conds can resolve `when ClassName` to
10630- # a static match. .
10631- bt = base_type(pred_type)
10632- obj_cname = bt[4, bt.length - 4]
10633- emit(" sp_" + obj_cname + " *" + ptmp + " = " + pred_val + ";")
10628+ # See compile_case_stmt — route the temp declaration through
10629+ # c_type so value-type classes get `sp_<X>` (no pointer) and
10630+ # standard heap classes get `sp_<X> *`. compile_when_conds then
10631+ # resolves `when ClassName` against the right shape.
10632+ emit(" " + c_type(pred_type) + " " + ptmp + " = " + pred_val + ";")
1063410633 else
1063510634 emit(" mrb_int " + ptmp + " = " + pred_val + ";")
1063610635 end
@@ -22423,13 +22422,12 @@
2242322422 if pred_type == "string"
2242422423 emit(" const char *" + tmp + " = " + pred_val + ";")
2242522424 elsif is_obj_type(pred_type) == 1
22426- # `case obj when ClassName` — keep the temp as the right pointer
22427- # type so the when arms can read its NULLability and the static
22428- # class match in compile_when_conds picks the matching cls_id
22429- # path. .
22430- bt = base_type(pred_type)
22431- obj_cname = bt[4, bt.length - 4]
22432- emit(" sp_" + obj_cname + " *" + tmp + " = " + pred_val + ";")
22425+ # `case obj when ClassName` — declare the temp at the C type
22426+ # c_type uses for this obj kind. That picks `sp_<X>` for
22427+ # value-type classes (per-class free-list pool) and `sp_<X> *`
22428+ # for the standard pointer representation, so the assignment
22429+ # matches the predicate's actual storage shape.
22430+ emit(" " + c_type(pred_type) + " " + tmp + " = " + pred_val + ";")
2243322431 elsif pred_type == "poly"
2243422432 # `case <poly_value> when ...` — keep the receiver tagged so
2243522433 # each when-arm can do tag-check + value-compare. .
@@ -22510,19 +22508,18 @@
2251022508 right = compile_expr(@nd_right[cid])
2251122509 cmp = range_excl_end(cid) == 1 ? "<" : "<="
2251222510 result = result + "(" + tmp + " >= " + left + " && " + tmp + " " + cmp + " " + right + ")"
22513- elsif is_obj_type(pred_type) == 1 && @nd_type[cid] == "ConstantReadNode"
22511+ elsif is_obj_type(pred_type) == 1 && (@nd_type[cid] == "ConstantReadNode" || @nd_type[cid] == "ConstantPathNode")
2251422512 # `case obj when ClassName` — resolve statically against the
22515- # predicate's known class. Predicate type `obj_X`:
22513+ # predicate's known class. ConstantPathNode (`Mod::Klass`) is
22514+ # routed through resolve_const_ref_name so the walked name
22515+ # matches the class registry. Predicate type `obj_X`:
2251622516 # when X (or any ancestor of X) → match (with a NULL guard
22517- # when the predicate is
22518- # nullable, since `nil` is
22519- # not a class instance)
22520- # when anything else → no match
22521- # Subclass matching across an `obj_<Parent>` predicate that
22522- # actually carries an `obj_<Child>` instance needs a runtime
22523- # cls_id check; that's a separate enhancement only
22524- # covers the static-class form of the bug).
22525- cname = @nd_name[cid]
22517+ # when the predicate is nullable, since `nil` is not a class
22518+ # instance); anything else → no match. Subclass matching
22519+ # across an `obj_<Parent>` predicate that actually carries an
22520+ # `obj_<Child>` instance needs a runtime cls_id check; covered
22521+ # by the descendant runtime branch in is_a?, separate concern.
22522+ cname = @nd_type[cid] == "ConstantPathNode" ? resolve_const_ref_name(cid) : @nd_name[cid]
2252622523 if find_class_idx(cname) >= 0
2252722524 bt = base_type(pred_type)
2252822525 pred_cname = bt[4, bt.length - 4]