Commit 531a0a1f
531a0a1fcd2dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
fix(runtime,codegen,analyze): Range#step(k) returns int_array (#731)
(1..10).step(3) used to fall through to the unresolved-call
warning ("cannot resolve call to 'step' on range") and then
.to_a on the resulting int 0 segfaulted.
Three pieces:
- lib/sp_runtime.h: sp_IntArray_from_range_step(s, e, k) walks s, s+k, s+2k, ... up to e. k <= 0 returns an empty array (CRuby's ArgumentError equivalent, soft-failed).
- spinel_codegen.rb: range recv arm in compile_range_method_expr
routes
step(no-block) to sp_IntArray_from_range_step, with the same exclude-end handling to_a uses. - spinel_analyze.rb: infer_type returns int_array for
range.stepso.to_aand chained array methods get the right return type.
Test: test/range_step.rb covers inclusive/exclusive ends, step larger than the range, and step that exactly hits the end.
473 tests pass; optcarrot checksum 59662.
Closes #731
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 531a0a1fcd2d
- Parent
- ed5a888bac65
lib/sp_runtime.hmodifiedView file
@@ -889,6 +889,12 @@
889889
static void sp_IntArray_fin(void*p){free(((sp_IntArray*)p)->data);}890890
static sp_IntArray*sp_IntArray_new(void){sp_IntArray*a=(sp_IntArray*)sp_gc_alloc(sizeof(sp_IntArray),sp_IntArray_fin,NULL);a->cap=16;a->data=(mrb_int*)malloc(sizeof(mrb_int)*a->cap);a->start=0;a->len=0;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}return a;}891891
static sp_IntArray*sp_IntArray_from_range(mrb_int s,mrb_int e){sp_IntArray*a=sp_IntArray_new();mrb_int n=e-s+1;if(n<0)n=0;if(n>a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_int)*a->cap;h->size-=sizeof(mrb_int)*a->cap;a->cap=n;a->data=(mrb_int*)realloc(a->data,sizeof(mrb_int)*a->cap);h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}for(mrb_int i=0;i<n;i++)a->data[i]=s+i;a->len=n;return a;}892+
/* `(a..b).step(k)` -- emit a, a+k, a+2k, ... up to b. k must be > 0;893+
k <= 0 returns empty (matches CRuby's ArgumentError for k <= 0 in894+
spirit; we soft-fail instead). Forward-declared since895+
sp_IntArray_push is defined below. Issue #731. */896+
static void sp_IntArray_push(sp_IntArray*a,mrb_int v);897+
static sp_IntArray*sp_IntArray_from_range_step(mrb_int s,mrb_int e,mrb_int k){sp_IntArray*a=sp_IntArray_new();if(k<=0)return a;mrb_int v=s;while(v<=e){sp_IntArray_push(a,v);v+=k;}return a;}892898
static sp_IntArray*sp_IntArray_dup(sp_IntArray*a){sp_IntArray*b=sp_IntArray_new();if(a->len>b->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)b-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_int)*b->cap;h->size-=sizeof(mrb_int)*b->cap;b->cap=a->len;b->data=(mrb_int*)realloc(b->data,sizeof(mrb_int)*b->cap);h->size+=sizeof(mrb_int)*b->cap;sp_gc_bytes+=sizeof(mrb_int)*b->cap;}memcpy(b->data,a->data+a->start,sizeof(mrb_int)*a->len);b->len=a->len;return b;}893899
/* a[start, len] / a[start..end] for IntArray. Negative start counts from894900
* the end. start past the array length yields an empty result; len isspinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/range_step.rbaddedView file
Loading diff…
test/range_step.rb.expectedaddedView file
Loading diff…