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

@@ -15968,6 +15968,50 @@
1596815968 r_idx = r_idx + 1
1596915969 }
1597015970 end
15971+ # Block parameters of `recv.method do |bp| ... end` need to be
15972+ # declared as locals so the enclosing scope's call-site widening
15973+ # (driven by scan_new_calls -> widen_ptypes_from_args ->
15974+ # infer_type(arg)) can resolve `bp` to its real type rather than
15975+ # the find_var_type-not-found fallback `int`. Mirrors the
15976+ # equivalent (more elaborate) block-param block in scan_locals.
15977+ # Without this, a `class M; def self.driver; arr.each { |pair|
15978+ # M.use(pair) } end end` shape leaves M.use's `s` param at
15979+ # mrb_int even though pair is `const char *`.
15980+ if @nd_type[nid] == "CallNode"
15981+ blk_fp = @nd_block[nid]
15982+ if blk_fp >= 0
15983+ bp_fp = @nd_parameters[blk_fp]
15984+ if bp_fp >= 0 && @nd_type[bp_fp] != "NumberedParametersNode"
15985+ inner_fp = @nd_parameters[bp_fp]
15986+ if inner_fp >= 0
15987+ reqs_fp = parse_id_list(@nd_requireds[inner_fp])
15988+ recv_t_fp = ""
15989+ if @nd_receiver[nid] >= 0
15990+ recv_t_fp = infer_type(@nd_receiver[nid])
15991+ end
15992+ elem_t_fp = "int"
15993+ if recv_t_fp == "str_array"
15994+ elem_t_fp = "string"
15995+ elsif recv_t_fp == "float_array"
15996+ elem_t_fp = "float"
15997+ elsif recv_t_fp == "sym_array"
15998+ elem_t_fp = "symbol"
15999+ elsif is_ptr_array_type(recv_t_fp) == 1
16000+ elem_t_fp = ptr_array_elem_type(recv_t_fp)
16001+ end
16002+ bk_fp = 0
16003+ while bk_fp < reqs_fp.length
16004+ bname_fp = @nd_name[reqs_fp[bk_fp]]
16005+ if not_in(bname_fp, names) == 1 && not_in(bname_fp, params) == 1
16006+ names.push(bname_fp)
16007+ types.push(elem_t_fp)
16008+ end
16009+ bk_fp = bk_fp + 1
16010+ end
16011+ end
16012+ end
16013+ end
16014+ end
1597116015 # Recurse
1597216016 if @nd_body[nid] >= 0
1597316017 scan_locals_first_type(@nd_body[nid], names, types, params)