-
Notifications
You must be signed in to change notification settings - Fork 1.4k
More slot impl #6618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More slot impl #6618
Conversation
|
Warning Rate limit exceeded@youknowone has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 12 minutes and 8 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the 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. 📒 Files selected for processing (11)
📝 WalkthroughWalkthroughThis pull request removes Python method exposure for Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
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. Comment |
aefd83e to
fbf02df
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
crates/vm/src/types/slot.rs (1)
685-700: Consider moving string interning outside the lock scope.The
ctx.intern_str()calls on lines 695-696 are made while holding theself.attributes.read()lock. If string interning acquires internal locks, this could introduce lock ordering concerns. Consider interning these strings before acquiring the attributes lock:🔎 Suggested refactor
let has_own = { + // Intern strings before acquiring the lock to avoid potential lock ordering issues + let setitem = ctx.intern_str("__setitem__"); + let delitem = ctx.intern_str("__delitem__"); let guard = self.attributes.read(); // Check the current method name let mut result = guard.contains_key(name); // For ass_item/ass_subscript slots, also check the paired method // (__setitem__ and __delitem__ share the same slot) if !result && (stringify!($slot) == "ass_item" || stringify!($slot) == "ass_subscript") { - let setitem = ctx.intern_str("__setitem__"); - let delitem = ctx.intern_str("__delitem__"); result = guard.contains_key(setitem) || guard.contains_key(delitem); } result };crates/vm/src/builtins/set.rs (1)
830-834: Consider usings.innerdirectly instead ofs.inner.clone().Since
s(aPyFrozenSet) is consumed by the closure, you can moveinnerdirectly without cloning:- r.map(|s| PySet { - inner: s.inner.clone(), - }) + r.map(|s| PySet { inner: s.inner })This applies to similar patterns on lines 849-852, 867-870, and 885-888. The clone is cheap (just an
Rcbump), but removing it is slightly more idiomatic.
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (14)
crates/vm/src/builtins/bool.rscrates/vm/src/builtins/bytearray.rscrates/vm/src/builtins/bytes.rscrates/vm/src/builtins/dict.rscrates/vm/src/builtins/list.rscrates/vm/src/builtins/mappingproxy.rscrates/vm/src/builtins/memory.rscrates/vm/src/builtins/set.rscrates/vm/src/builtins/tuple.rscrates/vm/src/protocol/number.rscrates/vm/src/stdlib/collections.rscrates/vm/src/stdlib/ctypes/array.rscrates/vm/src/stdlib/winreg.rscrates/vm/src/types/slot.rs
💤 Files with no reviewable changes (9)
- crates/vm/src/builtins/bytes.rs
- crates/vm/src/builtins/bytearray.rs
- crates/vm/src/builtins/tuple.rs
- crates/vm/src/stdlib/ctypes/array.rs
- crates/vm/src/builtins/list.rs
- crates/vm/src/builtins/memory.rs
- crates/vm/src/stdlib/collections.rs
- crates/vm/src/builtins/dict.rs
- crates/vm/src/stdlib/winreg.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style by runningcargo fmtto format Rust code
Always run clippy to lint code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass,pymodule,pyfunction, etc.) when implementing Python functionality in Rust
Files:
crates/vm/src/protocol/number.rscrates/vm/src/builtins/bool.rscrates/vm/src/builtins/mappingproxy.rscrates/vm/src/types/slot.rscrates/vm/src/builtins/set.rs
🧠 Learnings (2)
📚 Learning: 2025-08-26T05:20:54.540Z
Learnt from: youknowone
Repo: RustPython/RustPython PR: 6110
File: vm/src/frame.rs:1311-1316
Timestamp: 2025-08-26T05:20:54.540Z
Learning: In RustPython's pattern matching implementation, only certain builtin types should have the SEQUENCE flag: list and tuple are confirmed sequences. The user youknowone indicated that bytes, bytearray are not considered sequences in this context, even though they implement sequence-like protocols.
Applied to files:
crates/vm/src/builtins/set.rs
📚 Learning: 2025-08-26T05:20:54.540Z
Learnt from: youknowone
Repo: RustPython/RustPython PR: 6110
File: vm/src/frame.rs:1311-1316
Timestamp: 2025-08-26T05:20:54.540Z
Learning: In the RustPython codebase, only certain builtin types should be marked with the SEQUENCE flag for pattern matching. List and tuple are sequences, but bytes, bytearray, and range are not considered sequences in this context, even though they may implement sequence-like protocols.
Applied to files:
crates/vm/src/builtins/set.rs
🧬 Code graph analysis (3)
crates/vm/src/builtins/bool.rs (2)
crates/vm/src/protocol/mapping.rs (1)
slots(108-110)crates/vm/src/protocol/sequence.rs (1)
slots(134-136)
crates/vm/src/builtins/mappingproxy.rs (6)
crates/vm/src/types/slot.rs (1)
sequence_downcast(1905-1907)crates/vm/src/builtins/set.rs (2)
__len__(536-538)__len__(1008-1010)crates/vm/src/builtins/dict.rs (2)
__len__(215-217)__len__(766-768)crates/vm/src/builtins/list.rs (1)
__len__(185-187)crates/vm/src/builtins/memory.rs (1)
__len__(693-702)crates/vm/src/builtins/tuple.rs (1)
__len__(301-303)
crates/vm/src/types/slot.rs (1)
crates/vm/src/types/slot_defs.rs (1)
find_slot_defs_by_name(918-920)
🔇 Additional comments (12)
crates/vm/src/types/slot.rs (2)
610-612: Defensive collection looks reasonable.Collecting into a
Vecbefore iteration is a safe approach. Sincefind_slot_defs_by_nameiterates over a staticSLOT_DEFSarray and returns&'static SlotDefreferences, there shouldn't be actual mutation-while-iteration issues here. However, this defensive pattern is low-cost and prevents potential future issues if the implementation changes.
701-713: LGTM!The logic correctly prioritizes:
- Use wrapper when the type has its own Python method definition (ensuring Python method dispatch)
- Use native slot function from MRO when available (for efficiency)
- Fall back to wrapper otherwise
This properly handles the case where a subclass defines
__setitem__or__delitem__in Python while inheriting from a native type.crates/vm/src/builtins/mappingproxy.rs (2)
237-237: LGTM! Proper sequence protocol integration.The
lengthbinding correctly integrates__len__into the sequence protocol using thesequence_downcastpattern, mirroring the existing mapping protocol implementation at line 223.
176-179: len implementation correctly shifts to protocol-based slot lookups.Removal of
#[pymethod]is consistent with the architectural shift to protocol-based access. The method properly delegates to the underlying object's length through bothAsMapping(line 223) andAsSequence(line 237) protocol slots using the inheriteddowncastmethods. The implementation matches the pattern used in other builtins.crates/vm/src/protocol/number.rs (1)
356-476: LGTM! Consistent with From implementation pattern.The addition of right-hand operator slot assignments correctly mirrors the pattern established in the
From<&PyNumberMethods>implementation (lines 297-352). For native types, using the same function for both left and right slots is appropriate since these implementations perform their own type checking and handle mixed-type operations internally.crates/vm/src/builtins/bool.rs (1)
44-66: Solid refactoring to protocol-based truthiness evaluation.The slot-based approach correctly implements Python's truthiness semantics: attempts
__bool__via the number protocol first, then falls back to length checks via mapping/sequence protocols, finally defaulting totrue. This aligns with the PR's broader shift away from method-based dispatch.The use of
mapping_unchecked()andsequence_unchecked()is appropriate given the preceding slot existence checks.Protocol slot wiring is comprehensive across all major builtin types. All types that expose
__len__(list, dict, tuple, bytes, bytearray, set, frozenset, range, and others) have their length methods properly wired into the corresponding protocol slots viaas_mapping(),as_sequence(), oras_number()implementations. No incomplete conversions detected.crates/vm/src/builtins/set.rs (6)
536-542: LGTM - Slot-based method implementations.Both
__len__and__contains__are implemented without#[pymethod]attributes, which aligns with the PR's goal of using protocol-based slot lookups instead of method exposure. The implementations correctly delegate to the inner methods.
788-790: Correct routing through__contains__.The sequence
containsslot now properly routes through the__contains__method, maintaining consistency with the slot-based design.
818-893: Binary operation handling looks correct for mixed set/frozenset types.The implementation properly:
- Guards with
AnySet::check(a) && AnySet::check(b)to returnNotImplementedfor non-set operands- Handles the case where
ais aPyFrozenSet(via reflected ops) by wrapping the result inPySetThe comments explaining the
__rsub__operand swapping are helpful for maintainability.
1008-1014: LGTM - Consistent with PySet implementation.The
__len__and__contains__implementations forPyFrozenSetfollow the same slot-based pattern asPySet.
1168-1170: Consistent routing through__contains__.
1222-1272: LGTM - Correct handling for PyFrozenSet's AsNumber operations.The implementation correctly:
- Returns
NotImplementedwhen operands aren't sets- Preserves result type based on the first operand (
a)- Delegates to
PySetmethods whenais aPySet(via reflected ops)
fbf02df to
bb18d0c
Compare
Summary by CodeRabbit
Bug Fixes
Changes
✏️ Tip: You can customize this high-level summary in your review settings.