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

@@ -23215,6 +23215,14 @@
2321523215 # long long) and strcmp would reject it.
2321623216 if @nd_type[cid] == "SymbolNode"
2321723217 result = result + "0"
23218+ # `case <string> when ClassConst` — Module#===: matches when
23219+ # the recv is an instance of ClassConst. For a string predicate,
23220+ # only `when String` matches; other built-in class constants
23221+ # never match. Without this branch the generic strcmp arm
23222+ # below tried to compare the sp_Class struct with the string
23223+ # tmp, tripping the C compile.
23224+ elsif @nd_type[cid] == "ConstantReadNode" && not_in(@nd_name[cid], @builtin_class_names) == 0
23225+ result = result + (@nd_name[cid] == "String" ? "1" : "0")
2321823226 else
2321923227 result = result + "strcmp(" + tmp + ", " + compile_expr(cid) + ") == 0"
2322023228 end
@@ -23225,6 +23233,35 @@
2322523233 # char *` type mismatch in the generated C.
2322623234 if pred_type == "symbol" && @nd_type[cid] == "StringNode"
2322723235 result = result + "0"
23236+ # `case <scalar> when ClassConst` — Module#=== resolution
23237+ # against the predicate's known kind. Matches when the
23238+ # class name corresponds to the predicate's type (Integer
23239+ # for int, Float for float, Symbol for symbol, TrueClass /
23240+ # FalseClass for bool); otherwise no match. Same shape as
23241+ # the string-predicate branch above; without this the
23242+ # generic equality arm would compare an sp_Class struct to
23243+ # an mrb_int / mrb_float / etc.
23244+ elsif @nd_type[cid] == "ConstantReadNode" && not_in(@nd_name[cid], @builtin_class_names) == 0
23245+ cname_w2 = @nd_name[cid]
23246+ matches_pw = 0
23247+ if pred_type == "int"
23248+ if cname_w2 == "Integer" || cname_w2 == "Numeric"
23249+ matches_pw = 1
23250+ end
23251+ elsif pred_type == "float"
23252+ if cname_w2 == "Float" || cname_w2 == "Numeric"
23253+ matches_pw = 1
23254+ end
23255+ elsif pred_type == "symbol"
23256+ if cname_w2 == "Symbol"
23257+ matches_pw = 1
23258+ end
23259+ elsif pred_type == "bool"
23260+ if cname_w2 == "TrueClass" || cname_w2 == "FalseClass"
23261+ matches_pw = 1
23262+ end
23263+ end
23264+ result = result + matches_pw.to_s
2322823265 else
2322923266 result = result + tmp + " == " + compile_expr(cid)
2323023267 end