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 formcompile_range_method_expralready inlines for direct-typed range receivers.- An
int-arg arm inemit_poly_builtin_dispatch'sinclude?block that emitssp_range_includewhen 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.iso the call matchessp_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
}typedef struct{mrb_int first;mrb_int last;}sp_Range;static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}/* Inclusive-form `Range#include?`/`#cover?` on the boxed (SP_TAG_OBJ cls_id SP_BUILTIN_RANGE) Range value. The direct sp_Range typed path inlines this same check via compile_range_method_expr; poly-recv dispatch needs the wrapper so the cls_id arm in emit_poly_builtin_dispatch can land on a single C expression. The sp_Range struct doesn't track exclude_end, so behaviour matches the inclusive form -- consistent with the direct-typed emit. */static mrb_bool sp_range_include(sp_Range *r, mrb_int x){return r->first<=x && x<=r->last;}/* ---- Class object ---- Value-type Class reference: a single class id that indexes into case SP_TAG_BOOL: return v.v.b ? SPL("true") : SPL("false"); case SP_TAG_NIL: return SPL("nil"); case SP_TAG_SYM: return sp_str_concat(SPL(":"), sp_sym_to_s((sp_sym)v.v.i)); case SP_TAG_OBJ: return SPL("#<Object>"); case SP_TAG_OBJ: /* Built-in container / value-type tags get their typed inspect helper. Matches the dispatch shape in sp_poly_to_s above and the `puts` poly arm earlier in this file; without it, a Range / Time / typed Array stored as an sp_RbVal value (e.g. a sym_poly_hash that mixes `200..299` and `404`) reported "#<Object>" from `.inspect`, which was both wrong for CRuby parity and useless for debugging. */ switch (v.cls_id) { case SP_BUILTIN_INT_ARRAY: return sp_IntArray_inspect((sp_IntArray *)v.v.p); case SP_BUILTIN_FLT_ARRAY: return sp_FloatArray_inspect((sp_FloatArray *)v.v.p); case SP_BUILTIN_STR_ARRAY: return sp_StrArray_inspect((sp_StrArray *)v.v.p); case SP_BUILTIN_SYM_ARRAY: return sp_SymArray_inspect((sp_IntArray *)v.v.p); case SP_BUILTIN_PTR_ARRAY: return sp_PtrArray_inspect((sp_PtrArray *)v.v.p); case SP_BUILTIN_RANGE: return sp_Range_inspect((sp_Range *)v.v.p); case SP_BUILTIN_TIME: return sp_Time_inspect((sp_Time *)v.v.p); default: return SPL("#<Object>"); } default: return sp_str_empty; }}spinel_codegen.rbmodifiedView file
Loading diff…
test/poly_recv_range_sym_hash_dispatch.rbaddedView file
Loading diff…
test/poly_recv_range_sym_hash_dispatch.rb.expectedaddedView file
Loading diff…