Commit a0a69b34

a0a69b34ea63Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Handle splat / Rational / Complex conditions in when

Three related case/when condition shapes were broken in compile_when_conds (all fixed here since they share that one method):

  • when *array (#1151) lowered the splat to a single default value and never matched. Now emits a membership test (sp_IntArray_include / sp_StrArray_include) for a type-compatible subject; an incompatible subject/array can't match, so it emits 0.
  • when <Rational> (#1152) and when <Complex> (#1153) emitted mrb_int == <struct literal>, an invalid C comparison. A Rational/Complex literal can only === a numeric subject, so emit a value comparison for int/float subjects (Rational: whole-number equality; purely-imaginary Complex: matches 0 only when the coefficient is 0) and 0 for non-numeric subjects.

case 2; when *[1,2,3] ... # => matches (was: never) case 0; when 0r ... # => matches (was: compile error) case 0; when 0i ... # => matches (was: compile error)

Rational/Complex subjects compared against same-type literals aren't special-cased (rare); they fall to 0.

Fixes #1151 Fixes #1152 Fixes #1153

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
a0a69b34ea63
Parent
b8fce9e990cf

spinel_codegen.rbmodifiedView file

@@ -35674,6 +35674,23 @@
3567435674 emit(" }")
3567535675 end
3567635676
35677+ # True (1) when an ImaginaryNode's coefficient is the literal 0 (so the
35678+ # complex value is (0+0i), which can === a real subject). `i` alone is
35679+ # 1i (coefficient 1).
35680+ def imaginary_im_is_zero(nid)
35681+ inner = @nd_expression[nid]
35682+ if inner < 0
35683+ return 0
35684+ end
35685+ if @nd_type[inner] == "IntegerNode"
35686+ return @nd_value[inner].to_i == 0 ? 1 : 0
35687+ end
35688+ if @nd_type[inner] == "FloatNode"
35689+ return @nd_content[inner].to_f == 0.0 ? 1 : 0
35690+ end
35691+ 0
35692+ end
35693+
3567735694 def compile_when_conds(wid, tmp, pred_type)
3567835695 wconds = parse_id_list(@nd_conditions[wid])
3567935696 result = ""
@@ -35683,6 +35700,55 @@
3568335700 result = result + " || "
3568435701 end
3568535702 cid = wconds[k]
35703+ # `when *arr` -- Array#=== is membership: match when the subject is
35704+ # an element of the splatted array. Without this the splat lowered to
35705+ # a single default value and never matched.
35706+ if @nd_type[cid] == "SplatNode"
35707+ inner_wc = @nd_expression[cid]
35708+ it_wc = inner_wc >= 0 ? infer_type(inner_wc) : ""
35709+ if pred_type == "int" && (it_wc == "int_array" || it_wc == "sym_array")
35710+ result = result + "sp_IntArray_include(" + compile_expr(inner_wc) + ", " + tmp + ")"
35711+ elsif pred_type == "string" && it_wc == "str_array"
35712+ result = result + "sp_StrArray_include(" + compile_expr(inner_wc) + ", " + tmp + ")"
35713+ else
35714+ # Type-incompatible subject/array (or a shape we don't special-case):
35715+ # no element can be ===, so this arm never matches.
35716+ result = result + "0"
35717+ end
35718+ k = k + 1
35719+ next
35720+ end
35721+ # `when <Rational>` / `when <Complex>` -- a numeric literal of these
35722+ # value types can only === a numeric subject. CRuby Rational#== /
35723+ # Complex#== are value equality; emit a scalar comparison for int/float
35724+ # subjects, and `0` (never matches) for non-numeric subjects. This
35725+ # replaces the old generic `tmp == <struct literal>`, which was an
35726+ # invalid `mrb_int == sp_Rational`/`sp_Complex` in C.
35727+ if @nd_type[cid] == "RationalNode"
35728+ num_wc = @nd_rat_num[cid]
35729+ den_wc = @nd_rat_den[cid]
35730+ if pred_type == "int"
35731+ result = result + "(" + den_wc + "LL == 1 && " + tmp + " == " + num_wc + "LL)"
35732+ elsif pred_type == "float"
35733+ result = result + "(" + tmp + " == (mrb_float)" + num_wc + "LL / (mrb_float)" + den_wc + "LL)"
35734+ else
35735+ result = result + "0"
35736+ end
35737+ k = k + 1
35738+ next
35739+ end
35740+ if @nd_type[cid] == "ImaginaryNode"
35741+ # A purely-imaginary literal (re=0, im=N) === a real subject only
35742+ # when N==0 and the subject==0.
35743+ im_zero_wc = imaginary_im_is_zero(cid)
35744+ if (pred_type == "int" || pred_type == "float") && im_zero_wc == 1
35745+ result = result + "(" + tmp + " == 0)"
35746+ else
35747+ result = result + "0"
35748+ end
35749+ k = k + 1
35750+ next
35751+ end
3568635752 if @nd_type[cid] == "RangeNode"
3568735753 left = compile_expr(@nd_left[cid])
3568835754 right = compile_expr(@nd_right[cid])