Commit e1ffc6e4

e1ffc6e4aba0Merged

Ryan Seys <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

refactor(analyze): extract recv_class_idx_for_rebind helper

The LocalVariableReadNode / InstanceVariableReadNode cascade in ieval_rewrite_call resolves a rebind receiver to a class index using a non-trivial path: local_class map lookup, find_var_type fallback, base_type strip, cls_ivar_type for ivar receivers, obj_-prefix strip for object-typed values. Reused verbatim by the forthcoming iexec_rewrite_call; duplicating the cascade would silently diverge when one path is patched without the other.

The helper takes (recv, local_class) and returns the class index or -1. Behaviour preserved byte-identically: ieval_rewrite_call now just calls the helper instead of inlining the cascade. 504 tests pass, bootstrap fixpoint converges.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
e1ffc6e4aba0
Parent
7780fff2c6e8

spinel_analyze.rbmodifiedView file

@@ -7983,6 +7983,55 @@
79837983 -1
79847984 end
79857985
7986+ # Resolve the receiver of a rebind-and-splice call (instance_eval,
7987+ # instance_exec) to a class index using the same LocalVar / IVar
7988+ # cascade that ieval_rewrite_call shipped with. Returns -1 if the
7989+ # receiver type can't be statically classified.
7990+ #
7991+ # Factored from ieval_rewrite_call so iexec_rewrite_call (and any
7992+ # future rebind-and-splice detector) can reuse it byte-identically.
7993+ # The cascade is non-trivial -- find_var_type fallback for the
7994+ # LocalVar branch, cls_ivar_type lookup for the IVar branch with
7995+ # the obj_-prefix strip -- and duplicating it would silently
7996+ # diverge as one path is patched without the other.
7997+ def recv_class_idx_for_rebind(recv, local_class)
7998+ if @nd_type[recv] == "LocalVariableReadNode"
7999+ vname = @nd_name[recv]
8000+ if local_class.key?(vname)
8001+ return local_class[vname]
8002+ end
8003+ # Inside a class instance method, the top-level local_class
8004+ # map is intentionally empty. Fall back to find_var_type so a
8005+ # method param (or scan_locals-typed local) resolves through
8006+ # the scope chain that ieval_walk_class_methods sets up. The
8007+ # is_obj_type / base_type strip is the same shape used in the
8008+ # ivar branch and at every other obj_-prefix site in this file.
8009+ vt = find_var_type(vname)
8010+ bt = base_type(vt)
8011+ if is_obj_type(bt) == 1
8012+ return find_class_idx(bt[4, bt.length - 4])
8013+ end
8014+ return -1
8015+ end
8016+ if @nd_type[recv] == "InstanceVariableReadNode"
8017+ # `@ivar.instance_eval { }` inside a class method. ieval_walk_class_methods
8018+ # sets @current_class_idx so cls_ivar_type returns the ivar's stored
8019+ # type -- "obj_<Class>" when the ivar was bound to `Class.new` (and
8020+ # not since widened to poly). Strip the "obj_" prefix to look up
8021+ # the class index, the same shape `is_obj_type` / `base_type`
8022+ # gates use elsewhere in the codegen for object-typed values.
8023+ if @current_class_idx >= 0
8024+ it = cls_ivar_type(@current_class_idx, @nd_name[recv])
8025+ bt = base_type(it)
8026+ if is_obj_type(bt) == 1
8027+ return find_class_idx(bt[4, bt.length - 4])
8028+ end
8029+ end
8030+ return -1
8031+ end
8032+ -1
8033+ end
8034+
79868035 def ieval_rewrite_call(nid, local_class)
79878036 if @nd_name[nid] != "instance_eval"
79888037 return
@@ -8032,39 +8081,7 @@
80328081 end
80338082 end
80348083 end
8035- ci = -1
8036- if @nd_type[recv] == "LocalVariableReadNode"
8037- vname = @nd_name[recv]
8038- if local_class.key?(vname)
8039- ci = local_class[vname]
8040- else
8041- # Inside a class instance method, the v1 top-level local_class
8042- # map is intentionally empty. Fall back to find_var_type so a
8043- # method param (or scan_locals-typed local) resolves through
8044- # the scope chain that ieval_walk_class_methods sets up. The
8045- # is_obj_type / base_type strip is the same shape used in the
8046- # ivar branch and at every other obj_-prefix site in this file.
8047- vt = find_var_type(vname)
8048- bt = base_type(vt)
8049- if is_obj_type(bt) == 1
8050- ci = find_class_idx(bt[4, bt.length - 4])
8051- end
8052- end
8053- elsif @nd_type[recv] == "InstanceVariableReadNode"
8054- # `@ivar.instance_eval { }` inside a class method. ieval_walk_class_methods
8055- # sets @current_class_idx so cls_ivar_type returns the ivar's stored
8056- # type — "obj_<Class>" when the ivar was bound to `Class.new` (and
8057- # not since widened to poly). Strip the "obj_" prefix to look up
8058- # the class index, the same shape `is_obj_type` / `base_type`
8059- # gates use elsewhere in the codegen for object-typed values.
8060- if @current_class_idx >= 0
8061- it = cls_ivar_type(@current_class_idx, @nd_name[recv])
8062- bt = base_type(it)
8063- if is_obj_type(bt) == 1
8064- ci = find_class_idx(bt[4, bt.length - 4])
8065- end
8066- end
8067- end
8084+ ci = recv_class_idx_for_rebind(recv, local_class)
80688085 if ci < 0
80698086 return
80708087 end