Commit ddc868e2

ddc868e21a25Merged

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

Description

Track exclude_end in the runtime sp_Range so non-literal ... ranges work

The sp_Range value type held only {first, last}, so once a Range was stored in a variable its exclude_end flag was lost: r = 1...5; r.max returned 5, r.include?(5) returned true, r.to_a ran to 5, and each / map / size / count / step all over-ran by one. The codegen could only honour ... when the receiver was a literal RangeNode it could inspect at compile time.

Worse, max and the numeric include? were wrong even for literals: the last || end || max arm returned .last and shadowed the exclude_end-aware max handler below it (dead code), and the include? bound was an unconditional <= last.

Add an excl field (0 or 1) to sp_Range, populate it at the single construction site from range_excl_end, and have every consumer compute the inclusive upper bound as last - excl:

  • sp_range_include / sp_Range_inspect (runtime)
  • max, include?/cover?/===, overlap?, to_a, length/size/count, step
  • the .each / .map iteration loops and emit_iter_open

last/end still return the raw endpoint ((1...5).last == 5), only max subtracts the exclusion. Literal and non-literal receivers now go through the same runtime flag, so both forms agree with CRuby.

Closes #1086

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

Details

Commit
ddc868e21a25
Parent
0bc1d675ac21

lib/sp_runtime.hmodifiedView file

@@ -221,15 +221,15 @@
221221 /* String -> number parsers now live in libspinel_rt.a (lib/sp_core.c). */
222222 #include "sp_core.h"
223223
224-static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}
225-/* Inclusive-form `Range#include?`/`#cover?` on the boxed (SP_TAG_OBJ
226- cls_id SP_BUILTIN_RANGE) Range value. The direct sp_Range typed
227- path inlines this same check via compile_range_method_expr;
228- poly-recv dispatch needs the wrapper so the cls_id arm in
229- emit_poly_builtin_dispatch can land on a single C expression. The
230- sp_Range struct doesn't track exclude_end, so behaviour matches
231- the inclusive form -- consistent with the direct-typed emit. */
232-static mrb_bool sp_range_include(sp_Range *r, mrb_int x){return r->first<=x && x<=r->last;}
224+static sp_Range sp_range_new(mrb_int f,mrb_int l,mrb_int e){sp_Range r;r.first=f;r.last=l;r.excl=e;return r;}
225+/* `Range#include?`/`#cover?` on the boxed (SP_TAG_OBJ cls_id
226+ SP_BUILTIN_RANGE) Range value. The direct sp_Range typed path
227+ inlines this same check via compile_range_method_expr; poly-recv
228+ dispatch needs the wrapper so the cls_id arm in
229+ emit_poly_builtin_dispatch can land on a single C expression. An
230+ exclusive range stops one short of `last`, so the upper bound is
231+ `last - excl` (excl is 0 or 1). */
232+static mrb_bool sp_range_include(sp_Range *r, mrb_int x){return r->first<=x && x<=r->last-r->excl;}
233233
234234 /* ---- Class object ----
235235 Value-type Class reference: a single class id that indexes into
@@ -2376,9 +2376,10 @@
23762376 return sp_box_obj(p, SP_BUILTIN_RANGE);
23772377 }
23782378 static const char *sp_Range_inspect(sp_Range *r) {
2379- /* "first..last" form. Buffer sized for two int64s plus the dots. */
2379+ /* "first..last" / "first...last" form. Buffer sized for two int64s
2380+ plus the dots. */
23802381 char *buf = sp_str_alloc_raw(48);
2381- snprintf(buf, 48, "%lld..%lld", (long long)r->first, (long long)r->last);
2382+ snprintf(buf, 48, r->excl ? "%lld...%lld" : "%lld..%lld", (long long)r->first, (long long)r->last);
23822383 return buf;
23832384 }
23842385 /* Same heap-box rationale as sp_Range: sp_Time is 12+ bytes (tv_sec +