Commit 91d110ac

91d110ac4c57Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #461 from ryanseys/rs-case-when-class-fixes

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

Details

Commit
91d110ac4c57
Parent
53b92f29ed2d
Merge parent
0be81c976fba

spinel_codegen.rbmodifiedView file

@@ -10627,12 +10627,11 @@
1062710627 if pred_type == "string"
1062810628 emit(" const char *" + ptmp + " = " + pred_val + ";")
1062910629 elsif is_obj_type(pred_type) == 1
10630- # See compile_case_stmt — the temp must be the right pointer
10631- # type so compile_when_conds can resolve `when ClassName` to
10632- # a static match. .
10633- bt = base_type(pred_type)
10634- obj_cname = bt[4, bt.length - 4]
10635- emit(" sp_" + obj_cname + " *" + ptmp + " = " + pred_val + ";")
10630+ # See compile_case_stmt — route the temp declaration through
10631+ # c_type so value-type classes get `sp_<X>` (no pointer) and
10632+ # standard heap classes get `sp_<X> *`. compile_when_conds then
10633+ # resolves `when ClassName` against the right shape.
10634+ emit(" " + c_type(pred_type) + " " + ptmp + " = " + pred_val + ";")
1063610635 else
1063710636 emit(" mrb_int " + ptmp + " = " + pred_val + ";")
1063810637 end
@@ -22529,13 +22528,12 @@
2252922528 if pred_type == "string"
2253022529 emit(" const char *" + tmp + " = " + pred_val + ";")
2253122530 elsif is_obj_type(pred_type) == 1
22532- # `case obj when ClassName` — keep the temp as the right pointer
22533- # type so the when arms can read its NULLability and the static
22534- # class match in compile_when_conds picks the matching cls_id
22535- # path. .
22536- bt = base_type(pred_type)
22537- obj_cname = bt[4, bt.length - 4]
22538- emit(" sp_" + obj_cname + " *" + tmp + " = " + pred_val + ";")
22531+ # `case obj when ClassName` — declare the temp at the C type
22532+ # c_type uses for this obj kind. That picks `sp_<X>` for
22533+ # value-type classes (per-class free-list pool) and `sp_<X> *`
22534+ # for the standard pointer representation, so the assignment
22535+ # matches the predicate's actual storage shape.
22536+ emit(" " + c_type(pred_type) + " " + tmp + " = " + pred_val + ";")
2253922537 elsif pred_type == "poly"
2254022538 # `case <poly_value> when ...` — keep the receiver tagged so
2254122539 # each when-arm can do tag-check + value-compare. .
@@ -22616,19 +22614,18 @@
2261622614 right = compile_expr(@nd_right[cid])
2261722615 cmp = range_excl_end(cid) == 1 ? "<" : "<="
2261822616 result = result + "(" + tmp + " >= " + left + " && " + tmp + " " + cmp + " " + right + ")"
22619- elsif is_obj_type(pred_type) == 1 && @nd_type[cid] == "ConstantReadNode"
22617+ elsif is_obj_type(pred_type) == 1 && (@nd_type[cid] == "ConstantReadNode" || @nd_type[cid] == "ConstantPathNode")
2262022618 # `case obj when ClassName` — resolve statically against the
22621- # predicate's known class. Predicate type `obj_X`:
22619+ # predicate's known class. ConstantPathNode (`Mod::Klass`) is
22620+ # routed through resolve_const_ref_name so the walked name
22621+ # matches the class registry. Predicate type `obj_X`:
2262222622 # when X (or any ancestor of X) → match (with a NULL guard
22623- # when the predicate is
22624- # nullable, since `nil` is
22625- # not a class instance)
22626- # when anything else → no match
22627- # Subclass matching across an `obj_<Parent>` predicate that
22628- # actually carries an `obj_<Child>` instance needs a runtime
22629- # cls_id check; that's a separate enhancement only
22630- # covers the static-class form of the bug).
22631- cname = @nd_name[cid]
22623+ # when the predicate is nullable, since `nil` is not a class
22624+ # instance); anything else → no match. Subclass matching
22625+ # across an `obj_<Parent>` predicate that actually carries an
22626+ # `obj_<Child>` instance needs a runtime cls_id check; covered
22627+ # by the descendant runtime branch in is_a?, separate concern.
22628+ cname = @nd_type[cid] == "ConstantPathNode" ? resolve_const_ref_name(cid) : @nd_name[cid]
2263222629 if find_class_idx(cname) >= 0
2263322630 bt = base_type(pred_type)
2263422631 pred_cname = bt[4, bt.length - 4]