Commit 8f3f669f

8f3f669fa3faMerged

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

Description

Fix 5 codegen/analyze bugs: poly dispatch, super-inline, fetch type

  • Unbox poly-typed array index in sp_XxxArray_get calls; when a method param is widened to sp_RbVal for poly dispatch, array subscript @arr[i] passed lv_i directly to sp_IntArray_get which expects mrb_int (bundle_classd_28)

  • Inline super { block } when parent method uses yield: add emit_super_inline that expands the parent body at the super call site with the passed block active, mirroring emit_inline_call_x (bundle_classd_48)

  • Fix poly dispatch arity padding + truncation: zero-arg dispatch now fills defaults for methods with nrequired==0 but nparams>0; multi-arg dispatch now accepts any method where argc>=nrequired, emitting defaults for missing optional params (bundle_classd_18)

  • Fix hash#fetch(key){block} when block return type differs from hash value type: detect mismatch, box both ternary arms to sp_RbVal; also fix infer_type to return TY_POLY for fetch+block when types differ so local variable declarations use the right C type (hash_fetch_block_key_type)

Refs #665, #655

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

Details

Commit
8f3f669fa3fa
Parent
820329620540

src/analyze.cmodifiedView file

@@ -732,7 +732,17 @@
732732 if (recv >= 0 && ty_is_hash(rt)) {
733733 if (!strcmp(name, "[]")) return ty_hash_val(rt);
734734 if (!strcmp(name, "[]=")) return ty_hash_val(rt);
735- if (!strcmp(name, "fetch")) return ty_hash_val(rt);
735+ if (!strcmp(name, "fetch")) {
736+ TyKind vt = ty_hash_val(rt);
737+ int blk = nt_ref(nt, id, "block");
738+ if (blk >= 0) {
739+ int bbody = nt_ref(nt, blk, "body");
740+ int bn = 0; const int *bb = bbody >= 0 ? nt_arr(nt, bbody, "body", &bn) : NULL;
741+ TyKind bvt = bn > 0 ? infer_type(c, bb[bn - 1]) : vt;
742+ if (bvt != vt) return TY_POLY;
743+ }
744+ return vt;
745+ }
736746 if (!strcmp(name, "delete")) return ty_hash_val(rt);
737747 if (!strcmp(name, "length") || !strcmp(name, "size") ||
738748 !strcmp(name, "count")) return TY_INT;