Commit 342013d4
342013d41088Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Hash.new { |hash, key| default } default-proc blocks
Lower Hash.new with a block to a StrPolyHash carrying a default-proc: emit a dedicated _sp_hash_dproc_N(self, key) function computing the missing-key value (handling the value-producing hash[k]=v final form), and create the hash via sp_StrPolyHash_new_dproc. Symbol keys on a string-keyed hash coerce to the symbol name (literal :sym -> its string, symbol values via sp_sym_to_s) in get/set/index paths.
Fixes hash_new_default_block_assign and i912.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 342013d41088
- Parent
- f0364b82271f
src/analyze.cmodifiedView file
@@ -792,6 +792,9 @@
792792
if (cn && !strcmp(cn, "String")) return TY_STRING;793793
if (cn && !strcmp(cn, "StringIO")) return TY_STRINGIO;794794
if (cn && !strcmp(cn, "StringScanner")) return TY_STRINGSCANNER;795+
/* Hash.new { |hash, key| default } : a string-keyed poly hash with a796+
default-proc (the block computes the missing-key value). */797+
if (cn && !strcmp(cn, "Hash") && nt_ref(nt, id, "block") >= 0) return TY_STR_POLY_HASH;795798
if (cn && !strcmp(cn, "Hash")) return TY_UNKNOWN; /* hash type determined by key usage */796799
if (cn && !strcmp(cn, "Regexp")) return TY_REGEX;797800
/* Builtin object types */@@ -5652,6 +5655,33 @@
56525655
}56535656
}56545657
5658+
/* Hash.new { |hash, key| } : hash is the StrPolyHash, key the string key. */5659+
for (int id = 0; id < nt->count; id++) {5660+
const char *ty = nt_type(nt, id);5661+
if (!ty || strcmp(ty, "CallNode")) continue;5662+
const char *cname = nt_str(nt, id, "name");5663+
if (!cname || strcmp(cname, "new")) continue;5664+
int recv = nt_ref(nt, id, "receiver");5665+
if (recv < 0 || !nt_type(nt, recv) || strcmp(nt_type(nt, recv), "ConstantReadNode")) continue;5666+
const char *rn = nt_str(nt, recv, "name");5667+
if (!rn || strcmp(rn, "Hash")) continue;5668+
int blk = nt_ref(nt, id, "block");5669+
if (blk < 0) continue;5670+
int pn = nt_ref(nt, blk, "parameters");5671+
if (pn < 0) continue;5672+
int inner = nt_ref(nt, pn, "parameters");5673+
int pnode = inner >= 0 ? inner : pn;5674+
int rnp = 0; const int *reqs = nt_arr(nt, pnode, "requireds", &rnp);5675+
Scope *bs = comp_scope_of(c, blk);5676+
for (int k = 0; k < rnp; k++) {5677+
const char *p = nt_str(nt, reqs[k], "name");5678+
if (!p) continue;5679+
TyKind want = (k == 0) ? TY_STR_POLY_HASH : TY_STRING;5680+
LocalVar *lv = scope_local_intern(bs, p); lv->is_block_param = 1;5681+
if (lv->type != want) { lv->type = want; changed = 1; }5682+
}5683+
}5684+
56555685
/* recv.instance_eval { |me| } : the block params all receive the receiver56565686
(Ruby yields self), typed as the receiver's object type. */56575687
for (int id = 0; id < nt->count; id++) {src/codegen.cmodifiedView file
Loading diff…