Commit f411b843

f411b843fab0Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +3 added · ~1 modified

Description

fix(codegen): defer element type for empty [] across function boundaries

Issue #58: writes performed on a parameter inside the callee did not feed back to the caller's variable type. So

def foo(ary); ary.push("foo"); end a = [] foo(a)

made foo's ary parameter an sp_IntArray * (the default for an empty array literal), then the body's ary.push("foo") tried to push a char * into an mrb_int slot — a C type error.

The conceptual fix is the deferred-element-type idea: an empty [] literal carries no element data, so its element type should be picked by the first concrete use rather than nailed down at the literal site. Implementation-wise that lives as a flag rather than a new type tag — the new-type-tag form would have required parity branches at ~30+ existing int_array decision sites, including in the self-hosted codegen itself.

Three new pieces:

  1. Empty-flag tracking (@scan_empty_flags, @meth_param_empty, @cls_meth_ptypes_empty). A local that was assigned an empty array literal gets the flag; a subsequent concrete-array write clears it. The flag flows from caller arg to callee param via the call-site scan.

  2. Body-push promotion (infer_param_array_type_from_body). For each method whose param is int_array AND the empty-flag is set, scan the body for param.push(known_elem). If every observed push agrees on a single concrete element type (string / float / symbol), promote @meth_param_types[mi][k] (or @cls_meth_ptypes) to the matching typed-array. Without the empty-flag guard, a caller passing a real int_array ([1, 2, 3]) would be silently retyped — the flag keeps the promotion sound. Without the single-type check, a body that pushes mixed types stays at int_array and surfaces as a real error rather than picking one arbitrarily. Runs inside the existing iterative inference loop.

  3. Caller back-propagation (in scan_locals itself). When the call site's arg is a local with empty-flag and the matching param has been promoted to a concrete typed-array, lift the local's type to match. The loop converges over a few iterations because the inference signature now includes @cls_meth_ptypes.

Plus two codegen-side adjustments:

  • compile_call_args_with_defaults emits sp_StrArray_new() / sp_FloatArray_new() directly when the call site's empty-[] literal targets a typed-array param. Without this the literal always emitted sp_IntArray_new(), producing a -Wincompatible-pointer-types warning at the call.
  • detect_poly_in_node treats an empty literal as compatible with any concrete typed-array param. Otherwise the body-promoted str_array param would mismatch the caller's int_array-inferred [] and bounce the param back to poly.

Coverage:

  • test/empty_array_param_local.rb — issue's exact reproducer
  • test/empty_array_param_direct.rb — foo([]) (literal at call site)
  • test/empty_array_param_class_method.rb — instance method on user class

Inheritance gap (class Child < Base; end; Child.new.method_on_base([])) is intentionally out of scope: the existing class-method param-from- caller pass doesn't walk the inheritance chain to populate the parent class's @cls_meth_ptypes, so the body-promotion guard never fires. Filing as follow-up.

Bootstrap fixed-point clean; 131 tests + 56 benches pass.

Closes #58

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

Details

Commit
f411b843fab0
Parent
fdc0137303d7

spinel_codegen.rbmodifiedView file

@@ -96,6 +96,12 @@
9696 @meth_names = "".split(",")
9797 @meth_param_names = "".split(",")
9898 @meth_param_types = "".split(",")
99+ # Per-param "deferred element" flag: "1" means at least one caller
100+ # passed an empty `[]` literal (or a local that itself was assigned
101+ # an empty literal). Used by the param body-push promotion pass
102+ # (issue #58) to decide whether the param's int_array can be safely
103+ # promoted to a concrete typed-array based on body usage.
104+ @meth_param_empty = "".split(",")
99105 @meth_return_types = "".split(",")
100106 @meth_body_ids = []
101107 @meth_has_defaults = "".split(",")
@@ -111,6 +117,9 @@
111117 @cls_meth_returns = "".split(",")
112118 @cls_meth_bodies = "".split(",")
113119 @cls_meth_defaults = "".split(",")
120+ # Mirror of @meth_param_empty for class methods. Pipe-separated by
121+ # method, comma-separated by param. Issue #58.
122+ @cls_meth_ptypes_empty = "".split(",")
114123 @cls_attr_readers = "".split(",")
115124 @cls_attr_writers = "".split(",")
116125 @cls_cmeth_names = "".split(",")
@@ -3184,6 +3193,24 @@
31843193 0
31853194 end
31863195
3196+ # Issue #58: empty `[]` literal needs deferred element-type
3197+ # resolution. This helper distinguishes `[]` from `[1, 2, 3]` so the
3198+ # promotion machinery can know "writes haven't fixed the element type
3199+ # yet, so a later push can still pick it".
3200+ def is_empty_array_literal(nid)
3201+ if nid < 0
3202+ return 0
3203+ end
3204+ if @nd_type[nid] != "ArrayNode"
3205+ return 0
3206+ end
3207+ elems = parse_id_list(@nd_elements[nid])
3208+ if elems.length == 0
3209+ return 1
3210+ end
3211+ 0
3212+ end
3213+
31873214 def base_type(t)
31883215 if t.length > 1 && t[t.length - 1] == "?"
31893216 return t[0, t.length - 1]
@@ -3637,6 +3664,7 @@
36373664 params = collect_params_str(sid)
36383665 @meth_param_names.push(params)
36393666 @meth_param_types.push(collect_ptypes_str(sid, -1))
3667+ @meth_param_empty.push("")
36403668 @meth_return_types.push("int")
36413669 @meth_body_ids.push(@nd_body[sid])
36423670 @meth_has_yield.push(0)
@@ -3784,6 +3812,7 @@
37843812 @cls_meth_returns.push("void")
37853813 @cls_meth_bodies.push("-2")
37863814 @cls_meth_defaults.push(init_defaults)
3815+ @cls_meth_ptypes_empty.push("")
37873816 else
37883817 @cls_meth_names.push("")
37893818 @cls_meth_params.push("")
@@ -3791,6 +3820,7 @@
37913820 @cls_meth_returns.push("")
37923821 @cls_meth_bodies.push("")
37933822 @cls_meth_defaults.push("")
3823+ @cls_meth_ptypes_empty.push("")
37943824 end
37953825 @cls_attr_readers.push(attr_readers)
37963826 @cls_attr_writers.push(attr_writers)
@@ -4103,6 +4133,7 @@
41034133 @cls_meth_returns[ci] = @cls_meth_returns[ci] + ";" + ret
41044134 @cls_meth_bodies[ci] = @cls_meth_bodies[ci] + ";" + body_id.to_s
41054135 @cls_meth_defaults[ci] = @cls_meth_defaults[ci] + "|" + defaults
4136+ @cls_meth_ptypes_empty[ci] = @cls_meth_ptypes_empty[ci] + "|"
41064137 else
41074138 @cls_meth_names[ci] = name
41084139 @cls_meth_params[ci] = params
@@ -4110,6 +4141,7 @@
41104141 @cls_meth_returns[ci] = ret
41114142 @cls_meth_bodies[ci] = body_id.to_s
41124143 @cls_meth_defaults[ci] = defaults
4144+ @cls_meth_ptypes_empty[ci] = ""
41134145 end
41144146 end
41154147
@@ -4523,6 +4555,7 @@
45234555 @meth_names.push(mname)
45244556 @meth_param_names.push(params_str)
45254557 @meth_param_types.push(ptypes_str)
4558+ @meth_param_empty.push("")
45264559 @meth_return_types.push("int")
45274560 @meth_body_ids.push(body_id)
45284561 @meth_has_defaults.push(defaults_str)
@@ -4572,6 +4605,7 @@
45724605 @meth_names.push(mname)
45734606 @meth_param_names.push(params_str)
45744607 @meth_param_types.push(ptypes_str)
4608+ @meth_param_empty.push("")
45754609 @meth_return_types.push("int")
45764610 @meth_body_ids.push(body_id)
45774611 @meth_has_defaults.push("")
@@ -4625,6 +4659,7 @@
46254659 @meth_names.push(mname + "_cls_" + dmname)
46264660 @meth_param_names.push(collect_params_str(sid))
46274661 @meth_param_types.push(collect_ptypes_str(sid, -1))
4662+ @meth_param_empty.push("")
46284663 @meth_return_types.push("int")
46294664 @meth_body_ids.push(@nd_body[sid])
46304665 @meth_has_yield.push(0)
@@ -4671,6 +4706,7 @@
46714706 @cls_meth_returns.push("")
46724707 @cls_meth_bodies.push("")
46734708 @cls_meth_defaults.push("")
4709+ @cls_meth_ptypes_empty.push("")
46744710 @cls_attr_readers.push("")
46754711 @cls_attr_writers.push("")
46764712 @cls_cmeth_names.push("")
@@ -5395,6 +5431,258 @@
53955431 end
53965432 end
53975433
5434+ # Issue #58: collect every element type seen in `pname.push(elem)`
5435+ # or `pname << elem` patterns under nid. The deferred-element-type
5436+ # promotion pass uses this to decide what concrete typed-array a
5437+ # parameter should be promoted to when callers all passed empty
5438+ # `[]` literals.
5439+ def collect_param_push_elem_types(nid, pname, acc)
5440+ if nid < 0
5441+ return
5442+ end
5443+ if @nd_type[nid] == "CallNode"
5444+ if @nd_name[nid] == "push" || @nd_name[nid] == "<<"
5445+ recv = @nd_receiver[nid]
5446+ if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode"
5447+ if @nd_name[recv] == pname
5448+ args_id = @nd_arguments[nid]
5449+ if args_id >= 0
5450+ aargs = get_args(args_id)
5451+ if aargs.length > 0
5452+ at = infer_type(aargs[0])
5453+ if not_in(at, acc) == 1
5454+ acc.push(at)
5455+ end
5456+ end
5457+ end
5458+ end
5459+ end
5460+ end
5461+ end
5462+ if @nd_body[nid] >= 0
5463+ collect_param_push_elem_types(@nd_body[nid], pname, acc)
5464+ end
5465+ stmts = parse_id_list(@nd_stmts[nid])
5466+ k = 0
5467+ while k < stmts.length
5468+ collect_param_push_elem_types(stmts[k], pname, acc)
5469+ k = k + 1
5470+ end
5471+ if @nd_expression[nid] >= 0
5472+ collect_param_push_elem_types(@nd_expression[nid], pname, acc)
5473+ end
5474+ if @nd_left[nid] >= 0
5475+ collect_param_push_elem_types(@nd_left[nid], pname, acc)
5476+ end
5477+ if @nd_right[nid] >= 0
5478+ collect_param_push_elem_types(@nd_right[nid], pname, acc)
5479+ end
5480+ if @nd_arguments[nid] >= 0
5481+ collect_param_push_elem_types(@nd_arguments[nid], pname, acc)
5482+ end
5483+ args2 = parse_id_list(@nd_args[nid])
5484+ k = 0
5485+ while k < args2.length
5486+ collect_param_push_elem_types(args2[k], pname, acc)
5487+ k = k + 1
5488+ end
5489+ if @nd_receiver[nid] >= 0
5490+ collect_param_push_elem_types(@nd_receiver[nid], pname, acc)
5491+ end
5492+ if @nd_predicate[nid] >= 0
5493+ collect_param_push_elem_types(@nd_predicate[nid], pname, acc)
5494+ end
5495+ if @nd_subsequent[nid] >= 0
5496+ collect_param_push_elem_types(@nd_subsequent[nid], pname, acc)
5497+ end
5498+ if @nd_else_clause[nid] >= 0
5499+ collect_param_push_elem_types(@nd_else_clause[nid], pname, acc)
5500+ end
5501+ if @nd_block[nid] >= 0
5502+ collect_param_push_elem_types(@nd_block[nid], pname, acc)
5503+ end
5504+ end
5505+
5506+ # Issue #58: promote each top-level method parameter from int_array
5507+ # to a concrete typed-array (str_array, float_array, sym_array)
5508+ # when (a) every caller passed an empty `[]` literal — guarded by
5509+ # @meth_param_empty[mi][k] == "1" — and (b) the body's pushes on
5510+ # that parameter all agree on a single concrete element type.
5511+ # Without (a), a caller passing a real int_array would be silently
5512+ # miscompiled. Without (b), a body that pushes mixed types should
5513+ # surface as a type error rather than picking one arbitrarily.
5514+ def infer_param_array_type_from_body
5515+ iter = 0
5516+ changed = 1
5517+ while changed == 1 && iter < 4
5518+ changed = 0
5519+ iter = iter + 1
5520+ # Top-level methods. Set up the method's scope so that
5521+ # collect_param_push_elem_types' infer_type calls can resolve
5522+ # other parameters (e.g. `buf.push(name)` where `name` is a
5523+ # string-typed parameter on the same method).
5524+ mi = 0
5525+ while mi < @meth_names.length
5526+ bid = @meth_body_ids[mi]
5527+ if bid >= 0
5528+ pnames = @meth_param_names[mi].split(",")
5529+ ptypes = @meth_param_types[mi].split(",")
5530+ empty_str = ""
5531+ if mi < @meth_param_empty.length
5532+ empty_str = @meth_param_empty[mi]
5533+ end
5534+ empties = empty_str.split(",")
5535+ push_scope
5536+ dj = 0
5537+ while dj < pnames.length
5538+ pt = "int"
5539+ if dj < ptypes.length
5540+ pt = ptypes[dj]
5541+ end
5542+ declare_var(pnames[dj], pt)
5543+ dj = dj + 1
5544+ end
5545+ ml = "".split(",")
5546+ mt = "".split(",")
5547+ scan_locals(bid, ml, mt, pnames)
5548+ lk = 0
5549+ while lk < ml.length
5550+ declare_var(ml[lk], mt[lk])
5551+ lk = lk + 1
5552+ end
5553+ promoted = 0
5554+ pk = 0
5555+ while pk < pnames.length
5556+ if pk < ptypes.length && pk < empties.length
5557+ if empties[pk] == "1" && ptypes[pk] == "int_array"
5558+ elem_acc = "".split(",")
5559+ collect_param_push_elem_types(bid, pnames[pk], elem_acc)
5560+ promoted_type = empty_array_promotion_for(elem_acc)
5561+ if promoted_type != ""
5562+ ptypes[pk] = promoted_type
5563+ if promoted_type == "str_array"
5564+ @needs_str_array = 1
5565+ end
5566+ if promoted_type == "float_array"
5567+ @needs_float_array = 1
5568+ end
5569+ promoted = 1
5570+ changed = 1
5571+ end
5572+ end
5573+ end
5574+ pk = pk + 1
5575+ end
5576+ pop_scope
5577+ if promoted == 1
5578+ @meth_param_types[mi] = ptypes.join(",")
5579+ end
5580+ end
5581+ mi = mi + 1
5582+ end
5583+ # Class methods (instance methods on user classes). Same
5584+ # scope-setup so `buf.push(name)` resolves the param type.
5585+ ci = 0
5586+ while ci < @cls_names.length
5587+ @current_class_idx = ci
5588+ all_params = @cls_meth_params[ci].split("|")
5589+ all_ptypes = @cls_meth_ptypes[ci].split("|")
5590+ all_empty = @cls_meth_ptypes_empty[ci].split("|")
5591+ bodies = @cls_meth_bodies[ci].split(";")
5592+ cls_changed = 0
5593+ mj = 0
5594+ while mj < all_params.length
5595+ bid = -1
5596+ if mj < bodies.length
5597+ bid = bodies[mj].to_i
5598+ end
5599+ if bid >= 0
5600+ cm_pnames = all_params[mj].split(",")
5601+ cm_ptypes = "".split(",")
5602+ cm_empties = "".split(",")
5603+ if mj < all_ptypes.length
5604+ cm_ptypes = all_ptypes[mj].split(",")
5605+ end
5606+ if mj < all_empty.length
5607+ cm_empties = all_empty[mj].split(",")
5608+ end
5609+ push_scope
5610+ cdj = 0
5611+ while cdj < cm_pnames.length
5612+ cpt = "int"
5613+ if cdj < cm_ptypes.length
5614+ cpt = cm_ptypes[cdj]
5615+ end
5616+ declare_var(cm_pnames[cdj], cpt)
5617+ cdj = cdj + 1
5618+ end
5619+ cml = "".split(",")
5620+ cmt = "".split(",")
5621+ scan_locals(bid, cml, cmt, cm_pnames)
5622+ cmlk = 0
5623+ while cmlk < cml.length
5624+ declare_var(cml[cmlk], cmt[cmlk])
5625+ cmlk = cmlk + 1
5626+ end
5627+ pk = 0
5628+ cm_promoted = 0
5629+ while pk < cm_pnames.length
5630+ if pk < cm_ptypes.length && pk < cm_empties.length
5631+ if cm_empties[pk] == "1" && cm_ptypes[pk] == "int_array"
5632+ elem_acc = "".split(",")
5633+ collect_param_push_elem_types(bid, cm_pnames[pk], elem_acc)
5634+ promoted_type = empty_array_promotion_for(elem_acc)
5635+ if promoted_type != ""
5636+ cm_ptypes[pk] = promoted_type
5637+ if promoted_type == "str_array"
5638+ @needs_str_array = 1
5639+ end
5640+ if promoted_type == "float_array"
5641+ @needs_float_array = 1
5642+ end
5643+ cm_promoted = 1
5644+ changed = 1
5645+ end
5646+ end
5647+ end
5648+ pk = pk + 1
5649+ end
5650+ pop_scope
5651+ if cm_promoted == 1
5652+ all_ptypes[mj] = cm_ptypes.join(",")
5653+ cls_changed = 1
5654+ end
5655+ end
5656+ mj = mj + 1
5657+ end
5658+ if cls_changed == 1
5659+ @cls_meth_ptypes[ci] = all_ptypes.join("|")
5660+ end
5661+ ci = ci + 1
5662+ end
5663+ @current_class_idx = -1
5664+ end
5665+ end
5666+
5667+ # Helper: given the set of element types observed in pname.push(...)
5668+ # patterns, return the typed-array tag to promote to, or "" if the
5669+ # observations don't agree on a single concrete type.
5670+ def empty_array_promotion_for(elem_acc)
5671+ if elem_acc.length != 1
5672+ return ""
5673+ end
5674+ if elem_acc[0] == "string"
5675+ return "str_array"
5676+ end
5677+ if elem_acc[0] == "float"
5678+ return "float_array"
5679+ end
5680+ if elem_acc[0] == "symbol"
5681+ return "sym_array"
5682+ end
5683+ ""
5684+ end
5685+
53985686 # Does class `ci` provide `mname` as a reader, writer, or method?
53995687 # Walks parent classes for inherited members.
54005688 def class_has_method(ci, mname)
@@ -7149,11 +7437,22 @@
71497437 while k < arg_ids.length
71507438 at = infer_type_deep(arg_ids[k])
71517439 if k < ptypes.length
7152- ct = ptypes[k]
7153- # Skip rest/splat params (int_array) - they handle multiple args
7154- if ct == "int_array"
7155- k = k + 1
7156- next
7440+ ct = ptypes[k]
7441+ # Skip rest/splat params (int_array) - they handle multiple args
7442+ if ct == "int_array"
7443+ k = k + 1
7444+ next
7445+ end
7446+ # Issue #58: an empty `[]` literal at the call site is
7447+ # compatible with any concrete typed-array param type.
7448+ # Without this, `foo([])` against a body-promoted
7449+ # `str_array` param triggers the ct != at mismatch and
7450+ # bumps the param back to poly.
7451+ if is_empty_array_literal(arg_ids[k]) == 1
7452+ if ct == "str_array" || ct == "float_array" || ct == "sym_array" || is_ptr_array_type(ct) == 1
7453+ k = k + 1
7454+ next
7455+ end
71577456 end
71587457 if ct != at
71597458 if ct != "poly"
@@ -7650,7 +7949,7 @@
76507949 # refines. Identical fingerprints between successive iterations means a
76517950 # fixed point has been reached and further iterations are wasted work.
76527951 def inference_signature
7653- @meth_return_types.join("|") + "/" + @cls_ivar_types.join("|") + "/" + @meth_param_types.join("|")
7952+ @meth_return_types.join("|") + "/" + @cls_ivar_types.join("|") + "/" + @meth_param_types.join("|") + "/" + @cls_meth_ptypes.join("/")
76547953 end
76557954
76567955 def compile
@@ -7666,6 +7965,12 @@
76667965 while iter < 4
76677966 infer_all_returns
76687967 infer_ivar_types_from_writers
7968+ # Issue #58: after scan_locals has populated @meth_param_empty
7969+ # via the per-call-site forward propagation, promote int_array
7970+ # params to concrete typed-arrays where bodies push known types.
7971+ # Then the next iteration's scan_locals back-propagates those
7972+ # promoted types to caller-side locals.
7973+ infer_param_array_type_from_body
76697974 detect_poly_params
76707975 cur_sig = inference_signature
76717976 if cur_sig == prev_sig
@@ -10535,6 +10840,11 @@
1053510840 # a fresh (empty) names array.
1053610841 if names.length == 0
1053710842 @scan_literal_flags = "".split(",")
10843+ # Parallel to `names`: "1" if every write to this local so far was
10844+ # an empty `[]` literal — used to defer the array element type
10845+ # until first `push` (issue #58). A subsequent write with a
10846+ # concrete element resets the flag to "".
10847+ @scan_empty_flags = "".split(",")
1053810848 end
1053910849 if @nd_type[nid] == "MultiWriteNode"
1054010850 targets = parse_id_list(@nd_targets[nid])
@@ -10546,6 +10856,7 @@
1054610856 names.push(lname)
1054710857 types.push("int")
1054810858 @scan_literal_flags.push("")
10859+ @scan_empty_flags.push("")
1054910860 end
1055010861 end
1055110862 end
@@ -10566,11 +10877,29 @@
1056610877 else
1056710878 @scan_literal_flags.push("")
1056810879 end
10880+ # Track empty-array literal so a later push() can promote
10881+ # the local's element type (issue #58).
10882+ if is_empty_array_literal(@nd_expression[nid]) == 1
10883+ @scan_empty_flags.push("1")
10884+ else
10885+ @scan_empty_flags.push("")
10886+ end
1056910887 end
1057010888 else
1057110889 if not_in(lname, params) == 1
1057210890 # Check if type changed
1057310891 at = infer_type(@nd_expression[nid])
10892+ # Concrete (non-empty) array overwrite clears the deferred
10893+ # element-type flag — a `[1,2,3]` write commits to int_array.
10894+ if is_empty_array_literal(@nd_expression[nid]) == 0
10895+ ei = 0
10896+ while ei < names.length
10897+ if names[ei] == lname && ei < @scan_empty_flags.length
10898+ @scan_empty_flags[ei] = ""
10899+ end
10900+ ei = ei + 1
10901+ end
10902+ end
1057410903 ki = 0
1057510904 while ki < names.length
1057610905 if names[ki] == lname
@@ -10723,6 +11052,197 @@
1072311052 end
1072411053 end
1072511054 end
11055+ # Issue #58: empty-array param promotion at instance-method call
11056+ # sites — `obj.method(arg)`. Same forward/backward propagation as
11057+ # the top-level branch below, but reads/writes the per-class
11058+ # @cls_meth_ptypes / @cls_meth_ptypes_empty storage.
11059+ if @nd_type[nid] == "CallNode"
11060+ icm_recv = @nd_receiver[nid]
11061+ if icm_recv >= 0
11062+ icm_rt = infer_type(icm_recv)
11063+ # When the receiver is a local declared in this same
11064+ # scan_locals pass (`r = Recorder.new` followed by `r.method(...)`),
11065+ # infer_type still returns "int" because we haven't called
11066+ # declare_var yet. Fall back to the names/types accumulator.
11067+ if icm_rt == "int" && @nd_type[icm_recv] == "LocalVariableReadNode"
11068+ icm_recv_name = @nd_name[icm_recv]
11069+ icm_ni0 = 0
11070+ while icm_ni0 < names.length
11071+ if names[icm_ni0] == icm_recv_name
11072+ icm_rt = types[icm_ni0]
11073+ end
11074+ icm_ni0 = icm_ni0 + 1
11075+ end
11076+ end
11077+ if is_obj_type(icm_rt) == 1
11078+ icm_cname = icm_rt[4, icm_rt.length - 4]
11079+ icm_ci = find_class_idx(icm_cname)
11080+ if icm_ci >= 0
11081+ icm_mname = @nd_name[nid]
11082+ icm_midx = cls_find_method_direct(icm_ci, icm_mname)
11083+ # Walk parents if not found on the receiver class itself
11084+ icm_owner_ci = icm_ci
11085+ if icm_midx < 0
11086+ icm_owner_name = find_method_owner(icm_ci, icm_mname)
11087+ if icm_owner_name != ""
11088+ icm_owner_ci = find_class_idx(icm_owner_name)
11089+ if icm_owner_ci >= 0
11090+ icm_midx = cls_find_method_direct(icm_owner_ci, icm_mname)
11091+ end
11092+ end
11093+ end
11094+ if icm_midx >= 0
11095+ icm_args_id = @nd_arguments[nid]
11096+ if icm_args_id >= 0
11097+ icm_aargs = get_args(icm_args_id)
11098+ icm_all_ptypes = @cls_meth_ptypes[icm_owner_ci].split("|")
11099+ icm_all_empty = @cls_meth_ptypes_empty[icm_owner_ci].split("|")
11100+ icm_ptypes = "".split(",")
11101+ icm_empties = "".split(",")
11102+ if icm_midx < icm_all_ptypes.length
11103+ icm_ptypes = icm_all_ptypes[icm_midx].split(",")
11104+ end
11105+ if icm_midx < icm_all_empty.length
11106+ icm_empties = icm_all_empty[icm_midx].split(",")
11107+ end
11108+ icm_changed = 0
11109+ icm_k = 0
11110+ while icm_k < icm_aargs.length
11111+ icm_arg_id = icm_aargs[icm_k]
11112+ icm_arg_is_empty = is_empty_array_literal(icm_arg_id)
11113+ icm_local_idx = -1
11114+ if @nd_type[icm_arg_id] == "LocalVariableReadNode"
11115+ icm_arg_lname = @nd_name[icm_arg_id]
11116+ icm_ni = 0
11117+ while icm_ni < names.length
11118+ if names[icm_ni] == icm_arg_lname
11119+ icm_local_idx = icm_ni
11120+ end
11121+ icm_ni = icm_ni + 1
11122+ end
11123+ if icm_local_idx >= 0 && icm_local_idx < @scan_empty_flags.length
11124+ if @scan_empty_flags[icm_local_idx] == "1"
11125+ icm_arg_is_empty = 1
11126+ end
11127+ end
11128+ end
11129+ if icm_arg_is_empty == 1
11130+ while icm_empties.length <= icm_k
11131+ icm_empties.push("")
11132+ end
11133+ if icm_empties[icm_k] != "1"
11134+ icm_empties[icm_k] = "1"
11135+ icm_changed = 1
11136+ end
11137+ end
11138+ if icm_local_idx >= 0 && icm_k < icm_ptypes.length
11139+ icm_pt = icm_ptypes[icm_k]
11140+ if types[icm_local_idx] == "int_array" && icm_local_idx < @scan_empty_flags.length && @scan_empty_flags[icm_local_idx] == "1"
11141+ if icm_pt == "str_array"
11142+ types[icm_local_idx] = "str_array"
11143+ @needs_str_array = 1
11144+ end
11145+ if icm_pt == "float_array"
11146+ types[icm_local_idx] = "float_array"
11147+ @needs_float_array = 1
11148+ end
11149+ if icm_pt == "sym_array"
11150+ types[icm_local_idx] = "sym_array"
11151+ end
11152+ end
11153+ end
11154+ icm_k = icm_k + 1
11155+ end
11156+ if icm_changed == 1
11157+ icm_all_empty[icm_midx] = icm_empties.join(",")
11158+ @cls_meth_ptypes_empty[icm_owner_ci] = icm_all_empty.join("|")
11159+ end
11160+ end
11161+ end
11162+ end
11163+ end
11164+ end
11165+ end
11166+ # Issue #58: empty-array param promotion at top-level function
11167+ # call sites. Two directions in one place:
11168+ # (a) Forward: if `arg` is `[]` literal or a local with the
11169+ # empty flag set, mark @meth_param_empty[mi][k] = "1" so a
11170+ # later body-promotion pass can refine the param type.
11171+ # (b) Backward: if @meth_param_types[mi][k] has already been
11172+ # promoted to a concrete typed-array (str_array, etc.) and
11173+ # `arg` is a local with the empty flag, upgrade the local's
11174+ # type to match — this is what propagates the deferred
11175+ # resolution back to the caller's variable.
11176+ if @nd_type[nid] == "CallNode"
11177+ if @nd_receiver[nid] < 0
11178+ ea_mname = @nd_name[nid]
11179+ ea_mi = find_method_idx(ea_mname)
11180+ if ea_mi >= 0
11181+ ea_args_id = @nd_arguments[nid]
11182+ if ea_args_id >= 0
11183+ ea_aargs = get_args(ea_args_id)
11184+ ea_ptypes = @meth_param_types[ea_mi].split(",")
11185+ ea_empty_str = ""
11186+ if ea_mi < @meth_param_empty.length
11187+ ea_empty_str = @meth_param_empty[ea_mi]
11188+ end
11189+ ea_empties = ea_empty_str.split(",")
11190+ ea_changed = 0
11191+ ea_k = 0
11192+ while ea_k < ea_aargs.length
11193+ ea_arg_id = ea_aargs[ea_k]
11194+ ea_arg_is_empty = is_empty_array_literal(ea_arg_id)
11195+ ea_local_idx = -1
11196+ if @nd_type[ea_arg_id] == "LocalVariableReadNode"
11197+ ea_arg_lname = @nd_name[ea_arg_id]
11198+ ea_ni = 0
11199+ while ea_ni < names.length
11200+ if names[ea_ni] == ea_arg_lname
11201+ ea_local_idx = ea_ni
11202+ end
11203+ ea_ni = ea_ni + 1
11204+ end
11205+ if ea_local_idx >= 0 && ea_local_idx < @scan_empty_flags.length
11206+ if @scan_empty_flags[ea_local_idx] == "1"
11207+ ea_arg_is_empty = 1
11208+ end
11209+ end
11210+ end
11211+ if ea_arg_is_empty == 1
11212+ while ea_empties.length <= ea_k
11213+ ea_empties.push("")
11214+ end
11215+ if ea_empties[ea_k] != "1"
11216+ ea_empties[ea_k] = "1"
11217+ ea_changed = 1
11218+ end
11219+ end
11220+ # Backward: param already promoted, lift the local too.
11221+ if ea_local_idx >= 0 && ea_k < ea_ptypes.length
11222+ ea_pt = ea_ptypes[ea_k]
11223+ if types[ea_local_idx] == "int_array" && ea_local_idx < @scan_empty_flags.length && @scan_empty_flags[ea_local_idx] == "1"
11224+ if ea_pt == "str_array"
11225+ types[ea_local_idx] = "str_array"
11226+ @needs_str_array = 1
11227+ end
11228+ if ea_pt == "float_array"
11229+ types[ea_local_idx] = "float_array"
11230+ @needs_float_array = 1
11231+ end
11232+ if ea_pt == "sym_array"
11233+ types[ea_local_idx] = "sym_array"
11234+ end
11235+ end
11236+ end
11237+ ea_k = ea_k + 1
11238+ end
11239+ if ea_changed == 1
11240+ @meth_param_empty[ea_mi] = ea_empties.join(",")
11241+ end
11242+ end
11243+ end
11244+ end
11245+ end
1072611246 # Detect hash value type from h["key"] = val
1072711247 if @nd_type[nid] == "CallNode"
1072811248 if @nd_name[nid] == "[]="
@@ -16259,6 +16779,34 @@
1625916779 k = k + 1
1626016780 next
1626116781 end
16782+ # Issue #58: empty `[]` literal at the call site needs to
16783+ # construct the right typed-array container. The literal's
16784+ # own infer_type returns int_array (compile_array_literal
16785+ # emits sp_IntArray_new()), but if the param is a concrete
16786+ # typed-array, emit the matching constructor instead.
16787+ if is_empty_array_literal(positional_ids[k]) == 1
16788+ if ptypes[k] == "str_array"
16789+ @needs_str_array = 1
16790+ @needs_gc = 1
16791+ result = result + "sp_StrArray_new()"
16792+ k = k + 1
16793+ next
16794+ end
16795+ if ptypes[k] == "float_array"
16796+ @needs_float_array = 1
16797+ @needs_gc = 1
16798+ result = result + "sp_FloatArray_new()"
16799+ k = k + 1
16800+ next
16801+ end
16802+ if ptypes[k] == "sym_array"
16803+ @needs_int_array = 1
16804+ @needs_gc = 1
16805+ result = result + "sp_IntArray_new()"
16806+ k = k + 1
16807+ next
16808+ end
16809+ end
1626216810 end
1626316811 result = result + compile_expr(positional_ids[k])
1626416812 else