Commit 7894b685

7894b68548bdMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Run-correctness: hash==nil NULL test + empty-{} arg adopts hash param type

Two fixes that take the spinelc-compiled legacy analyze pass from a runtime segfault to running to completion:

  • h == nil / h != nil for a hash/array/proc/io/fiber/exception/regex receiver compiled to the always-false fallback ((h), 0) instead of a NULL test. A NULL pointer encodes nil for these heap types, so a lazy-init idiom @cache = {} if @cache == nil never initialized (the guard was always false) and a later @cache.has_key? dereferenced NULL. Emit (h == 0).

  • An empty-{}-only local passed to a hash parameter is the hash container, filled inside the callee through the reference; bind_call_params now types such a local as the param's hash so it is constructed (sp_Hash_new()) rather than passed as a NULL-deref'ing poly nil (symmetric to the param-hash-value seeding).

Gates: 845 test, 57 bench, optcarrot checksum 59662 -- all green.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
7894b68548bd
Parent
466d417be9fa

src/analyze_pass.cmodifiedView file

@@ -131,6 +131,26 @@
131131 return changed;
132132 }
133133
134+/* 1 if local `name` in scope `sc` has at least one write and every write
135+ assigns an empty `{}` hash literal -- i.e. it is a hash container whose
136+ contents come from elsewhere (passed by reference into a callee). Such a
137+ local can safely adopt a hash type from a parameter it is passed to. */
138+static int local_all_writes_empty_hash(Compiler *c, Scope *sc, const char *name) {
139+ const NodeTable *nt = c->nt;
140+ int saw = 0;
141+ for (int id = 0; id < nt->count; id++) {
142+ if (nt_kind(nt, id) != NK_LocalVariableWriteNode) continue;
143+ const char *wn = nt_str(nt, id, "name");
144+ if (!wn || strcmp(wn, name) || comp_scope_of(c, id) != sc) continue;
145+ int v = nt_ref(nt, id, "value");
146+ if (v < 0 || nt_kind(nt, v) != NK_HashNode) return 0;
147+ int hn = 0; nt_arr(nt, v, "elements", &hn);
148+ if (hn != 0) return 0;
149+ saw = 1;
150+ }
151+ return saw;
152+}
153+
134154 int infer_write_types(Compiler *c) {
135155 const NodeTable *nt = c->nt;
136156 int changed = 0;
@@ -1043,6 +1063,20 @@
10431063 if (at == TY_NIL && p->type != TY_UNKNOWN && p->type != TY_NIL && !ty_is_object(p->type)) at = TY_POLY;
10441064 TyKind merged = ty_unify(p->type, at);
10451065 if (merged != p->type) { p->type = merged; changed = 1; }
1066+ /* Reverse binding: an empty-`{}`-only local passed to a hash parameter is
1067+ that hash container, filled inside the callee through the reference.
1068+ Type the local as the param's hash so it is constructed (sp_<H>Hash_new)
1069+ rather than passed as a NULL-deref'ing poly nil. */
1070+ if (ty_is_hash(p->type) && apty && !strcmp(apty, "LocalVariableReadNode")) {
1071+ const char *an = nt_str(nt, argv[k], "name");
1072+ Scope *asc = an ? comp_scope_of(c, argv[k]) : NULL;
1073+ LocalVar *al = asc ? scope_local(asc, an) : NULL;
1074+ if (al && !al->is_param && !al->is_block_param &&
1075+ (al->type == TY_UNKNOWN || al->type == TY_POLY) &&
1076+ local_all_writes_empty_hash(c, asc, an)) {
1077+ al->type = p->type; changed = 1;
1078+ }
1079+ }
10461080 if (merged == TY_PROC) {
10471081 TyKind pr = proc_ret_of(c, argv[k]);
10481082 if (pr != TY_UNKNOWN && p->proc_ret != (int)pr) { p->proc_ret = (int)pr; changed = 1; }