Commit 705afa1f

705afa1fb749Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(codegen,runtime): poly-recv dispatch for Range#include? + sym-hash with poly key

Two related poly-receiver dispatch fixes from Sam Ruby's roundhouse controller-test pipeline (issues #588 and #590).

#588 -- case x; when Range; r.include?(x): the poly-include? dispatch table enumerated IntArray / IntStrHash / Sym* / Str* / Flt* / PolyArray cls_ids but missed SP_BUILTIN_RANGE. A Range narrowed via case/when fell through to the default 0 (always-false). Adds:

  • sp_range_include(sp_Range *r, mrb_int x) runtime helper (lib/sp_runtime.h) matching the inclusive form compile_range_method_expr already inlines for direct-typed range receivers.
  • An int-arg arm in emit_poly_builtin_dispatch's include? block that emits sp_range_include when the cls_id is SP_BUILTIN_RANGE.

#590 -- is_a?(Symbol)-narrowed local feeding a sym-keyed hash lookup: H[expected] against a sp_RbVal expected emitted sp_SymPolyHash_get(h, lv_expected) where the parameter expects sp_sym (long int), failing C compile with an incompatible-type error. Adds:

  • compile_arg0_as_sym: when the arg's static type is "poly", extract .v.i so the call matches sp_sym. SymbolNode literals and "symbol"-typed args pass through unchanged.
  • sym_int_hash / sym_str_hash / sym_poly_hash arms route their [] / has_key? / key? / include? / member? lookups through the new helper. The prior "concrete non-symbol key -> short-circuit to default" fast-path still fires for int / string args (correctly says "miss"), but poly args fall through to the real lookup (the common is_a?(Symbol)-narrowed shape).

Side fix while looking at the same runtime: sp_poly_inspect was returning the bare #<Object> placeholder for every SP_TAG_OBJ tag. It now dispatches built-in container cls_ids (Range, Time, the typed Arrays) the same way sp_poly_to_s already did, so a Range stored as a poly hash value reports 200..299 instead of #<Object> from .inspect.

make test: 565/565 pass. make optcarrot: checksum 59662 (no regression). Stage-2 bootstrap fixpoint clean.

Closes #588 Closes #590

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

Details

Commit
705afa1fb749
Parent
4fc5db6f5a25

lib/sp_runtime.hmodifiedView file

@@ -132,6 +132,14 @@
132132 }
133133 typedef struct{mrb_int first;mrb_int last;}sp_Range;
134134 static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}
135+/* Inclusive-form `Range#include?`/`#cover?` on the boxed (SP_TAG_OBJ
136+ cls_id SP_BUILTIN_RANGE) Range value. The direct sp_Range typed
137+ path inlines this same check via compile_range_method_expr;
138+ poly-recv dispatch needs the wrapper so the cls_id arm in
139+ emit_poly_builtin_dispatch can land on a single C expression. The
140+ sp_Range struct doesn't track exclude_end, so behaviour matches
141+ the inclusive form -- consistent with the direct-typed emit. */
142+static mrb_bool sp_range_include(sp_Range *r, mrb_int x){return r->first<=x && x<=r->last;}
135143
136144 /* ---- Class object ----
137145 Value-type Class reference: a single class id that indexes into
@@ -1639,7 +1647,24 @@
16391647 case SP_TAG_BOOL: return v.v.b ? SPL("true") : SPL("false");
16401648 case SP_TAG_NIL: return SPL("nil");
16411649 case SP_TAG_SYM: return sp_str_concat(SPL(":"), sp_sym_to_s((sp_sym)v.v.i));
1642- case SP_TAG_OBJ: return SPL("#<Object>");
1650+ case SP_TAG_OBJ:
1651+ /* Built-in container / value-type tags get their typed inspect
1652+ helper. Matches the dispatch shape in sp_poly_to_s above and the
1653+ `puts` poly arm earlier in this file; without it, a Range / Time
1654+ / typed Array stored as an sp_RbVal value (e.g. a sym_poly_hash
1655+ that mixes `200..299` and `404`) reported "#<Object>" from
1656+ `.inspect`, which was both wrong for CRuby parity and useless
1657+ for debugging. */
1658+ switch (v.cls_id) {
1659+ case SP_BUILTIN_INT_ARRAY: return sp_IntArray_inspect((sp_IntArray *)v.v.p);
1660+ case SP_BUILTIN_FLT_ARRAY: return sp_FloatArray_inspect((sp_FloatArray *)v.v.p);
1661+ case SP_BUILTIN_STR_ARRAY: return sp_StrArray_inspect((sp_StrArray *)v.v.p);
1662+ case SP_BUILTIN_SYM_ARRAY: return sp_SymArray_inspect((sp_IntArray *)v.v.p);
1663+ case SP_BUILTIN_PTR_ARRAY: return sp_PtrArray_inspect((sp_PtrArray *)v.v.p);
1664+ case SP_BUILTIN_RANGE: return sp_Range_inspect((sp_Range *)v.v.p);
1665+ case SP_BUILTIN_TIME: return sp_Time_inspect((sp_Time *)v.v.p);
1666+ default: return SPL("#<Object>");
1667+ }
16431668 default: return sp_str_empty;
16441669 }
16451670 }