Commit b6f25665
b6f25665a42dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): case/when ClassConst on scalar predicate
case <scalar> when ClassConst previously fell through to the
generic equality arm in compile_when_conds, emitting
strcmp(<tmp>, <ClassConst>) for a string predicate (the
sp_Class struct vs const char *, C type mismatch) and a similar
mismatch for int / float / symbol / bool predicates compared
against the sp_Class struct.
Fix: at the per-arm dispatch, detect a ConstantReadNode when-arg
that names a built-in class (in @builtin_class_names) and
resolve Module#=== statically against the predicate's known
kind. String matches a string predicate; Integer / Numeric
match int; Float / Numeric match float; Symbol matches
symbol; TrueClass / FalseClass match bool. Anything else
resolves to a static 0. Mirrors the existing obj-predicate
branch (line ~23137) one level down for built-in scalar types.
Validation: tests 457 pass (added regression covering string + int + symbol predicates against multiple class consts), bench 57/57, bootstrap 4-way fixpoint OK.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- b6f25665a42d
- Parent
- cde32be3990a
spinel_codegen.rbmodifiedView file
# long long) and strcmp would reject it. if @nd_type[cid] == "SymbolNode" result = result + "0" # `case <string> when ClassConst` — Module#===: matches when # the recv is an instance of ClassConst. For a string predicate, # only `when String` matches; other built-in class constants # never match. Without this branch the generic strcmp arm # below tried to compare the sp_Class struct with the string # tmp, tripping the C compile. elsif @nd_type[cid] == "ConstantReadNode" && not_in(@nd_name[cid], @builtin_class_names) == 0 result = result + (@nd_name[cid] == "String" ? "1" : "0") else result = result + "strcmp(" + tmp + ", " + compile_expr(cid) + ") == 0" end # char *` type mismatch in the generated C. if pred_type == "symbol" && @nd_type[cid] == "StringNode" result = result + "0" # `case <scalar> when ClassConst` — Module#=== resolution # against the predicate's known kind. Matches when the # class name corresponds to the predicate's type (Integer # for int, Float for float, Symbol for symbol, TrueClass / # FalseClass for bool); otherwise no match. Same shape as # the string-predicate branch above; without this the # generic equality arm would compare an sp_Class struct to # an mrb_int / mrb_float / etc. elsif @nd_type[cid] == "ConstantReadNode" && not_in(@nd_name[cid], @builtin_class_names) == 0 cname_w2 = @nd_name[cid] matches_pw = 0 if pred_type == "int" if cname_w2 == "Integer" || cname_w2 == "Numeric" matches_pw = 1 end elsif pred_type == "float" if cname_w2 == "Float" || cname_w2 == "Numeric" matches_pw = 1 end elsif pred_type == "symbol" if cname_w2 == "Symbol" matches_pw = 1 end elsif pred_type == "bool" if cname_w2 == "TrueClass" || cname_w2 == "FalseClass" matches_pw = 1 end end result = result + matches_pw.to_s else result = result + tmp + " == " + compile_expr(cid) endtest/case_when_class_scalar_predicate.rbaddedView file
Loading diff…
test/case_when_class_scalar_predicate.rb.expectedaddedView file
Loading diff…