Commit baf27ece
baf27ece4e13Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +1 added · ~2 modified
Description
Hash#to_h with no block is the identity
h.to_h on a hash receiver was rejected ('unsupported call: to_h'); the
to_h handlers only covered array-of-pairs receivers. A hash's own to_h
returns self, so type it as the receiver hash and emit the receiver directly.
Fixes #1403 Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- baf27ece4e13
- Parent
- f9754211e765
src/analyze_infer.cmodifiedView file
@@ -1754,6 +1754,7 @@
17541754
if (recv >= 0 && ty_is_hash(rt)) {17551755
if (!strcmp(name, "to_proc")) return TY_PROC;17561756
if (!strcmp(name, "key") && argc == 1 && rt == TY_SYM_POLY_HASH) return TY_SYMBOL;1757+
if (!strcmp(name, "to_h") && argc == 0 && nt_ref(nt, id, "block") < 0) return rt; /* identity */17571758
if (!strcmp(name, "[]")) return ty_hash_val(rt);17581759
if (!strcmp(name, "[]=")) return argc >= 2 ? ty_unify(infer_type(c, argv[1]), ty_hash_val(rt)) : ty_hash_val(rt);17591760
if (!strcmp(name, "fetch")) {src/codegen_call.cmodifiedView file
Loading diff…
test/hash_to_h_identity.rbaddedView file
Loading diff…