del statement support

Closed
KM
7mo ago
+892-43

del statement support

Implements del across the full pipeline — parsing, prepare, bytecode compilation, and VM
execution.

What works

  • del x — unbinds variables (locals, globals, cells), subsequent access raises NameError or
    UnboundLocalError exactly as CPython does
  • del d['key'] — dict item removal via __delitem__
  • del lst[i] — list item removal with negative index support
  • del a, b — multi-target, executed left-to-right
  • Closures — del on captured variables correctly propagates: outer scope gets
    UnboundLocalError, inner closures get NameError for free variables
  • nonlocal + del — deleting via nonlocal unbinds the cell in the enclosing scope

New opcodes

  • DeleteSubscr — pops index + container, calls __delitem__
  • DeleteLocalW — wide variant of DeleteLocal for slots > 255
  • DeleteCell — stores Undefined in a closure cell

All three appended to the enum to preserve discriminant stability.

Bug fix (pre-existing)

load_global was raising UnboundLocalError for undefined module-level variables that had
previously been assigned. CPython always raises NameError for globals. Fixed.

Not yet supported

  • del obj.attr — blocked on user-defined classes

Written in collaboration with claude-opus-4-6 and gpt-5.4-high