Commit 1ee517c8
1ee517c8458bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Multi-assign splat destructure + Float#divmod guards
MultiWriteNode: handle rest (splat) target and rights (post-splat
fixed targets) in both analyze.c and codegen.c. Fixes a, *b = arr,
*xs, last = arr, and first, *mid, fin = arr patterns. Handles both
ArrayNode literal RHS and call/super/yield returning a typed array.
Also handles InstanceVariableTargetNode targets in the typed-array
runtime path.
Float#divmod: add NaN and zero-division guards to avoid C UB when the divisor is 0 or NaN, or the dividend is NaN.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 1ee517c8458b
- Parent
- 78ffd9dd726e
src/analyze.cmodifiedView file
@@ -822,7 +822,7 @@
822822
}823823
if (recv >= 0 && ty_is_hash(rt)) {824824
if (!strcmp(name, "[]")) return ty_hash_val(rt);825-
if (!strcmp(name, "[]=")) return ty_hash_val(rt);825+
if (!strcmp(name, "[]=")) return argc >= 2 ? infer_type(c, argv[1]) : ty_hash_val(rt);826826
if (!strcmp(name, "fetch")) {827827
TyKind vt = ty_hash_val(rt);828828
int blk = nt_ref(nt, id, "block");@@ -1959,6 +1959,68 @@
19591959
}19601960
}19611961
}1962+
/* call/super/yield returning a typed array: assign element types to targets */1963+
if (multi_src && value >= 0) {1964+
TyKind st = infer_type(c, value);1965+
if (ty_is_array(st)) {1966+
TyKind elem = ty_array_elem(st);1967+
int rn2 = 0;1968+
const int *rights2 = nt_arr(nt, id, "rights", &rn2);1969+
Scope *ms_arr = comp_scope_of(c, id);1970+
for (int i = 0; i < ln; i++) {1971+
const char *lty_ms = nt_type(nt, lefts[i]) ? nt_type(nt, lefts[i]) : "";1972+
if (!strcmp(lty_ms, "LocalVariableTargetNode")) {1973+
const char *lnm = nt_str(nt, lefts[i], "name");1974+
LocalVar *lv = lnm ? scope_local(ms_arr, lnm) : NULL;1975+
if (!lv || lv->is_param || lv->is_block_param) continue;1976+
lv->type = ty_unify(lv->type, elem);1977+
}1978+
else if (!strcmp(lty_ms, "InstanceVariableTargetNode") &&1979+
ms_arr && ms_arr->class_id >= 0) {1980+
const char *ivnm = nt_str(nt, lefts[i], "name");1981+
int iv_ms = ivnm ? comp_ivar_index(&c->classes[ms_arr->class_id], ivnm) : -1;1982+
if (iv_ms < 0) continue;1983+
TyKind mg = ty_unify(c->classes[ms_arr->class_id].ivar_types[iv_ms], elem);1984+
if (mg != c->classes[ms_arr->class_id].ivar_types[iv_ms]) {1985+
c->classes[ms_arr->class_id].ivar_types[iv_ms] = mg; changed = 1;1986+
}1987+
}1988+
}1989+
for (int j = 0; j < rn2; j++) {1990+
const char *lty_ms = nt_type(nt, rights2[j]) ? nt_type(nt, rights2[j]) : "";1991+
if (!strcmp(lty_ms, "LocalVariableTargetNode")) {1992+
const char *rnm2 = nt_str(nt, rights2[j], "name");1993+
LocalVar *lv = rnm2 ? scope_local(ms_arr, rnm2) : NULL;1994+
if (!lv || lv->is_param || lv->is_block_param) continue;1995+
lv->type = ty_unify(lv->type, elem);1996+
}1997+
else if (!strcmp(lty_ms, "InstanceVariableTargetNode") &&1998+
ms_arr && ms_arr->class_id >= 0) {1999+
const char *ivnm2 = nt_str(nt, rights2[j], "name");2000+
int iv_ms2 = ivnm2 ? comp_ivar_index(&c->classes[ms_arr->class_id], ivnm2) : -1;2001+
if (iv_ms2 < 0) continue;2002+
TyKind mg2 = ty_unify(c->classes[ms_arr->class_id].ivar_types[iv_ms2], elem);2003+
if (mg2 != c->classes[ms_arr->class_id].ivar_types[iv_ms2]) {2004+
c->classes[ms_arr->class_id].ivar_types[iv_ms2] = mg2; changed = 1;2005+
}2006+
}2007+
}2008+
int rest_nid2 = nt_ref(nt, id, "rest");2009+
if (rest_nid2 >= 0) {2010+
const char *rsty2 = nt_type(nt, rest_nid2);2011+
int inner2 = -1;2012+
if (rsty2 && !strcmp(rsty2, "SplatNode"))2013+
inner2 = nt_ref(nt, rest_nid2, "expression");2014+
if (inner2 >= 0 && nt_type(nt, inner2) &&2015+
!strcmp(nt_type(nt, inner2), "LocalVariableTargetNode")) {2016+
const char *rnm3 = nt_str(nt, inner2, "name");2017+
LocalVar *lv3 = rnm3 ? scope_local(comp_scope_of(c, id), rnm3) : NULL;2018+
if (lv3 && !lv3->is_param && !lv3->is_block_param)2019+
lv3->type = ty_unify(lv3->type, st);2020+
}2021+
}2022+
}2023+
}19622024
continue;19632025
}19642026
int en = 0;@@ -1994,6 +2056,42 @@
19942056
}19952057
}19962058
}2059+
/* rights targets (post-splat fixed targets) */2060+
int rn = 0;2061+
const int *rights = nt_arr(nt, id, "rights", &rn);2062+
for (int j = 0; j < rn; j++) {2063+
int ridx = en - rn + j;2064+
if (ridx < 0 || ridx >= en) continue;2065+
const char *rty3 = nt_type(nt, rights[j]);2066+
if (!rty3 || strcmp(rty3, "LocalVariableTargetNode")) continue;2067+
const char *rnm2 = nt_str(nt, rights[j], "name");2068+
TyKind et = infer_type(c, els[ridx]);2069+
if (et == TY_NIL) continue;2070+
LocalVar *lv = rnm2 ? scope_local(comp_scope_of(c, id), rnm2) : NULL;2071+
if (!lv || lv->is_param || lv->is_block_param) continue;2072+
lv->type = ty_unify(lv->type, et);2073+
}2074+
/* rest (splat) target: elements [ln, en-rn) become a typed array */2075+
int rest_nid = nt_ref(nt, id, "rest");2076+
if (rest_nid >= 0) {2077+
const char *rsty = nt_type(nt, rest_nid);2078+
int inner = -1;2079+
if (rsty && !strcmp(rsty, "SplatNode"))2080+
inner = nt_ref(nt, rest_nid, "expression");2081+
if (inner >= 0 && nt_type(nt, inner) &&2082+
!strcmp(nt_type(nt, inner), "LocalVariableTargetNode")) {2083+
const char *rnm = nt_str(nt, inner, "name");2084+
int rstart = ln, rend = en - rn;2085+
if (rend < rstart) rend = rstart;2086+
TyKind rest_elem = TY_UNKNOWN;2087+
for (int i = rstart; i < rend; i++)2088+
rest_elem = ty_unify(rest_elem, infer_type(c, els[i]));2089+
TyKind rest_arr = (rest_elem != TY_UNKNOWN) ? ty_array_of(rest_elem) : TY_INT_ARRAY;2090+
LocalVar *lv = rnm ? scope_local(comp_scope_of(c, id), rnm) : NULL;2091+
if (lv && !lv->is_param && !lv->is_block_param)2092+
lv->type = ty_unify(lv->type, rest_arr);2093+
}2094+
}19972095
}19982096
19992097
/* Fold container usage into the local type so an empty `[]` / `{}` getssrc/codegen.cmodifiedView file
Loading diff…