Commit 410a62ab

410a62ab30fdMerged

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

Description

Fix fetch(key, {}) on typed hash: box both ternary arms to sp_RbVal

When Hash#fetch(key, default) has an empty hash literal default {} and the hash has a non-hash value type (e.g. StrIntHash -> int), the two ternary arms have incompatible C types.

  • analyze: treat empty HashNode default as TY_POLY_POLY_HASH when checking arm compatibility, return TY_POLY for the fetch expression
  • codegen: apply same detection to drive the needs_box path, which boxes the hash-get arm via emit_boxed_text and the default via emit_boxed

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
410a62ab30fd
Parent
1adfa330a411

src/analyze.cmodifiedView file

@@ -1018,6 +1018,17 @@
10181018 if (!strcmp(name, "[]=")) return argc >= 2 ? ty_unify(infer_type(c, argv[1]), ty_hash_val(rt)) : ty_hash_val(rt);
10191019 if (!strcmp(name, "fetch")) {
10201020 TyKind vt = ty_hash_val(rt);
1021+ if (argc == 2) {
1022+ TyKind dt = infer_type(c, argv[1]);
1023+ /* A hash literal default `{}` infers TY_UNKNOWN but is still a hash value
1024+ — incompatible with a non-hash hash-val type like TY_INT. */
1025+ if (dt == TY_UNKNOWN) {
1026+ const char *atn = nt_type(nt, argv[1]);
1027+ if (atn && (!strcmp(atn, "HashNode") || !strcmp(atn, "KeywordHashNode")))
1028+ dt = TY_POLY_POLY_HASH;
1029+ }
1030+ if (ty_unify(vt, dt) == TY_POLY) return TY_POLY;
1031+ }
10211032 int blk = nt_ref(nt, id, "block");
10221033 if (blk >= 0) {
10231034 int bbody = nt_ref(nt, blk, "body");