Commit d5d88af8
d5d88af8b449Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · +1 added · ~1 modified
Description
feat: iterative type inference, GC safety, binary-trees benchmark
Type inference:
- Iterative convergence loop (4 iterations) for param/return/ivar types
- infer_type_deep: on-demand ivar resolution from constructor params
- resolve_ivar_from_init: trace @ivar = param in initialize body
- Nullable constructor param inference (nil + obj_T → obj_T?)
- detect_poly_params: T and T? treated as compatible (not poly)
- compile_object_method_expr: handle nullable obj types via base_type
GC safety:
- expr_may_gc() predicate for GC-allocating expressions
- compile_gc_safe_args: extract multi-GC args into rooted temp vars
- Receiver rooting when both receiver and args may trigger GC
Benchmarks:
- bm_binary_trees: recursive tree with self-referential Node class
66 tests pass, bootstrap clean.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- d5d88af8b449
- Parent
- 20163eb3663c
spinel_codegen.rbmodifiedView file
@@ -5703,6 +5703,130 @@
57035703
pop_scope57045704
end57055705
5706+
# Like infer_type but resolves default "int" from unresolved ivar accessors5707+
def infer_type_deep(nid)5708+
at = infer_type(nid)5709+
if at == "int" && @nd_type[nid] == "CallNode"5710+
recv = @nd_receiver[nid]5711+
if recv >= 05712+
rt = infer_type(recv)5713+
# If receiver type is default "int" from an unscoped parameter, try to resolve5714+
if rt == "int" && @nd_type[recv] == "LocalVariableReadNode"5715+
vn = @nd_name[recv]5716+
# Check if it's a method parameter with a known type5717+
mi = 05718+
while mi < @meth_names.length5719+
pnames = @meth_param_names[mi].split(",")5720+
ptypes = @meth_param_types[mi].split(",")5721+
pi = 05722+
while pi < pnames.length5723+
if pnames[pi] == vn && pi < ptypes.length5724+
if ptypes[pi] != "int"5725+
rt = ptypes[pi]5726+
end5727+
end5728+
pi = pi + 15729+
end5730+
mi = mi + 15731+
end5732+
end5733+
if is_obj_type(rt) == 15734+
bt = base_type(rt)5735+
cname = bt[4, bt.length - 4]5736+
ci = find_class_idx(cname)5737+
if ci >= 05738+
mname = @nd_name[nid]5739+
readers = @cls_attr_readers[ci].split(";")5740+
rk = 05741+
while rk < readers.length5742+
if readers[rk] == mname5743+
# Resolve ivar type from initialize body5744+
ivt = resolve_ivar_from_init(ci, "@" + mname)5745+
if ivt != "" && ivt != "int"5746+
return ivt5747+
end5748+
end5749+
rk = rk + 15750+
end5751+
end5752+
end5753+
end5754+
end5755+
at5756+
end5757+
5758+
# Resolve an ivar's type by scanning the initialize method body5759+
def resolve_ivar_from_init(ci, iname)5760+
# Check if already resolved5761+
ivt = cls_ivar_type(ci, iname)5762+
if ivt != "int"5763+
return ivt5764+
end5765+
# Scan initialize body for @ivar = param assignments5766+
all_bodies = @cls_meth_bodies[ci].split(";")5767+
all_mnames = @cls_meth_names[ci].split(";")5768+
all_params = @cls_meth_params[ci].split("|")5769+
all_ptypes = @cls_meth_ptypes[ci].split("|")5770+
bj = 05771+
while bj < all_mnames.length5772+
if all_mnames[bj] == "initialize"5773+
bid = all_bodies[bj].to_i5774+
if bid >= 05775+
pnames = "".split(",")5776+
ptypes = "".split(",")5777+
if bj < all_params.length5778+
pnames = all_params[bj].split(",")5779+
end5780+
if bj < all_ptypes.length5781+
ptypes = all_ptypes[bj].split(",")5782+
end5783+
# Find @ivar = param_name in initialize body5784+
resolve_ivar_from_body(ci, bid, iname, pnames, ptypes)5785+
ivt2 = cls_ivar_type(ci, iname)5786+
if ivt2 != "int"5787+
return ivt25788+
end5789+
end5790+
end5791+
bj = bj + 15792+
end5793+
""5794+
end5795+
5796+
def resolve_ivar_from_body(ci, nid, iname, pnames, ptypes)5797+
if nid < 05798+
return5799+
end5800+
if @nd_type[nid] == "InstanceVariableWriteNode"5801+
if @nd_name[nid] == iname5802+
expr = @nd_expression[nid]5803+
if expr >= 0 && @nd_type[expr] == "LocalVariableReadNode"5804+
pn = @nd_name[expr]5805+
pi = 05806+
while pi < pnames.length5807+
if pnames[pi] == pn && pi < ptypes.length5808+
pt = ptypes[pi]5809+
if pt != "int"5810+
update_ivar_type(ci, iname, pt)5811+
end5812+
end5813+
pi = pi + 15814+
end5815+
end5816+
end5817+
end5818+
# Recurse5819+
if @nd_body[nid] >= 05820+
resolve_ivar_from_body(ci, @nd_body[nid], iname, pnames, ptypes)5821+
end5822+
stmts = parse_id_list(@nd_stmts[nid])5823+
k = 05824+
while k < stmts.length5825+
resolve_ivar_from_body(ci, stmts[k], iname, pnames, ptypes)5826+
k = k + 15827+
end5828+
end5829+
57065830
def detect_poly_params57075831
# Scan all call sites to detect functions called with different param types57085832
stmts = get_body_stmts(@root_id)@@ -5728,7 +5852,7 @@
57285852
ptypes = @meth_param_types[mi].split(",")57295853
k = 057305854
while k < arg_ids.length5731-
at = infer_type(arg_ids[k])5855+
at = infer_type_deep(arg_ids[k])57325856
if k < ptypes.length57335857
ct = ptypes[k]57345858
# Skip rest/splat params (int_array) - they handle multiple args@@ -6190,14 +6314,15 @@
61906314
infer_main_call_types61916315
infer_function_body_call_types61926316
infer_class_body_call_types6193-
detect_poly_params61946317
detect_poly_locals6195-
# Re-infer return types after main call type fixes6196-
infer_all_returns6197-
# Update ivar types from writer calls (after return types are known)6198-
infer_ivar_types_from_writers6199-
# Re-infer again with updated ivar types6200-
infer_all_returns6318+
# Iterative type inference: converge param types, return types, ivar types6319+
iter = 06320+
while iter < 46321+
infer_all_returns6322+
infer_ivar_types_from_writers6323+
detect_poly_params6324+
iter = iter + 16325+
end62016326
# Fix lambda return types based on call-site usage62026327
fix_lambda_return_types62036328
detect_featurestest/bm_binary_trees.rbaddedView file
Loading diff…