Commit f4be7c7d

f4be7c7d52beMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): infer block fn-ptr signature from yield arg types

The block fn-pointer parameter (void (*_block)(...) on every yield-using method) had its C type list emitted via block_params_csig, which always produced mrb_int, mrb_int, ... regardless of what the method actually yielded. A method that yielded (String, sp_RbVal) (e.g. HWIA's each, walking str_poly_hash entries) then mismatched its own if (_block) _block(lv_k, lv_v, _benv) invocation site — gcc errored incompatible type for argument 2 of '_block' where lv_v was sp_RbVal but the slot expected mrb_int.

Fix: replace block_params_csig(arity) with block_params_csig_for_body(bid, arity) in both yield_params_suffix paths. The new helper walks the body for YieldNodes (ignoring nested DefNode boundaries), runs scan_locals first so locally-typed reads in yield args (yield k, v where k/v were just assigned from typed expressions) resolve, and unifies the per-position type via infer_type. The c_type for each unified slot replaces the all-mrb_int default; positions that don't infer cleanly fall back to mrb_int (preserves existing behavior).

Companion fix: pin @current_class_idx during the forward-decl emit pass. Without it, @ivar reads inside yield arg expressions resolved to int default at decl-emit time but correctly at impl-emit time, producing a decl/definition signature mismatch (decl (mrb_int, mrb_int) vs impl (mrb_int, sp_RbVal)).

After the fix, all 4 Roundhouse archive tests (article/comment/articles_controller/comments_controller) emit C-error-clean — first time the archive has reached the link phase since rubys/roundhouse@a17f18a updated the test shape on 2026-05-06. matz/spinel#314 follow-up.

Refs #314

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
f4be7c7d52be
Parent
d958d3114ef1

spinel_codegen.rbmodifiedView file

@@ -16685,6 +16685,16 @@
1668516685 i = 0
1668616686 while i < @cls_names.length
1668716687 cname = @cls_names[i]
16688+ # Issue #314 follow-up: pin @current_class_idx during this
16689+ # forward-decl emit pass so block_params_csig_for_body's
16690+ # infer_type calls (on `@ivar` reads inside yield arg
16691+ # expressions) resolve against this class's ivar table.
16692+ # Without this, ivars infer to int default and yields like
16693+ # `yield k, @data[k]` lower to `(mrb_int, mrb_int)` while
16694+ # the impl emit (which DOES set @current_class_idx) lowers
16695+ # to `(string, sp_RbVal)` — declaration/definition mismatch.
16696+ saved_ci_decl = @current_class_idx
16697+ @current_class_idx = i
1668816698 # Constructor
1668916699 init_idx = cls_find_method_direct(i, "initialize")
1669016700 if @cls_is_value_type[i] == 1
@@ -16743,6 +16753,7 @@
1674316753 j = j + 1
1674416754 end
1674516755 end
16756+ @current_class_idx = saved_ci_decl
1674616757 i = i + 1
1674716758 end
1674816759 # Hoisted instance_eval block functions: emit prototypes here. v1
@@ -16766,7 +16777,8 @@
1676616777 end
1676716778
1676816779 def yield_params_suffix(mi)
16769- csig = block_params_csig(method_yield_arity(mi))
16780+ bid = mi >= 0 && mi < @meth_body_ids.length ? @meth_body_ids[mi] : -1
16781+ csig = block_params_csig_for_body(bid, method_yield_arity(mi))
1677016782 pd = method_params_decl(mi)
1677116783 if pd == ""
1677216784 return "void (*_block)(" + csig + ", void*), void *_benv"
@@ -16776,10 +16788,107 @@
1677616788
1677716789 def yield_params_suffix_cls(ci, midx)
1677816790 # For class instance methods (always have self first)
16779- csig = block_params_csig(cls_method_yield_arity(ci, midx))
16791+ bid = -1
16792+ if ci >= 0 && ci < @cls_meth_bodies.length
16793+ bodies = @cls_meth_bodies[ci].split(";")
16794+ if midx >= 0 && midx < bodies.length
16795+ bid = bodies[midx].to_i
16796+ end
16797+ end
16798+ csig = block_params_csig_for_body(bid, cls_method_yield_arity(ci, midx))
1678016799 return ", void (*_block)(" + csig + ", void*), void *_benv"
1678116800 end
1678216801
16802+ # Issue #314 follow-up: build the block fn-pointer C signature
16803+ # from the yield call sites' inferred arg types. Without this,
16804+ # block_params_csig emitted `mrb_int, mrb_int, ...` regardless,
16805+ # so a method that yields (String, sp_RbVal) had the function-
16806+ # pointer declared `void(*)(mrb_int, mrb_int, void*)` while the
16807+ # actual yield invocation site emitted `_block(const_char*,
16808+ # sp_RbVal, _benv)` — gcc errored with `incompatible type for
16809+ # argument`.
16810+ #
16811+ # Called BEFORE the function's scope has been pushed, so
16812+ # locally-declared vars referenced in yield args (`yield k, v`
16813+ # where `k`, `v` were assigned from typed expressions) are not
16814+ # in @scope_names. Run scan_locals on the body and push a
16815+ # temporary scope so body_yield_arg_types' infer_type calls
16816+ # resolve those reads. (Re-runs both at forward-decl emit time
16817+ # and at impl emit time; results converge once @meth_*/@cls_*
16818+ # tables stabilize through the type-inference fixpoint.)
16819+ def block_params_csig_for_body(bid, arity)
16820+ if bid < 0 || arity <= 0
16821+ return block_params_csig(arity)
16822+ end
16823+ types = "".split(",")
16824+ k = 0
16825+ while k < arity
16826+ types.push("")
16827+ k = k + 1
16828+ end
16829+ push_scope
16830+ lnames = "".split(",")
16831+ ltypes = "".split(",")
16832+ scan_locals(bid, lnames, ltypes, "".split(","))
16833+ j = 0
16834+ while j < lnames.length
16835+ declare_var(lnames[j], ltypes[j])
16836+ j = j + 1
16837+ end
16838+ body_yield_arg_types(bid, types)
16839+ pop_scope
16840+ parts = "".split(",")
16841+ k = 0
16842+ while k < arity
16843+ t = types[k]
16844+ if t == "" || t == "int"
16845+ parts.push("mrb_int")
16846+ else
16847+ parts.push(c_type(t))
16848+ end
16849+ k = k + 1
16850+ end
16851+ parts.join(", ")
16852+ end
16853+
16854+ # Walk `nid` for YieldNode, accumulating the per-position arg
16855+ # type into `types`. Stops at nested DefNode boundaries (those
16856+ # introduce a new method scope with its own yield arity).
16857+ # Mirrors body_max_yield_arity's traversal shape.
16858+ def body_yield_arg_types(nid, types)
16859+ if nid < 0
16860+ return
16861+ end
16862+ if @nd_type[nid] == "YieldNode"
16863+ if @nd_arguments[nid] >= 0
16864+ args = get_args(@nd_arguments[nid])
16865+ k = 0
16866+ while k < args.length
16867+ if k < types.length
16868+ at = infer_type(args[k])
16869+ if types[k] == ""
16870+ types[k] = at
16871+ elsif types[k] != at
16872+ types[k] = unify_call_types(types[k], at, args[k])
16873+ end
16874+ end
16875+ k = k + 1
16876+ end
16877+ end
16878+ return
16879+ end
16880+ if @nd_type[nid] == "DefNode"
16881+ return
16882+ end
16883+ cs = []
16884+ push_child_ids(nid, cs)
16885+ k = 0
16886+ while k < cs.length
16887+ body_yield_arg_types(cs[k], types)
16888+ k = k + 1
16889+ end
16890+ end
16891+
1678316892 def cls_method_has_yield(ci, midx)
1678416893 ystr = @cls_meth_has_yield[ci].split(";")
1678516894 if midx < ystr.length