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:
-
Parser case in spinel_parse.c -- emits the node with `names` as an array of SymbolNode child ids.
-
New @nd_names array field + alloc_node push + set_array_field handler for "names".
-
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
# for globals). @nd_new_name = [] @nd_old_name = [] # UndefNode -- comma-separated child ids for the SymbolNode names. @nd_names = "".split(",") @nd_count = 0 @root_id = 0 @galias_new = "".split(",") @galias_old = "".split(",") # `undef foo` -- per-(class, method-name) registry of removed # methods. Recorded by collect_class_method_undef; compile-time # enforcement of "call after undef fails" is currently a # documented out-of-scope. @undef_class_idx = [] @undef_method = "".split(",") # ---- Scope stack for local variables ---- @scope_names = "".split(",") @scope_types = "".split(",") @nd_posts.push("") @nd_new_name.push(-1) @nd_old_name.push(-1) @nd_names.push("") @nd_count = @nd_count + 1 nid end if field == "posts" @nd_posts[nid] = ids_str end if field == "names" # UndefNode -- list of SymbolNode names to undef. @nd_names[nid] = ids_str end end # ---- Convenience: get stmts of a body node ---- collect_class_method_alias(ci, nn, on) end end if @nd_type[sid] == "UndefNode" # `undef foo, bar` -- record the removal in @undef_*. Spinel # doesn't currently enforce "calling an undef'd method # fails" at the compile-time dispatch path; the recording # is the foundation for that future check. unames = parse_id_list(@nd_names[sid]) uk = 0 while uk < unames.length uname = symbol_node_literal(unames[uk]) if uname != "" collect_class_method_undef(ci, uname) end uk = uk + 1 end end } # Collect ivars. Pin the lexical scope to this class so any # `@x = Foo.new(...)` inside its methods resolves `Foo` against # the same scope chain the call site sees — without this, # Extract the literal name from a SymbolNode argument. Returns "" # for InterpolatedSymbolNode and other shapes Spinel doesn't # support as alias source/target at compile time. # support as alias source/target or undef target at compile time. def symbol_node_literal(nid) if nid < 0 return "" end end # `undef foo` -- mark the method as removed in the @undef_* # tracker. Spinel currently doesn't enforce "call after undef # fails to compile" -- the recording is the foundation for that # future check. Documented out of scope in test/undef.rb. def collect_class_method_undef(ci, name) @undef_class_idx.push(ci) @undef_method.push(name) end def collect_module_methods_into_class(ci, mod_name) # Find the module and add its methods to the class mi = 0 # finds it like any other method. return end if t == "UndefNode" # `undef foo` -- compile-time only. The class-collect pass # already recorded the removal in @undef_*; nothing to emit. return end if t == "LocalVariableWriteNode" lname = @nd_name[nid] # Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/undef.rbaddedView file
Loading diff…
test/undef.rb.expectedaddedView file
Loading diff…