Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Use ruff for unparse backend
  • Loading branch information
ShaharNaveh committed Sep 3, 2025
commit 38a73167bc279944fbf33f54c4cc18118f980e1d
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ ruff_python_parser = { git = "https://github.com/astral-sh/ruff.git", tag = "0.1
ruff_python_ast = { git = "https://github.com/astral-sh/ruff.git", tag = "0.11.0" }
ruff_text_size = { git = "https://github.com/astral-sh/ruff.git", tag = "0.11.0" }
ruff_source_file = { git = "https://github.com/astral-sh/ruff.git", tag = "0.11.0" }
ruff_python_codegen = { git = "https://github.com/astral-sh/ruff.git", tag = "0.11.0" }

ahash = "0.8.11"
ascii = "1.1"
Expand Down
3 changes: 3 additions & 0 deletions compiler/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ rustpython-compiler-core = { workspace = true }
rustpython-literal = {workspace = true }
rustpython-wtf8 = { workspace = true }
ruff_python_ast = { workspace = true }
ruff_python_parser = { workspace = true }
ruff_python_codegen = { workspace = true }
ruff_source_file = { workspace = true }
ruff_text_size = { workspace = true }

ahash = { workspace = true }
Expand Down
21 changes: 16 additions & 5 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::{
error::{CodegenError, CodegenErrorType, InternalError, PatternUnreachableReason},
ir::{self, BlockIdx},
symboltable::{self, CompilerScope, SymbolFlags, SymbolScope, SymbolTable},
unparse::UnparseExpr,
};
use itertools::Itertools;
use malachite_bigint::BigInt;
Expand All @@ -31,6 +30,7 @@ use ruff_python_ast::{
TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, UnaryOp,
WithItem,
};
use ruff_source_file::LineEnding;
use ruff_text_size::{Ranged, TextRange};
use rustpython_compiler_core::{
Mode, OneIndexed, SourceFile, SourceLocation,
Expand Down Expand Up @@ -147,6 +147,19 @@ enum ComprehensionType {
Dict,
}

fn unparse_expr(expr: &Expr) -> String {
// Hack, because we can't do `ruff_python_codegen::Indentation::default()`
// https://github.com/astral-sh/ruff/pull/20216
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see that it was already merged astral-sh/ruff#20216 (that was fast 😅 )

I'll wait for it be released, and I wanted to upgrade ruff anyway so it will be a good excuse to do so

let indentation = {
let contents = r"x = 1";
let module = ruff_python_parser::parse_module(contents).unwrap();
let stylist = ruff_python_codegen::Stylist::from_tokens(module.tokens(), contents);
stylist.indentation().clone()
};

ruff_python_codegen::Generator::new(&indentation, LineEnding::default()).expr(expr)
}

/// Compile an Mod produced from ruff parser
pub fn compile_top(
ast: ruff_python_ast::Mod,
Expand Down Expand Up @@ -3592,7 +3605,7 @@ impl Compiler {
| Expr::NoneLiteral(_)
);
let key_repr = if is_literal {
UnparseExpr::new(key, &self.source_file).to_string()
unparse_expr(key)
} else if is_attribute {
String::new()
} else {
Expand Down Expand Up @@ -4146,9 +4159,7 @@ impl Compiler {
fn compile_annotation(&mut self, annotation: &Expr) -> CompileResult<()> {
if self.future_annotations {
self.emit_load_const(ConstantData::Str {
value: UnparseExpr::new(annotation, &self.source_file)
.to_string()
.into(),
value: unparse_expr(annotation).into(),
});
} else {
let was_in_annotation = self.in_annotation;
Expand Down
1 change: 0 additions & 1 deletion compiler/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub mod error;
pub mod ir;
mod string_parser;
pub mod symboltable;
mod unparse;

pub use compile::CompileOpts;
use ruff_python_ast::Expr;
Expand Down
Loading