Support the `del` statement

Closed
FE
4d ago
+1028-83

Implements del, which the parser previously rejected with NotImplementedError.
Earlier attempts were #68 (subscripts only) and #309 (closed after a performance regression stalled).

What works

  • Names: locals, globals (declared or module level), cells and nonlocal, class bodies, REPL feeds and exec() snippets.
    Errors match CPython: NameError at module and class level, UnboundLocalError in functions.
  • Subscripts: dict, list (including extended slices with any step) and deque.
  • Attributes: sandbox instances and classes, host class instances.
    Frozen dataclasses raise FrozenInstanceError: cannot delete field 'x'.
  • Target groups: del a, (b[0], c.d) deletes left to right.

Implementation notes

  • New opcodes DeleteSubscr, DeleteAttr and DeleteClassLocal (125–127).
    Load opcodes and their dispatch are untouched, so code without del runs the same instructions as before.
  • DUMP_VERSION 13 → 14, the first bump since v1.1.0, and dump_v14.bin replaces dump_v13.bin.
  • Class-body del name drops the name from the assembled namespace.
    Class bodies have no control flow, so this is decided once after parsing the body.
  • List slice deletion uses Vec::extract_if, so no second value buffer is allocated.

Tests

  • test_cases/del__statement.py covers all of the above with exact exception messages and runs against CPython and Monty.
  • Removed the stale del entries from limitations/ (language, index, classes, collections, dataclasses, eval_exec).

I have not run the benchmarks (make bench); since no existing opcode changed, I don't expect a regression, but that is not measured.

šŸ¤– Generated with Claude Code


Summary by cubic

Implements the del statement, which previously failed at parse time with NotImplementedError. Deletion now works for names (locals, globals, cells, nonlocal, class bodies, REPL feeds, and exec() snippets), subscripts (dict, list including extended slices, deque), and attributes (instances, classes, host classes), with error messages matching CPython — including FrozenInstanceError for frozen dataclass fields, RuntimeError for deque mutation during iteration, TypeError for immutable-type attributes, and the sequence-path wording for unsupported item deletion.

Implementation notes

  • Adds three new opcodes (DeleteSubscr, DeleteAttr, DeleteClassLocal) without changing existing load opcodes, and bumps DUMP_VERSION 13 → 14 (the first since v1.1.0) with the dump fixture regenerated.
  • On sequence-slot types, unsupported item deletion converts an index-protocol key via its __index__ first (running user code, propagating its exception, or raising IndexError on overflow) before reporting the CPython wording.
  • A class body binds __doc__ up front only when a docstring is present, so del __doc__ without one raises NameError; type()'s None default is applied after the body.
  • Adds test_cases/del__statement.py covering all targets and error paths, removes stale del entries from limitations/ docs, and documents that del C.__annotations__ diverges from CPython 3.14.
  • Benchmarks were not run; no existing opcode changed, so no regression is expected.