Commit 383080dd

383080dd80a1Merged

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

Description

fix(codegen): Range#=== as cover (case-when membership)

(1..10) === 5 (Range#===) is the implicit operator behind case x when 1..10, which Ruby documents as testing for "membership" and implementing as cover? for numeric ranges.

Spinel routed === through the generic operator dispatch and emitted range_struct == arg, which is meaningless. Adds two arms to the existing range method handler:

  • mname == "===" joins the include?/cover? branch in compile_range_method_expr → reuses the same first<=x<=last emission.
  • infer_call_type adds === to the bool list so result use sites type-check.

Test coverage extends the existing test/range.rb with a Range#=== section (5 cases: low/mid/high inside, plus boundary fails on each side).

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

Details

Commit
383080dd80a1
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
@@ -22438,7 +22441,9 @@
2243822441 # so they share the same emission. Exclude_end isn't tracked in the
2243922442 # runtime sp_Range struct; non-literal receivers fall back to the
2244022443 # inclusive form (matches length/size).
22441- if mname == "include?" || mname == "cover?"
22444+ if mname == "include?" || mname == "cover?" || mname == "==="
22445+ # Range#=== is the case-when membership operator and behaves like
22446+ # cover? for numeric ranges (Ruby docs: "tests for membership").
2244222447 tmp = new_temp
2244322448 emit(" sp_Range " + tmp + " = " + rc + ";")
2244422449 arg = compile_arg0(nid)