Commit 69679071
69679071e661Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Add M23: empty {} / Hash.new promotion + string-hash get fix
- analyze: the container-usage fold now promotes a local from an empty
origin using string-keyed
h[k] = v/h[k] op= vto the matching hash variant (int-keyed stays array). Empty{}and Hash.new no longer pin a type. - codegen:
h = {}-> sp_Hash_new();h = Hash.new(d)-> sp_Hash_new_with_default(d), both using the variable's resolved variant. - Fix hash
[]: only int-valued hashes have the nullable get_opt; string-valued hashes use get (get_opt doesn't exist for them, which was emitting a bad int->pointer cast for every StrStr/IntStr [] read).
Empty-hash build, Hash.new(0) counting (h[k]+=1), and string-valued
hashes match CRuby. make test: 29 (up from 26).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 69679071e661
- Parent
- d74c83f8ff38
src/analyze.cmodifiedView file
@@ -738,32 +738,51 @@
738738
}739739
}740740
741-
/* Fold container usage into the local type: `a << x` / `a.push(x)` /742-
`a[i] = x` on a local `a` promotes it to an array of x's type (this is743-
how an empty `[]` gets its element type). Part of the same recompute744-
frame so it survives the reset and the change check stays consistent. */741+
/* Fold container usage into the local type so an empty `[]` / `{}` gets742+
its element / key+value type from how it is filled. `a << x` /743+
`a.push(x)` / `a[i] = x` (int key) -> array; `h[k] = v` / `h[k] op= v`744+
(string key) -> hash. Part of the recompute frame so it survives reset. */745745
for (int id = 0; id < nt->count; id++) {746746
const char *ty = nt_type(nt, id);747-
if (!ty || strcmp(ty, "CallNode")) continue;748-
int recv = nt_ref(nt, id, "receiver");747+
if (!ty) continue;748+
int recv, kt = TY_UNKNOWN, vt = TY_UNKNOWN, is_push = 0;749+
if (!strcmp(ty, "CallNode")) {750+
recv = nt_ref(nt, id, "receiver");751+
const char *name = nt_str(nt, id, "name");752+
int args = nt_ref(nt, id, "arguments");753+
int an = 0;754+
const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;755+
if (name && (!strcmp(name, "push") || !strcmp(name, "<<")) && an == 1) {756+
is_push = 1; vt = infer_type(c, argv[0]);757+
} else if (name && !strcmp(name, "[]=") && an == 2) {758+
kt = infer_type(c, argv[0]); vt = infer_type(c, argv[1]);759+
} else continue;760+
} else if (!strcmp(ty, "IndexOperatorWriteNode")) {761+
recv = nt_ref(nt, id, "receiver");762+
int args = nt_ref(nt, id, "arguments");763+
int an = 0;764+
const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;765+
if (an != 1) continue;766+
kt = infer_type(c, argv[0]); vt = infer_type(c, nt_ref(nt, id, "value"));767+
} else {768+
continue;769+
}749770
if (recv < 0 || strcmp(nt_type(nt, recv) ? nt_type(nt, recv) : "", "LocalVariableReadNode")) continue;750-
const char *name = nt_str(nt, id, "name");751-
if (!name) continue;752-
int args = nt_ref(nt, id, "arguments");753-
int an = 0;754-
const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;755-
TyKind elem = TY_UNKNOWN;756-
if ((!strcmp(name, "push") || !strcmp(name, "<<")) && an == 1) elem = infer_type(c, argv[0]);757-
else if (!strcmp(name, "[]=") && an == 2) elem = infer_type(c, argv[1]);758-
else continue;759-
if (elem == TY_UNKNOWN) continue;760771
const char *rnm = nt_str(nt, recv, "name");761772
LocalVar *lv = rnm ? scope_local(comp_scope_of(c, recv), rnm) : NULL;762773
if (!lv || lv->is_param || lv->is_block_param) continue;763-
/* only promote a container local: a known string uses `<<` for append,764-
not array push -- don't pollute it */765-
if (lv->type != TY_UNKNOWN && !ty_is_array(lv->type)) continue;766-
lv->type = ty_unify(lv->type, ty_array_of(elem));774+
775+
if (is_push || kt == TY_INT) {776+
/* array (a known string's `<<` is append, not push -- don't pollute) */777+
if (vt == TY_UNKNOWN) continue;778+
if (lv->type != TY_UNKNOWN && !ty_is_array(lv->type)) continue;779+
lv->type = ty_unify(lv->type, ty_array_of(vt));780+
} else if (kt == TY_STRING) {781+
TyKind hv = ty_hash_of(TY_STRING, vt);782+
if (hv == TY_UNKNOWN) continue;783+
if (lv->type != TY_UNKNOWN && !ty_is_hash(lv->type)) continue;784+
lv->type = ty_unify(lv->type, hv);785+
}767786
}768787
769788
/* detect change vs the stashed old types */src/codegen.cmodifiedView file
Loading diff…