compiler set_qualname by youknowone · Pull Request #5930 · RustPython/RustPython
Summary by CodeRabbit
- Refactor
- Improved the assignment of qualified names to functions, classes, and comprehensions, resulting in clearer and more consistent naming in nested and special scopes for better code object identification.
Walkthrough
This change refactors how the qualified name (qualname) of code objects is computed and assigned in the compiler. It introduces centralized methods for setting and generating qualname, replacing previous direct assignments with context-aware logic that considers scope, symbol tables, and nesting.
Changes
| File(s) | Change Summary |
|---|---|
| compiler/codegen/src/compile.rs | Added set_qualname and make_qualname methods to compute and assign qualified names contextually; removed qualified_path field and related push/pop logic; updated all code object creation sites to use the new centralized qualified name assignment. |
Sequence Diagram(s)
sequenceDiagram
participant Compiler
participant SymbolTable
participant CodeInfo
Compiler->>Compiler: push_output()
Compiler->>Compiler: set_qualname()
Compiler->>Compiler: make_qualname()
Compiler->>SymbolTable: inspect current scope, parent, stack
Compiler->>Compiler: compute qualified name string
Compiler->>CodeInfo: assign qualname
Possibly related PRs
- PyCode::qualname #5929: Refactored and centralized the computation of
qualnamein the compiler, laying the groundwork for the improved context-aware logic introduced in this PR.
Poem
In the warren where compilers dwell,
Names once tangled, now set well—
With context deep and scope in mind,
Each qualname’s thread is now aligned.
Hopping through code, I set things right,
🐇✨ Qualified names now shine so bright!
📜 Recent review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
compiler/codegen/src/compile.rs(8 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.rs`: Follow the default rustfmt code style (`cargo fmt` to format) Always ...
**/*.rs: Follow the default rustfmt code style (cargo fmtto format)
Always run clippy to lint code (cargo clippy) before completing tasks. Fix any warnings or lints that are introduced by your 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
📄 Source: CodeRabbit Inference Engine (.github/copilot-instructions.md)
List of files the instruction was applied to:
compiler/codegen/src/compile.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
- GitHub Check: Run snippets and cpython tests on wasm-wasi
- GitHub Check: Check the WASM package and demo
- GitHub Check: Run tests under miri
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Run rust tests (ubuntu-latest)
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Check Rust code with rustfmt and clippy
- GitHub Check: Ensure compilation on various targets
🔇 Additional comments (8)
compiler/codegen/src/compile.rs (8)
402-403: LGTM! Clear documentation of deferred assignment.The comment clearly explains that the qualname will be set later via the new
set_qualnamemethod, which improves code clarity and maintainability.
464-554: Well-structured qualified name computation following CPython semantics.The implementation correctly handles the various cases for qualified name generation:
- Module-level objects get simple names
- Type parameter scopes are handled specially
- Global symbols are forced to use simple names
- Function scopes get
.<locals>appended- Class scopes use direct concatenation
The logic appears to follow Python's qualified name rules accurately and the code is well-commented.
636-637: Correct integration of new qualified name method.The call to
set_qualname()is properly placed after the code object is pushed onto the stack, ensuring the symbol table context is available for qualified name computation.
670-671: Appropriate qualified name handling for lambda expressions.Setting the qualified name for lambda expressions ensures consistent qualified name assignment across all code object types.
857-858: Correct qualified name assignment for class definitions.The call to
set_qualname()is properly positioned for class compilation, ensuring the qualified name is computed with the correct context.
869-873: Proper use of computed qualified name in class compilation.The code correctly uses the qualified name returned by
set_qualname()for both the constant value and the__qualname__attribute assignment.
728-728: Consistent qualified name usage in function compilation.The qualified name is properly used for the function's qualified name constant, maintaining consistency with the new qualified name computation approach.
4142-4142: Appropriate qualified name handling for comprehensions.Setting qualified names for comprehensions ensures all code objects have properly computed qualified names based on their compilation context.
✨ Finishing Touches
- 📝 Generate Docstrings
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.
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>, please review it.Explain this complex logic.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai explain this code block.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read src/utils.ts and explain its main purpose.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Support
Need help? Create a ticket on our support page for assistance with any issues or questions.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
youknowone
changed the title
set_qualname
compiler set_qualname
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
compiler/codegen/src/compile.rs(7 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.rs`: Follow the default rustfmt code style (`cargo fmt` to format) Always ...
**/*.rs: Follow the default rustfmt code style (cargo fmtto format)
Always run clippy to lint code (cargo clippy) before completing tasks. Fix any warnings or lints that are introduced by your 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
📄 Source: CodeRabbit Inference Engine (.github/copilot-instructions.md)
List of files the instruction was applied to:
compiler/codegen/src/compile.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
- GitHub Check: Run tests under miri
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Check the WASM package and demo
- GitHub Check: Run snippets and cpython tests on wasm-wasi
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Run rust tests (ubuntu-latest)
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Check Rust code with rustfmt and clippy
- GitHub Check: Ensure compilation on various targets
🔇 Additional comments (5)
compiler/codegen/src/compile.rs (5)
405-406: LGTM! Deferred qualname assignment aligns with the new centralized approach.The change to initialize
qualnameasNoneand set it later viaset_qualnameis appropriate for the refactoring.
497-507: Verify the logic when code stack size exceeds symbol table stack size.The condition
stack_size > self.symbol_table_stack.len()suggests we're in a state where the symbol table hasn't been pushed yet. While the comment acknowledges this, it would be good to verify that this is the expected behavior and doesn't indicate a timing issue.Can you confirm that this condition is expected during normal compilation flow? It might be worth adding more detailed documentation about when this state occurs.
1598-1600: Appropriate placement of qualname computation for functions.The
set_qualnamecall is correctly placed after entering the function and setting up the context, ensuring all necessary information is available for proper qualname generation.
1824-1826: Correct qualname computation for class definitions.The placement after
push_outputensures the symbol table and code stack are properly set up for accurate qualname generation.
545-552: Qualified_path duplication guard validatedWe searched the codebase for nested modules and functions sharing the same name and found no instances where a parent and child scope use identical identifiers. The check
self.qualified_path[path_len - 1] == current_obj_nametherefore correctly avoids duplicate segments in the qualified path without risk of skipping a deeper, distinct scope. No changes are needed.