Commit ac4e9f89
ac4e9f893e39Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(runtime,codegen): Array#compact on poly_array (#725)
[1, nil, 2].compact on a poly_array used to emit an "unresolved
call" warning and segfault: the codegen poly_array recv arm had
no compact branch, so the call's C expression stayed at the
fallback 0 (NULL pointer) which the caller then dereferenced as a
poly_array pointer.
Fix:
- lib/sp_runtime.h: sp_PolyArray_compact walks the source array and pushes only elements whose tag is not SP_TAG_NIL.
- spinel_codegen.rb: poly_array recv arm in compile_call routes
compactto sp_PolyArray_compact. analyze.rb's compact arm already returns recv-type so infer_type stays correct.
Test: test/poly_array_compact.rb covers the issue example plus a heterogeneous-element case and a no-nil passthrough.
463 tests pass; optcarrot checksum 59662.
Closes #725
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- ac4e9f893e39
- Parent
- 05f4040b531c
lib/sp_runtime.hmodifiedView file
@@ -1976,6 +1976,8 @@
19761976
return r;19771977
}19781978
static sp_PolyArray *sp_PolyArray_dup(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_push(b, a->data[i]); return b; }1979+
/* Array#compact for poly_array: keep elements whose tag is not SP_TAG_NIL. */1980+
static sp_PolyArray *sp_PolyArray_compact(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); if (!a) return b; for (mrb_int i = 0; i < a->len; i++) { if (a->data[i].tag != SP_TAG_NIL) sp_PolyArray_push(b, a->data[i]); } return b; }19791981
/* Sum the integer-tagged elements of a poly_array. Used by19801982
`Array#sum` on a poly_array whose runtime tags are uniform int19811983
(e.g. the result of `arr.map { _1[:int_key] }`). Non-int tagsspinel_codegen.rbmodifiedView file
Loading diff…
test/poly_array_compact.rbaddedView file
Loading diff…
test/poly_array_compact.rb.expectedaddedView file
Loading diff…