Commit d611dded
d611dded7ab1Merged
Chris Hasiński <[email protected]> · 6 months ago · 2 files · ~2 modified
Description
feat(codegen): instance_eval { } accepts ivar receivers
Extends PR #15's recv.instance_eval { block } lift to handle @ivar.instance_eval { ... } sites inside class instance methods. First of three planned follow-ups widening the v1 receiver gate (ivars / params / method returns).
How it works:
- After the v1 top-level walk completes, ieval_walk_class_methods visits each class's instance method bodies with @current_class_idx set. ieval_rewrite_call gains an InstanceVariableReadNode branch that resolves recv via cls_ivar_type → find_class_idx, the same is_obj_type / base_type strip pattern used elsewhere.
- The local_class map stays unthreaded into method bodies. Method params and method-local copies of class instances are out of scope here; they're handled by the next two follow-ups.
- emit_forward_decls now emits sp_ieval_ prototypes. v1's call sites all sat below the function definitions in the C output (both landed in main / post-class-methods); ivar receivers put the call inside a class method body that emits before emit_ieval_funcs, so the prototypes are needed.
- infer_call_type gains a _sp_ieval early case. v1's lift was always called in statement position so the warn-fallback "int" was harmless; a class method whose tail expression is an ivar instance_eval now needs its return type to match the value the comma-expression form returns (the receiver). The synthetic id's registered class is read directly from @ieval_class_idxs rather than re-inferring recv.
Disjoint with PR #124's instance_eval(&b) trampoline: that detector fires on receiverless instance_eval inside def m(&b); ...; end. This path still requires both a literal receiver and a literal block. AST shapes don't intersect.
Verified:
- Bootstrap closes (gen2.c == gen3.c).
- make test: 199 pass, 0 fail, 0 error.
- make bench: 56 pass, 0 fail.
- test/bm_instance_eval.rb gains two sections: ivar receiver in a void-returning class method, and ivar receiver in tail position (where the comma-expression return-value path matters).
Follow-ups (separate PRs):
- Method-param receivers: needs caller-side arg-type propagation with top-level locals in scope.
- Method-return receivers: largely free once the type system sees
past
recv.instance_eval { }in tail position.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- d611dded7ab1
- Parent
- c27a063c7115
spinel_codegen.rbmodifiedView file
@@ -1765,6 +1765,27 @@
17651765
mname = @nd_name[nid]17661766
recv = @nd_receiver[nid]17671767
1768+
# `recv.__sp_ieval_<N>(...)`: the rewritten form of an1769+
# `recv.instance_eval { ... }` call. v1 only fired on top-level call1770+
# sites, where the call's value was always discarded — so its return1771+
# type didn't matter and the warn-fallback "int" was harmless. Now1772+
# that the rewrite can land inside a class method body whose tail1773+
# expression IS the instance_eval call, the enclosing method's1774+
# signature has to match the value the lift actually emits:1775+
# `compile_ieval_call_expr` returns the receiver via a comma1776+
# expression, so the type is recv's class. Read the synthetic id's1777+
# registered class directly from `@ieval_class_idxs` rather than1778+
# re-inferring recv — by the time this runs (compile-side type1779+
# iteration), recv's type may have been refined and the registered1780+
# class is the canonical answer.1781+
if is_ieval_call_name(mname) == 11782+
suffix = mname[11, mname.length - 11]1783+
n = suffix.to_i1784+
if n >= 0 && n < @ieval_class_idxs.length1785+
return "obj_" + @cls_names[@ieval_class_idxs[n]]1786+
end1787+
end1788+
17681789
# Issue #126: chain return type for `Module.accessor.<method>`.17691790
# All resolved candidates' class methods should agree on a return17701791
# type; if they disagree the chain becomes poly. Returning early@@ -4414,11 +4435,42 @@
44144435
local_class = {}44154436
# Walk the AST recursively from the root, respecting scope boundaries.44164437
# `local_class` maps `name -> class_idx` for the current scope only.4417-
# Method/lambda/class/module/block bodies are NOT entered: their4418-
# locals belong to a different scope, so the top-level map must not4419-
# apply to them. A reassignment to a non-`Class.new` RHS poisons the4420-
# mapping for that name.4438+
# Method/lambda/module/block bodies are NOT entered for local tracking:4439+
# their locals belong to a different scope. A reassignment to a4440+
# non-`Class.new` RHS poisons the mapping for that name. ClassNode4441+
# bodies are visited for the side effect of walking each instance4442+
# method's body with `@current_class_idx` set, so an `@ivar.instance_eval { }`4443+
# site inside a class method can resolve its receiver class via4444+
# `cls_ivar_type`. The local_class map is intentionally not threaded4445+
# into method bodies — locals there are out of scope, and the4446+
# ivar-only extension does not (yet) try to type method-local copies4447+
# of class instances.44214448
ieval_walk(@root_id, local_class)4449+
ieval_walk_class_methods4450+
end4451+
4452+
# Visit each class's instance-method bodies with `@current_class_idx`4453+
# set, so `@ivar.instance_eval { ... }` resolves recv's class through4454+
# `cls_ivar_type`. Class methods (singleton-side) are intentionally4455+
# excluded: they don't see the instance's @ivars, and `self` rebinding4456+
# against a class object would be a different (singleton-class) lift.4457+
def ieval_walk_class_methods4458+
ci = 04459+
while ci < @cls_names.length4460+
@current_class_idx = ci4461+
bodies = @cls_meth_bodies[ci].split(";")4462+
bj = 04463+
while bj < bodies.length4464+
bid = bodies[bj].to_i4465+
if bid >= 04466+
empty_locals = {}4467+
ieval_walk(bid, empty_locals)4468+
end4469+
bj = bj + 14470+
end4471+
@current_class_idx = -14472+
ci = ci + 14473+
end44224474
end44234475
44244476
def ieval_walk(nid, local_class)@@ -4569,14 +4621,30 @@
45694621
if @nd_parameters[blk] >= 045704622
return45714623
end4572-
if @nd_type[recv] != "LocalVariableReadNode"4573-
return4624+
ci = -14625+
if @nd_type[recv] == "LocalVariableReadNode"4626+
vname = @nd_name[recv]4627+
if local_class.key?(vname)4628+
ci = local_class[vname]4629+
end4630+
elsif @nd_type[recv] == "InstanceVariableReadNode"4631+
# `@ivar.instance_eval { }` inside a class method. ieval_walk_class_methods4632+
# sets @current_class_idx so cls_ivar_type returns the ivar's stored4633+
# type — "obj_<Class>" when the ivar was bound to `Class.new` (and4634+
# not since widened to poly). Strip the "obj_" prefix to look up4635+
# the class index, the same shape `is_obj_type` / `base_type`4636+
# gates use elsewhere in the codegen for object-typed values.4637+
if @current_class_idx >= 04638+
it = cls_ivar_type(@current_class_idx, @nd_name[recv])4639+
bt = base_type(it)4640+
if is_obj_type(bt) == 14641+
ci = find_class_idx(bt[4, bt.length - 4])4642+
end4643+
end45744644
end4575-
vname = @nd_name[recv]4576-
if local_class.key?(vname) == false4645+
if ci < 045774646
return45784647
end4579-
ci = local_class[vname]45804648
body_id = @nd_body[blk]45814649
# v1: bail if the block uses yield/block_given?. Lifting it as a45824650
# plain function would lose the enclosing method's block plumbing.@@ -11441,6 +11509,23 @@
1144111509
end1144211510
i = i + 11144311511
end11512+
# Hoisted instance_eval block functions: emit prototypes here. v111513+
# fired only on top-level call sites, where the call always followed11514+
# the corresponding `static void sp_ieval_<N>(...)` definition (both11515+
# land in `emit_main` / after `emit_class_methods`). Now that ivar11516+
# receivers can put the call inside a class method body — emitted11517+
# before `emit_ieval_funcs` runs — the call would be an implicit11518+
# declaration without these prototypes.11519+
n = 011520+
while n < @ieval_class_idxs.length11521+
icn = @cls_names[@ieval_class_idxs[n]]11522+
if @cls_is_value_type[@ieval_class_idxs[n]] == 111523+
emit_raw("static void sp_ieval_" + n.to_s + "(sp_" + icn + " self);")11524+
else11525+
emit_raw("static void sp_ieval_" + n.to_s + "(sp_" + icn + " *self);")11526+
end11527+
n = n + 111528+
end1144411529
emit_raw("")1144511530
end1144611531
test/bm_instance_eval.rbmodifiedView file
Loading diff…