◐ Shell
clean mode source ↗

traverse and clear by youknowone · Pull Request #6780 · RustPython/RustPython

Warning

Rate limit exceeded

@youknowone has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 1 minutes and 48 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 3009d91 and f2b3a06.

📒 Files selected for processing (13)
  • crates/derive-impl/src/pyclass.rs
  • crates/derive-impl/src/pystructseq.rs
  • crates/derive-impl/src/util.rs
  • crates/vm/src/builtins/dict.rs
  • crates/vm/src/builtins/function.rs
  • crates/vm/src/builtins/list.rs
  • crates/vm/src/builtins/str.rs
  • crates/vm/src/builtins/tuple.rs
  • crates/vm/src/dict_inner.rs
  • crates/vm/src/frame.rs
  • crates/vm/src/object/core.rs
  • crates/vm/src/object/traverse.rs
  • crates/vm/src/object/traverse_object.rs
📝 Walkthrough

Walkthrough

This PR systematically refactors the garbage collection traversal system across RustPython, transitioning from an older "trace" mechanism to a "traverse" model with explicit support for circular reference clearing via new HAS_TRAVERSE, HAS_CLEAR flags and optional clear methods on types.

Changes

Cohort / File(s) Summary
Derive macro infrastructure
crates/derive-impl/src/pyclass.rs, crates/derive-impl/src/pystructseq.rs, crates/derive-impl/src/util.rs
Updated code generation to emit MaybeTraverse with HAS_TRAVERSE/HAS_CLEAR constants and try_clear hooks. PyClass now supports conditional traverse derivation and optional clear paths. Added "clear" to allowed item attributes.
Core traversal abstractions
crates/vm/src/object/traverse.rs, crates/vm/src/object/core.rs, crates/vm/src/object/traverse_object.rs
Replaced IS_TRACE with HAS_TRAVERSE/HAS_CLEAR constants. Added clear hook to Traverse trait. Renamed try_trace_obj to try_traverse_obj and updated all references.
Builtin type traversal implementations
crates/vm/src/builtins/dict.rs, crates/vm/src/builtins/list.rs, crates/vm/src/builtins/tuple.rs, crates/vm/src/builtins/function.rs, crates/vm/src/builtins/str.rs
Added manual Traverse implementations with explicit traverse and clear methods. Updated pyclass declarations to use traverse = "manual". Function.rs traverses additional fields (type_params, annotations, module, doc) and implements clearing logic.
Supporting garbage collection utilities
crates/vm/src/dict_inner.rs, crates/vm/src/frame.rs
Added pop_all_entries method to Dict for circular reference resolution. Introduced Traverse implementations for FrameState and Frame with manual traversal support.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • #6319: Modifies MaybeTraverse trait implementation and generated pystructseq/pyclass traversal wiring with HAS_TRAVERSE/HAS_CLEAR semantics.
  • #5959: Updates derive-impl traversal code generation to add Traverse bounds and trait implementations.
  • #6320: Updates PyObject payload traversal helper with PyPayload bounds that pairs with try_traverse_obj renaming and HAS_TRAVERSE semantics.

Suggested reviewers

  • ShaharNaveh
  • arihant2math
  • coolreader18

Poem

🐰 Through forests deep where references dance,
We trace each path, give cycles a chance,
With HAS_TRAVERSE flags held high and clear,
The garbage collector's work draws near,
And when the dust settles, all cleaned and bright,
Our heap hops forward, unburdened and light!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 71.43% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main changes: introducing traverse and clear semantics for garbage collection across multiple builtin types and the derive macro system.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.