Commit 247167f6

247167f6d129Merged

Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

feat(parse,codegen): support UndefNode (undef foo, bar)

Compile-time recording of `undef foo` inside a class body. Spinel records the (class_idx, method_name) tuple in @undef_class_idx / @undef_method during the class-body collect's third pass (alongside AliasMethodNode handling). The recording is the foundation for a future "call after undef fails to compile" check; v1 leaves that enforcement out of scope and the test fixture documents this.

Three pieces:

  1. Parser case in spinel_parse.c -- emits the node with `names` as an array of SymbolNode child ids.

  2. New @nd_names array field + alloc_node push + set_array_field handler for "names".

  3. collect_class_method_undef(ci, name) helper + extension of the class-body third pass (collect_class_with_prefix) that walks each UndefNode's names and records each in @undef_*. compile_stmt arm is a no-op (the recording happened at collect).

Out of scope (documented in test/undef.rb):

  • Compile-time enforcement that calling an undef'd method fails; Spinel's call-dispatch path doesn't yet consult @undef_*.
  • undef inside a method body (the recording happens at static collect time).

Test exercises (test/undef.rb):

  • Defining `foo` and `bar`, then `undef foo` -- verifies that `bar` still works (i.e. undef removes only the named method, not the whole class).

Tests: 277 pass. Bootstrap: gen2.c == gen3.c.

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

Details

Commit
247167f6d129
Parent
d7db4dd36138

spinel_codegen.rbmodifiedView file

@@ -102,6 +102,8 @@
102102 # for globals).
103103 @nd_new_name = []
104104 @nd_old_name = []
105+ # UndefNode -- comma-separated child ids for the SymbolNode names.
106+ @nd_names = "".split(",")
105107
106108 @nd_count = 0
107109 @root_id = 0
@@ -215,6 +217,13 @@
215217 @galias_new = "".split(",")
216218 @galias_old = "".split(",")
217219
220+ # `undef foo` -- per-(class, method-name) registry of removed
221+ # methods. Recorded by collect_class_method_undef; compile-time
222+ # enforcement of "call after undef fails" is currently a
223+ # documented out-of-scope.
224+ @undef_class_idx = []
225+ @undef_method = "".split(",")
226+
218227 # ---- Scope stack for local variables ----
219228 @scope_names = "".split(",")
220229 @scope_types = "".split(",")
@@ -506,6 +515,7 @@
506515 @nd_posts.push("")
507516 @nd_new_name.push(-1)
508517 @nd_old_name.push(-1)
518+ @nd_names.push("")
509519 @nd_count = @nd_count + 1
510520 nid
511521 end
@@ -834,6 +844,10 @@
834844 if field == "posts"
835845 @nd_posts[nid] = ids_str
836846 end
847+ if field == "names"
848+ # UndefNode -- list of SymbolNode names to undef.
849+ @nd_names[nid] = ids_str
850+ end
837851 end
838852
839853 # ---- Convenience: get stmts of a body node ----
@@ -6494,8 +6508,24 @@
64946508 collect_class_method_alias(ci, nn, on)
64956509 end
64966510 end
6511+ if @nd_type[sid] == "UndefNode"
6512+ # `undef foo, bar` -- record the removal in @undef_*. Spinel
6513+ # doesn't currently enforce "calling an undef'd method
6514+ # fails" at the compile-time dispatch path; the recording
6515+ # is the foundation for that future check.
6516+ unames = parse_id_list(@nd_names[sid])
6517+ uk = 0
6518+ while uk < unames.length
6519+ uname = symbol_node_literal(unames[uk])
6520+ if uname != ""
6521+ collect_class_method_undef(ci, uname)
6522+ end
6523+ uk = uk + 1
6524+ end
6525+ end
64976526 }
64986527
6528+
64996529 # Collect ivars. Pin the lexical scope to this class so any
65006530 # `@x = Foo.new(...)` inside its methods resolves `Foo` against
65016531 # the same scope chain the call site sees — without this,
@@ -6511,7 +6541,7 @@
65116541
65126542 # Extract the literal name from a SymbolNode argument. Returns ""
65136543 # for InterpolatedSymbolNode and other shapes Spinel doesn't
6514- # support as alias source/target at compile time.
6544+ # support as alias source/target or undef target at compile time.
65156545 def symbol_node_literal(nid)
65166546 if nid < 0
65176547 return ""
@@ -6554,6 +6584,16 @@
65546584 end
65556585 end
65566586
6587+ # `undef foo` -- mark the method as removed in the @undef_*
6588+ # tracker. Spinel currently doesn't enforce "call after undef
6589+ # fails to compile" -- the recording is the foundation for that
6590+ # future check. Documented out of scope in test/undef.rb.
6591+ def collect_class_method_undef(ci, name)
6592+ @undef_class_idx.push(ci)
6593+ @undef_method.push(name)
6594+ end
6595+
6596+
65576597 def collect_module_methods_into_class(ci, mod_name)
65586598 # Find the module and add its methods to the class
65596599 mi = 0
@@ -26232,6 +26272,11 @@
2623226272 # finds it like any other method.
2623326273 return
2623426274 end
26275+ if t == "UndefNode"
26276+ # `undef foo` -- compile-time only. The class-collect pass
26277+ # already recorded the removal in @undef_*; nothing to emit.
26278+ return
26279+ end
2623526280 if t == "LocalVariableWriteNode"
2623626281 lname = @nd_name[nid]
2623726282 # Check for method(:name) assignment