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 an
1769+ # `recv.instance_eval { ... }` call. v1 only fired on top-level call
1770+ # sites, where the call's value was always discarded — so its return
1771+ # type didn't matter and the warn-fallback "int" was harmless. Now
1772+ # that the rewrite can land inside a class method body whose tail
1773+ # expression IS the instance_eval call, the enclosing method's
1774+ # signature has to match the value the lift actually emits:
1775+ # `compile_ieval_call_expr` returns the receiver via a comma
1776+ # expression, so the type is recv's class. Read the synthetic id's
1777+ # registered class directly from `@ieval_class_idxs` rather than
1778+ # re-inferring recv — by the time this runs (compile-side type
1779+ # iteration), recv's type may have been refined and the registered
1780+ # class is the canonical answer.
1781+ if is_ieval_call_name(mname) == 1
1782+ suffix = mname[11, mname.length - 11]
1783+ n = suffix.to_i
1784+ if n >= 0 && n < @ieval_class_idxs.length
1785+ return "obj_" + @cls_names[@ieval_class_idxs[n]]
1786+ end
1787+ end
1788+
17681789 # Issue #126: chain return type for `Module.accessor.<method>`.
17691790 # All resolved candidates' class methods should agree on a return
17701791 # 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: their
4418- # locals belong to a different scope, so the top-level map must not
4419- # apply to them. A reassignment to a non-`Class.new` RHS poisons the
4420- # 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 a
4440+ # non-`Class.new` RHS poisons the mapping for that name. ClassNode
4441+ # bodies are visited for the side effect of walking each instance
4442+ # method's body with `@current_class_idx` set, so an `@ivar.instance_eval { }`
4443+ # site inside a class method can resolve its receiver class via
4444+ # `cls_ivar_type`. The local_class map is intentionally not threaded
4445+ # into method bodies — locals there are out of scope, and the
4446+ # ivar-only extension does not (yet) try to type method-local copies
4447+ # of class instances.
44214448 ieval_walk(@root_id, local_class)
4449+ ieval_walk_class_methods
4450+ end
4451+
4452+ # Visit each class's instance-method bodies with `@current_class_idx`
4453+ # set, so `@ivar.instance_eval { ... }` resolves recv's class through
4454+ # `cls_ivar_type`. Class methods (singleton-side) are intentionally
4455+ # excluded: they don't see the instance's @ivars, and `self` rebinding
4456+ # against a class object would be a different (singleton-class) lift.
4457+ def ieval_walk_class_methods
4458+ ci = 0
4459+ while ci < @cls_names.length
4460+ @current_class_idx = ci
4461+ bodies = @cls_meth_bodies[ci].split(";")
4462+ bj = 0
4463+ while bj < bodies.length
4464+ bid = bodies[bj].to_i
4465+ if bid >= 0
4466+ empty_locals = {}
4467+ ieval_walk(bid, empty_locals)
4468+ end
4469+ bj = bj + 1
4470+ end
4471+ @current_class_idx = -1
4472+ ci = ci + 1
4473+ end
44224474 end
44234475
44244476 def ieval_walk(nid, local_class)
@@ -4569,14 +4621,30 @@
45694621 if @nd_parameters[blk] >= 0
45704622 return
45714623 end
4572- if @nd_type[recv] != "LocalVariableReadNode"
4573- return
4624+ ci = -1
4625+ if @nd_type[recv] == "LocalVariableReadNode"
4626+ vname = @nd_name[recv]
4627+ if local_class.key?(vname)
4628+ ci = local_class[vname]
4629+ end
4630+ elsif @nd_type[recv] == "InstanceVariableReadNode"
4631+ # `@ivar.instance_eval { }` inside a class method. ieval_walk_class_methods
4632+ # sets @current_class_idx so cls_ivar_type returns the ivar's stored
4633+ # type — "obj_<Class>" when the ivar was bound to `Class.new` (and
4634+ # not since widened to poly). Strip the "obj_" prefix to look up
4635+ # 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 >= 0
4638+ it = cls_ivar_type(@current_class_idx, @nd_name[recv])
4639+ bt = base_type(it)
4640+ if is_obj_type(bt) == 1
4641+ ci = find_class_idx(bt[4, bt.length - 4])
4642+ end
4643+ end
45744644 end
4575- vname = @nd_name[recv]
4576- if local_class.key?(vname) == false
4645+ if ci < 0
45774646 return
45784647 end
4579- ci = local_class[vname]
45804648 body_id = @nd_body[blk]
45814649 # v1: bail if the block uses yield/block_given?. Lifting it as a
45824650 # plain function would lose the enclosing method's block plumbing.
@@ -11441,6 +11509,23 @@
1144111509 end
1144211510 i = i + 1
1144311511 end
11512+ # Hoisted instance_eval block functions: emit prototypes here. v1
11513+ # fired only on top-level call sites, where the call always followed
11514+ # the corresponding `static void sp_ieval_<N>(...)` definition (both
11515+ # land in `emit_main` / after `emit_class_methods`). Now that ivar
11516+ # receivers can put the call inside a class method body — emitted
11517+ # before `emit_ieval_funcs` runs — the call would be an implicit
11518+ # declaration without these prototypes.
11519+ n = 0
11520+ while n < @ieval_class_idxs.length
11521+ icn = @cls_names[@ieval_class_idxs[n]]
11522+ if @cls_is_value_type[@ieval_class_idxs[n]] == 1
11523+ emit_raw("static void sp_ieval_" + n.to_s + "(sp_" + icn + " self);")
11524+ else
11525+ emit_raw("static void sp_ieval_" + n.to_s + "(sp_" + icn + " *self);")
11526+ end
11527+ n = n + 1
11528+ end
1144411529 emit_raw("")
1144511530 end
1144611531