Commit 153e9f28

153e9f28e04dMerged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(analyze): collect kwrest/posts ptypes for top-level methods

collect_toplevel_method built its ptypes_str inline and only handled reqs / opts / kws / rest / block — it omitted the kwrest (**kw) slot and the post-rest required slots (*r, x, y) that the parallel collect_params_str does emit. A method like

def sow(title = nil, **_chrome, &block) Sapp.new(title: title || "default", body: block) end

therefore ended up with param_names.length == 6 and param_types.length == 5. The per-method scope build in infer_function_body_call_types walks pnames and pushes ptypes[pk] (nil for the missing trailing slot) onto @scope_types via declare_var. When the body later read that local — body: block — infer_type fetched the nil and unify_call_types tripped through base_type, which has no nil guard:

spinel_analyze.rb:5569:in 'Compiler#base_type' NoMethodError: undefined method 'length' for nil

Switch the inline builder to collect_ptypes_str(nid, -1), the shared helper already used by class-method registration. That helper emits sym_poly_hash for kwrest and int for posts, so pnames / ptypes counts stay aligned and the body's LocalVariableReadNode for the block param resolves to its declared proc type.

Regression test (test/toplevel_kwrest_block_param.rb) mirrors the failing shape: top-level def with **kwrest and &block, body passes the block local to a .new call site.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
153e9f28e04d
Parent
8e604f0a7ef9

spinel_analyze.rbmodifiedView file

@@ -8703,70 +8703,16 @@
87038703 mname = @nd_name[nid]
87048704 body_id = @nd_body[nid]
87058705 params_str = collect_params_str(nid)
8706- ptypes_str = ""
8706+ # Use the shared ptypes builder so kwrest (`**kw`) and post-rest
8707+ # required params (`def f(*r, x, y)`) get slots that match the
8708+ # names emitted by collect_params_str above. An earlier inline
8709+ # build here omitted both, leaving param_names.length one (or
8710+ # more) ahead of param_types.length — the per-method scope build
8711+ # in infer_function_body_call_types then pushed nil into the
8712+ # scope_types array for any missing slot, which later tripped
8713+ # base_type / unify_call_types when a body referenced that local.
8714+ ptypes_str = collect_ptypes_str(nid, -1)
87078715 defaults_str = collect_defaults_str(nid)
8708-
8709- # Infer param types from defaults
8710- params = @nd_parameters[nid]
8711- if params >= 0
8712- reqs = parse_id_list(@nd_requireds[params])
8713- opts = parse_id_list(@nd_optionals[params])
8714- kws = parse_id_list(@nd_keywords[params])
8715- k = 0
8716- while k < reqs.length
8717- if ptypes_str != ""
8718- ptypes_str = ptypes_str + ","
8719- end
8720- ptypes_str = ptypes_str + "int"
8721- k = k + 1
8722- end
8723- k = 0
8724- while k < opts.length
8725- if ptypes_str != ""
8726- ptypes_str = ptypes_str + ","
8727- end
8728- def_id = @nd_expression[opts[k]]
8729- if def_id >= 0
8730- ptypes_str = ptypes_str + infer_type(def_id)
8731- else
8732- ptypes_str = ptypes_str + "int"
8733- end
8734- k = k + 1
8735- end
8736- k = 0
8737- while k < kws.length
8738- if ptypes_str != ""
8739- ptypes_str = ptypes_str + ","
8740- end
8741- def_id = @nd_expression[kws[k]]
8742- if def_id >= 0
8743- ptypes_str = ptypes_str + infer_type(def_id)
8744- else
8745- ptypes_str = ptypes_str + "int"
8746- end
8747- k = k + 1
8748- end
8749- # Rest param (splat)
8750- rest = @nd_rest[params]
8751- if rest >= 0
8752- if @nd_type[rest] == "RestParameterNode"
8753- if ptypes_str != ""
8754- ptypes_str = ptypes_str + ","
8755- end
8756- ptypes_str = ptypes_str + "int_array"
8757- end
8758- end
8759- # Block param (&block)
8760- blk = @nd_block[params]
8761- if blk >= 0
8762- if @nd_type[blk] == "BlockParameterNode"
8763- if ptypes_str != ""
8764- ptypes_str = ptypes_str + ","
8765- end
8766- ptypes_str = ptypes_str + "proc"
8767- end
8768- end
8769- end
87708716
87718717 @meth_names.push(mname)
87728718 @meth_param_names.push(params_str)