Commit 9e17ebea
9e17ebea7103Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): infer block-param type from user-class yield-method body (#395)
Closes #395 (string-yield sub-variant).
Previous commit (ca613cb) covered the int-yielding variant of `c.each { |k| ... }` -- self / locals / unless inside the inlined body. The string-yielding variant still failed: the block param `k` got declared `mrb_int` at the parent scope (`scan_locals`'s obj-recv arm fell through to the int default), but the yield expansion assigned a `const char *` (the yielding method's local read of an str_array element).
Fix: in `scan_locals`'s block-param-typing branch, when the receiver is `obj_`, look up the user class's yield method, push a temp scope with the method's locals declared, set `@current_class_idx` so ivar reads resolve through `cls_ivar_type`, then run `body_yield_arg_types` over the method body. The per-position yield-arg type pushes onto the block-param `types` array.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 9e17ebea7103
- Parent
- 80fa8bdebba5
spinel_analyze.rbmodifiedView file
bk = bk + 1 next end # Issue #395 string-yield sub-variant: when calling # a yield-bearing method on a user class # (`c.each { |k| ... }`), infer the block param's # type from the method's yield-arg type. Without # this, `k` defaults to mrb_int at the parent # scope, but the yield expansion assigns the # method's `n = @keys[i]` (a `const char *`) to # it -- mismatch. if is_obj_type(recv_type) == 1 cn_each = recv_type[4, recv_type.length - 4] cci_each = find_class_idx(cn_each) if cci_each >= 0 midx_each = cls_find_method(cci_each, mname) if midx_each >= 0 && cls_method_has_yield(cci_each, midx_each) == 1 owner_each = find_method_owner(cci_each, mname) owner_ci_each = find_class_idx(owner_each) if owner_ci_each >= 0 owner_midx_each = cls_find_method(owner_ci_each, mname) arity_each = cls_method_yield_arity(owner_ci_each, owner_midx_each) ytypes_each = "".split(",") ka = 0 while ka < arity_each ytypes_each.push("") ka = ka + 1 end ybid_each = cls_method_body_id(owner_ci_each, owner_midx_each) # Push a temporary scope with the yielding # method's locals declared so body_yield_arg_types' # infer_type calls resolve LocalVariableReadNodes # to their actual types -- without this, a yield # arg like `n` (a `const char *` local in the # method) would just read as `int` (the no-scope # default) and we'd miss the body-driven type. if ybid_each >= 0 push_scope yl_names = "".split(",") yl_types = "".split(",") saved_ci_each = @current_class_idx @current_class_idx = owner_ci_each scan_locals(ybid_each, yl_names, yl_types, "".split(",")) yj = 0 while yj < yl_names.length declare_var(yl_names[yj], yl_types[yj]) yj = yj + 1 end body_yield_arg_types(ybid_each, ytypes_each) @current_class_idx = saved_ci_each pop_scope end if bk < ytypes_each.length && ytypes_each[bk] != "" && ytypes_each[bk] != "int" types.push(ytypes_each[bk]) bk = bk + 1 next end end end end end if mname == "times" || mname == "upto" || mname == "downto" types.push("int") elsif mname == "each" || mname == "each_pair" || mname == "map" || mname == "select" || mname == "filter" || mname == "reject" || mname == "find" || mname == "detect" || mname == "any?" || mname == "all?" || mname == "none?" || mname == "one?" || mname == "count" || mname == "min" || mname == "max" || mname == "sum" || mname == "min_by" || mname == "max_by" || mname == "sort_by" || mname == "flat_map" || mname == "filter_map" || mname == "cycle" || mname == "partition"