Skip to content

compiler set_qualname #5930

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

Merged
merged 2 commits into from
Jul 11, 2025
Merged

compiler set_qualname #5930

merged 2 commits into from
Jul 11, 2025

Conversation

youknowone
Copy link
Member

@youknowone youknowone commented Jul 10, 2025

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.

Copy link
Contributor

coderabbitai bot commented Jul 10, 2025

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
Loading

Possibly related PRs

  • PyCode::qualname #5929: Refactored and centralized the computation of qualname in 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

📥 Commits

Reviewing files that changed from the base of the PR and between bbb9c5b and e075538.

📒 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 fmt to 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_qualname method, 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.

❤️ Share
🪧 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 @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in 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 pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere 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 youknowone changed the title set_qualname compiler set_qualname Jul 10, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 18d7c1b and 61e0866.

📒 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 fmt to 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 qualname as None and set it later via set_qualname is 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_qualname call 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_output ensures the symbol table and code stack are properly set up for accurate qualname generation.


545-552: Qualified_path duplication guard validated

We 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_name

therefore correctly avoids duplicate segments in the qualified path without risk of skipping a deeper, distinct scope. No changes are needed.

@youknowone youknowone merged commit 59c7fcb into RustPython:main Jul 11, 2025
12 checks passed
@youknowone youknowone deleted the set-qualname branch July 11, 2025 09:21
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.

1 participant