Commit 3ef0d029
3ef0d029067cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Hash#each_value / each_key with a block
Bind the single block param to the hash value (each_value) or key (each_key) type in analyze, and emit a key-order loop in codegen. When the param name is shared across hashes of differing element types it resolves to poly, so a concrete element is boxed into the poly slot before assignment.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 3ef0d029067c
- Parent
- fbd9c10a1f13
src/analyze.cmodifiedView file
@@ -2046,6 +2046,16 @@
20462046
continue;20472047
}20482048
2049+
/* hash.each_value { |v| } binds value; each_key { |k| } binds key */2050+
if ((!strcmp(name, "each_value") || !strcmp(name, "each_key")) && ty_is_hash(rt)) {2051+
Scope *hs = comp_scope_of(c, block);2052+
LocalVar *vp = scope_local_intern(hs, p0); vp->is_block_param = 1;2053+
TyKind want = !strcmp(name, "each_value") ? ty_hash_val(rt) : ty_hash_key(rt);2054+
TyKind vm = ty_unify(vp->type, want);2055+
if (vm != vp->type) { vp->type = vm; changed = 1; }2056+
continue;2057+
}2058+
20492059
/* hash.each / each_pair { |k, v| } binds two params */20502060
if ((!strcmp(name, "each") || !strcmp(name, "each_pair") || !strcmp(name, "map") ||20512061
!strcmp(name, "collect") || !strcmp(name, "flat_map") || !strcmp(name, "select") ||src/codegen.cmodifiedView file
Loading diff…