Commit bcc60b69

bcc60b692718Merged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): Class#=== for primitive type names (case-when)

Integer === 5, String === "hi", etc — the operator behind case x; when Integer; ... — was hitting compile_operator_expr's generic <recv> == <arg> emission with Integer treated as a plain C identifier. The resulting C didn't compile (undeclared identifier Integer).

Adds an early arm in compile_call_expr that fires when:

  • mname is "===", AND
  • receiver is a ConstantReadNode / ConstantPathNode

It resolves the constant name and matches it against Ruby's primitive-class taxonomy (Integer / Numeric / Comparable group; Float; String; Symbol; Array; Hash; Range; TrueClass / FalseClass; NilClass; Object / Kernel / BasicObject). The arg's inferred type drives the answer at compile time — no runtime branch.

Falls through unchanged when the receiver is a user class, so Foo === bar still routes through the regular operator path.

Test: class_threeq.rb covers each primitive type with both a true-side case (matching arg) and a false-side case (mismatched arg type) for the major shapes.

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

Details

Commit
bcc60b692718
Parent
8ff9aa915a30

spinel_codegen.rbmodifiedView file

@@ -3060,6 +3060,9 @@
30603060 if mname == "cover?"
30613061 return "bool"
30623062 end
3063+ if mname == "==="
3064+ return "bool"
3065+ end
30633066 if mname == "match?"
30643067 return "bool"
30653068 end
@@ -20329,6 +20332,93 @@
2032920332 end
2033020333 end
2033120334
20335+ # `Class === expr` — case-when membership for primitive type names.
20336+ # Resolve at compile time based on the arg's inferred type. Has to
20337+ # run before compile_operator_expr / compile_eq (both of which would
20338+ # mishandle the constant receiver as a regular C identifier and emit
20339+ # `<undeclared identifier> == arg`).
20340+ if mname == "===" && recv >= 0 && (@nd_type[recv] == "ConstantReadNode" || @nd_type[recv] == "ConstantPathNode")
20341+ rcname_threeq = resolve_const_ref_name(recv)
20342+ args_id_threeq = @nd_arguments[nid]
20343+ if args_id_threeq >= 0
20344+ a_threeq = get_args(args_id_threeq)
20345+ if a_threeq.length > 0
20346+ at_threeq = infer_type(a_threeq[0])
20347+ if rcname_threeq == "Integer"
20348+ return at_threeq == "int" ? "TRUE" : "FALSE"
20349+ end
20350+ if rcname_threeq == "Float"
20351+ return at_threeq == "float" ? "TRUE" : "FALSE"
20352+ end
20353+ if rcname_threeq == "Numeric"
20354+ return (at_threeq == "int" || at_threeq == "float") ? "TRUE" : "FALSE"
20355+ end
20356+ if rcname_threeq == "Comparable"
20357+ # Comparable is included by Integer, Float, String, Symbol.
20358+ return (at_threeq == "int" || at_threeq == "float" ||
20359+ at_threeq == "string" || at_threeq == "mutable_str" ||
20360+ at_threeq == "symbol") ? "TRUE" : "FALSE"
20361+ end
20362+ if rcname_threeq == "String"
20363+ return at_threeq == "string" || at_threeq == "mutable_str" ? "TRUE" : "FALSE"
20364+ end
20365+ if rcname_threeq == "Symbol"
20366+ return at_threeq == "symbol" ? "TRUE" : "FALSE"
20367+ end
20368+ if rcname_threeq == "Array"
20369+ return is_array_type(at_threeq) == 1 ? "TRUE" : "FALSE"
20370+ end
20371+ if rcname_threeq == "Hash"
20372+ # Hash type names all end in `_hash` (sym_int_hash, str_str_hash,
20373+ # sym_poly_hash, etc.); poly_hash also matches the runtime form.
20374+ is_hash = 0
20375+ if at_threeq == "poly_hash"
20376+ is_hash = 1
20377+ elsif at_threeq.length > 5 && at_threeq[at_threeq.length - 5, 5] == "_hash"
20378+ is_hash = 1
20379+ end
20380+ return is_hash == 1 ? "TRUE" : "FALSE"
20381+ end
20382+ if rcname_threeq == "Range"
20383+ return at_threeq == "range" ? "TRUE" : "FALSE"
20384+ end
20385+ if rcname_threeq == "TrueClass"
20386+ # TrueClass === arg matches only the literal `true` value.
20387+ # Compile-time decide for TrueNode/FalseNode literals; emit a
20388+ # runtime check for other bool-typed values; FALSE for non-bool.
20389+ if @nd_type[a_threeq[0]] == "TrueNode"
20390+ return "TRUE"
20391+ end
20392+ if @nd_type[a_threeq[0]] == "FalseNode"
20393+ return "FALSE"
20394+ end
20395+ if at_threeq == "bool"
20396+ return "((" + compile_expr(a_threeq[0]) + ") == TRUE)"
20397+ end
20398+ return "FALSE"
20399+ end
20400+ if rcname_threeq == "FalseClass"
20401+ if @nd_type[a_threeq[0]] == "FalseNode"
20402+ return "TRUE"
20403+ end
20404+ if @nd_type[a_threeq[0]] == "TrueNode"
20405+ return "FALSE"
20406+ end
20407+ if at_threeq == "bool"
20408+ return "((" + compile_expr(a_threeq[0]) + ") == FALSE)"
20409+ end
20410+ return "FALSE"
20411+ end
20412+ if rcname_threeq == "NilClass"
20413+ return at_threeq == "nil" ? "TRUE" : "FALSE"
20414+ end
20415+ if rcname_threeq == "Object" || rcname_threeq == "Kernel" || rcname_threeq == "BasicObject"
20416+ return "TRUE"
20417+ end
20418+ end
20419+ end
20420+ end
20421+
2033220422 # Operator on object with custom method
2033320423 if is_operator_name(mname) == 1
2033420424 r = compile_obj_operator_expr(nid, mname, recv)