Commit c036aa7d

c036aa7ddf59Merged

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

Description

fix(codegen): static-class match for case obj when ClassName

A case obj when ClassName predicate over an obj-typed value was lowered as mrb_int _t = lv_obj; if (_t == ClassName) .... Both halves were broken: the temp's type didn't match the pointer it was initialized from, and the when arms compared against bare class identifiers that weren't declared in C.

Two pieces, scoped to the monomorphic case (predicate has a statically-known obj_X):

  • compile_case_stmt and the case-as-expression emitter give the predicate temp the right pointer type (sp_<X> *) so the value is comparable as-is.
  • compile_when_conds resolves when ClassName against an obj- typed predicate at compile time. is_class_or_ancestor(X, when) matches the same class or any ancestor; an unrelated when-class folds to a literal 0. Nullable predicates (obj_<X>?) match only when the value is non-NULL (Ruby's nil is not a class instance).

Subclass matching against a parent-typed predicate (where the runtime instance is actually a child class) still requires a runtime cls_id check; that's a separate enhancement.

test/case_when_class.rb covers the issue's reproducer plus the expression form, an else fall-through, and ancestor matching.

Closes #67

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

Details

Commit
c036aa7ddf59
Parent
43fcd7c7d9d5

spinel_codegen.rbmodifiedView file

@@ -12749,6 +12749,13 @@
1274912749 ptmp = new_temp
1275012750 if pred_type == "string"
1275112751 emit(" const char *" + ptmp + " = " + pred_val + ";")
12752+ elsif is_obj_type(pred_type) == 1
12753+ # See compile_case_stmt — the temp must be the right pointer
12754+ # type so compile_when_conds can resolve `when ClassName` to
12755+ # a static match. Issue #67.
12756+ bt = base_type(pred_type)
12757+ obj_cname = bt[4, bt.length - 4]
12758+ emit(" sp_" + obj_cname + " *" + ptmp + " = " + pred_val + ";")
1275212759 else
1275312760 emit(" mrb_int " + ptmp + " = " + pred_val + ";")
1275412761 end
@@ -19411,6 +19418,14 @@
1941119418 tmp = new_temp
1941219419 if pred_type == "string"
1941319420 emit(" const char *" + tmp + " = " + pred_val + ";")
19421+ elsif is_obj_type(pred_type) == 1
19422+ # `case obj when ClassName` — keep the temp as the right pointer
19423+ # type so the when arms can read its NULLability and the static
19424+ # class match in compile_when_conds picks the matching cls_id
19425+ # path. Issue #67.
19426+ bt = base_type(pred_type)
19427+ obj_cname = bt[4, bt.length - 4]
19428+ emit(" sp_" + obj_cname + " *" + tmp + " = " + pred_val + ";")
1941419429 else
1941519430 emit(" mrb_int " + tmp + " = " + pred_val + ";")
1941619431 end
@@ -19487,6 +19502,34 @@
1948719502 right = compile_expr(@nd_right[cid])
1948819503 cmp = range_excl_end(cid) == 1 ? "<" : "<="
1948919504 result = result + "(" + tmp + " >= " + left + " && " + tmp + " " + cmp + " " + right + ")"
19505+ elsif is_obj_type(pred_type) == 1 && @nd_type[cid] == "ConstantReadNode"
19506+ # `case obj when ClassName` — resolve statically against the
19507+ # predicate's known class. Predicate type `obj_X`:
19508+ # when X (or any ancestor of X) → match (with a NULL guard
19509+ # when the predicate is
19510+ # nullable, since `nil` is
19511+ # not a class instance)
19512+ # when anything else → no match
19513+ # Subclass matching across an `obj_<Parent>` predicate that
19514+ # actually carries an `obj_<Child>` instance needs a runtime
19515+ # cls_id check; that's a separate enhancement (issue #67 only
19516+ # covers the static-class form of the bug).
19517+ cname = @nd_name[cid]
19518+ if find_class_idx(cname) >= 0
19519+ bt = base_type(pred_type)
19520+ pred_cname = bt[4, bt.length - 4]
19521+ if is_class_or_ancestor(pred_cname, cname) == 1
19522+ if is_nullable_type(pred_type) == 1
19523+ result = result + tmp + " != NULL"
19524+ else
19525+ result = result + "1"
19526+ end
19527+ else
19528+ result = result + "0"
19529+ end
19530+ else
19531+ result = result + "0"
19532+ end
1949019533 else
1949119534 if pred_type == "string"
1949219535 result = result + "strcmp(" + tmp + ", " + compile_expr(cid) + ") == 0"