Commit 1ca17ac2
1ca17ac27e8dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): scan_locals_first_type picks up block param types
Closes #484. Inside a class method, an .each block-local passed
as a positional arg to another class method (arr.each { |bp| M.use(bp) }) didn't propagate its type to the callee's param
slot — the callee's s stayed at the mrb_int default and the
call site failed C compile with const char * -> mrb_int.
The same shape at file scope already worked. The mismatch was in the local-scan helper: infer_main_call_types runs the full scan_locals walker, which already collects block params into the local-name/type arrays before scan_new_calls walks call sites. infer_class_body_call_types runs scan_locals_first_type — a leaner variant that omitted the block-param branch entirely. The upshot was that inside the cmeth path, infer_type(pair) at the call site fell back to int because pair was never declared in scope.
Fix: scan_locals_first_type now mirrors the equivalent branch in scan_locals — when nid is a CallNode with a do/end block whose parameters are RequiredParameterNodes, push each block-param name with the receiver-array element type (str_array -> string, float_array -> float, sym_array -> symbol, *_ptr_array -> element class type). NumberedParametersNode is left for a follow-up.
Validation: tests 450 (+1), bench 56/56.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 1ca17ac27e8d
- Parent
- 1c9a79c2781f
spinel_analyze.rbmodifiedView file
r_idx = r_idx + 1 } end # Block parameters of `recv.method do |bp| ... end` need to be # declared as locals so the enclosing scope's call-site widening # (driven by scan_new_calls -> widen_ptypes_from_args -> # infer_type(arg)) can resolve `bp` to its real type rather than # the find_var_type-not-found fallback `int`. Mirrors the # equivalent (more elaborate) block-param block in scan_locals. # Without this, a `class M; def self.driver; arr.each { |pair| # M.use(pair) } end end` shape leaves M.use's `s` param at # mrb_int even though pair is `const char *`. if @nd_type[nid] == "CallNode" blk_fp = @nd_block[nid] if blk_fp >= 0 bp_fp = @nd_parameters[blk_fp] if bp_fp >= 0 && @nd_type[bp_fp] != "NumberedParametersNode" inner_fp = @nd_parameters[bp_fp] if inner_fp >= 0 reqs_fp = parse_id_list(@nd_requireds[inner_fp]) recv_t_fp = "" if @nd_receiver[nid] >= 0 recv_t_fp = infer_type(@nd_receiver[nid]) end elem_t_fp = "int" if recv_t_fp == "str_array" elem_t_fp = "string" elsif recv_t_fp == "float_array" elem_t_fp = "float" elsif recv_t_fp == "sym_array" elem_t_fp = "symbol" elsif is_ptr_array_type(recv_t_fp) == 1 elem_t_fp = ptr_array_elem_type(recv_t_fp) end bk_fp = 0 while bk_fp < reqs_fp.length bname_fp = @nd_name[reqs_fp[bk_fp]] if not_in(bname_fp, names) == 1 && not_in(bname_fp, params) == 1 names.push(bname_fp) types.push(elem_t_fp) end bk_fp = bk_fp + 1 end end end end end # Recurse if @nd_body[nid] >= 0 scan_locals_first_type(@nd_body[nid], names, types, params)test/block_local_arg_to_method_call.rbaddedView file
Loading diff…
test/block_local_arg_to_method_call.rb.expectedaddedView file
Loading diff…