Commit 6469de04
6469de046d4cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Handle global variable splat-rest targets in multi-assignment
In $x, *$rest = 1, 2, 3, 4:
- analyze: infer the type of the splat-rest global ($rest) as an int/str/poly array from the rest elements of the literal ArrayNode RHS.
- codegen: extract a
rest_gvarname when the SplatNode in MultiWriteNode rest field wraps a GlobalVariableTargetNode; emit the array construction and global assignment analogous to the local-variable rest path.
Fixes test/global_multi_assign.rb (was ERR).
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 6469de046d4c
- Parent
- ffbb01170c74
src/analyze.cmodifiedView file
@@ -1851,6 +1851,8 @@
18511851
const char *vty = nt_type(nt, value);18521852
int en = 0;18531853
const int *els = (vty && !strcmp(vty, "ArrayNode")) ? nt_arr(nt, value, "elements", &en) : NULL;1854+
int rn_count = 0;1855+
nt_arr(nt, id, "rights", &rn_count);18541856
for (int i = 0; i < ln; i++) {18551857
const char *lty2 = nt_type(nt, lefts[i]);18561858
if (!lty2 || strcmp(lty2, "GlobalVariableTargetNode")) continue;@@ -1863,6 +1865,28 @@
18631865
TyKind merged2 = ty_unify(glv->type, vt2);18641866
if (merged2 != glv->type) { glv->type = merged2; changed = 1; }18651867
}1868+
/* handle splat-rest global target (*$rest = ...) */1869+
int rest_nid2 = nt_ref(nt, id, "rest");1870+
if (rest_nid2 >= 0) {1871+
const char *rsty2 = nt_type(nt, rest_nid2);1872+
int rest_inner2 = (rsty2 && !strcmp(rsty2, "SplatNode")) ? nt_ref(nt, rest_nid2, "expression") : -1;1873+
const char *rinty2 = rest_inner2 >= 0 ? nt_type(nt, rest_inner2) : NULL;1874+
if (rinty2 && !strcmp(rinty2, "GlobalVariableTargetNode")) {1875+
const char *gnm2 = nt_str(nt, rest_inner2, "name");1876+
const char *rn3 = gnm2 ? comp_resolve_gvar(c, gnm2 + 1) : NULL;1877+
LocalVar *glv2 = rn3 ? comp_gvar(c, rn3) : NULL;1878+
if (glv2 && els) {1879+
TyKind rest_elem = TY_UNKNOWN;1880+
for (int i = ln; i < en - rn_count; i++)1881+
rest_elem = ty_unify(rest_elem, infer_type(c, els[i]));1882+
TyKind rest_arr_t = (rest_elem != TY_UNKNOWN) ? ty_array_of(rest_elem) : TY_UNKNOWN;1883+
if (rest_arr_t != TY_UNKNOWN) {1884+
TyKind merged3 = ty_unify(glv2->type, rest_arr_t);1885+
if (merged3 != glv2->type) { glv2->type = merged3; changed = 1; }1886+
}1887+
}1888+
}1889+
}18661890
continue;18671891
}18681892
else {src/codegen.cmodifiedView file
Loading diff…