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

@@ -17475,6 +17475,64 @@
1747517475 bk = bk + 1
1747617476 next
1747717477 end
17478+ # Issue #395 string-yield sub-variant: when calling
17479+ # a yield-bearing method on a user class
17480+ # (`c.each { |k| ... }`), infer the block param's
17481+ # type from the method's yield-arg type. Without
17482+ # this, `k` defaults to mrb_int at the parent
17483+ # scope, but the yield expansion assigns the
17484+ # method's `n = @keys[i]` (a `const char *`) to
17485+ # it -- mismatch.
17486+ if is_obj_type(recv_type) == 1
17487+ cn_each = recv_type[4, recv_type.length - 4]
17488+ cci_each = find_class_idx(cn_each)
17489+ if cci_each >= 0
17490+ midx_each = cls_find_method(cci_each, mname)
17491+ if midx_each >= 0 && cls_method_has_yield(cci_each, midx_each) == 1
17492+ owner_each = find_method_owner(cci_each, mname)
17493+ owner_ci_each = find_class_idx(owner_each)
17494+ if owner_ci_each >= 0
17495+ owner_midx_each = cls_find_method(owner_ci_each, mname)
17496+ arity_each = cls_method_yield_arity(owner_ci_each, owner_midx_each)
17497+ ytypes_each = "".split(",")
17498+ ka = 0
17499+ while ka < arity_each
17500+ ytypes_each.push("")
17501+ ka = ka + 1
17502+ end
17503+ ybid_each = cls_method_body_id(owner_ci_each, owner_midx_each)
17504+ # Push a temporary scope with the yielding
17505+ # method's locals declared so body_yield_arg_types'
17506+ # infer_type calls resolve LocalVariableReadNodes
17507+ # to their actual types -- without this, a yield
17508+ # arg like `n` (a `const char *` local in the
17509+ # method) would just read as `int` (the no-scope
17510+ # default) and we'd miss the body-driven type.
17511+ if ybid_each >= 0
17512+ push_scope
17513+ yl_names = "".split(",")
17514+ yl_types = "".split(",")
17515+ saved_ci_each = @current_class_idx
17516+ @current_class_idx = owner_ci_each
17517+ scan_locals(ybid_each, yl_names, yl_types, "".split(","))
17518+ yj = 0
17519+ while yj < yl_names.length
17520+ declare_var(yl_names[yj], yl_types[yj])
17521+ yj = yj + 1
17522+ end
17523+ body_yield_arg_types(ybid_each, ytypes_each)
17524+ @current_class_idx = saved_ci_each
17525+ pop_scope
17526+ end
17527+ if bk < ytypes_each.length && ytypes_each[bk] != "" && ytypes_each[bk] != "int"
17528+ types.push(ytypes_each[bk])
17529+ bk = bk + 1
17530+ next
17531+ end
17532+ end
17533+ end
17534+ end
17535+ end
1747817536 if mname == "times" || mname == "upto" || mname == "downto"
1747917537 types.push("int")
1748017538 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"