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_stmtand the case-as-expression emitter give the predicate temp the right pointer type (sp_<X> *) so the value is comparable as-is.compile_when_condsresolveswhen ClassNameagainst 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 literal0. Nullable predicates (obj_<X>?) match only when the value is non-NULL (Ruby'snilis 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
ptmp = new_temp if pred_type == "string" emit(" const char *" + ptmp + " = " + pred_val + ";") elsif is_obj_type(pred_type) == 1 # See compile_case_stmt — the temp must be the right pointer # type so compile_when_conds can resolve `when ClassName` to # a static match. Issue #67. bt = base_type(pred_type) obj_cname = bt[4, bt.length - 4] emit(" sp_" + obj_cname + " *" + ptmp + " = " + pred_val + ";") else emit(" mrb_int " + ptmp + " = " + pred_val + ";") end tmp = new_temp if pred_type == "string" emit(" const char *" + tmp + " = " + pred_val + ";") elsif is_obj_type(pred_type) == 1 # `case obj when ClassName` — keep the temp as the right pointer # type so the when arms can read its NULLability and the static # class match in compile_when_conds picks the matching cls_id # path. Issue #67. bt = base_type(pred_type) obj_cname = bt[4, bt.length - 4] emit(" sp_" + obj_cname + " *" + tmp + " = " + pred_val + ";") else emit(" mrb_int " + tmp + " = " + pred_val + ";") end right = compile_expr(@nd_right[cid]) cmp = range_excl_end(cid) == 1 ? "<" : "<=" result = result + "(" + tmp + " >= " + left + " && " + tmp + " " + cmp + " " + right + ")" elsif is_obj_type(pred_type) == 1 && @nd_type[cid] == "ConstantReadNode" # `case obj when ClassName` — resolve statically against the # predicate's known class. Predicate type `obj_X`: # when X (or any ancestor of X) → match (with a NULL guard # when the predicate is # nullable, since `nil` is # not a class instance) # when anything else → no match # Subclass matching across an `obj_<Parent>` predicate that # actually carries an `obj_<Child>` instance needs a runtime # cls_id check; that's a separate enhancement (issue #67 only # covers the static-class form of the bug). cname = @nd_name[cid] if find_class_idx(cname) >= 0 bt = base_type(pred_type) pred_cname = bt[4, bt.length - 4] if is_class_or_ancestor(pred_cname, cname) == 1 if is_nullable_type(pred_type) == 1 result = result + tmp + " != NULL" else result = result + "1" end else result = result + "0" end else result = result + "0" end else if pred_type == "string" result = result + "strcmp(" + tmp + ", " + compile_expr(cid) + ") == 0"test/case_when_class.rbaddedView file
Loading diff…