Skip to content

Remove misplaced SymbolScope::TypeParams #5975

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 15, 2025

Conversation

youknowone
Copy link
Member

@youknowone youknowone commented Jul 15, 2025

Summary by CodeRabbit

  • New Features

    • Added support for a new scope type: AsyncFunction, allowing improved handling of asynchronous functions in symbol analysis.
  • Refactor

    • Replaced all uses of the previous scope type enum with a new, extended enum for representing code scopes.
    • Updated function and method signatures to use the new enum, ensuring consistent scope handling across the codebase.
    • Removed the TypeParams scope variant from symbol analysis logic.

Copy link
Contributor

coderabbitai bot commented Jul 15, 2025

Walkthrough

The changes systematically replace the SymbolTableType enum with a new CompilerScope enum throughout the codebase, updating all type annotations, function signatures, and logic to use the new type. The CompilerScope enum introduces an additional AsyncFunction variant, and the handling of TypeParams as a symbol scope is removed.

Changes

File(s) Change Summary
compiler/codegen/src/compile.rs Replaced all usage of SymbolTableType with CompilerScope; updated function signatures and match arms accordingly; removed handling of TypeParams as a symbol scope.
compiler/codegen/src/symboltable.rs Renamed SymbolTableType to CompilerScope, added AsyncFunction variant; removed TypeParams as a symbol scope; updated all related logic and function signatures.
vm/src/stdlib/symtable.rs Switched import and usage from SymbolTableType to CompilerScope; updated logic in is_optimized to match the new enum.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Compiler
    participant SymbolTable

    User->>Compiler: enter_scope(name, scope_type, key, lineno)
    Compiler->>SymbolTable: enter_scope(name, CompilerScope, lineno)
    SymbolTable-->>Compiler: Scope entered (with new enum)
    Compiler-->>User: Compilation continues
Loading

Possibly related PRs

  • RustPython/RustPython#5950: Refactors the enter_scope function signature and usage in compiler/codegen/src/compile.rs, which directly relates to the enum and method changes in this PR.
  • RustPython/RustPython#5941: Adds explicit support for SymbolScope::TypeParams, which this PR now removes, making their changes directly related but in opposition.

Poem

A scope by any other name
Would still compile the same—
From tables to code we hop,
With AsyncFunction now on top!
Farewell, TypeParams, you’ve run your course,
CompilerScope now leads the force.
🐇✨

✨ 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.

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 2dd0ce5 and 647f540.

📒 Files selected for processing (3)
  • compiler/codegen/src/compile.rs (10 hunks)
  • compiler/codegen/src/symboltable.rs (22 hunks)
  • vm/src/stdlib/symtable.rs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • .github/copilot-instructions.md
🧠 Learnings (2)
vm/src/stdlib/symtable.rs (1)
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-30T10:08:48.858Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
compiler/codegen/src/compile.rs (1)

undefined

<retrieved_learning>
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-30T10:08:48.858Z
Learning: Applies to **/*.rs : Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust
</retrieved_learning>

🔇 Additional comments (18)
vm/src/stdlib/symtable.rs (1)

8-10: Import update looks correct
CompilerScope is now re-exported from rustpython_codegen::symboltable; the path and naming are accurate.

compiler/codegen/src/compile.rs (6)

412-412: LGTM: Function parameter type correctly updated

The function parameter type change from SymbolTableType to CompilerScope is correct and aligns with the systematic enum replacement throughout the codebase.


455-455: LGTM: Debug assertions correctly updated

The debug assertions are properly updated to use CompilerScope::Class instead of the old enum variant, maintaining the same validation logic.

Also applies to: 462-462


482-502: LGTM: Match arms comprehensively updated

The match arms are systematically updated to use CompilerScope variants. The logic correctly handles all scope types including the new AsyncFunction variant and the TypeParams variant (which is now properly scoped as a compiler scope rather than a symbol scope).


533-533: LGTM: Scope type comparisons correctly updated

All comparisons with scope types are properly updated to use CompilerScope variants, maintaining the same conditional logic with the new enum type.

Also applies to: 547-547, 552-552, 572-572


953-953: LGTM: Commented code appropriately documents removed variant

The commented line indicates that SymbolScope::Unknown was removed as part of this refactoring, which is appropriate since it's no longer needed in the current implementation.


2203-2203: LGTM: Function call correctly updated

The call to enter_scope is properly updated to pass CompilerScope::Class instead of the old enum variant, maintaining the same functionality.

compiler/codegen/src/symboltable.rs (11)

34-34: LGTM: Enum type correctly updated.

The type annotation has been properly updated from SymbolTableType to CompilerScope.


60-60: LGTM: Function parameter type correctly updated.

The parameter type has been properly updated to use CompilerScope.


91-99: LGTM: Enum renaming and extension looks good.

The enum has been properly renamed from SymbolTableType to CompilerScope with the addition of the AsyncFunction variant. The variant naming is consistent with the existing pattern.


101-119: LGTM: Display implementation correctly updated.

The Display trait implementation has been properly updated to handle the new enum name and includes the new AsyncFunction variant with appropriate formatting.


537-537: LGTM: Comprehensive async function handling.

The match arm correctly includes all function-like scopes including the new AsyncFunction variant alongside Function and Lambda. This ensures consistent handling of async functions in symbol analysis.


641-641: LGTM: Module scope initialization correctly updated.

The top-level scope initialization properly uses the new CompilerScope::Module enum reference.


656-656: LGTM: Function signature correctly updated.

The function parameter type has been properly updated to use CompilerScope.


660-660: LGTM: Enum reference correctly updated.

The comparison properly uses the new CompilerScope::Function enum reference.


751-751: LGTM: Type parameter scope references correctly updated.

All references to CompilerScope::TypeParams are properly updated and consistent throughout the file.

Also applies to: 779-779, 971-971


1456-1456: LGTM: Function scope creation correctly updated.

The function scope creation properly uses the new CompilerScope::Function enum reference.


326-326: LGTM: All remaining enum references correctly updated.

All remaining references to the enum have been systematically and correctly updated from SymbolTableType to CompilerScope throughout the file. The changes are consistent and maintain the correct functionality.

Also applies to: 352-352, 362-362, 368-368, 432-433, 455-455, 480-480, 485-485, 526-526, 529-529, 555-555, 586-586, 1173-1173, 1193-1193, 1259-1259, 1290-1290

Comment on lines 71 to 74
#[pymethod]
fn is_optimized(&self) -> bool {
self.symtable.typ == SymbolTableType::Function
self.symtable.typ == CompilerScope::Function
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

is_optimized() ignores AsyncFunction scopes
With CompilerScope now containing an AsyncFunction variant, the optimisation check should treat both Function and AsyncFunction the same way (mirroring CPython, where async defs share the function-block semantics).

-        fn is_optimized(&self) -> bool {
-            self.symtable.typ == CompilerScope::Function
+        fn is_optimized(&self) -> bool {
+            matches!(
+                self.symtable.typ,
+                CompilerScope::Function | CompilerScope::AsyncFunction
+            )
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[pymethod]
fn is_optimized(&self) -> bool {
self.symtable.typ == SymbolTableType::Function
self.symtable.typ == CompilerScope::Function
}
#[pymethod]
fn is_optimized(&self) -> bool {
matches!(
self.symtable.typ,
CompilerScope::Function | CompilerScope::AsyncFunction
)
}
🤖 Prompt for AI Agents
In vm/src/stdlib/symtable.rs around lines 71 to 74, the is_optimized() method
currently only checks if self.symtable.typ equals CompilerScope::Function,
ignoring the AsyncFunction variant. Update the method to return true if
self.symtable.typ is either CompilerScope::Function or
CompilerScope::AsyncFunction, ensuring both are treated equivalently for
optimization checks.

@youknowone youknowone merged commit b965ce7 into RustPython:main Jul 15, 2025
12 checks passed
@youknowone youknowone deleted the symbol-scope branch July 15, 2025 08:56
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