Commit 8d1162db

8d1162db55daMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

feat: promote Struct ctor param to poly on incompatible call-site types

Previous behaviour: scan_new_calls upgraded "int" to whatever non-int type the first call site provided, but never detected mismatches — subsequent calls with a different type silently clobbered, and the conflicting call site emitted a type-mismatched arg (e.g., passing int to a const char * param).

Route both the keyword-arg and positional paths through a new unify_call_types(old, at, arg_id) helper. The helper keeps the existing "int is fallback" rule when the new arg was not a literal (i.e., likely an unresolved read that defaulted to int), but promotes to "poly" when a literal integer meets a non-numeric concrete type, or when two genuinely incompatible types meet.

When a ctor param ends up poly, compile_constructor_args now boxes each matching arg via box_expr_to_poly at the call site, so emissions like Pt.new(v: 1) and Pt.new(v: "hello") become sp_Pt_new(sp_box_int(1)) sp_Pt_new(sp_box_str("hello")) and the shared ctor takes sp_RbVal.

Pt-with-mixed-type positional and keyword constructors both compile clean and produce the Ruby-matching output. Bootstrap reproducible, 74 tests / 55 benchmarks pass, total test/bench warnings remains 0.

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

Details

Commit
8d1162db55da
Parent
c3c59c98c111

spinel_codegen.rbmodifiedView file

@@ -4395,6 +4395,78 @@
43954395 scan_new_calls(@root_id)
43964396 end
43974397
4398+ # Merge `at` (inferred from a new call-site argument) into the
4399+ # accumulated ctor param type `old_pt`. "int" is normally treated as
4400+ # a fallback/placeholder (many unresolved reads default to it), but a
4401+ # literal IntegerNode is concrete — if old_pt is already a different
4402+ # concrete pointer type, int becomes genuine polymorphism. `arg_id`
4403+ # lets us distinguish literal from inferred.
4404+ def unify_call_types(old_pt, at, arg_id)
4405+ if old_pt == at
4406+ return old_pt
4407+ end
4408+ arg_is_literal = 0
4409+ if arg_id >= 0 && is_literal_value_expr(arg_id) == 1
4410+ arg_is_literal = 1
4411+ end
4412+ if old_pt == "nil"
4413+ if at == "nil"
4414+ return "nil"
4415+ end
4416+ if is_nullable_pointer_type(at) == 1
4417+ if is_nullable_type(at) == 1
4418+ return at
4419+ end
4420+ return at + "?"
4421+ end
4422+ return at
4423+ end
4424+ if at == "nil"
4425+ if is_nullable_pointer_type(old_pt) == 1
4426+ if is_nullable_type(old_pt) == 1
4427+ return old_pt
4428+ end
4429+ return old_pt + "?"
4430+ end
4431+ return old_pt
4432+ end
4433+ if old_pt == "int"
4434+ if at == "int"
4435+ return "int"
4436+ end
4437+ return at
4438+ end
4439+ if at == "int"
4440+ # Numeric compat: int + float is safe in both directions.
4441+ if old_pt == "float"
4442+ return "float"
4443+ end
4444+ # Literal int into a non-numeric concrete type: genuine poly.
4445+ if arg_is_literal == 1
4446+ @needs_rb_value = 1
4447+ return "poly"
4448+ end
4449+ # Inferred int (likely fallback): keep existing type.
4450+ return old_pt
4451+ end
4452+ if base_type(old_pt) == base_type(at)
4453+ # Nullable-compatible variants of the same base.
4454+ if is_nullable_type(at) == 1
4455+ return at
4456+ end
4457+ if is_nullable_type(old_pt) == 1
4458+ return old_pt
4459+ end
4460+ return old_pt
4461+ end
4462+ if (old_pt == "float" && at == "int") || (old_pt == "int" && at == "float")
4463+ return "float"
4464+ end
4465+ # Genuinely incompatible types: fall back to polymorphic value.
4466+ @needs_rb_value = 1
4467+ "poly"
4468+ end
4469+
43984470 def scan_new_calls(nid)
43994471 if nid < 0
44004472 return
@@ -4495,16 +4567,13 @@
44954567 if @nd_type[key_id] == "SymbolNode"
44964568 kname = @nd_content[key_id]
44974569 end
4498- at = infer_type(@nd_expression[elems[ek]])
4570+ expr_id = @nd_expression[elems[ek]]
4571+ at = infer_type(expr_id)
44994572 pi = 0
45004573 while pi < pnames.length
45014574 if pnames[pi] == kname
45024575 if pi < ptypes.length
4503- if ptypes[pi] == "int"
4504- if at != "int"
4505- ptypes[pi] = at
4506- end
4507- end
4576+ ptypes[pi] = unify_call_types(ptypes[pi], at, expr_id)
45084577 end
45094578 end
45104579 pi = pi + 1
@@ -4516,22 +4585,7 @@
45164585 else
45174586 at = infer_type(arg_ids[k])
45184587 if k < ptypes.length
4519- old_pt = ptypes[k]
4520- if old_pt == "int"
4521- if at != "int"
4522- ptypes[k] = at
4523- end
4524- elsif old_pt == "nil" && at != "nil" && at != "int"
4525- if is_nullable_pointer_type(at) == 1
4526- ptypes[k] = at + "?"
4527- else
4528- ptypes[k] = at
4529- end
4530- elsif at == "nil" && old_pt != "nil" && is_nullable_pointer_type(old_pt) == 1
4531- if old_pt[old_pt.length - 1] != "?"
4532- ptypes[k] = old_pt + "?"
4533- end
4534- end
4588+ ptypes[k] = unify_call_types(ptypes[k], at, arg_ids[k])
45354589 end
45364590 end
45374591 k = k + 1
@@ -15159,11 +15213,52 @@
1515915213 ak = ak + 1
1516015214 end
1516115215 if has_kw == 0
15216+ # Positional args: still need to box any arg whose corresponding
15217+ # ctor param is poly.
15218+ init_ci_p = find_init_class(ci)
15219+ if init_ci_p >= 0
15220+ init_idx_p = cls_find_method_direct(init_ci_p, "initialize")
15221+ if init_idx_p >= 0
15222+ all_ptypes_p = @cls_meth_ptypes[init_ci_p].split("|")
15223+ if init_idx_p < all_ptypes_p.length
15224+ ptypes_p = all_ptypes_p[init_idx_p].split(",")
15225+ has_poly = 0
15226+ kpp = 0
15227+ while kpp < ptypes_p.length
15228+ if ptypes_p[kpp] == "poly"
15229+ has_poly = 1
15230+ end
15231+ kpp = kpp + 1
15232+ end
15233+ if has_poly == 1
15234+ result_p = ""
15235+ kp = 0
15236+ while kp < arg_ids.length
15237+ if kp > 0
15238+ result_p = result_p + ", "
15239+ end
15240+ pt_p = "int"
15241+ if kp < ptypes_p.length
15242+ pt_p = ptypes_p[kp]
15243+ end
15244+ if pt_p == "poly"
15245+ result_p = result_p + box_expr_to_poly(arg_ids[kp])
15246+ else
15247+ result_p = result_p + compile_expr(arg_ids[kp])
15248+ end
15249+ kp = kp + 1
15250+ end
15251+ return result_p
15252+ end
15253+ end
15254+ end
15255+ end
1516215256 return compile_call_args(nid)
1516315257 end
15164- # Extract keyword pairs
15258+ # Extract keyword pairs — remember the expression nid too so we can
15259+ # box it (sp_box_int etc.) when the matching ctor param is poly.
1516515260 kw_names = "".split(",")
15166- kw_vals = "".split(",")
15261+ kw_exprs = []
1516715262 ak = 0
1516815263 while ak < arg_ids.length
1516915264 if @nd_type[arg_ids[ak]] == "KeywordHashNode"
@@ -15178,7 +15273,7 @@
1517815273 kname = @nd_content[key_id]
1517915274 end
1518015275 kw_names.push(kname)
15181- kw_vals.push(compile_expr(@nd_expression[elems[ek]]))
15276+ kw_exprs.push(@nd_expression[elems[ek]])
1518215277 end
1518315278 end
1518415279 ek = ek + 1
@@ -15186,7 +15281,7 @@
1518615281 end
1518715282 ak = ak + 1
1518815283 end
15189- # Get init param names from class
15284+ # Get init param names/types from class
1519015285 init_ci = find_init_class(ci)
1519115286 if init_ci < 0
1519215287 return compile_call_args(nid)
@@ -15196,10 +15291,15 @@
1519615291 return compile_call_args(nid)
1519715292 end
1519815293 all_params = @cls_meth_params[init_ci].split("|")
15294+ all_ptypes = @cls_meth_ptypes[init_ci].split("|")
1519915295 pnames = "".split(",")
15296+ ptypes = "".split(",")
1520015297 if init_idx < all_params.length
1520115298 pnames = all_params[init_idx].split(",")
1520215299 end
15300+ if init_idx < all_ptypes.length
15301+ ptypes = all_ptypes[init_idx].split(",")
15302+ end
1520315303 # Build args in param order using keyword values
1520415304 result = ""
1520515305 pk = 0
@@ -15207,17 +15307,30 @@
1520715307 if pk > 0
1520815308 result = result + ", "
1520915309 end
15310+ pt = "int"
15311+ if pk < ptypes.length
15312+ pt = ptypes[pk]
15313+ end
1521015314 found = 0
1521115315 ki = 0
1521215316 while ki < kw_names.length
1521315317 if kw_names[ki] == pnames[pk]
15214- result = result + kw_vals[ki]
15318+ expr_id = kw_exprs[ki]
15319+ if pt == "poly"
15320+ result = result + box_expr_to_poly(expr_id)
15321+ else
15322+ result = result + compile_expr(expr_id)
15323+ end
1521515324 found = 1
1521615325 end
1521715326 ki = ki + 1
1521815327 end
1521915328 if found == 0
15220- result = result + "0"
15329+ if pt == "poly"
15330+ result = result + "sp_box_nil()"
15331+ else
15332+ result = result + "0"
15333+ end
1522115334 end
1522215335 pk = pk + 1
1522315336 end