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_calls
44674467 @ieval_counter = 0
4468+ # Widen class-method ptypes through obj-typed receivers before the
4469+ # 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 for
4472+ # why we don't just call scan_new_calls here.
4473+ propagate_recv_method_arg_types_for_ieval
44684474 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 intentionally
44884494 # excluded: they don't see the instance's @ivars, and `self` rebinding
44894495 # against a class object would be a different (singleton-class) lift.
4496+ #
4497+ # Per-method scope: declare each method's params (with the ptypes
4498+ # widened by infer_param_types_from_callsites at Pass 2.55) and the
4499+ # body locals from scan_locals_first_type, so a LocalVariableReadNode
4500+ # receiver inside the body can resolve its class via find_var_type.
4501+ # That covers `def configure(app); app.instance_eval { } end` and
4502+ # also method-local copies whose RHS is statically classifiable
4503+ # (e.g., `routes = Routes.new`). Method returns whose call type
4504+ # depends on infer_all_returns having run are still out of reach
4505+ # at this Pass 2.6 timing — that's the next follow-up.
44904506 def ieval_walk_class_methods
44914507 ci = 0
44924508 while ci < @cls_names.length
44934509 @current_class_idx = ci
44944510 bodies = @cls_meth_bodies[ci].split(";")
4511+ all_params = @cls_meth_params[ci].split("|")
4512+ all_ptypes = @cls_meth_ptypes[ci].split("|")
44954513 bj = 0
44964514 while bj < bodies.length
44974515 bid = bodies[bj].to_i
44984516 if bid >= 0
4517+ push_scope
4518+ pnames = "".split(",")
4519+ ptypes = "".split(",")
4520+ if bj < all_params.length
4521+ pnames = all_params[bj].split(",")
4522+ end
4523+ if bj < all_ptypes.length
4524+ ptypes = all_ptypes[bj].split(",")
4525+ end
4526+ k = 0
4527+ while k < pnames.length
4528+ pt = "int"
4529+ if k < ptypes.length
4530+ pt = ptypes[k]
4531+ end
4532+ declare_var(pnames[k], pt)
4533+ k = k + 1
4534+ end
4535+ # Body locals: scan_locals_first_type matches what
4536+ # infer_all_returns does in its class-methods preamble
4537+ # (Pass 3). Pulls in `routes = Routes.new` with type
4538+ # 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 = 0
4543+ while lk < lnames.length
4544+ declare_var(lnames[lk], ltypes[lk])
4545+ lk = lk + 1
4546+ end
44994547 empty_locals = {}
45004548 ieval_walk(bid, empty_locals)
4549+ pop_scope
45014550 end
45024551 bj = bj + 1
45034552 end
@@ -4659,6 +4708,18 @@
46594708 vname = @nd_name[recv]
46604709 if local_class.key?(vname)
46614710 ci = local_class[vname]
4711+ else
4712+ # Inside a class instance method, the v1 top-level local_class
4713+ # map is intentionally empty. Fall back to find_var_type so a
4714+ # method param (or scan_locals-typed local) resolves through
4715+ # the scope chain that ieval_walk_class_methods sets up. The
4716+ # is_obj_type / base_type strip is the same shape used in the
4717+ # 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) == 1
4721+ ci = find_class_idx(bt[4, bt.length - 4])
4722+ end
46624723 end
46634724 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 end
62726333
6334+ # Narrow pre-pass for `rewrite_instance_eval_calls`: walk top-level
6335+ # CallNodes shaped `recv.method(args)` where recv resolves to an
6336+ # obj_<C> via top-level scope, and let scan_new_calls' receiver-method
6337+ # branch widen the class method's ptypes. Without this, a method-param
6338+ # receiver inside `def configure(app); app.instance_eval { } end`
6339+ # has `app` ptype stuck at "int" at Pass 2.6 time — the existing
6340+ # `infer_main_call_types` does the same scope-wrap but only runs in
6341+ # `compile()` after `collect_all` returns.
6342+ #
6343+ # Why not reuse `infer_main_call_types`: that pass also runs the
6344+ # top-level-method branch and the constructor branch, both of which
6345+ # detect_poly_params later refines via different rules. Running them
6346+ # twice (once at Pass 2.55, once in compile()) re-orders the inputs
6347+ # detect_poly_params sees and demonstrably regresses
6348+ # `test/poly_dispatch_builtin_all.rb` (lenof's poly param drops to
6349+ # int_array when the early run primes ptypes ahead of the iterative
6350+ # loop). This pre-pass scans the same AST but skips both other
6351+ # branches; it only widens class-method ptypes through obj-typed
6352+ # receivers — the exact piece rewrite_instance_eval_calls needs.
6353+ def propagate_recv_method_arg_types_for_ieval
6354+ push_scope
6355+ if @nd_type[@root_id] == "ProgramNode"
6356+ tl_body = @nd_body[@root_id]
6357+ if tl_body >= 0
6358+ 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 = 0
6363+ while ti < tl_lnames.length
6364+ declare_var(tl_lnames[ti], tl_ltypes[ti])
6365+ ti = ti + 1
6366+ end
6367+ end
6368+ end
6369+ walk_recv_method_calls(@root_id)
6370+ pop_scope
6371+ end
6372+
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_ptypes
6375+ # widening at lines ~6603-6615 of scan_new_calls (the same int->concrete
6376+ # promotion gate) and falls through to recursion. Other branches of
6377+ # scan_new_calls (constructor and top-level method) are deliberately
6378+ # absent — running them earlier than master's compile() pipeline
6379+ # interacts badly with detect_poly_params (see commentary above).
6380+ def walk_recv_method_calls(nid)
6381+ if nid < 0
6382+ return
6383+ end
6384+ if @nd_type[nid] == "CallNode"
6385+ mname = @nd_name[nid]
6386+ recv = @nd_receiver[nid]
6387+ if recv >= 0
6388+ rt = infer_type(recv)
6389+ if is_obj_type(rt) == 1
6390+ cname = rt[4, rt.length - 4]
6391+ ci = find_class_idx(cname)
6392+ if ci >= 0
6393+ owner_ci = ci
6394+ midx = cls_find_method_direct(ci, mname)
6395+ if midx < 0
6396+ owner = find_method_owner(ci, mname)
6397+ if owner != "" && owner != cname
6398+ owner_ci = find_class_idx(owner)
6399+ if owner_ci >= 0
6400+ midx = cls_find_method_direct(owner_ci, mname)
6401+ end
6402+ end
6403+ end
6404+ if midx >= 0
6405+ args_id = @nd_arguments[nid]
6406+ if args_id >= 0
6407+ arg_ids = get_args(args_id)
6408+ all_ptypes = @cls_meth_ptypes[owner_ci].split("|")
6409+ if midx < all_ptypes.length
6410+ ptypes = all_ptypes[midx].split(",")
6411+ kk = 0
6412+ while kk < arg_ids.length
6413+ at = infer_type(arg_ids[kk])
6414+ if kk < ptypes.length
6415+ if ptypes[kk] == "int"
6416+ if at != "int"
6417+ ptypes[kk] = at
6418+ end
6419+ end
6420+ end
6421+ kk = kk + 1
6422+ end
6423+ all_ptypes[midx] = ptypes.join(",")
6424+ @cls_meth_ptypes[owner_ci] = all_ptypes.join("|")
6425+ end
6426+ end
6427+ end
6428+ end
6429+ end
6430+ end
6431+ end
6432+ walk_recv_method_calls(@nd_body[nid])
6433+ stmts = parse_id_list(@nd_stmts[nid])
6434+ k = 0
6435+ while k < stmts.length
6436+ walk_recv_method_calls(stmts[k])
6437+ k = k + 1
6438+ end
6439+ 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 = 0
6443+ while k < args.length
6444+ walk_recv_method_calls(args[k])
6445+ k = k + 1
6446+ end
6447+ 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 = 0
6455+ while k < conds.length
6456+ walk_recv_method_calls(conds[k])
6457+ k = k + 1
6458+ end
6459+ end
6460+
62736461 # Merge `at` (inferred from a new call-site argument) into the
62746462 # accumulated ctor param type `old_pt`. "int" is normally treated as
62756463 # a fallback/placeholder (many unresolved reads default to it), but a