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_scope
57045704 end
57055705
5706+ # Like infer_type but resolves default "int" from unresolved ivar accessors
5707+ 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 >= 0
5712+ rt = infer_type(recv)
5713+ # If receiver type is default "int" from an unscoped parameter, try to resolve
5714+ if rt == "int" && @nd_type[recv] == "LocalVariableReadNode"
5715+ vn = @nd_name[recv]
5716+ # Check if it's a method parameter with a known type
5717+ mi = 0
5718+ while mi < @meth_names.length
5719+ pnames = @meth_param_names[mi].split(",")
5720+ ptypes = @meth_param_types[mi].split(",")
5721+ pi = 0
5722+ while pi < pnames.length
5723+ if pnames[pi] == vn && pi < ptypes.length
5724+ if ptypes[pi] != "int"
5725+ rt = ptypes[pi]
5726+ end
5727+ end
5728+ pi = pi + 1
5729+ end
5730+ mi = mi + 1
5731+ end
5732+ end
5733+ if is_obj_type(rt) == 1
5734+ bt = base_type(rt)
5735+ cname = bt[4, bt.length - 4]
5736+ ci = find_class_idx(cname)
5737+ if ci >= 0
5738+ mname = @nd_name[nid]
5739+ readers = @cls_attr_readers[ci].split(";")
5740+ rk = 0
5741+ while rk < readers.length
5742+ if readers[rk] == mname
5743+ # Resolve ivar type from initialize body
5744+ ivt = resolve_ivar_from_init(ci, "@" + mname)
5745+ if ivt != "" && ivt != "int"
5746+ return ivt
5747+ end
5748+ end
5749+ rk = rk + 1
5750+ end
5751+ end
5752+ end
5753+ end
5754+ end
5755+ at
5756+ end
5757+
5758+ # Resolve an ivar's type by scanning the initialize method body
5759+ def resolve_ivar_from_init(ci, iname)
5760+ # Check if already resolved
5761+ ivt = cls_ivar_type(ci, iname)
5762+ if ivt != "int"
5763+ return ivt
5764+ end
5765+ # Scan initialize body for @ivar = param assignments
5766+ 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 = 0
5771+ while bj < all_mnames.length
5772+ if all_mnames[bj] == "initialize"
5773+ bid = all_bodies[bj].to_i
5774+ if bid >= 0
5775+ pnames = "".split(",")
5776+ ptypes = "".split(",")
5777+ if bj < all_params.length
5778+ pnames = all_params[bj].split(",")
5779+ end
5780+ if bj < all_ptypes.length
5781+ ptypes = all_ptypes[bj].split(",")
5782+ end
5783+ # Find @ivar = param_name in initialize body
5784+ resolve_ivar_from_body(ci, bid, iname, pnames, ptypes)
5785+ ivt2 = cls_ivar_type(ci, iname)
5786+ if ivt2 != "int"
5787+ return ivt2
5788+ end
5789+ end
5790+ end
5791+ bj = bj + 1
5792+ end
5793+ ""
5794+ end
5795+
5796+ def resolve_ivar_from_body(ci, nid, iname, pnames, ptypes)
5797+ if nid < 0
5798+ return
5799+ end
5800+ if @nd_type[nid] == "InstanceVariableWriteNode"
5801+ if @nd_name[nid] == iname
5802+ expr = @nd_expression[nid]
5803+ if expr >= 0 && @nd_type[expr] == "LocalVariableReadNode"
5804+ pn = @nd_name[expr]
5805+ pi = 0
5806+ while pi < pnames.length
5807+ if pnames[pi] == pn && pi < ptypes.length
5808+ pt = ptypes[pi]
5809+ if pt != "int"
5810+ update_ivar_type(ci, iname, pt)
5811+ end
5812+ end
5813+ pi = pi + 1
5814+ end
5815+ end
5816+ end
5817+ end
5818+ # Recurse
5819+ if @nd_body[nid] >= 0
5820+ resolve_ivar_from_body(ci, @nd_body[nid], iname, pnames, ptypes)
5821+ end
5822+ stmts = parse_id_list(@nd_stmts[nid])
5823+ k = 0
5824+ while k < stmts.length
5825+ resolve_ivar_from_body(ci, stmts[k], iname, pnames, ptypes)
5826+ k = k + 1
5827+ end
5828+ end
5829+
57065830 def detect_poly_params
57075831 # Scan all call sites to detect functions called with different param types
57085832 stmts = get_body_stmts(@root_id)
@@ -5728,7 +5852,7 @@
57285852 ptypes = @meth_param_types[mi].split(",")
57295853 k = 0
57305854 while k < arg_ids.length
5731- at = infer_type(arg_ids[k])
5855+ at = infer_type_deep(arg_ids[k])
57325856 if k < ptypes.length
57335857 ct = ptypes[k]
57345858 # Skip rest/splat params (int_array) - they handle multiple args
@@ -6190,14 +6314,15 @@
61906314 infer_main_call_types
61916315 infer_function_body_call_types
61926316 infer_class_body_call_types
6193- detect_poly_params
61946317 detect_poly_locals
6195- # Re-infer return types after main call type fixes
6196- infer_all_returns
6197- # Update ivar types from writer calls (after return types are known)
6198- infer_ivar_types_from_writers
6199- # Re-infer again with updated ivar types
6200- infer_all_returns
6318+ # Iterative type inference: converge param types, return types, ivar types
6319+ iter = 0
6320+ while iter < 4
6321+ infer_all_returns
6322+ infer_ivar_types_from_writers
6323+ detect_poly_params
6324+ iter = iter + 1
6325+ end
62016326 # Fix lambda return types based on call-site usage
62026327 fix_lambda_return_types
62036328 detect_features