Commit 3ef414b9
3ef414b9109fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
analyze: fix array-param type inference when [] read causes premature hash promotion
Three related fixes for the case where a local variable x = [] is
passed to a method param buf, the body uses buf.push(v), but
x[i] (read) appears in the caller -- the body-usage pass promotes
x to TY_POLY_POLY_HASH before the push can narrow it to an array.
-
infer_array_params: fire even when the param already has a hash type (push wins over [] read promotion). -
Backstop for empty-array locals: also reset hash-typed locals to TY_POLY_ARRAY when their only write is an empty array literal.
-
Post-fixpoint mini-loop: include
infer_write_typesso locals whose types derive from function return types get updated after param promotion.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 3ef414b9109f
- Parent
- d426578de4e9
src/analyze.cmodifiedView file
if (!rty || strcmp(rty, "LocalVariableReadNode")) continue; Scope *s = comp_scope_of(c, id); LocalVar *lv = scope_local(s, nt_str(nt, recv, "name")); if (lv && lv->is_param && lv->type == TY_UNKNOWN) { lv->type = TY_POLY_ARRAY; changed = 1; } /* If caller-side [] read already widened this param to a hash type, push wins: a param that receives push() is an array, not a hash. */ if (lv && lv->is_param && (lv->type == TY_UNKNOWN || ty_is_hash(lv->type))) { lv->type = TY_POLY_ARRAY; changed = 1; } } return changed;} const char *nm = nt_str(c->nt, id, "name"); Scope *s = comp_scope_of(c, id); LocalVar *lv = nm ? scope_local(s, nm) : NULL; if (lv && lv->type == TY_UNKNOWN) lv->type = TY_POLY_ARRAY; /* Also reset any hash type that crept in via premature [] read promotion: a variable whose only write is an empty array literal is definitively an array, not a hash. */ if (lv && (lv->type == TY_UNKNOWN || ty_is_hash(lv->type))) lv->type = TY_POLY_ARRAY; } /* A read-only ivar (referenced but never assigned a typed value) stays TY_UNKNOWN -> it has no C type. Such a slot always reads nil at runtime; int ch = 0; ch |= infer_param_types(c); ch |= infer_return_types(c); /* Re-run write-type inference so locals whose types derive from function return types (e.g. `x = f([])` after `f`'s param was promoted from UNKNOWN to POLY_ARRAY) get updated. */ ch |= infer_write_types(c); if (!ch) break; } }