-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fn unparse_expr
-> UnparseExpr::new
#6121
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
Conversation
WalkthroughReplaces the free function unparse::unparse_expr with the associated constructor UnparseExpr::new across the codegen. Updates compile.rs call sites accordingly. Removes the old function, adds the constructor, and keeps formatting behavior unchanged via Display on UnparseExpr. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Caller as Codegen caller
participant UE as UnparseExpr
participant Disp as Display
participant U as Unparser
Caller->>UE: UnparseExpr::new(expr, source)
Note right of UE: Stores expr/source
Caller->>Disp: to_string() on UnparseExpr
Disp->>U: Unparser::new(f, source)
U-->>Disp: write formatted expr
Disp-->>Caller: String
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
Status, Documentation and Community
|
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
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
compiler/codegen/src/compile.rs
(3 hunks)compiler/codegen/src/unparse.rs
(7 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.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
Files:
compiler/codegen/src/compile.rs
compiler/codegen/src/unparse.rs
🧬 Code graph analysis (1)
compiler/codegen/src/compile.rs (1)
compiler/codegen/src/unparse.rs (2)
new
(34-36)new
(613-615)
⏰ 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). (10)
- GitHub Check: Check the WASM package and demo
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Run snippets and cpython tests on wasm-wasi
- GitHub Check: Ensure compilation on various targets
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Check Rust code with rustfmt and clippy
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Run rust tests (ubuntu-latest)
🔇 Additional comments (5)
compiler/codegen/src/compile.rs (3)
3595-3604
: Call-site migration preserves behavior.
UnparseExpr::new(key, &self.source_file).to_string()
is a drop-in replacement for the prior helper. No functional change.
4149-4152
: Annotation unparsing change is correct.
UnparseExpr::new(...).to_string().into()
properly producesWtf8Buf
.
17-18
: Approve import switch—no lingering free function calls
Verified that allunparse_expr(
occurrences are method definitions orself.unparse_expr
calls incompiler/codegen/src/unparse.rs
; no standalone free‐function invocations remain.compiler/codegen/src/unparse.rs (2)
34-36
: Minor constructor style tweak — OK.Using
Self { .. }
is idiomatic.
612-616
: Public constructor addition — OK.
pub const fn new(...) -> Self
mirrors the old free function’s const-ness and keeps lifetimes intact.
write!(self, ": {}", UnparseExpr::new(body, self.source))?; | ||
}) |
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.
write!(self, ...) requires implementing fmt::Write for Unparser (compile error otherwise).
write!
calls target std::fmt::Write::write_fmt
, so the destination must implement fmt::Write
. Unparser
currently does not, which will fail to compile.
Apply this diff to implement fmt::Write
and forward to the inner formatter:
@@
impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
@@
fn write_fmt(&mut self, f: fmt::Arguments<'_>) -> fmt::Result {
self.f.write_fmt(f)
}
}
+impl<'a, 'b, 'c> fmt::Write for Unparser<'a, 'b, 'c> {
+ fn write_str(&mut self, s: &str) -> fmt::Result {
+ self.f.write_str(s)
+ }
+ fn write_char(&mut self, c: char) -> fmt::Result {
+ self.f.write_char(c)
+ }
+}
Alternatively, replace each write!(self, "...", ...)
with self.write_fmt(format_args!("...", ...))?
, but the trait impl keeps call sites cleaner.
Also applies to: 197-199, 275-277, 285-289, 483-486, 492-494
🤖 Prompt for AI Agents
In compiler/codegen/src/unparse.rs around lines 172-173 (also applies to
197-199, 275-277, 285-289, 483-486, 492-494): the code uses write!(self, ...)
but Unparser does not implement std::fmt::Write so compilation fails; implement
the fmt::Write trait for Unparser and forward its methods to the inner
formatter/collector (i.e., implement fn write_str(&mut self, s: &str) ->
fmt::Result and any other required fmt::Write methods by delegating to the inner
formatter or its write methods), so existing write! call sites compile unchanged
(alternatively replace write! calls with self.write_fmt(format_args!(...)) at
each site if you prefer not to add the trait impl).
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.
The change looks good.
On the other hand, Ruff also have the corresponding part. Checking if we can share the implement may be worth
Summary by CodeRabbit
Refactor
Style