Commit a3ceb085

a3ceb085529dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~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
a3ceb085529d
Parent
9daca9d7db76

spinel_codegen.rbmodifiedView file

@@ -21196,6 +21196,64 @@
2119621196 bk = bk + 1
2119721197 next
2119821198 end
21199+ # Issue #395 string-yield sub-variant: when calling
21200+ # a yield-bearing method on a user class
21201+ # (`c.each { |k| ... }`), infer the block param's
21202+ # type from the method's yield-arg type. Without
21203+ # this, `k` defaults to mrb_int at the parent
21204+ # scope, but the yield expansion assigns the
21205+ # method's `n = @keys[i]` (a `const char *`) to
21206+ # it -- mismatch.
21207+ if is_obj_type(recv_type) == 1
21208+ cn_each = recv_type[4, recv_type.length - 4]
21209+ cci_each = find_class_idx(cn_each)
21210+ if cci_each >= 0
21211+ midx_each = cls_find_method(cci_each, mname)
21212+ if midx_each >= 0 && cls_method_has_yield(cci_each, midx_each) == 1
21213+ owner_each = find_method_owner(cci_each, mname)
21214+ owner_ci_each = find_class_idx(owner_each)
21215+ if owner_ci_each >= 0
21216+ owner_midx_each = cls_find_method(owner_ci_each, mname)
21217+ arity_each = cls_method_yield_arity(owner_ci_each, owner_midx_each)
21218+ ytypes_each = "".split(",")
21219+ ka = 0
21220+ while ka < arity_each
21221+ ytypes_each.push("")
21222+ ka = ka + 1
21223+ end
21224+ ybid_each = cls_method_body_id(owner_ci_each, owner_midx_each)
21225+ # Push a temporary scope with the yielding
21226+ # method's locals declared so body_yield_arg_types'
21227+ # infer_type calls resolve LocalVariableReadNodes
21228+ # to their actual types -- without this, a yield
21229+ # arg like `n` (a `const char *` local in the
21230+ # method) would just read as `int` (the no-scope
21231+ # default) and we'd miss the body-driven type.
21232+ if ybid_each >= 0
21233+ push_scope
21234+ yl_names = "".split(",")
21235+ yl_types = "".split(",")
21236+ saved_ci_each = @current_class_idx
21237+ @current_class_idx = owner_ci_each
21238+ scan_locals(ybid_each, yl_names, yl_types, "".split(","))
21239+ yj = 0
21240+ while yj < yl_names.length
21241+ declare_var(yl_names[yj], yl_types[yj])
21242+ yj = yj + 1
21243+ end
21244+ body_yield_arg_types(ybid_each, ytypes_each)
21245+ @current_class_idx = saved_ci_each
21246+ pop_scope
21247+ end
21248+ if bk < ytypes_each.length && ytypes_each[bk] != "" && ytypes_each[bk] != "int"
21249+ types.push(ytypes_each[bk])
21250+ bk = bk + 1
21251+ next
21252+ end
21253+ end
21254+ end
21255+ end
21256+ end
2119921257 if mname == "times" || mname == "upto" || mname == "downto"
2120021258 types.push("int")
2120121259 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"