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
if pred_type == "poly" # `case <poly> when <lit>` — Issue #387. The pred_tmp is # an sp_RbVal; emit a tag-check + value-compare matched # to the literal's type. Class consts (`when Foo`) are # left for the future Class-object work; here we only # cover the value-literal arms (sym/str/int/float/nil). # to the literal's type. ct = @nd_type[cid] if ct == "SymbolNode" # Issue #404 Phase 3 Tier 4 follow-up: `case <poly> when # ClassConst` -- Module#=== treats the recv as the class # and the arg as the instance, so it's equivalent to # `arg.is_a?(recv)`. sp_class_le over the precomputed # ancestors handles user classes, modules, and built-ins # uniformly through sp_class_for_poly. handled_class_when = 0 if ct == "ConstantReadNode" cname_w = @nd_name[cid] cuid = unified_cls_id_for_name(cname_w) if cuid >= 0 @needs_class_table = 1 @needs_class_ancestors = 1 result = result + "sp_class_le(sp_class_for_poly(" + tmp + "), (sp_Class){" + cuid.to_s + "LL})" handled_class_when = 1 end end if handled_class_when == 1 # already appended above; skip literal-arms cascade elsif ct == "SymbolNode" result = result + "(" + tmp + ".tag == SP_TAG_SYM && " + tmp + ".v.i == " + compile_expr(cid) + ")" elsif ct == "StringNode" result = result + "(" + tmp + ".tag == SP_TAG_STR && strcmp(" + tmp + ".v.s, " + compile_expr(cid) + ") == 0)"test/case_when_class.rbmodifiedView file
Loading diff…
test/case_when_class.rb.expectedmodifiedView file
Loading diff…