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:

  1. The delete_at call hit the unresolved-call warning and emitted 0 (the arr.delete_at(0) line was effectively a no-op).

  2. Even if it did the right thing, body_mutates_var? -- the helper that decides whether arr.length can hoist outside a while -- knew about push / pop / delete / clear etc. but not delete_at, so length got 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_call gains the delete_at case, mirroring the existing push / [] / first / last arms. Result cast to the element class so v = arr.delete_at(0) lowers cleanly.

  • Return-type inference (infer_method_name_type): delete_at already covered str_array -> string, float_array -> float; add is_ptr_array_type(rt) -> ptr_array_elem_type(rt). Without this the assignment target defaults to int and trips an incompatible pointer to integer conversion C-compile error. (Post-#416 split: this branch lives in spinel_analyze.rb, not spinel_codegen.rb.)

  • Loop-invariant analysis (body_mutates_var?): delete_at joins push / pop / shift / etc. in the mutating-method set so while arr.length > 0 no longer hoists length past the body's delete_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

@@ -374,6 +374,12 @@
374374 static inline void sp_PtrArray_set(sp_PtrArray*a,mrb_int i,void*v){if(i<0)i+=a->len;a->data[i]=v;}
375375 static inline mrb_int sp_PtrArray_length(sp_PtrArray*a){return a->len;}
376376 static inline mrb_bool sp_PtrArray_empty(sp_PtrArray*a){return a->len==0;}
377+/* `Array#delete_at(i)` -- remove and return the element at index i.
378+ Negative indices count from the end. Returns NULL when i is out
379+ of range (matches CRuby's nil for typed-element arrays). Mirrors
380+ sp_IntArray_delete_at / sp_StrArray_delete_at; without this peer
381+ user-class arrays can't shrink. */
382+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;}
377383 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;}}
378384 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);}
379385 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;}