Commit fe8cf4d5
fe8cf4d54652Merged
Ori Pekelman <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
fix(codegen): Array#delete_at on user-class ptr_array
Pre-fix the codegen had delete_at arms for IntArray and
StrArray but not for user-class PtrArray. A user-class
array could grow via push but never shrink, and the
while-drain pattern (while arr.length > 0; arr.delete_at(0); end)
hung the program because:
-
The
delete_atcall hit the unresolved-call warning and emitted 0 (thearr.delete_at(0)line was effectively a no-op). -
Even if it did the right thing,
body_mutates_var?-- the helper that decides whetherarr.lengthcan hoist outside awhile-- knew aboutpush/pop/delete/clearetc. but notdelete_at, solengthgot hoisted and the loop spun forever.
Four small pieces:
-
Runtime helper (lib/sp_runtime.h):
sp_PtrArray_delete_at(a, i)peer to the existing IntArray / StrArray versions. Returns the popped pointer or NULL on out-of-range. Negative indices count from end. -
Codegen dispatch: ptr_array arm in
compile_poly_method_callgains thedelete_atcase, mirroring the existingpush/[]/first/lastarms. Result cast to the element class sov = arr.delete_at(0)lowers cleanly. -
Return-type inference (
infer_method_name_type):delete_atalready coveredstr_array-> string,float_array-> float; addis_ptr_array_type(rt)->ptr_array_elem_type(rt). Without this the assignment target defaults to int and trips anincompatible pointer to integer conversionC-compile error. (Post-#416 split: this branch lives in spinel_analyze.rb, not spinel_codegen.rb.) -
Loop-invariant analysis (
body_mutates_var?):delete_atjoinspush/pop/shift/ etc. in the mutating-method set sowhile arr.length > 0no longer hoistslengthpast the body'sdelete_at.
test/ptr_array_delete_at.rb covers push / delete_at round trip, return-the-popped-instance, middle-index delete with shift, negative index, out-of-range, and the drain loop that surfaced the bug while building Tep::Scheduler's fiber-slot list.
Closes #410
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- fe8cf4d54652
- Parent
- 97a359a737f4
lib/sp_runtime.hmodifiedView file
static inline void sp_PtrArray_set(sp_PtrArray*a,mrb_int i,void*v){if(i<0)i+=a->len;a->data[i]=v;}static inline mrb_int sp_PtrArray_length(sp_PtrArray*a){return a->len;}static inline mrb_bool sp_PtrArray_empty(sp_PtrArray*a){return a->len==0;}/* `Array#delete_at(i)` -- remove and return the element at index i. Negative indices count from the end. Returns NULL when i is out of range (matches CRuby's nil for typed-element arrays). Mirrors sp_IntArray_delete_at / sp_StrArray_delete_at; without this peer user-class arrays can't shrink. */static void*sp_PtrArray_delete_at(sp_PtrArray*a,mrb_int i){if(i<0)i+=a->len;if(i<0||i>=a->len)return NULL;void*v=a->data[i];for(mrb_int j=i;j<a->len-1;j++)a->data[j]=a->data[j+1];a->len--;return v;}static void sp_PtrArray_reverse_bang(sp_PtrArray*a){for(mrb_int i=0,j=a->len-1;i<j;i++,j--){void*t=a->data[i];a->data[i]=a->data[j];a->data[j]=t;}}static void sp_PtrArray_rotate_bang(sp_PtrArray*a,mrb_int n){if(a->len<=0)return;n=((n%a->len)+a->len)%a->len;if(n==0)return;void**tmp=(void**)malloc(sizeof(void*)*a->len);for(mrb_int i=0;i<a->len;i++)tmp[i]=a->data[(i+n)%a->len];for(mrb_int i=0;i<a->len;i++)a->data[i]=tmp[i];free(tmp);}static sp_PtrArray*sp_PtrArray_dup(sp_PtrArray*a){sp_PtrArray*b=sp_PtrArray_new_scan(a->scan_elem);for(mrb_int i=0;i<a->len;i++)sp_PtrArray_push(b,a->data[i]);return b;}spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/ptr_array_delete_at.rbaddedView file
Loading diff…
test/ptr_array_delete_at.rb.expectedaddedView file
Loading diff…