Commit 56fdbe6b

56fdbe6b0712Merged

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

Description

feat(codegen): case-when on Class const for poly recv (#404)

Pre-Tier-4 the case <poly> when ClassConst arm was explicitly deferred ("Class consts (when Foo) are left for the future Class-object work") and fell through to a fallback that compared the unboxed sp_RbVal against a sp_Class struct -- the C compiler rejected the operand types and the user saw a confusing emit error.

With sp_class_for_poly and the precomputed ancestors table in place, Module#=== reduces to arg.is_a?(recv) -- exactly the shape sp_class_le already evaluates. Detect a ConstantReadNode arm condition in the case-when poly cascade, resolve the name through unified_cls_id_for_name (covers built-ins, user classes, and modules), and emit sp_class_le(sp_class_for_poly(<recv>), ((sp_Class){<cuid>LL})). Non-class ConstantReadNodes (arbitrary user constants) fall through to the existing literal arms unchanged.

Coverage in test/case_when_class.rb: built-in class dispatch through a poly recv (Integer / String / Float / Symbol with else-fallthrough). User-class and module dispatch share the same code path via unified_cls_id_for_name; the test stays on primitives to exercise the sp_class_for_poly mapping that Tier 4 added.

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

Details

Commit
56fdbe6b0712
Parent
f934eea0a9dd

spinel_codegen.rbmodifiedView file

@@ -21490,11 +21490,28 @@
2149021490 if pred_type == "poly"
2149121491 # `case <poly> when <lit>` — Issue #387. The pred_tmp is
2149221492 # an sp_RbVal; emit a tag-check + value-compare matched
21493- # to the literal's type. Class consts (`when Foo`) are
21494- # left for the future Class-object work; here we only
21495- # cover the value-literal arms (sym/str/int/float/nil).
21493+ # to the literal's type.
2149621494 ct = @nd_type[cid]
21497- if ct == "SymbolNode"
21495+ # Issue #404 Phase 3 Tier 4 follow-up: `case <poly> when
21496+ # ClassConst` -- Module#=== treats the recv as the class
21497+ # and the arg as the instance, so it's equivalent to
21498+ # `arg.is_a?(recv)`. sp_class_le over the precomputed
21499+ # ancestors handles user classes, modules, and built-ins
21500+ # uniformly through sp_class_for_poly.
21501+ handled_class_when = 0
21502+ if ct == "ConstantReadNode"
21503+ cname_w = @nd_name[cid]
21504+ cuid = unified_cls_id_for_name(cname_w)
21505+ if cuid >= 0
21506+ @needs_class_table = 1
21507+ @needs_class_ancestors = 1
21508+ result = result + "sp_class_le(sp_class_for_poly(" + tmp + "), (sp_Class){" + cuid.to_s + "LL})"
21509+ handled_class_when = 1
21510+ end
21511+ end
21512+ if handled_class_when == 1
21513+ # already appended above; skip literal-arms cascade
21514+ elsif ct == "SymbolNode"
2149821515 result = result + "(" + tmp + ".tag == SP_TAG_SYM && " + tmp + ".v.i == " + compile_expr(cid) + ")"
2149921516 elsif ct == "StringNode"
2150021517 result = result + "(" + tmp + ".tag == SP_TAG_STR && strcmp(" + tmp + ".v.s, " + compile_expr(cid) + ") == 0)"