Skip to content

Convert native base references without consulting the Python class - #9090

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowone:native-base-cast
Oct 13, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
youknowone:native-base-cast

Conversation

@youknowone

@youknowone youknowone commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Extracted from #8963 by @1ndahous3 (the commit keeps their authorship).

PyRef::into_base went through a class-based downcast and hit unreachable_unchecked on failure. A downcast consults the object's MRO, while the physical base prefix is fixed at allocation, so an object whose class has a custom mro() that omits the native base turned this into undefined behavior. Py::to_base had the same assumption as a debug assertion.

Both now cast the pointer directly. The unsafe impl PySubclass contract from #9012 is what guarantees the prefix, so no runtime check is needed, and into_base loses the downcast it used to perform.

Not included from #8963: the native layout id, the supports_native_layout vtable entry, and the runtime assertion in upcast / upcast_ref.

Testing

native_base_conversions_ignore_python_mro builds an OSError subclass, replaces its MRO with [cls, object], and converts through two base levels by reference and by value. cargo test -p rustpython-vm --features threading --lib and cargo clippy -p rustpython-vm --features threading --all-targets -- -D warnings pass on macOS.

AI assistance

Original change written with Codex (gpt-6); extraction and test trimmed with Claude (claude-opus-5-5).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Typed conversions to a base class now continue to access the original object when Python’s method resolution order omits native base classes. This prevents valid conversions from failing due to differences between Python’s class hierarchy and the object’s native layout.

PyRef::into_base and Py::to_base cast the pointer directly instead of
going through a class-based downcast. into_base no longer reaches
unreachable_unchecked when the object's MRO omits the native base, and
to_base drops the equivalent debug assertion.

Extracted from RustPython#8963.

Assisted-by: Codex:gpt-6
Assisted-by: Claude:claude-opus-5-5
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T02:54:59.323800Z 2b3b68b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 52343ae9-37b5-4d8e-89e4-71965ad5c8a9
📥 Commits

Reviewing files that changed from the base of the PR and between 85edb52 and 2b3b68b.

📒 Files selected for processing (1)
  • crates/vm/src/object/core.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📝 Walkthrough

Base-reference conversions now use the PySubclass layout guarantee instead of runtime downcasts. A regression test checks these conversions when an OS-error subclass has an MRO that omits native bases.

Changes: Typed base conversions

Layer / File(s) Summary
Base conversions and regression test
crates/vm/src/object/core.rs
PyRef<T>::into_base and Py<T>::to_base now convert using the base-layout guarantee. A regression test checks borrowed and consuming conversions after the test changes an OS-error subclass's MRO.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: bschoenmaeckers

Merge Risk: ⚪ Minimal · up to 2b3b6

No actionable issue remains from this review; the change is mergeable after normal checks.

Pre-merge checks · Passed 5
Passed Passed checks (5 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title accurately and specifically summarizes the main change: direct native base reference conversion without consulting the Python class or MRO.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 1 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@youknowone
youknowone merged commit 0803a55 into RustPython:main Oct 13, 2026
21 checks passed
@youknowone
youknowone deleted the native-base-cast branch October 13, 2026 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants