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
i = 0 while i < @cls_names.length cname = @cls_names[i] # Issue #314 follow-up: pin @current_class_idx during this # forward-decl emit pass so block_params_csig_for_body's # infer_type calls (on `@ivar` reads inside yield arg # expressions) resolve against this class's ivar table. # Without this, ivars infer to int default and yields like # `yield k, @data[k]` lower to `(mrb_int, mrb_int)` while # the impl emit (which DOES set @current_class_idx) lowers # to `(string, sp_RbVal)` — declaration/definition mismatch. saved_ci_decl = @current_class_idx @current_class_idx = i # Constructor init_idx = cls_find_method_direct(i, "initialize") if @cls_is_value_type[i] == 1 j = j + 1 end end @current_class_idx = saved_ci_decl i = i + 1 end # Hoisted instance_eval block functions: emit prototypes here. v1 end def yield_params_suffix(mi) csig = block_params_csig(method_yield_arity(mi)) bid = mi >= 0 && mi < @meth_body_ids.length ? @meth_body_ids[mi] : -1 csig = block_params_csig_for_body(bid, method_yield_arity(mi)) pd = method_params_decl(mi) if pd == "" return "void (*_block)(" + csig + ", void*), void *_benv" def yield_params_suffix_cls(ci, midx) # For class instance methods (always have self first) csig = block_params_csig(cls_method_yield_arity(ci, midx)) bid = -1 if ci >= 0 && ci < @cls_meth_bodies.length bodies = @cls_meth_bodies[ci].split(";") if midx >= 0 && midx < bodies.length bid = bodies[midx].to_i end end csig = block_params_csig_for_body(bid, cls_method_yield_arity(ci, midx)) return ", void (*_block)(" + csig + ", void*), void *_benv" end # Issue #314 follow-up: build the block fn-pointer C signature # from the yield call sites' inferred arg types. Without this, # block_params_csig emitted `mrb_int, mrb_int, ...` regardless, # so a method that yields (String, sp_RbVal) had the function- # pointer declared `void(*)(mrb_int, mrb_int, void*)` while the # actual yield invocation site emitted `_block(const_char*, # sp_RbVal, _benv)` — gcc errored with `incompatible type for # argument`. # # Called BEFORE the function's scope has been pushed, so # locally-declared vars referenced in yield args (`yield k, v` # where `k`, `v` were assigned from typed expressions) are not # in @scope_names. Run scan_locals on the body and push a # temporary scope so body_yield_arg_types' infer_type calls # resolve those reads. (Re-runs both at forward-decl emit time # and at impl emit time; results converge once @meth_*/@cls_* # tables stabilize through the type-inference fixpoint.) def block_params_csig_for_body(bid, arity) if bid < 0 || arity <= 0 return block_params_csig(arity) end types = "".split(",") k = 0 while k < arity types.push("") k = k + 1 end push_scope lnames = "".split(",") ltypes = "".split(",") scan_locals(bid, lnames, ltypes, "".split(",")) j = 0 while j < lnames.length declare_var(lnames[j], ltypes[j]) j = j + 1 end body_yield_arg_types(bid, types) pop_scope parts = "".split(",") k = 0 while k < arity t = types[k] if t == "" || t == "int" parts.push("mrb_int") else parts.push(c_type(t)) end k = k + 1 end parts.join(", ") end # Walk `nid` for YieldNode, accumulating the per-position arg # type into `types`. Stops at nested DefNode boundaries (those # introduce a new method scope with its own yield arity). # Mirrors body_max_yield_arity's traversal shape. def body_yield_arg_types(nid, types) if nid < 0 return end if @nd_type[nid] == "YieldNode" if @nd_arguments[nid] >= 0 args = get_args(@nd_arguments[nid]) k = 0 while k < args.length if k < types.length at = infer_type(args[k]) if types[k] == "" types[k] = at elsif types[k] != at types[k] = unify_call_types(types[k], at, args[k]) end end k = k + 1 end end return end if @nd_type[nid] == "DefNode" return end cs = [] push_child_ids(nid, cs) k = 0 while k < cs.length body_yield_arg_types(cs[k], types) k = k + 1 end end def cls_method_has_yield(ci, midx) ystr = @cls_meth_has_yield[ci].split(";") if midx < ystr.length