Commit fc67cce3
fc67cce30cd0Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Fix block-param scope collision for in-place array methods
analyze.c:
- infer_block_params: guard against widening an array-typed variable to a scalar type when the same name is used as a block param for a typed array iteration. infer_write_types runs first in each fixpoint iteration, so the array type is already set before infer_block_params runs; the guard keeps it there instead of unifying to TY_POLY.
- map!/collect! return type: for typed arrays (int/str/float), preserve the receiver type instead of re-deriving from block body. The block param is shared across multiple blocks and may widen to TY_POLY, which would produce a poly-array return even for typed in-place mutations.
- inject(:op) type inference: a single SymbolNode argument is the operator, not an init value; skip the "return argv[0] type" path in that case so inject(:+) returns the element type rather than TY_SYMBOL.
codegen.c:
- Poly-array select!/filter!/keep_if/reject!/delete_if: use emit_cond instead of sp_poly_truthy for the condition expression so bool/int conditions (is_a?, even?, comparison operators) are emitted without a type mismatch.
- Poly-dispatch size/length/count: add a SP_TAG_SYM branch alongside the existing SP_TAG_STR branch so Symbol#size returns the correct string length.
Fixes: array_map_bang, array_select_bang, inject_symbol_op (pre-existing bug)
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- fc67cce30cd0
- Parent
- aba5e824bdd8
src/analyze.cmodifiedView file
@@ -593,10 +593,16 @@
593593
return ty_array_elem(rt);594594
}595595
if (!strcmp(name, "inject") || !strcmp(name, "reduce")) {596-
/* When an init argument is provided, the return type matches the init type. */596+
/* When an init argument is provided, the return type matches the init type.597+
inject(:op) is the no-init operator form — the sole symbol arg is the598+
operator, NOT an init value, so skip the "return argv[0] type" path. */597599
if (argc > 0 && argv) {598-
TyKind it = infer_type(c, argv[0]);599-
if (it != TY_UNKNOWN) return it;600+
const char *a0ty = nt_type(nt, argv[0]);601+
int is_sym_op = a0ty && !strcmp(a0ty, "SymbolNode") && argc == 1;602+
if (!is_sym_op) {603+
TyKind it = infer_type(c, argv[0]);604+
if (it != TY_UNKNOWN) return it;605+
}600606
}601607
/* Block body last expression determines the return type when available. */602608
int blk = nt_ref(nt, id, "block");@@ -625,6 +631,11 @@
625631
if ((!strcmp(name, "bsearch") || !strcmp(name, "find") || !strcmp(name, "detect")) && block >= 0)626632
return ty_array_elem(rt); /* element or nil */627633
if ((!strcmp(name, "map!") || !strcmp(name, "collect!")) && block >= 0) {634+
/* Typed arrays (int/str/float): in-place mutation preserves element type.635+
The block param may be widened to TY_POLY when shared with other blocks,636+
but the array type is determined by the receiver, not the block body. */637+
if (ty_array_elem(rt) != TY_POLY)638+
return rt;628639
int body = nt_ref(nt, block, "body");629640
int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;630641
TyKind bt = bn > 0 ? infer_type(c, bb[bn - 1]) : TY_UNKNOWN;@@ -898,6 +909,8 @@
898909
!strcmp(name, "round") || !strcmp(name, "truncate")) return TY_INT; /* no precision arg -> self */899910
if (!strcmp(name, "divmod") && argc == 1) return TY_INT_ARRAY; /* [quotient, remainder] */900911
if ((!strcmp(name, "allbits?") || !strcmp(name, "anybits?") || !strcmp(name, "nobits?")) && argc == 1) return TY_BOOL;912+
if (!strcmp(name, "even?") || !strcmp(name, "odd?") || !strcmp(name, "zero?") ||913+
!strcmp(name, "positive?") || !strcmp(name, "negative?")) return TY_BOOL;901914
if ((!strcmp(name, "ceildiv") || !strcmp(name, "pow")) && argc >= 1) return TY_INT;902915
if ((!strcmp(name, "pred") || !strcmp(name, "succ") || !strcmp(name, "next")) && argc == 0) return TY_INT;903916
if (!strcmp(name, "nonzero?") && argc == 0) return TY_INT; /* self or nil (nullable int) */@@ -2642,6 +2655,13 @@
26422655
if (pt == TY_UNKNOWN) continue;26432656
Scope *s = comp_scope_of(c, block);26442657
LocalVar *lv = scope_local_intern(s, p0); lv->is_block_param = 1;2658+
/* Don't widen an array-typed variable to a scalar via block-param2659+
inference. When the variable already holds an array (set by a write2660+
site in the same iteration, before infer_block_params runs), widening2661+
it to the element scalar type collapses the outer array type to TY_POLY.2662+
Codegen emits a scoped shadow for the block param instead. */2663+
if (ty_is_array(lv->type) && !ty_is_array(pt))2664+
continue;26452665
TyKind merged = ty_unify(lv->type, pt);26462666
if (merged != lv->type) { lv->type = merged; changed = 1; }26472667
}src/codegen.cmodifiedView file
Loading diff…