8000 Update unparse to work with ruff & remove ruff_python_codegen · RustPython/RustPython@e331c35 · GitHub
[go: up one dir, main page]

Skip to content

Commit e331c35

Browse files
committed
Update unparse to work with ruff & remove ruff_python_codegen
1 parent 8081e0d commit e331c35

File tree

8 files changed

+219
-275
lines changed

8 files changed

+219
-275
lines changed

Cargo.lock

Lines changed: 1 addition & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_future_stmt/test_future.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@ def _exec_future(self, code):
198198
)
199199
return scope
200200

201-
# TODO: RUSTPYTHON
202-
@unittest.expectedFailure
203201
def test_annotations(self):
204202
eq = self.assertAnnotationEqual
205203
eq('...')
@@ -364,8 +362,6 @@ def test_annotations(self):
364362
eq('(((a, b)))', '(a, b)')
365363
eq("1 + 2 + 3")
366364

367-
# TODO: RUSTPYTHON
368-
@unittest.expectedFailure
369365
def test_fstring_debug_annotations(self):
370366
# f-strings with '=' don't round trip very well, so set the expected
371367
# result explicitly.
@@ -376,8 +372,6 @@ def test_fstring_debug_annotations(self):
376372
self.assertAnnotationEqual("f'{x=!a}'", expected="f'x={x!a}'")
377373
self.assertAnnotationEqual("f'{x=!s:*^20}'", expected="f'x={x!s:*^20}'")
378374

379-
# TODO: RUSTPYTHON
380-
@unittest.expectedFailure
381375
def test_infinity_numbers(self):
382376
inf = "1e" + repr(sys.float_info.max_10_exp + 1)
383377
infj = f"{inf}j"

compiler/codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ license.workspace = true
1414
# rustpython-parser-core = { workspace = true }
1515
rustpython-compiler-core = { workspace = true }
1616
rustpython-compiler-source = {workspace = true }
17+
rustpython-literal = {workspace = true }
1718
rustpython-wtf8 = { workspace = true }
1819
ruff_python_ast = { workspace = true }
1920
ruff_text_size = { workspace = true }
2021
ruff_source_file = { workspace = true }
21-
ruff_python_codegen = { workspace = true }
2222

2323
ahash = { workspace = true }
2424
bitflags = { workspace = true }

compiler/codegen/src/compile.rs

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
error::{CodegenError, CodegenErrorType},
1313
ir,
1414
symboltable::{self, SymbolFlags, SymbolScope, SymbolTable},
15+
unparse::unparse_expr,
1516
};
1617
use itertools::Itertools;
1718
use malachite_bigint::BigInt;
@@ -2026,11 +2027,10 @@ impl Compiler<'_> {
20262027

20272028
fn compile_annotation(&mut self, annotation: &Expr) -> CompileResult<()> {
20282029
if self.future_annotations {
2029-
// FIXME: codegen?
2030-
let ident = Default::default();
2031-
let codegen = ruff_python_codegen::Generator::new(&ident, Default::default());
20322030
self.emit_load_const(ConstantData::Str {
2033-
value: codegen.expr(annotation).into(),
2031+
value: unparse_expr(annotation, &self.source_code)
2032+
.to_string()
2033+
.into(),
20342034
});
20352035
} else {
2036 67E6 2036
self.compile_expression(annotation)?;
@@ -3397,7 +3397,9 @@ impl Compiler<'_> {
33973397
flags: FStringFlags,
33983398
fstring_elements: &FStringElements,
33993399
) -> CompileResult<()> {
3400+
let mut element_count = 0;
34003401
for element in fstring_elements {
3402+
element_count += 1;
34013403
match element {
34023404
FStringElement::Literal(string) => {
34033405
if string.value.contains(char::REPLACEMENT_CHARACTER) {
@@ -3419,26 +3421,14 @@ impl Compiler<'_> {
34193421
FStringElement::Expression(fstring_expr) => {
34203422
let mut conversion = fstring_expr.conversion;
34213423

3422-
let debug_text_count = match &fstring_expr.debug_text {
3423-
None => 0,
3424-
Some(DebugText { leading, trailing }) => {
3425-
let range = fstring_expr.expression.range();
3426-
let source = self.source_code.get_range(range);
3427-
let source = source.to_string();
3424+
if let Some(DebugText { leading, trailing }) = &fstring_expr.debug_text {
3425+
let range = fstring_expr.expression.range();
3426+
let source = self.source_code.get_range(range);
3427+
let text = [leading, source, trailing].concat();
34283428

3429-
self.emit_load_const(ConstantData::Str {
3430-
value: leading.to_string().into(),
3431-
});
3432-
self.emit_load_const(ConstantData::Str {
3433-
value: source.into(),
3434-
});
3435-
self.emit_load_const(ConstantData::Str {
3436-
value: trailing.to_string().into(),
3437-
});
3438-
3439-
3
3440-
}
3441-
};
3429+
self.emit_load_const(ConstantData::Str { value: text.into() });
3430+
element_count += 1;
3431+
}
34423432

34433433
match &fstring_expr.format_spec {
34443434
None => {
@@ -3447,7 +3437,9 @@ impl Compiler<'_> {
34473437
});
34483438
// Match CPython behavior: If debug text is present, apply repr conversion.
34493439
// See: https://github.com/python/cpython/blob/f61afca262d3a0aa6a8a501db0b1936c60858e35/Parser/action_helpers.c#L1456
3450-
if conversion == ConversionFlag::None && debug_text_count > 0 {
3440+
if conversion == ConversionFlag::None
3441+
&& fstring_expr.debug_text.is_some()
3442+
{
34513443
conversion = ConversionFlag::Repr;
34523444
}
34533445
}
@@ -3465,24 +3457,10 @@ impl Compiler<'_> {
34653457
ConversionFlag::Repr => bytecode::ConversionFlag::Repr,
34663458
};
34673459
emit!(self, Instruction::FormatValue { conversion });
3468-
3469-
// concatenate formatted string and debug text (if present)
3470-
if debug_text_count > 0 {
3471-
emit!(
3472-
self,
3473-
Instruction::BuildString {
3474-
size: debug_text_count + 1
3475-
}
3476-
);
3477-
}
34783460
}
34793461
}
34803462
}
34813463

3482-
let element_count: u32 = fstring_elements
3483-
.len()
3484-
.try_into()
3485-
.expect("BuildString size overflowed");
34863464
if element_count == 0 {
34873465
// ensure to put an empty string on the stack if there aren't any fstring elements
34883466
self.emit_load_const(ConstantData::Str {

compiler/codegen/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod error;
1313
pub mod ir;
1414
mod string_parser;
1515
pub mod symboltable;
16+
mod unparse;
1617

1718
pub use compile::CompileOpts;
1819
use ruff_python_ast::Expr;

0 commit comments

Comments
 (0)
0