Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
error::{CodegenError, CodegenErrorType, InternalError, PatternUnreachableReason},
ir::{self, BlockIdx},
symboltable::{self, CompilerScope, SymbolFlags, SymbolScope, SymbolTable},
unparse::unparse_expr,
unparse::UnparseExpr,
};
use itertools::Itertools;
use malachite_bigint::BigInt;
Expand Down Expand Up @@ -3592,7 +3592,7 @@ impl Compiler {
| Expr::NoneLiteral(_)
);
let key_repr = if is_literal {
unparse_expr(key, &self.source_file).to_string()
UnparseExpr::new(key, &self.source_file).to_string()
} else if is_attribute {
String::new()
} else {
Expand Down Expand Up @@ -4146,7 +4146,7 @@ impl Compiler {
fn compile_annotation(&mut self, annotation: &Expr) -> CompileResult<()> {
if self.future_annotations {
self.emit_load_const(ConstantData::Str {
value: unparse_expr(annotation, &self.source_file)
value: UnparseExpr::new(annotation, &self.source_file)
.to_string()
.into(),
});
Expand Down
24 changes: 15 additions & 9 deletions compiler/codegen/src/unparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct Unparser<'a, 'b, 'c> {

impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
const fn new(f: &'b mut fmt::Formatter<'a>, source: &'c SourceFile) -> Self {
Unparser { f, source }
Self { f, source }
}

fn p(&mut self, s: &str) -> fmt::Result {
Expand Down Expand Up @@ -169,7 +169,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
} else {
self.p("lambda")?;
}
write!(self, ": {}", unparse_expr(body, self.source))?;
write!(self, ": {}", UnparseExpr::new(body, self.source))?;
})
Comment on lines +172 to 173
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

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

}
Expr::If(ruff::ExprIf {
Expand All @@ -195,7 +195,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
for item in items {
self.p_delim(&mut first, ", ")?;
if let Some(k) = &item.key {
write!(self, "{}: ", unparse_expr(k, self.source))?;
write!(self, "{}: ", UnparseExpr::new(k, self.source))?;
} else {
self.p("**")?;
}
Expand Down Expand Up @@ -273,7 +273,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
range: _range,
}) => {
if let Some(value) = value {
write!(self, "(yield {})", unparse_expr(value, self.source))?;
write!(self, "(yield {})", UnparseExpr::new(value, self.source))?;
} else {
self.p("(yield)")?;
}
Expand All @@ -282,7 +282,11 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
value,
range: _range,
}) => {
write!(self, "(yield from {})", unparse_expr(value, self.source))?;
write!(
self,
"(yield from {})",
UnparseExpr::new(value, self.source)
)?;
}
Expr::Compare(ruff::ExprCompare {
left,
Expand Down Expand Up @@ -478,15 +482,15 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
fn unparse_function_arg(&mut self, arg: &ParameterWithDefault) -> fmt::Result {
self.unparse_arg(&arg.parameter)?;
if let Some(default) = &arg.default {
write!(self, "={}", unparse_expr(default, self.source))?;
write!(self, "={}", UnparseExpr::new(default, self.source))?;
}
Ok(())
}

fn unparse_arg(&mut self, arg: &Parameter) -> fmt::Result {
self.p_id(&arg.name)?;
if let Some(ann) = &arg.annotation {
write!(self, ": {}", unparse_expr(ann, self.source))?;
write!(self, ": {}", UnparseExpr::new(ann, self.source))?;
}
Ok(())
}
Expand Down Expand Up @@ -605,8 +609,10 @@ pub struct UnparseExpr<'a> {
source: &'a SourceFile,
}

pub const fn unparse_expr<'a>(expr: &'a Expr, source: &'a SourceFile) -> UnparseExpr<'a> {
UnparseExpr { expr, source }
impl<'a> UnparseExpr<'a> {
pub const fn new(expr: &'a Expr, source: &'a SourceFile) -> Self {
Self { expr, source }
}
}

impl fmt::Display for UnparseExpr<'_> {
Expand Down
Loading