Commit b6690c81
b6690c81b492Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified
Description
Merge pull request #168 from khasinski/feat/instance-eval-method-params
feat(codegen): instance_eval { } accepts method-param receivers
Details
- Commit
- b6690c81b492
- Parent
- 1aaa5feed711
- Merge parent
- b832a313791f
spinel_codegen.rbmodifiedView file
@@ -4465,6 +4465,12 @@
44654465
44664466
def rewrite_instance_eval_calls44674467
@ieval_counter = 04468+
# Widen class-method ptypes through obj-typed receivers before the4469+
# walk so a method-param receiver (e.g. `def configure(app);4470+
# app.instance_eval { } end` invoked as `cfg.configure(routes)`)4471+
# has `app` typed as obj_<C>. Surgical fork — see the helper for4472+
# why we don't just call scan_new_calls here.4473+
propagate_recv_method_arg_types_for_ieval44684474
local_class = {}44694475
# Walk the AST recursively from the root, respecting scope boundaries.44704476
# `local_class` maps `name -> class_idx` for the current scope only.@@ -4487,17 +4493,60 @@
44874493
# `cls_ivar_type`. Class methods (singleton-side) are intentionally44884494
# excluded: they don't see the instance's @ivars, and `self` rebinding44894495
# against a class object would be a different (singleton-class) lift.4496+
#4497+
# Per-method scope: declare each method's params (with the ptypes4498+
# widened by infer_param_types_from_callsites at Pass 2.55) and the4499+
# body locals from scan_locals_first_type, so a LocalVariableReadNode4500+
# receiver inside the body can resolve its class via find_var_type.4501+
# That covers `def configure(app); app.instance_eval { } end` and4502+
# also method-local copies whose RHS is statically classifiable4503+
# (e.g., `routes = Routes.new`). Method returns whose call type4504+
# depends on infer_all_returns having run are still out of reach4505+
# at this Pass 2.6 timing — that's the next follow-up.44904506
def ieval_walk_class_methods44914507
ci = 044924508
while ci < @cls_names.length44934509
@current_class_idx = ci44944510
bodies = @cls_meth_bodies[ci].split(";")4511+
all_params = @cls_meth_params[ci].split("|")4512+
all_ptypes = @cls_meth_ptypes[ci].split("|")44954513
bj = 044964514
while bj < bodies.length44974515
bid = bodies[bj].to_i44984516
if bid >= 04517+
push_scope4518+
pnames = "".split(",")4519+
ptypes = "".split(",")4520+
if bj < all_params.length4521+
pnames = all_params[bj].split(",")4522+
end4523+
if bj < all_ptypes.length4524+
ptypes = all_ptypes[bj].split(",")4525+
end4526+
k = 04527+
while k < pnames.length4528+
pt = "int"4529+
if k < ptypes.length4530+
pt = ptypes[k]4531+
end4532+
declare_var(pnames[k], pt)4533+
k = k + 14534+
end4535+
# Body locals: scan_locals_first_type matches what4536+
# infer_all_returns does in its class-methods preamble4537+
# (Pass 3). Pulls in `routes = Routes.new` with type4538+
# obj_Routes when the RHS is statically classifiable.4539+
lnames = "".split(",")4540+
ltypes = "".split(",")4541+
scan_locals_first_type(bid, lnames, ltypes, pnames)4542+
lk = 04543+
while lk < lnames.length4544+
declare_var(lnames[lk], ltypes[lk])4545+
lk = lk + 14546+
end44994547
empty_locals = {}45004548
ieval_walk(bid, empty_locals)4549+
pop_scope45014550
end45024551
bj = bj + 145034552
end@@ -4659,6 +4708,18 @@
46594708
vname = @nd_name[recv]46604709
if local_class.key?(vname)46614710
ci = local_class[vname]4711+
else4712+
# Inside a class instance method, the v1 top-level local_class4713+
# map is intentionally empty. Fall back to find_var_type so a4714+
# method param (or scan_locals-typed local) resolves through4715+
# the scope chain that ieval_walk_class_methods sets up. The4716+
# is_obj_type / base_type strip is the same shape used in the4717+
# ivar branch and at every other obj_-prefix site in this file.4718+
vt = find_var_type(vname)4719+
bt = base_type(vt)4720+
if is_obj_type(bt) == 14721+
ci = find_class_idx(bt[4, bt.length - 4])4722+
end46624723
end46634724
elsif @nd_type[recv] == "InstanceVariableReadNode"46644725
# `@ivar.instance_eval { }` inside a class method. ieval_walk_class_methods@@ -6270,6 +6331,133 @@
62706331
scan_new_calls(@root_id)62716332
end62726333
6334+
# Narrow pre-pass for `rewrite_instance_eval_calls`: walk top-level6335+
# CallNodes shaped `recv.method(args)` where recv resolves to an6336+
# obj_<C> via top-level scope, and let scan_new_calls' receiver-method6337+
# branch widen the class method's ptypes. Without this, a method-param6338+
# receiver inside `def configure(app); app.instance_eval { } end`6339+
# has `app` ptype stuck at "int" at Pass 2.6 time — the existing6340+
# `infer_main_call_types` does the same scope-wrap but only runs in6341+
# `compile()` after `collect_all` returns.6342+
#6343+
# Why not reuse `infer_main_call_types`: that pass also runs the6344+
# top-level-method branch and the constructor branch, both of which6345+
# detect_poly_params later refines via different rules. Running them6346+
# twice (once at Pass 2.55, once in compile()) re-orders the inputs6347+
# detect_poly_params sees and demonstrably regresses6348+
# `test/poly_dispatch_builtin_all.rb` (lenof's poly param drops to6349+
# int_array when the early run primes ptypes ahead of the iterative6350+
# loop). This pre-pass scans the same AST but skips both other6351+
# branches; it only widens class-method ptypes through obj-typed6352+
# receivers — the exact piece rewrite_instance_eval_calls needs.6353+
def propagate_recv_method_arg_types_for_ieval6354+
push_scope6355+
if @nd_type[@root_id] == "ProgramNode"6356+
tl_body = @nd_body[@root_id]6357+
if tl_body >= 06358+
empty_params = "".split(",")6359+
tl_lnames = "".split(",")6360+
tl_ltypes = "".split(",")6361+
scan_locals_first_type(tl_body, tl_lnames, tl_ltypes, empty_params)6362+
ti = 06363+
while ti < tl_lnames.length6364+
declare_var(tl_lnames[ti], tl_ltypes[ti])6365+
ti = ti + 16366+
end6367+
end6368+
end6369+
walk_recv_method_calls(@root_id)6370+
pop_scope6371+
end6372+
6373+
# Surgical fork of scan_new_calls: only the `obj.method(args)` branch,6374+
# only when `obj`'s static type is obj_<C>. Mirrors the cls_meth_ptypes6375+
# widening at lines ~6603-6615 of scan_new_calls (the same int->concrete6376+
# promotion gate) and falls through to recursion. Other branches of6377+
# scan_new_calls (constructor and top-level method) are deliberately6378+
# absent — running them earlier than master's compile() pipeline6379+
# interacts badly with detect_poly_params (see commentary above).6380+
def walk_recv_method_calls(nid)6381+
if nid < 06382+
return6383+
end6384+
if @nd_type[nid] == "CallNode"6385+
mname = @nd_name[nid]6386+
recv = @nd_receiver[nid]6387+
if recv >= 06388+
rt = infer_type(recv)6389+
if is_obj_type(rt) == 16390+
cname = rt[4, rt.length - 4]6391+
ci = find_class_idx(cname)6392+
if ci >= 06393+
owner_ci = ci6394+
midx = cls_find_method_direct(ci, mname)6395+
if midx < 06396+
owner = find_method_owner(ci, mname)6397+
if owner != "" && owner != cname6398+
owner_ci = find_class_idx(owner)6399+
if owner_ci >= 06400+
midx = cls_find_method_direct(owner_ci, mname)6401+
end6402+
end6403+
end6404+
if midx >= 06405+
args_id = @nd_arguments[nid]6406+
if args_id >= 06407+
arg_ids = get_args(args_id)6408+
all_ptypes = @cls_meth_ptypes[owner_ci].split("|")6409+
if midx < all_ptypes.length6410+
ptypes = all_ptypes[midx].split(",")6411+
kk = 06412+
while kk < arg_ids.length6413+
at = infer_type(arg_ids[kk])6414+
if kk < ptypes.length6415+
if ptypes[kk] == "int"6416+
if at != "int"6417+
ptypes[kk] = at6418+
end6419+
end6420+
end6421+
kk = kk + 16422+
end6423+
all_ptypes[midx] = ptypes.join(",")6424+
@cls_meth_ptypes[owner_ci] = all_ptypes.join("|")6425+
end6426+
end6427+
end6428+
end6429+
end6430+
end6431+
end6432+
walk_recv_method_calls(@nd_body[nid])6433+
stmts = parse_id_list(@nd_stmts[nid])6434+
k = 06435+
while k < stmts.length6436+
walk_recv_method_calls(stmts[k])6437+
k = k + 16438+
end6439+
walk_recv_method_calls(@nd_expression[nid])6440+
walk_recv_method_calls(@nd_arguments[nid])6441+
args = parse_id_list(@nd_args[nid])6442+
k = 06443+
while k < args.length6444+
walk_recv_method_calls(args[k])6445+
k = k + 16446+
end6447+
walk_recv_method_calls(@nd_predicate[nid])6448+
walk_recv_method_calls(@nd_subsequent[nid])6449+
walk_recv_method_calls(@nd_else_clause[nid])6450+
walk_recv_method_calls(@nd_rescue_clause[nid])6451+
walk_recv_method_calls(@nd_ensure_clause[nid])6452+
walk_recv_method_calls(@nd_receiver[nid])6453+
conds = parse_id_list(@nd_conditions[nid])6454+
k = 06455+
while k < conds.length6456+
walk_recv_method_calls(conds[k])6457+
k = k + 16458+
end6459+
end6460+
62736461
# Merge `at` (inferred from a new call-site argument) into the62746462
# accumulated ctor param type `old_pt`. "int" is normally treated as62756463
# a fallback/placeholder (many unresolved reads default to it), but atest/bm_instance_eval.rbmodifiedView file
Loading diff…