diff --git a/Cargo.lock b/Cargo.lock index 88146885d85..efbe2bbf6e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -143,6 +143,36 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "attribute-derive" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0053e96dd3bec5b4879c23a138d6ef26f2cb936c9cdc96274ac2b9ed44b5bb54" +dependencies = [ + "attribute-derive-macro", + "derive-where", + "manyhow", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "attribute-derive-macro" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "463b53ad0fd5b460af4b1915fe045ff4d946d025fb6c4dc3337752eaa980f71b" +dependencies = [ + "collection_literals", + "interpolator", + "manyhow", + "proc-macro-utils", + "proc-macro2", + "quote", + "quote-use", + "syn", +] + [[package]] name = "autocfg" version = "1.5.0" @@ -381,24 +411,16 @@ dependencies = [ ] [[package]] -name = "colorchoice" -version = "1.0.4" +name = "collection_literals" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" +checksum = "26b3f65b8fb8e88ba339f7d23a390fe1b0896217da05e2a66c584c9b29a91df8" [[package]] -name = "compact_str" -version = "0.8.1" +name = "colorchoice" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b79c4069c6cad78e2e0cdfcbd26275770669fb39fd308a752dc110e83b9af32" -dependencies = [ - "castaway", - "cfg-if", - "itoa", - "rustversion", - "ryu", - "static_assertions", -] +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" [[package]] name = "compact_str" @@ -527,7 +549,7 @@ dependencies = [ "cranelift-entity", "cranelift-isle", "gimli", - "hashbrown", + "hashbrown 0.15.5", "log", "regalloc2", "rustc-hash", @@ -729,6 +751,17 @@ dependencies = [ "memchr", ] +[[package]] +name = "derive-where" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef941ded77d15ca19b40374869ac6000af1c9f2a4c0f3d4c70926287e6364a8f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "digest" version = "0.10.7" @@ -961,6 +994,29 @@ dependencies = [ "version_check", ] +[[package]] +name = "get-size-derive2" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a443e77201a230c25f0c11574e9b20e5705f749520e0f30ab0d0974fb1a794" +dependencies = [ + "attribute-derive", + "quote", + "syn", +] + +[[package]] +name = "get-size2" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0594e2a78d082f2f8b1615c728391c6a5277f6c017474a7249934fc735945d55" +dependencies = [ + "compact_str", + "get-size-derive2", + "hashbrown 0.16.0", + "smallvec", +] + [[package]] name = "gethostname" version = "1.0.2" @@ -1041,6 +1097,12 @@ dependencies = [ "foldhash", ] +[[package]] +name = "hashbrown" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d" + [[package]] name = "heck" version = "0.5.0" @@ -1100,12 +1162,12 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.11.3" +version = "2.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92119844f513ffa41556430369ab02c295a3578af21cf945caa3e9e0c2481ac3" +checksum = "4b0f83760fb341a774ed326568e19f5a863af4a952def8c39f9ab92fd95b88e5" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.16.0", ] [[package]] @@ -1125,6 +1187,12 @@ dependencies = [ "similar", ] +[[package]] +name = "interpolator" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71dd52191aae121e8611f1e8dc3e324dd0dd1dee1e6dd91d10ee07a3cfb4d9d8" + [[package]] name = "is-macro" version = "0.3.7" @@ -1406,7 +1474,7 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c738d3789301e957a8f7519318fcbb1b92bb95863b28f6938ae5a05be6259f34" dependencies = [ - "hashbrown", + "hashbrown 0.15.5", "itertools 0.14.0", "libm", "ryu", @@ -1448,6 +1516,29 @@ dependencies = [ "malachite-nz", ] +[[package]] +name = "manyhow" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b33efb3ca6d3b07393750d4030418d594ab1139cee518f0dc88db70fec873587" +dependencies = [ + "manyhow-macros", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "manyhow-macros" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46fce34d199b78b6e6073abf984c9cf5fd3e9330145a93ee0738a7443e371495" +dependencies = [ + "proc-macro-utils", + "proc-macro2", + "quote", +] + [[package]] name = "maplit" version = "1.0.2" @@ -1855,6 +1946,17 @@ dependencies = [ "syn", ] +[[package]] +name = "proc-macro-utils" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eeaf08a13de400bc215877b5bdc088f241b12eb42f0a548d3390dc1c56bb7071" +dependencies = [ + "proc-macro2", + "quote", + "smallvec", +] + [[package]] name = "proc-macro2" version = "1.0.101" @@ -1943,6 +2045,28 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "quote-use" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9619db1197b497a36178cfc736dc96b271fe918875fbf1344c436a7e93d0321e" +dependencies = [ + "quote", + "quote-use-macros", +] + +[[package]] +name = "quote-use-macros" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82ebfb7faafadc06a7ab141a6f67bcfb24cb8beb158c6fe933f2f035afa99f35" +dependencies = [ + "proc-macro-utils", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "r-efi" version = "5.3.0" @@ -2081,7 +2205,7 @@ checksum = "dc06e6b318142614e4a48bc725abbf08ff166694835c43c9dae5a9009704639a" dependencies = [ "allocator-api2", "bumpalo", - "hashbrown", + "hashbrown 0.15.5", "log", "rustc-hash", "smallvec", @@ -2149,23 +2273,6 @@ dependencies = [ "syn", ] -[[package]] -name = "ruff_python_ast" -version = "0.0.0" -source = "git+https://github.com/astral-sh/ruff.git?tag=0.11.0#2cd25ef6410fb5fca96af1578728a3d828d2d53a" -dependencies = [ - "aho-corasick", - "bitflags 2.9.4", - "compact_str 0.8.1", - "is-macro", - "itertools 0.14.0", - "memchr", - "ruff_python_trivia 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", - "ruff_source_file 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", - "ruff_text_size 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", - "rustc-hash", -] - [[package]] name = "ruff_python_ast" version = "0.0.0" @@ -2173,13 +2280,14 @@ source = "git+https://github.com/astral-sh/ruff.git?tag=0.13.1#706be0a6e7e099365 dependencies = [ "aho-corasick", "bitflags 2.9.4", - "compact_str 0.9.0", + "compact_str", + "get-size2", "is-macro", "itertools 0.14.0", "memchr", - "ruff_python_trivia 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.13.1)", - "ruff_source_file 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.13.1)", - "ruff_text_size 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.13.1)", + "ruff_python_trivia", + "ruff_source_file", + "ruff_text_size", "rustc-hash", "thiserror 2.0.16", ] @@ -2187,15 +2295,16 @@ dependencies = [ [[package]] name = "ruff_python_parser" version = "0.0.0" -source = "git+https://github.com/astral-sh/ruff.git?tag=0.11.0#2cd25ef6410fb5fca96af1578728a3d828d2d53a" +source = "git+https://github.com/astral-sh/ruff.git?tag=0.13.1#706be0a6e7e09936511198f2ff8982915520d138" dependencies = [ "bitflags 2.9.4", "bstr", - "compact_str 0.8.1", + "compact_str", + "get-size2", "memchr", - "ruff_python_ast 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", - "ruff_python_trivia 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", - "ruff_text_size 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", + "ruff_python_ast", + "ruff_python_trivia", + "ruff_text_size", "rustc-hash", "static_assertions", "unicode-ident", @@ -2203,55 +2312,33 @@ dependencies = [ "unicode_names2 1.3.0", ] -[[package]] -name = "ruff_python_trivia" -version = "0.0.0" -source = "git+https://github.com/astral-sh/ruff.git?tag=0.11.0#2cd25ef6410fb5fca96af1578728a3d828d2d53a" -dependencies = [ - "itertools 0.14.0", - "ruff_source_file 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", - "ruff_text_size 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", - "unicode-ident", -] - [[package]] name = "ruff_python_trivia" version = "0.0.0" source = "git+https://github.com/astral-sh/ruff.git?tag=0.13.1#706be0a6e7e09936511198f2ff8982915520d138" dependencies = [ "itertools 0.14.0", - "ruff_source_file 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.13.1)", - "ruff_text_size 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.13.1)", + "ruff_source_file", + "ruff_text_size", "unicode-ident", ] -[[package]] -name = "ruff_source_file" -version = "0.0.0" -source = "git+https://github.com/astral-sh/ruff.git?tag=0.11.0#2cd25ef6410fb5fca96af1578728a3d828d2d53a" -dependencies = [ - "memchr", - "ruff_text_size 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", -] - [[package]] name = "ruff_source_file" version = "0.0.0" source = "git+https://github.com/astral-sh/ruff.git?tag=0.13.1#706be0a6e7e09936511198f2ff8982915520d138" dependencies = [ "memchr", - "ruff_text_size 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.13.1)", + "ruff_text_size", ] -[[package]] -name = "ruff_text_size" -version = "0.0.0" -source = "git+https://github.com/astral-sh/ruff.git?tag=0.11.0#2cd25ef6410fb5fca96af1578728a3d828d2d53a" - [[package]] name = "ruff_text_size" version = "0.0.0" source = "git+https://github.com/astral-sh/ruff.git?tag=0.13.1#706be0a6e7e09936511198f2ff8982915520d138" +dependencies = [ + "get-size2", +] [[package]] name = "rustc-hash" @@ -2309,9 +2396,9 @@ dependencies = [ "memchr", "num-complex", "num-traits", - "ruff_python_ast 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.13.1)", + "ruff_python_ast", "ruff_python_parser", - "ruff_text_size 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", + "ruff_text_size", "rustpython-compiler-core", "rustpython-literal", "rustpython-wtf8", @@ -2350,10 +2437,10 @@ dependencies = [ name = "rustpython-compiler" version = "0.4.0" dependencies = [ - "ruff_python_ast 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.13.1)", + "ruff_python_ast", "ruff_python_parser", - "ruff_source_file 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", - "ruff_text_size 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", + "ruff_source_file", + "ruff_text_size", "rustpython-codegen", "rustpython-compiler-core", "thiserror 2.0.16", @@ -2368,7 +2455,7 @@ dependencies = [ "lz4_flex", "malachite-bigint", "num-complex", - "ruff_source_file 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", + "ruff_source_file", "rustpython-wtf8", ] @@ -2570,9 +2657,9 @@ dependencies = [ "parking_lot", "paste", "result-like", - "ruff_python_ast 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.13.1)", + "ruff_python_ast", "ruff_python_parser", - "ruff_text_size 0.0.0 (git+https://github.com/astral-sh/ruff.git?tag=0.11.0)", + "ruff_text_size", "rustix", "rustpython-codegen", "rustpython-common", diff --git a/Cargo.toml b/Cargo.toml index 4412fb9354a..79019e1098b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -158,10 +158,10 @@ rustpython-sre_engine = { path = "vm/sre_engine", version = "0.4.0" } rustpython-wtf8 = { path = "wtf8", version = "0.4.0" } rustpython-doc = { git = "https://github.com/RustPython/__doc__", tag = "0.3.0", version = "0.3.0" } -ruff_python_parser = { git = "https://github.com/astral-sh/ruff.git", tag = "0.11.0" } -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_parser = { git = "https://github.com/astral-sh/ruff.git", tag = "0.13.1" } +ruff_python_ast = { git = "https://github.com/astral-sh/ruff.git", tag = "0.13.1" } +ruff_text_size = { git = "https://github.com/astral-sh/ruff.git", tag = "0.13.1" } +ruff_source_file = { git = "https://github.com/astral-sh/ruff.git", tag = "0.13.1" } ahash = "0.8.12" ascii = "1.1" diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index c509052a56a..0662c9ece2b 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -24,16 +24,16 @@ use ruff_python_ast::{ Alias, Arguments, BoolOp, CmpOp, Comprehension, ConversionFlag, DebugText, Decorator, DictItem, ExceptHandler, ExceptHandlerExceptHandler, Expr, ExprAttribute, ExprBoolOp, ExprContext, ExprFString, ExprList, ExprName, ExprSlice, ExprStarred, ExprSubscript, ExprTuple, ExprUnaryOp, - FString, FStringElement, FStringElements, FStringFlags, FStringPart, Identifier, Int, Keyword, - MatchCase, ModExpression, ModModule, Operator, Parameters, Pattern, PatternMatchAs, - PatternMatchClass, PatternMatchMapping, PatternMatchOr, PatternMatchSequence, - PatternMatchSingleton, PatternMatchStar, PatternMatchValue, Singleton, Stmt, StmtExpr, - TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, UnaryOp, - WithItem, + FString, FStringFlags, FStringPart, Identifier, Int, InterpolatedElement, + InterpolatedStringElement, InterpolatedStringElements, Keyword, MatchCase, ModExpression, + ModModule, Operator, Parameters, Pattern, PatternMatchAs, PatternMatchClass, + PatternMatchMapping, PatternMatchOr, PatternMatchSequence, PatternMatchSingleton, + PatternMatchStar, PatternMatchValue, Singleton, Stmt, StmtExpr, TypeParam, TypeParamParamSpec, + TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, UnaryOp, WithItem, }; use ruff_text_size::{Ranged, TextRange}; use rustpython_compiler_core::{ - Mode, OneIndexed, SourceFile, SourceLocation, + Mode, OneIndexed, PositionEncoding, SourceFile, SourceLocation, bytecode::{ self, Arg as OpArgMarker, BinaryOperator, CodeObject, ComparisonOperator, ConstantData, Instruction, OpArg, OpArgType, UnpackExArgs, @@ -240,18 +240,18 @@ fn eprint_location(zelf: &Compiler) { let start = zelf .source_file .to_source_code() - .source_location(zelf.current_source_range.start()); + .source_location(zelf.current_source_range.start(), PositionEncoding::Utf8); let end = zelf .source_file .to_source_code() - .source_location(zelf.current_source_range.end()); + .source_location(zelf.current_source_range.end(), PositionEncoding::Utf8); eprintln!( "LOCATION: {} from {}:{} to {}:{}", zelf.source_file.name(), - start.row, - start.column, - end.row, - end.column + start.line, + start.character_offset, + end.line, + end.character_offset ); } @@ -531,7 +531,7 @@ impl Compiler { let location = self .source_file .to_source_code() - .source_location(range.start()); + .source_location(range.start(), PositionEncoding::Utf8); CodegenError { error, location: Some(location), @@ -631,8 +631,8 @@ impl Compiler { ) -> CompileResult<()> { // Create location let location = SourceLocation { - row: OneIndexed::new(lineno as usize).unwrap_or(OneIndexed::MIN), - column: OneIndexed::new(1).unwrap(), + line: OneIndexed::new(lineno as usize).unwrap_or(OneIndexed::MIN), + character_offset: OneIndexed::MIN, }; // Allocate a new compiler unit @@ -769,8 +769,8 @@ impl Compiler { let _resume_loc = if scope_type == CompilerScope::Module { // Module scope starts with lineno 0 SourceLocation { - row: OneIndexed::MIN, - column: OneIndexed::MIN, + line: OneIndexed::MIN, + character_offset: OneIndexed::MIN, } } else { location @@ -4872,6 +4872,7 @@ impl Compiler { Expr::Named(ExprNamed { target, value, + node_index: _, range: _, }) => { self.compile_expression(value)?; @@ -4881,6 +4882,9 @@ impl Compiler { Expr::FString(fstring) => { self.compile_expr_fstring(fstring)?; } + Expr::TString(_) => { + return Err(self.error(CodegenErrorType::NotImplementedYet)); + } Expr::StringLiteral(string) => { let value = string.value.to_str(); if value.contains(char::REPLACEMENT_CHARACTER) { @@ -5334,7 +5338,7 @@ impl Compiler { let location = self .source_file .to_source_code() - .source_location(range.start()); + .source_location(range.start(), PositionEncoding::Utf8); // TODO: insert source filename self.current_block().instructions.push(ir::InstructionInfo { instr, @@ -5524,27 +5528,14 @@ impl Compiler { Expr::Named(ExprNamed { target, value, + node_index: _, range: _, }) => Self::contains_await(target) || Self::contains_await(value), - Expr::FString(ExprFString { value, range: _ }) => { - fn expr_element_contains_await bool>( - expr_element: &FStringExpressionElement, - contains_await: F, - ) -> bool { - contains_await(&expr_element.expression) - || expr_element - .format_spec - .iter() - .flat_map(|spec| spec.elements.expressions()) - .any(|element| expr_element_contains_await(element, contains_await)) - } - - value.elements().any(|element| match element { - FStringElement::Expression(expr_element) => { - expr_element_contains_await(expr_element, Self::contains_await) - } - FStringElement::Literal(_) => false, - }) + Expr::FString(fstring) => { + Self::interpolated_string_contains_await(fstring.value.elements()) + } + Expr::TString(tstring) => { + Self::interpolated_string_contains_await(tstring.value.elements()) } Expr::StringLiteral(_) | Expr::BytesLiteral(_) @@ -5556,6 +5547,29 @@ impl Compiler { } } + fn interpolated_string_contains_await<'a>( + mut elements: impl Iterator, + ) -> bool { + fn interpolated_element_contains_await bool>( + expr_element: &InterpolatedElement, + contains_await: F, + ) -> bool { + contains_await(&expr_element.expression) + || expr_element + .format_spec + .iter() + .flat_map(|spec| spec.elements.interpolations()) + .any(|element| interpolated_element_contains_await(element, contains_await)) + } + + elements.any(|element| match element { + InterpolatedStringElement::Interpolation(expr_element) => { + interpolated_element_contains_await(expr_element, Self::contains_await) + } + InterpolatedStringElement::Literal(_) => false, + }) + } + fn compile_expr_fstring(&mut self, fstring: &ExprFString) -> CompileResult<()> { let fstring = &fstring.value; for part in fstring { @@ -5602,13 +5616,13 @@ impl Compiler { fn compile_fstring_elements( &mut self, flags: FStringFlags, - fstring_elements: &FStringElements, + fstring_elements: &InterpolatedStringElements, ) -> CompileResult<()> { let mut element_count = 0; for element in fstring_elements { element_count += 1; match element { - FStringElement::Literal(string) => { + InterpolatedStringElement::Literal(string) => { if string.value.contains(char::REPLACEMENT_CHARACTER) { // might have a surrogate literal; should reparse to be sure let source = self.source_file.slice(string.range); @@ -5625,7 +5639,7 @@ impl Compiler { }); } } - FStringElement::Expression(fstring_expr) => { + InterpolatedStringElement::Interpolation(fstring_expr) => { let mut conversion = fstring_expr.conversion; if let Some(DebugText { leading, trailing }) = &fstring_expr.debug_text { @@ -5858,21 +5872,27 @@ mod ruff_tests { // f'{x}' let expr_x = Expr::Name(ExprName { + node_index: AtomicNodeIndex::NONE, range, id: Name::new("x"), ctx: ExprContext::Load, }); let not_present = &Expr::FString(ExprFString { + node_index: AtomicNodeIndex::NONE, range, value: FStringValue::single(FString { + node_index: AtomicNodeIndex::NONE, range, - elements: vec![FStringElement::Expression(FStringExpressionElement { - range, - expression: Box::new(expr_x), - debug_text: None, - conversion: ConversionFlag::None, - format_spec: None, - })] + elements: vec![InterpolatedStringElement::Interpolation( + InterpolatedElement { + node_index: AtomicNodeIndex::NONE, + range, + expression: Box::new(expr_x), + debug_text: None, + conversion: ConversionFlag::None, + format_spec: None, + }, + )] .into(), flags, }), @@ -5881,24 +5901,31 @@ mod ruff_tests { // f'{await x}' let expr_await_x = Expr::Await(ExprAwait { + node_index: AtomicNodeIndex::NONE, range, value: Box::new(Expr::Name(ExprName { + node_index: AtomicNodeIndex::NONE, range, id: Name::new("x"), ctx: ExprContext::Load, })), }); let present = &Expr::FString(ExprFString { + node_index: AtomicNodeIndex::NONE, range, value: FStringValue::single(FString { + node_index: AtomicNodeIndex::NONE, range, - elements: vec![FStringElement::Expression(FStringExpressionElement { - range, - expression: Box::new(expr_await_x), - debug_text: None, - conversion: ConversionFlag::None, - format_spec: None, - })] + elements: vec![InterpolatedStringElement::Interpolation( + InterpolatedElement { + node_index: AtomicNodeIndex::NONE, + range, + expression: Box::new(expr_await_x), + debug_text: None, + conversion: ConversionFlag::None, + format_spec: None, + }, + )] .into(), flags, }), @@ -5907,39 +5934,51 @@ mod ruff_tests { // f'{x:{await y}}' let expr_x = Expr::Name(ExprName { + node_index: AtomicNodeIndex::NONE, range, id: Name::new("x"), ctx: ExprContext::Load, }); let expr_await_y = Expr::Await(ExprAwait { + node_index: AtomicNodeIndex::NONE, range, value: Box::new(Expr::Name(ExprName { + node_index: AtomicNodeIndex::NONE, range, id: Name::new("y"), ctx: ExprContext::Load, })), }); let present = &Expr::FString(ExprFString { + node_index: AtomicNodeIndex::NONE, range, value: FStringValue::single(FString { + node_index: AtomicNodeIndex::NONE, range, - elements: vec![FStringElement::Expression(FStringExpressionElement { - range, - expression: Box::new(expr_x), - debug_text: None, - conversion: ConversionFlag::None, - format_spec: Some(Box::new(FStringFormatSpec { + elements: vec![InterpolatedStringElement::Interpolation( + InterpolatedElement { + node_index: AtomicNodeIndex::NONE, range, - elements: vec![FStringElement::Expression(FStringExpressionElement { + expression: Box::new(expr_x), + debug_text: None, + conversion: ConversionFlag::None, + format_spec: Some(Box::new(InterpolatedStringFormatSpec { + node_index: AtomicNodeIndex::NONE, range, - expression: Box::new(expr_await_y), - debug_text: None, - conversion: ConversionFlag::None, - format_spec: None, - })] - .into(), - })), - })] + elements: vec![InterpolatedStringElement::Interpolation( + InterpolatedElement { + node_index: AtomicNodeIndex::NONE, + range, + expression: Box::new(expr_await_y), + debug_text: None, + conversion: ConversionFlag::None, + format_spec: None, + }, + )] + .into(), + })), + }, + )] .into(), flags, }), diff --git a/compiler/codegen/src/ir.rs b/compiler/codegen/src/ir.rs index 31c89260913..7cd173c821d 100644 --- a/compiler/codegen/src/ir.rs +++ b/compiler/codegen/src/ir.rs @@ -182,7 +182,7 @@ impl CodeInfo { *arg = new_arg; } let (extras, lo_arg) = arg.split(); - locations.extend(std::iter::repeat_n(info.location.clone(), arg.instr_size())); + locations.extend(std::iter::repeat_n(info.location, arg.instr_size())); instructions.extend( extras .map(|byte| CodeUnit::new(Instruction::ExtendedArg, byte)) @@ -422,8 +422,8 @@ fn generate_linetable(locations: &[SourceLocation], first_line: i32) -> Box<[u8] // Get line and column information // SourceLocation always has row and column (both are OneIndexed) - let line = loc.row.get() as i32; - let col = (loc.column.get() as i32) - 1; // Convert 1-based to 0-based + let line = loc.line.get() as i32; + let col = loc.character_offset.to_zero_indexed() as i32; let line_delta = line - prev_line; diff --git a/compiler/codegen/src/lib.rs b/compiler/codegen/src/lib.rs index 9b444de994d..291b57d7f67 100644 --- a/compiler/codegen/src/lib.rs +++ b/compiler/codegen/src/lib.rs @@ -56,6 +56,7 @@ impl ToPythonName for Expr { Self::Starred { .. } => "starred", Self::Slice { .. } => "slice", Self::FString { .. } => "f-string expression", + Self::TString { .. } => "t-string expression", Self::Name { .. } => "name", Self::Lambda { .. } => "lambda", Self::If { .. } => "conditional expression", diff --git a/compiler/codegen/src/symboltable.rs b/compiler/codegen/src/symboltable.rs index 16e88ad6908..0464e09c138 100644 --- a/compiler/codegen/src/symboltable.rs +++ b/compiler/codegen/src/symboltable.rs @@ -19,7 +19,7 @@ use ruff_python_ast::{ Stmt, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, }; use ruff_text_size::{Ranged, TextRange}; -use rustpython_compiler_core::{SourceFile, SourceLocation}; +use rustpython_compiler_core::{PositionEncoding, SourceFile, SourceLocation}; use std::{borrow::Cow, fmt}; /// Captures all symbols in the current scope, and has a list of sub-scopes in this scope. @@ -793,6 +793,7 @@ impl SymbolTableBuilder { decorator_list, type_params, range, + node_index: _, }) => { if let Some(type_params) = type_params { self.enter_type_param_block( @@ -910,6 +911,7 @@ impl SymbolTableBuilder { value, simple, range, + node_index: _, }) => { // https://github.com/python/cpython/blob/main/Python/symtable.c#L1233 match &**target { @@ -1046,7 +1048,7 @@ impl SymbolTableBuilder { location: Some( self.source_file .to_source_code() - .source_location(expression.range().start()), + .source_location(expression.range().start(), PositionEncoding::Utf8), ), }); } @@ -1089,7 +1091,11 @@ impl SymbolTableBuilder { }) => { self.scan_expression(value, ExpressionContext::Load)?; } - Expr::Dict(ExprDict { items, range: _ }) => { + Expr::Dict(ExprDict { + items, + node_index: _, + range: _, + }) => { for item in items { if let Some(key) = &item.key { self.scan_expression(key, context)?; @@ -1097,15 +1103,27 @@ impl SymbolTableBuilder { self.scan_expression(&item.value, context)?; } } - Expr::Await(ExprAwait { value, range: _ }) => { + Expr::Await(ExprAwait { + value, + node_index: _, + range: _, + }) => { self.scan_expression(value, context)?; } - Expr::Yield(ExprYield { value, range: _ }) => { + Expr::Yield(ExprYield { + value, + node_index: _, + range: _, + }) => { if let Some(expression) = value { self.scan_expression(expression, context)?; } } - Expr::YieldFrom(ExprYieldFrom { value, range: _ }) => { + Expr::YieldFrom(ExprYieldFrom { + value, + node_index: _, + range: _, + }) => { self.scan_expression(value, context)?; } Expr::UnaryOp(ExprUnaryOp { @@ -1127,6 +1145,7 @@ impl SymbolTableBuilder { lower, upper, step, + node_index: _, range: _, }) => { if let Some(lower) = lower { @@ -1151,6 +1170,7 @@ impl SymbolTableBuilder { elt, generators, range, + node_index: _, }) => { self.scan_comprehension("genexpr", elt, None, generators, *range)?; } @@ -1158,6 +1178,7 @@ impl SymbolTableBuilder { elt, generators, range, + node_index: _, }) => { self.scan_comprehension("genexpr", elt, None, generators, *range)?; } @@ -1166,12 +1187,14 @@ impl SymbolTableBuilder { value, generators, range, + node_index: _, }) => { self.scan_comprehension("genexpr", key, Some(value), generators, *range)?; } Expr::Call(ExprCall { func, arguments, + node_index: _, range: _, }) => { match context { @@ -1218,6 +1241,7 @@ impl SymbolTableBuilder { Expr::Lambda(ExprLambda { body, parameters, + node_index: _, range: _, }) => { if let Some(parameters) = parameters { @@ -1244,15 +1268,25 @@ impl SymbolTableBuilder { self.leave_scope(); } Expr::FString(ExprFString { value, .. }) => { - for expr in value.elements().filter_map(|x| x.as_expression()) { + for expr in value.elements().filter_map(|x| x.as_interpolation()) { self.scan_expression(&expr.expression, ExpressionContext::Load)?; if let Some(format_spec) = &expr.format_spec { - for element in format_spec.elements.expressions() { + for element in format_spec.elements.interpolations() { self.scan_expression(&element.expression, ExpressionContext::Load)? } } } } + Expr::TString(tstring) => { + return Err(SymbolTableError { + error: "not yet implemented".into(), + location: Some( + self.source_file + .to_source_code() + .source_location(tstring.range.start(), PositionEncoding::Utf8), + ), + }); + } // Constants Expr::StringLiteral(_) | Expr::BytesLiteral(_) @@ -1265,6 +1299,7 @@ impl SymbolTableBuilder { test, body, orelse, + node_index: _, range: _, }) => { self.scan_expression(test, ExpressionContext::Load)?; @@ -1276,13 +1311,14 @@ impl SymbolTableBuilder { target, value, range, + node_index: _, }) => { // named expressions are not allowed in the definition of // comprehension iterator definitions if let ExpressionContext::IterDefinitionExp = context { return Err(SymbolTableError { error: "assignment expression cannot be used in a comprehension iterable expression".to_string(), - location: Some(self.source_file.to_source_code().source_location(target.range().start())), + location: Some(self.source_file.to_source_code().source_location(target.range().start(), PositionEncoding::Utf8)), }); } @@ -1393,6 +1429,7 @@ impl SymbolTableBuilder { bound, range: type_var_range, default, + node_index: _, }) => { self.register_name(name.as_str(), SymbolUsage::TypeParam, *type_var_range)?; @@ -1416,6 +1453,7 @@ impl SymbolTableBuilder { name, range: param_spec_range, default, + node_index: _, }) => { self.register_name(name, SymbolUsage::TypeParam, *param_spec_range)?; @@ -1429,6 +1467,7 @@ impl SymbolTableBuilder { name, range: type_var_tuple_range, default, + node_index: _, }) => { self.register_name(name, SymbolUsage::TypeParam, *type_var_tuple_range)?; @@ -1565,7 +1604,7 @@ impl SymbolTableBuilder { let location = self .source_file .to_source_code() - .source_location(range.start()); + .source_location(range.start(), PositionEncoding::Utf8); let location = Some(location); let scope_depth = self.tables.len(); let table = self.tables.last_mut().unwrap(); diff --git a/compiler/codegen/src/unparse.rs b/compiler/codegen/src/unparse.rs index f2fb86ed4ef..7f2bcf16b47 100644 --- a/compiler/codegen/src/unparse.rs +++ b/compiler/codegen/src/unparse.rs @@ -85,6 +85,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Expr::BoolOp(ruff::ExprBoolOp { op, values, + node_index: _, range: _range, }) => { let (op, prec) = op_prec!(bin, op, BoolOp, And("and", AND), Or("or", OR)); @@ -99,6 +100,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Expr::Named(ruff::ExprNamed { target, value, + node_index: _, range: _range, }) => { group_if!(precedence::TUPLE, { @@ -111,6 +113,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { left, op, right, + node_index: _, range: _range, }) => { let right_associative = matches!(op, Operator::Pow); @@ -141,6 +144,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Expr::UnaryOp(ruff::ExprUnaryOp { op, operand, + node_index: _, range: _range, }) => { let (op, prec) = op_prec!( @@ -160,6 +164,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Expr::Lambda(ruff::ExprLambda { parameters, body, + node_index: _, range: _range, }) => { group_if!(precedence::TEST, { @@ -176,6 +181,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { test, body, orelse, + node_index: _, range: _range, }) => { group_if!(precedence::TEST, { @@ -188,6 +194,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } Expr::Dict(ruff::ExprDict { items, + node_index: _, range: _range, }) => { self.p("{")?; @@ -205,6 +212,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } Expr::Set(ruff::ExprSet { elts, + node_index: _, range: _range, }) => { self.p("{")?; @@ -218,6 +226,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Expr::ListComp(ruff::ExprListComp { elt, generators, + node_index: _, range: _range, }) => { self.p("[")?; @@ -228,6 +237,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Expr::SetComp(ruff::ExprSetComp { elt, generators, + node_index: _, range: _range, }) => { self.p("{")?; @@ -239,6 +249,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { key, value, generators, + node_index: _, range: _range, }) => { self.p("{")?; @@ -252,6 +263,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { parenthesized: _, elt, generators, + node_index: _, range: _range, }) => { self.p("(")?; @@ -261,6 +273,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } Expr::Await(ruff::ExprAwait { value, + node_index: _, range: _range, }) => { group_if!(precedence::AWAIT, { @@ -270,6 +283,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } Expr::Yield(ruff::ExprYield { value, + node_index: _, range: _range, }) => { if let Some(value) = value { @@ -280,6 +294,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { } Expr::YieldFrom(ruff::ExprYieldFrom { value, + node_index: _, range: _range, }) => { write!( @@ -292,6 +307,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { left, ops, comparators, + node_index: _, range: _range, }) => { group_if!(precedence::CMP, { @@ -308,6 +324,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Expr::Call(ruff::ExprCall { func, arguments: Arguments { args, keywords, .. }, + node_index: _, range: _range, }) => { self.unparse_expr(func, precedence::ATOM)?; @@ -317,6 +334,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Expr::Generator(ruff::ExprGenerator { elt, generators, + node_index: _, range: _range, .. }), @@ -347,6 +365,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { self.p(")")?; } Expr::FString(ruff::ExprFString { value, .. }) => self.unparse_fstring(value)?, + Expr::TString(_) => self.p("t\"\"")?, Expr::StringLiteral(ruff::ExprStringLiteral { value, .. }) => { if value.is_unicode() { self.p("u")? @@ -435,6 +454,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { lower, upper, step, + node_index: _, range: _range, }) => { if let Some(lower) = lower { @@ -513,7 +533,10 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Ok(()) } - fn unparse_fstring_body(&mut self, elements: &[ruff::FStringElement]) -> fmt::Result { + fn unparse_fstring_body( + &mut self, + elements: &[ruff::InterpolatedStringElement], + ) -> fmt::Result { for elem in elements { self.unparse_fstring_elem(elem)?; } @@ -525,7 +548,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { val: &Expr, debug_text: Option<&ruff::DebugText>, conversion: ConversionFlag, - spec: Option<&ruff::FStringFormatSpec>, + spec: Option<&ruff::InterpolatedStringFormatSpec>, ) -> fmt::Result { let buffered = to_string_fmt(|f| { Unparser::new(f, self.source).unparse_expr(val, precedence::TEST + 1) @@ -562,9 +585,9 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { Ok(()) } - fn unparse_fstring_elem(&mut self, elem: &ruff::FStringElement) -> fmt::Result { + fn unparse_fstring_elem(&mut self, elem: &ruff::InterpolatedStringElement) -> fmt::Result { match elem { - ruff::FStringElement::Expression(ruff::FStringExpressionElement { + ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement { expression, debug_text, conversion, @@ -576,9 +599,10 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { *conversion, format_spec.as_deref(), ), - ruff::FStringElement::Literal(ruff::FStringLiteralElement { value, .. }) => { - self.unparse_fstring_str(value) - } + ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement { + value, + .. + }) => self.unparse_fstring_str(value), } } diff --git a/compiler/core/src/bytecode.rs b/compiler/core/src/bytecode.rs index c2ce4e52c07..0007c560ff8 100644 --- a/compiler/core/src/bytecode.rs +++ b/compiler/core/src/bytecode.rs @@ -1190,14 +1190,14 @@ impl CodeObject { level: usize, ) -> fmt::Result { let label_targets = self.label_targets(); - let line_digits = (3).max(self.locations.last().unwrap().row.to_string().len()); - let offset_digits = (4).max(self.instructions.len().to_string().len()); + let line_digits = (3).max(self.locations.last().unwrap().line.digits().get()); + let offset_digits = (4).max(1 + self.instructions.len().ilog10() as usize); let mut last_line = OneIndexed::MAX; let mut arg_state = OpArgState::default(); for (offset, &instruction) in self.instructions.iter().enumerate() { let (instruction, arg) = arg_state.get(instruction); // optional line number - let line = self.locations[offset].row; + let line = self.locations[offset].line; if line != last_line { if last_line != OneIndexed::MAX { writeln!(f)?; diff --git a/compiler/core/src/lib.rs b/compiler/core/src/lib.rs index 0ce4a9defb1..08cdc0ec21f 100644 --- a/compiler/core/src/lib.rs +++ b/compiler/core/src/lib.rs @@ -8,4 +8,6 @@ mod mode; pub use mode::Mode; -pub use ruff_source_file::{LineIndex, OneIndexed, SourceFile, SourceFileBuilder, SourceLocation}; +pub use ruff_source_file::{ + LineIndex, OneIndexed, PositionEncoding, SourceFile, SourceFileBuilder, SourceLocation, +}; diff --git a/compiler/core/src/marshal.rs b/compiler/core/src/marshal.rs index b8044a1ab95..5e16e59102a 100644 --- a/compiler/core/src/marshal.rs +++ b/compiler/core/src/marshal.rs @@ -198,8 +198,8 @@ pub fn deserialize_code( let locations = (0..len) .map(|_| { Ok(SourceLocation { - row: OneIndexed::new(rdr.read_u32()? as _).ok_or(MarshalError::InvalidLocation)?, - column: OneIndexed::from_zero_indexed(rdr.read_u32()? as _), + line: OneIndexed::new(rdr.read_u32()? as _).ok_or(MarshalError::InvalidLocation)?, + character_offset: OneIndexed::from_zero_indexed(rdr.read_u32()? as _), }) }) .collect::>>()?; @@ -656,8 +656,8 @@ pub fn serialize_code(buf: &mut W, code: &CodeObject) write_len(buf, code.locations.len()); for loc in &*code.locations { - buf.write_u32(loc.row.get() as _); - buf.write_u32(loc.column.to_zero_indexed() as _); + buf.write_u32(loc.line.get() as _); + buf.write_u32(loc.character_offset.to_zero_indexed() as _); } buf.write_u16(code.flags.bits()); diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index 111b9aec2e7..84e64f3c27f 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -1,4 +1,4 @@ -use ruff_source_file::{SourceFile, SourceFileBuilder, SourceLocation}; +use ruff_source_file::{PositionEncoding, SourceFile, SourceFileBuilder, SourceLocation}; use rustpython_codegen::{compile, symboltable}; pub use rustpython_codegen::compile::CompileOpts; @@ -46,7 +46,7 @@ impl CompileError { pub fn from_ruff_parse_error(error: parser::ParseError, source_file: &SourceFile) -> Self { let location = source_file .to_source_code() - .source_location(error.location.start()); + .source_location(error.location.start(), PositionEncoding::Utf8); Self::Parse(ParseError { error: error.error, raw_location: error.location, @@ -55,26 +55,18 @@ impl CompileError { }) } - pub fn location(&self) -> Option { + pub const fn location(&self) -> Option { match self { - Self::Codegen(codegen_error) => codegen_error.location.clone(), - Self::Parse(parse_error) => Some(parse_error.location.clone()), + Self::Codegen(codegen_error) => codegen_error.location, + Self::Parse(parse_error) => Some(parse_error.location), } } pub const fn python_location(&self) -> (usize, usize) { - match self { - Self::Codegen(codegen_error) => { - if let Some(location) = &codegen_error.location { - (location.row.get(), location.column.get()) - } else { - (0, 0) - } - } - Self::Parse(parse_error) => ( - parse_error.location.row.get(), - parse_error.location.column.get(), - ), + if let Some(location) = self.location() { + (location.line.get(), location.character_offset.get()) + } else { + (0, 0) } } diff --git a/src/shell.rs b/src/shell.rs index e38b0a50def..15918349665 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1,7 +1,7 @@ mod helper; use rustpython_compiler::{ - CompileError, ParseError, parser::FStringErrorType, parser::LexicalErrorType, + CompileError, ParseError, parser::InterpolatedStringErrorType, parser::LexicalErrorType, parser::ParseErrorType, }; use rustpython_vm::{ @@ -52,7 +52,7 @@ fn shell_exec( Err(CompileError::Parse(ParseError { error: ParseErrorType::Lexical(LexicalErrorType::FStringError( - FStringErrorType::UnterminatedTripleQuotedString, + InterpolatedStringErrorType::UnterminatedTripleQuotedString, )), .. })) => ShellExecResult::ContinueLine, diff --git a/stdlib/src/faulthandler.rs b/stdlib/src/faulthandler.rs index f358129c873..5c9196ad33f 100644 --- a/stdlib/src/faulthandler.rs +++ b/stdlib/src/faulthandler.rs @@ -10,7 +10,7 @@ mod decl { stderr, " File \"{}\", line {} in {}", frame.code.source_path, - frame.current_location().row, + frame.current_location().line, frame.code.obj_name ) } diff --git a/vm/src/builtins/frame.rs b/vm/src/builtins/frame.rs index 65ac3e798d7..17dc88ac042 100644 --- a/vm/src/builtins/frame.rs +++ b/vm/src/builtins/frame.rs @@ -60,7 +60,7 @@ impl Frame { #[pygetset] pub fn f_lineno(&self) -> usize { - self.current_location().row.get() + self.current_location().line.get() } #[pygetset] diff --git a/vm/src/frame.rs b/vm/src/frame.rs index ba9abffc6e7..fa5860244f1 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -171,7 +171,7 @@ impl Frame { } pub fn current_location(&self) -> SourceLocation { - self.code.locations[self.lasti() as usize - 1].clone() + self.code.locations[self.lasti() as usize - 1] } pub fn lasti(&self) -> u32 { @@ -388,11 +388,15 @@ impl ExecutingFrame<'_> { // 2. Add new entry with current execution position (filename, lineno, code_object) to traceback. // 3. Unwind block stack till appropriate handler is found. - let loc = frame.code.locations[idx].clone(); + let loc = frame.code.locations[idx]; let next = exception.__traceback__(); - let new_traceback = - PyTraceback::new(next, frame.object.to_owned(), frame.lasti(), loc.row); - vm_trace!("Adding to traceback: {:?} {:?}", new_traceback, loc.row); + let new_traceback = PyTraceback::new( + next, + frame.object.to_owned(), + frame.lasti(), + loc.line, + ); + vm_trace!("Adding to traceback: {:?} {:?}", new_traceback, loc.line); exception.set_traceback_typed(Some(new_traceback.into_ref(&vm.ctx))); vm.contextualize_exception(&exception); diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index 1533f903c5c..00aad0213f3 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -23,7 +23,7 @@ use node::Node; use ruff_python_ast as ruff; use ruff_text_size::{Ranged, TextRange, TextSize}; use rustpython_compiler_core::{ - LineIndex, OneIndexed, SourceFile, SourceFileBuilder, SourceLocation, + LineIndex, OneIndexed, PositionEncoding, SourceFile, SourceFileBuilder, SourceLocation, }; #[cfg(feature = "parser")] @@ -92,8 +92,8 @@ pub struct PySourceLocation { impl PySourceLocation { const fn to_source_location(&self) -> SourceLocation { SourceLocation { - row: self.row.get_one_indexed(), - column: self.column.get_one_indexed(), + line: self.row.get_one_indexed(), + character_offset: self.column.get_one_indexed(), } } } @@ -194,14 +194,14 @@ fn source_range_to_text_range(source_file: &SourceFile, location: PySourceRange) } let start = index.offset( - location.start.row.get_one_indexed(), - location.start.column.get_one_indexed(), + location.start.to_source_location(), source, + PositionEncoding::Utf8, ); let end = index.offset( - location.end.row.get_one_indexed(), - location.end.column.get_one_indexed(), + location.end.to_source_location(), source, + PositionEncoding::Utf8, ); TextRange::new(start, end) @@ -273,9 +273,11 @@ pub(crate) fn compile( let ast: Mod = Node::ast_from_object(vm, &source_file, object)?; let ast = match ast { Mod::Module(m) => ruff::Mod::Module(m), - Mod::Interactive(ModInteractive { range, body }) => { - ruff::Mod::Module(ruff::ModModule { range, body }) - } + Mod::Interactive(ModInteractive { range, body }) => ruff::Mod::Module(ruff::ModModule { + node_index: Default::default(), + range, + body, + }), Mod::Expression(e) => ruff::Mod::Expression(e), Mod::FunctionType(_) => todo!(), }; diff --git a/vm/src/stdlib/ast/argument.rs b/vm/src/stdlib/ast/argument.rs index a1207f2c054..a13200e6502 100644 --- a/vm/src/stdlib/ast/argument.rs +++ b/vm/src/stdlib/ast/argument.rs @@ -57,6 +57,7 @@ pub(super) fn merge_function_call_arguments( let range = pos_args.range.cover(key_args.range); ruff::Arguments { + node_index: Default::default(), range, args: pos_args.args, keywords: key_args.keywords, @@ -67,6 +68,7 @@ pub(super) fn split_function_call_arguments( args: ruff::Arguments, ) -> (PositionalArguments, KeywordArguments) { let ruff::Arguments { + node_index: _, range: _, args, keywords, @@ -105,6 +107,7 @@ pub(super) fn split_class_def_args( Some(args) => *args, }; let ruff::Arguments { + node_index: _, range: _, args, keywords, @@ -155,6 +158,7 @@ pub(super) fn merge_class_def_args( }; Some(Box::new(ruff::Arguments { + node_index: Default::default(), range: Default::default(), // TODO args, keywords, diff --git a/vm/src/stdlib/ast/constant.rs b/vm/src/stdlib/ast/constant.rs index b6c3e9596ba..83b2a7f7015 100644 --- a/vm/src/stdlib/ast/constant.rs +++ b/vm/src/stdlib/ast/constant.rs @@ -247,14 +247,21 @@ impl Node for ConstantLiteral { fn constant_to_ruff_expr(value: Constant) -> ruff::Expr { let Constant { value, range } = value; match value { - ConstantLiteral::None => ruff::Expr::NoneLiteral(ruff::ExprNoneLiteral { range }), - ConstantLiteral::Bool(value) => { - ruff::Expr::BooleanLiteral(ruff::ExprBooleanLiteral { range, value }) - } + ConstantLiteral::None => ruff::Expr::NoneLiteral(ruff::ExprNoneLiteral { + node_index: Default::default(), + range, + }), + ConstantLiteral::Bool(value) => ruff::Expr::BooleanLiteral(ruff::ExprBooleanLiteral { + node_index: Default::default(), + range, + value, + }), ConstantLiteral::Str { value, prefix } => { ruff::Expr::StringLiteral(ruff::ExprStringLiteral { + node_index: Default::default(), range, value: ruff::StringLiteralValue::single(ruff::StringLiteral { + node_index: Default::default(), range, value, flags: ruff::StringLiteralFlags::empty().with_prefix(prefix), @@ -263,8 +270,10 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr { } ConstantLiteral::Bytes(value) => { ruff::Expr::BytesLiteral(ruff::ExprBytesLiteral { + node_index: Default::default(), range, value: ruff::BytesLiteralValue::single(ruff::BytesLiteral { + node_index: Default::default(), range, value, flags: ruff::BytesLiteralFlags::empty(), // TODO @@ -272,10 +281,12 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr { }) } ConstantLiteral::Int(value) => ruff::Expr::NumberLiteral(ruff::ExprNumberLiteral { + node_index: Default::default(), range, value: ruff::Number::Int(value), }), ConstantLiteral::Tuple(value) => ruff::Expr::Tuple(ruff::ExprTuple { + node_index: Default::default(), range, elts: value .into_iter() @@ -291,14 +302,17 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr { parenthesized: true, }), ConstantLiteral::FrozenSet(value) => ruff::Expr::Call(ruff::ExprCall { + node_index: Default::default(), range, // idk lol func: Box::new(ruff::Expr::Name(ruff::ExprName { + node_index: Default::default(), range: TextRange::default(), id: ruff::name::Name::new_static("frozenset"), ctx: ruff::ExprContext::Load, })), arguments: ruff::Arguments { + node_index: Default::default(), range, args: value .into_iter() @@ -313,18 +327,21 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr { }, }), ConstantLiteral::Float(value) => ruff::Expr::NumberLiteral(ruff::ExprNumberLiteral { + node_index: Default::default(), range, value: ruff::Number::Float(value), }), ConstantLiteral::Complex { real, imag } => { ruff::Expr::NumberLiteral(ruff::ExprNumberLiteral { + node_index: Default::default(), range, value: ruff::Number::Complex { real, imag }, }) } - ConstantLiteral::Ellipsis => { - ruff::Expr::EllipsisLiteral(ruff::ExprEllipsisLiteral { range }) - } + ConstantLiteral::Ellipsis => ruff::Expr::EllipsisLiteral(ruff::ExprEllipsisLiteral { + node_index: Default::default(), + range, + }), } } @@ -333,7 +350,11 @@ pub(super) fn number_literal_to_object( source_file: &SourceFile, constant: ruff::ExprNumberLiteral, ) -> PyObjectRef { - let ruff::ExprNumberLiteral { range, value } = constant; + let ruff::ExprNumberLiteral { + node_index: _, + range, + value, + } = constant; let c = match value { ruff::Number::Int(n) => Constant::new_int(n, range), ruff::Number::Float(n) => Constant::new_float(n, range), @@ -347,7 +368,11 @@ pub(super) fn string_literal_to_object( source_file: &SourceFile, constant: ruff::ExprStringLiteral, ) -> PyObjectRef { - let ruff::ExprStringLiteral { range, value } = constant; + let ruff::ExprStringLiteral { + node_index: _, + range, + value, + } = constant; let prefix = value .iter() .next() @@ -361,7 +386,11 @@ pub(super) fn bytes_literal_to_object( source_file: &SourceFile, constant: ruff::ExprBytesLiteral, ) -> PyObjectRef { - let ruff::ExprBytesLiteral { range, value } = constant; + let ruff::ExprBytesLiteral { + node_index: _, + range, + value, + } = constant; let bytes = value.as_slice().iter().flat_map(|b| b.value.iter()); let c = Constant::new_bytes(bytes.copied().collect(), range); c.ast_to_object(vm, source_file) @@ -372,7 +401,11 @@ pub(super) fn boolean_literal_to_object( source_file: &SourceFile, constant: ruff::ExprBooleanLiteral, ) -> PyObjectRef { - let ruff::ExprBooleanLiteral { range, value } = constant; + let ruff::ExprBooleanLiteral { + node_index: _, + range, + value, + } = constant; let c = Constant::new_bool(value, range); c.ast_to_object(vm, source_file) } @@ -382,7 +415,10 @@ pub(super) fn none_literal_to_object( source_file: &SourceFile, constant: ruff::ExprNoneLiteral, ) -> PyObjectRef { - let ruff::ExprNoneLiteral { range } = constant; + let ruff::ExprNoneLiteral { + node_index: _, + range, + } = constant; let c = Constant::new_none(range); c.ast_to_object(vm, source_file) } @@ -392,7 +428,10 @@ pub(super) fn ellipsis_literal_to_object( source_file: &SourceFile, constant: ruff::ExprEllipsisLiteral, ) -> PyObjectRef { - let ruff::ExprEllipsisLiteral { range } = constant; + let ruff::ExprEllipsisLiteral { + node_index: _, + range, + } = constant; let c = Constant::new_ellipsis(range); c.ast_to_object(vm, source_file) } diff --git a/vm/src/stdlib/ast/elif_else_clause.rs b/vm/src/stdlib/ast/elif_else_clause.rs index 2b427b1ec13..581fc499b8a 100644 --- a/vm/src/stdlib/ast/elif_else_clause.rs +++ b/vm/src/stdlib/ast/elif_else_clause.rs @@ -7,7 +7,12 @@ pub(super) fn ast_to_object( vm: &VirtualMachine, source_file: &SourceFile, ) -> PyObjectRef { - let ruff::ElifElseClause { range, test, body } = clause; + let ruff::ElifElseClause { + node_index: _, + range, + test, + body, + } = clause; let Some(test) = test else { assert!(rest.len() == 0); return body.ast_to_object(vm, source_file); @@ -55,6 +60,7 @@ pub(super) fn ast_from_object( let elif_else_clauses = if let [ruff::Stmt::If(_)] = &*orelse { let Some(ruff::Stmt::If(ruff::StmtIf { + node_index: _, range, test, body, @@ -66,6 +72,7 @@ pub(super) fn ast_from_object( elif_else_clauses.insert( 0, ruff::ElifElseClause { + node_index: Default::default(), range, test: Some(*test), body, @@ -74,6 +81,7 @@ pub(super) fn ast_from_object( elif_else_clauses } else { vec![ruff::ElifElseClause { + node_index: Default::default(), range, test: None, body: orelse, @@ -81,6 +89,7 @@ pub(super) fn ast_from_object( }; Ok(ruff::StmtIf { + node_index: Default::default(), test, body, elif_else_clauses, diff --git a/vm/src/stdlib/ast/exception.rs b/vm/src/stdlib/ast/exception.rs index df6b391aa1d..b5b3ca2709a 100644 --- a/vm/src/stdlib/ast/exception.rs +++ b/vm/src/stdlib/ast/exception.rs @@ -35,6 +35,7 @@ impl Node for ruff::ExceptHandler { impl Node for ruff::ExceptHandlerExceptHandler { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, type_, name, body, @@ -63,6 +64,7 @@ impl Node for ruff::ExceptHandlerExceptHandler { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), type_: get_node_field_opt(_vm, &_object, "type")? .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, diff --git a/vm/src/stdlib/ast/expression.rs b/vm/src/stdlib/ast/expression.rs index 31b65bd13a9..83d77374380 100644 --- a/vm/src/stdlib/ast/expression.rs +++ b/vm/src/stdlib/ast/expression.rs @@ -36,6 +36,7 @@ impl Node for ruff::Expr { Self::NumberLiteral(cons) => constant::number_literal_to_object(vm, source_file, cons), Self::StringLiteral(cons) => constant::string_literal_to_object(vm, source_file, cons), Self::FString(cons) => string::fstring_to_object(vm, source_file, cons), + Self::TString(_) => unimplemented!(), Self::BytesLiteral(cons) => constant::bytes_literal_to_object(vm, source_file, cons), Self::BooleanLiteral(cons) => { constant::boolean_literal_to_object(vm, source_file, cons) @@ -145,7 +146,12 @@ impl Node for ruff::Expr { // constructor impl Node for ruff::ExprBoolOp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { op, values, range } = self; + let Self { + node_index: _, + op, + values, + range, + } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprBoolOp::static_type().to_owned()) .unwrap(); @@ -164,6 +170,7 @@ impl Node for ruff::ExprBoolOp { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), op: Node::ast_from_object( vm, source_file, @@ -183,6 +190,7 @@ impl Node for ruff::ExprBoolOp { impl Node for ruff::ExprNamed { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, target, value, range, @@ -205,6 +213,7 @@ impl Node for ruff::ExprNamed { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), target: Node::ast_from_object( vm, source_file, @@ -224,6 +233,7 @@ impl Node for ruff::ExprNamed { impl Node for ruff::ExprBinOp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, left, op, right, @@ -249,6 +259,7 @@ impl Node for ruff::ExprBinOp { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), left: Node::ast_from_object( vm, source_file, @@ -272,7 +283,12 @@ impl Node for ruff::ExprBinOp { // constructor impl Node for ruff::ExprUnaryOp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { op, operand, range } = self; + let Self { + node_index: _, + op, + operand, + range, + } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprUnaryOp::static_type().to_owned()) .unwrap(); @@ -290,6 +306,7 @@ impl Node for ruff::ExprUnaryOp { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), op: Node::ast_from_object( vm, source_file, @@ -309,6 +326,7 @@ impl Node for ruff::ExprUnaryOp { impl Node for ruff::ExprLambda { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, parameters, body, range: _range, @@ -331,6 +349,7 @@ impl Node for ruff::ExprLambda { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), parameters: Node::ast_from_object( vm, source_file, @@ -350,6 +369,7 @@ impl Node for ruff::ExprLambda { impl Node for ruff::ExprIf { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, test, body, orelse, @@ -375,6 +395,7 @@ impl Node for ruff::ExprIf { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), test: Node::ast_from_object( vm, source_file, @@ -398,7 +419,11 @@ impl Node for ruff::ExprIf { // constructor impl Node for ruff::ExprDict { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { items, range } = self; + let Self { + node_index: _, + items, + range, + } = self; let (keys, values) = items .into_iter() @@ -440,6 +465,7 @@ impl Node for ruff::ExprDict { .map(|(key, value)| ruff::DictItem { key, value }) .collect(); Ok(Self { + node_index: Default::default(), items, range: range_from_object(vm, source_file, object, "Dict")?, }) @@ -449,7 +475,11 @@ impl Node for ruff::ExprDict { // constructor impl Node for ruff::ExprSet { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { elts, range } = self; + let Self { + node_index: _, + elts, + range, + } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprSet::static_type().to_owned()) .unwrap(); @@ -465,6 +495,7 @@ impl Node for ruff::ExprSet { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), elts: Node::ast_from_object( vm, source_file, @@ -479,6 +510,7 @@ impl Node for ruff::ExprSet { impl Node for ruff::ExprListComp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, elt, generators, range, @@ -501,6 +533,7 @@ impl Node for ruff::ExprListComp { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), elt: Node::ast_from_object( vm, source_file, @@ -520,6 +553,7 @@ impl Node for ruff::ExprListComp { impl Node for ruff::ExprSetComp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, elt, generators, range, @@ -542,6 +576,7 @@ impl Node for ruff::ExprSetComp { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), elt: Node::ast_from_object( vm, source_file, @@ -561,6 +596,7 @@ impl Node for ruff::ExprSetComp { impl Node for ruff::ExprDictComp { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, key, value, generators, @@ -586,6 +622,7 @@ impl Node for ruff::ExprDictComp { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), key: Node::ast_from_object( vm, source_file, @@ -610,6 +647,7 @@ impl Node for ruff::ExprDictComp { impl Node for ruff::ExprGenerator { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, elt, generators, range, @@ -633,6 +671,7 @@ impl Node for ruff::ExprGenerator { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), elt: Node::ast_from_object( vm, source_file, @@ -653,7 +692,11 @@ impl Node for ruff::ExprGenerator { // constructor impl Node for ruff::ExprAwait { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { value, range } = self; + let Self { + node_index: _, + value, + range, + } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprAwait::static_type().to_owned()) .unwrap(); @@ -669,6 +712,7 @@ impl Node for ruff::ExprAwait { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), value: Node::ast_from_object( vm, source_file, @@ -682,7 +726,11 @@ impl Node for ruff::ExprAwait { // constructor impl Node for ruff::ExprYield { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { value, range } = self; + let Self { + node_index: _, + value, + range, + } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprYield::static_type().to_owned()) .unwrap(); @@ -699,6 +747,7 @@ impl Node for ruff::ExprYield { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), value: get_node_field_opt(vm, &object, "value")? .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, @@ -710,7 +759,11 @@ impl Node for ruff::ExprYield { // constructor impl Node for ruff::ExprYieldFrom { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { value, range } = self; + let Self { + node_index: _, + value, + range, + } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprYieldFrom::static_type().to_owned()) .unwrap(); @@ -727,6 +780,7 @@ impl Node for ruff::ExprYieldFrom { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), value: Node::ast_from_object( vm, source_file, @@ -741,6 +795,7 @@ impl Node for ruff::ExprYieldFrom { impl Node for ruff::ExprCompare { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, left, ops, comparators, @@ -770,6 +825,7 @@ impl Node for ruff::ExprCompare { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), left: Node::ast_from_object( vm, source_file, @@ -800,6 +856,7 @@ impl Node for ruff::ExprCompare { impl Node for ruff::ExprCall { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, func, arguments, range, @@ -833,6 +890,7 @@ impl Node for ruff::ExprCall { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), func: Node::ast_from_object( vm, source_file, @@ -859,6 +917,7 @@ impl Node for ruff::ExprCall { impl Node for ruff::ExprAttribute { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, value, attr, ctx, @@ -884,6 +943,7 @@ impl Node for ruff::ExprAttribute { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), value: Node::ast_from_object( vm, source_file, @@ -908,6 +968,7 @@ impl Node for ruff::ExprAttribute { impl Node for ruff::ExprSubscript { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, value, slice, ctx, @@ -932,6 +993,7 @@ impl Node for ruff::ExprSubscript { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), value: Node::ast_from_object( vm, source_file, @@ -955,7 +1017,12 @@ impl Node for ruff::ExprSubscript { // constructor impl Node for ruff::ExprStarred { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { value, ctx, range } = self; + let Self { + node_index: _, + value, + ctx, + range, + } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprStarred::static_type().to_owned()) .unwrap(); @@ -973,6 +1040,7 @@ impl Node for ruff::ExprStarred { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), value: Node::ast_from_object( vm, source_file, @@ -991,7 +1059,12 @@ impl Node for ruff::ExprStarred { // constructor impl Node for ruff::ExprName { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { id, ctx, range } = self; + let Self { + node_index: _, + id, + ctx, + range, + } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprName::static_type().to_owned()) .unwrap(); @@ -1009,6 +1082,7 @@ impl Node for ruff::ExprName { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), id: Node::ast_from_object(vm, source_file, get_node_field(vm, &object, "id", "Name")?)?, ctx: Node::ast_from_object( vm, @@ -1023,7 +1097,12 @@ impl Node for ruff::ExprName { // constructor impl Node for ruff::ExprList { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { elts, ctx, range } = self; + let Self { + node_index: _, + elts, + ctx, + range, + } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprList::static_type().to_owned()) .unwrap(); @@ -1042,6 +1121,7 @@ impl Node for ruff::ExprList { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), elts: Node::ast_from_object( vm, source_file, @@ -1061,6 +1141,7 @@ impl Node for ruff::ExprList { impl Node for ruff::ExprTuple { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, elts, ctx, range: _range, @@ -1084,6 +1165,7 @@ impl Node for ruff::ExprTuple { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), elts: Node::ast_from_object( vm, source_file, @@ -1104,6 +1186,7 @@ impl Node for ruff::ExprTuple { impl Node for ruff::ExprSlice { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, lower, upper, step, @@ -1129,6 +1212,7 @@ impl Node for ruff::ExprSlice { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), lower: get_node_field_opt(vm, &object, "lower")? .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, @@ -1185,6 +1269,7 @@ impl Node for ruff::ExprContext { impl Node for ruff::Comprehension { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, target, iter, ifs, @@ -1212,6 +1297,7 @@ impl Node for ruff::Comprehension { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), target: Node::ast_from_object( vm, source_file, diff --git a/vm/src/stdlib/ast/module.rs b/vm/src/stdlib/ast/module.rs index 33ee4c567b1..6fae8f10a33 100644 --- a/vm/src/stdlib/ast/module.rs +++ b/vm/src/stdlib/ast/module.rs @@ -66,6 +66,7 @@ impl Node for Mod { impl Node for ruff::ModModule { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, body, // type_ignores, range, @@ -95,6 +96,7 @@ impl Node for ruff::ModModule { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), body: Node::ast_from_object( vm, source_file, @@ -147,7 +149,11 @@ impl Node for ModInteractive { // constructor impl Node for ruff::ModExpression { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { body, range } = self; + let Self { + node_index: _, + body, + range, + } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeModExpression::static_type().to_owned()) .unwrap(); @@ -164,6 +170,7 @@ impl Node for ruff::ModExpression { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), body: Node::ast_from_object( vm, source_file, diff --git a/vm/src/stdlib/ast/other.rs b/vm/src/stdlib/ast/other.rs index eddbef97482..780d7cc7124 100644 --- a/vm/src/stdlib/ast/other.rs +++ b/vm/src/stdlib/ast/other.rs @@ -55,7 +55,11 @@ impl Node for ruff::Decorator { ) -> PyResult { let expression = ruff::Expr::ast_from_object(vm, source_file, object)?; let range = expression.range(); - Ok(Self { expression, range }) + Ok(Self { + node_index: Default::default(), + expression, + range, + }) } } @@ -63,6 +67,7 @@ impl Node for ruff::Decorator { impl Node for ruff::Alias { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, name, asname, range: _range, @@ -85,6 +90,7 @@ impl Node for ruff::Alias { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), name: Node::ast_from_object( vm, source_file, @@ -102,6 +108,7 @@ impl Node for ruff::Alias { impl Node for ruff::WithItem { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, context_expr, optional_vars, range: _range, @@ -131,6 +138,7 @@ impl Node for ruff::WithItem { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), context_expr: Node::ast_from_object( vm, source_file, diff --git a/vm/src/stdlib/ast/parameter.rs b/vm/src/stdlib/ast/parameter.rs index 347b3b4ec0c..87fa736687b 100644 --- a/vm/src/stdlib/ast/parameter.rs +++ b/vm/src/stdlib/ast/parameter.rs @@ -5,6 +5,7 @@ use rustpython_compiler_core::SourceFile; impl Node for ruff::Parameters { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, posonlyargs, args, vararg, @@ -80,6 +81,7 @@ impl Node for ruff::Parameters { let (posonlyargs, args) = merge_positional_parameter_defaults(posonlyargs, args, defaults); Ok(Self { + node_index: Default::default(), posonlyargs, args, vararg: get_node_field_opt(vm, &object, "vararg")? @@ -102,6 +104,7 @@ impl Node for ruff::Parameters { impl Node for ruff::Parameter { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, name, annotation, // type_comment, @@ -135,6 +138,7 @@ impl Node for ruff::Parameter { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), name: Node::ast_from_object( _vm, source_file, @@ -155,6 +159,7 @@ impl Node for ruff::Parameter { impl Node for ruff::Keyword { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, arg, value, range: _range, @@ -176,6 +181,7 @@ impl Node for ruff::Keyword { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), arg: get_node_field_opt(_vm, &_object, "arg")? .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, @@ -328,6 +334,7 @@ fn merge_positional_parameter_defaults( let mut posonlyargs: Vec<_> = as IntoIterator>::into_iter(posonlyargs) .map(|parameter| ruff::ParameterWithDefault { + node_index: Default::default(), range: Default::default(), parameter, default: None, @@ -335,6 +342,7 @@ fn merge_positional_parameter_defaults( .collect(); let mut args: Vec<_> = as IntoIterator>::into_iter(args) .map(|parameter| ruff::ParameterWithDefault { + node_index: Default::default(), range: Default::default(), parameter, default: None, @@ -397,6 +405,7 @@ fn merge_keyword_parameter_defaults( ) -> Vec { std::iter::zip(kw_only_args.keywords, defaults.defaults) .map(|(parameter, default)| ruff::ParameterWithDefault { + node_index: Default::default(), parameter, default, range: Default::default(), diff --git a/vm/src/stdlib/ast/pattern.rs b/vm/src/stdlib/ast/pattern.rs index 0c484d6787a..9567ed38d41 100644 --- a/vm/src/stdlib/ast/pattern.rs +++ b/vm/src/stdlib/ast/pattern.rs @@ -5,6 +5,7 @@ use rustpython_compiler_core::SourceFile; impl Node for ruff::MatchCase { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, pattern, guard, body, @@ -29,6 +30,7 @@ impl Node for ruff::MatchCase { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), pattern: Node::ast_from_object( _vm, source_file, @@ -127,6 +129,7 @@ impl Node for ruff::Pattern { impl Node for ruff::PatternMatchValue { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, value, range: _range, } = self; @@ -146,6 +149,7 @@ impl Node for ruff::PatternMatchValue { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), value: Node::ast_from_object( _vm, source_file, @@ -160,6 +164,7 @@ impl Node for ruff::PatternMatchValue { impl Node for ruff::PatternMatchSingleton { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, value, range: _range, } = self; @@ -182,6 +187,7 @@ impl Node for ruff::PatternMatchSingleton { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), value: Node::ast_from_object( _vm, source_file, @@ -210,6 +216,7 @@ impl Node for ruff::Singleton { impl Node for ruff::PatternMatchSequence { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, patterns, range: _range, } = self; @@ -232,6 +239,7 @@ impl Node for ruff::PatternMatchSequence { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), patterns: Node::ast_from_object( _vm, source_file, @@ -246,6 +254,7 @@ impl Node for ruff::PatternMatchSequence { impl Node for ruff::PatternMatchMapping { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, keys, patterns, rest, @@ -274,6 +283,7 @@ impl Node for ruff::PatternMatchMapping { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), keys: Node::ast_from_object( _vm, source_file, @@ -296,6 +306,7 @@ impl Node for ruff::PatternMatchMapping { impl Node for ruff::PatternMatchClass { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, cls, arguments, range: _range, @@ -344,6 +355,7 @@ impl Node for ruff::PatternMatchClass { let (patterns, keywords) = merge_pattern_match_class(patterns, kwd_attrs, kwd_patterns); Ok(Self { + node_index: Default::default(), cls: Node::ast_from_object( vm, source_file, @@ -351,6 +363,7 @@ impl Node for ruff::PatternMatchClass { )?, range: range_from_object(vm, source_file, object, "MatchClass")?, arguments: ruff::PatternArguments { + node_index: Default::default(), range: Default::default(), patterns, keywords, @@ -416,6 +429,7 @@ impl Node for PatternMatchClassKeywordPatterns { impl Node for ruff::PatternMatchStar { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, name, range: _range, } = self; @@ -435,6 +449,7 @@ impl Node for ruff::PatternMatchStar { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), name: get_node_field_opt(_vm, &_object, "name")? .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, @@ -447,6 +462,7 @@ impl Node for ruff::PatternMatchStar { impl Node for ruff::PatternMatchAs { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, pattern, name, range: _range, @@ -469,6 +485,7 @@ impl Node for ruff::PatternMatchAs { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), pattern: get_node_field_opt(_vm, &_object, "pattern")? .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, @@ -484,6 +501,7 @@ impl Node for ruff::PatternMatchAs { impl Node for ruff::PatternMatchOr { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, patterns, range: _range, } = self; @@ -502,6 +520,7 @@ impl Node for ruff::PatternMatchOr { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), patterns: Node::ast_from_object( _vm, source_file, diff --git a/vm/src/stdlib/ast/statement.rs b/vm/src/stdlib/ast/statement.rs index fab7ea76961..5925ca1fc2a 100644 --- a/vm/src/stdlib/ast/statement.rs +++ b/vm/src/stdlib/ast/statement.rs @@ -172,6 +172,7 @@ impl Node for ruff::Stmt { impl Node for ruff::StmtFunctionDef { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, name, parameters, body, @@ -225,6 +226,7 @@ impl Node for ruff::StmtFunctionDef { let _cls = _object.class(); let is_async = _cls.is(pyast::NodeStmtAsyncFunctionDef::static_type()); Ok(Self { + node_index: Default::default(), name: Node::ast_from_object( _vm, source_file, @@ -267,6 +269,7 @@ impl Node for ruff::StmtFunctionDef { impl Node for ruff::StmtClassDef { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, name, arguments, body, @@ -318,6 +321,7 @@ impl Node for ruff::StmtClassDef { get_node_field(_vm, &_object, "keywords", "ClassDef")?, )?; Ok(Self { + node_index: Default::default(), name: Node::ast_from_object( _vm, source_file, @@ -347,6 +351,7 @@ impl Node for ruff::StmtClassDef { impl Node for ruff::StmtReturn { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, value, range: _range, } = self; @@ -365,6 +370,7 @@ impl Node for ruff::StmtReturn { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), value: get_node_field_opt(_vm, &_object, "value")? .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, @@ -376,6 +382,7 @@ impl Node for ruff::StmtReturn { impl Node for ruff::StmtDelete { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, targets, range: _range, } = self; @@ -394,6 +401,7 @@ impl Node for ruff::StmtDelete { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), targets: Node::ast_from_object( _vm, source_file, @@ -408,6 +416,7 @@ impl Node for ruff::StmtDelete { impl Node for ruff::StmtAssign { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, targets, value, // type_comment, @@ -432,6 +441,7 @@ impl Node for ruff::StmtAssign { object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), targets: Node::ast_from_object( vm, source_file, @@ -454,6 +464,7 @@ impl Node for ruff::StmtAssign { impl Node for ruff::StmtTypeAlias { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, name, type_params, value, @@ -483,6 +494,7 @@ impl Node for ruff::StmtTypeAlias { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), name: Node::ast_from_object( _vm, source_file, @@ -507,6 +519,7 @@ impl Node for ruff::StmtTypeAlias { impl Node for ruff::StmtAugAssign { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, target, op, value, @@ -531,6 +544,7 @@ impl Node for ruff::StmtAugAssign { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), target: Node::ast_from_object( _vm, source_file, @@ -555,6 +569,7 @@ impl Node for ruff::StmtAugAssign { impl Node for ruff::StmtAnnAssign { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, target, annotation, value, @@ -586,6 +601,7 @@ impl Node for ruff::StmtAnnAssign { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), target: Node::ast_from_object( _vm, source_file, @@ -613,6 +629,7 @@ impl Node for ruff::StmtAnnAssign { impl Node for ruff::StmtFor { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, is_async, target, iter, @@ -656,6 +673,7 @@ impl Node for ruff::StmtFor { ); let is_async = _cls.is(pyast::NodeStmtAsyncFor::static_type()); Ok(Self { + node_index: Default::default(), target: Node::ast_from_object( _vm, source_file, @@ -689,6 +707,7 @@ impl Node for ruff::StmtFor { impl Node for ruff::StmtWhile { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, test, body, orelse, @@ -714,6 +733,7 @@ impl Node for ruff::StmtWhile { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), test: Node::ast_from_object( _vm, source_file, @@ -737,6 +757,7 @@ impl Node for ruff::StmtWhile { impl Node for ruff::StmtIf { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, test, body, range, @@ -744,6 +765,7 @@ impl Node for ruff::StmtIf { } = self; elif_else_clause::ast_to_object( ruff::ElifElseClause { + node_index: Default::default(), range, test: Some(*test), body, @@ -765,6 +787,7 @@ impl Node for ruff::StmtIf { impl Node for ruff::StmtWith { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, is_async, items, body, @@ -801,6 +824,7 @@ impl Node for ruff::StmtWith { ); let is_async = _cls.is(pyast::NodeStmtAsyncWith::static_type()); Ok(Self { + node_index: Default::default(), items: Node::ast_from_object( _vm, source_file, @@ -823,6 +847,7 @@ impl Node for ruff::StmtWith { impl Node for ruff::StmtMatch { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, subject, cases, range: _range, @@ -844,6 +869,7 @@ impl Node for ruff::StmtMatch { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), subject: Node::ast_from_object( _vm, source_file, @@ -862,6 +888,7 @@ impl Node for ruff::StmtMatch { impl Node for ruff::StmtRaise { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, exc, cause, range: _range, @@ -883,6 +910,7 @@ impl Node for ruff::StmtRaise { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), exc: get_node_field_opt(_vm, &_object, "exc")? .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, @@ -897,6 +925,7 @@ impl Node for ruff::StmtRaise { impl Node for ruff::StmtTry { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, body, handlers, orelse, @@ -940,6 +969,7 @@ impl Node for ruff::StmtTry { ); Ok(Self { + node_index: Default::default(), body: Node::ast_from_object( _vm, source_file, @@ -969,6 +999,7 @@ impl Node for ruff::StmtTry { impl Node for ruff::StmtAssert { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, test, msg, range: _range, @@ -990,6 +1021,7 @@ impl Node for ruff::StmtAssert { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), test: Node::ast_from_object( _vm, source_file, @@ -1006,6 +1038,7 @@ impl Node for ruff::StmtAssert { impl Node for ruff::StmtImport { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, names, range: _range, } = self; @@ -1024,6 +1057,7 @@ impl Node for ruff::StmtImport { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), names: Node::ast_from_object( _vm, source_file, @@ -1037,6 +1071,7 @@ impl Node for ruff::StmtImport { impl Node for ruff::StmtImportFrom { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, module, names, level, @@ -1061,6 +1096,7 @@ impl Node for ruff::StmtImportFrom { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), module: get_node_field_opt(vm, &_object, "module")? .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, @@ -1081,6 +1117,7 @@ impl Node for ruff::StmtImportFrom { impl Node for ruff::StmtGlobal { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, names, range: _range, } = self; @@ -1099,6 +1136,7 @@ impl Node for ruff::StmtGlobal { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), names: Node::ast_from_object( _vm, source_file, @@ -1112,6 +1150,7 @@ impl Node for ruff::StmtGlobal { impl Node for ruff::StmtNonlocal { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, names, range: _range, } = self; @@ -1130,6 +1169,7 @@ impl Node for ruff::StmtNonlocal { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), names: Node::ast_from_object( _vm, source_file, @@ -1143,6 +1183,7 @@ impl Node for ruff::StmtNonlocal { impl Node for ruff::StmtExpr { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, value, range: _range, } = self; @@ -1161,6 +1202,7 @@ impl Node for ruff::StmtExpr { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), value: Node::ast_from_object( _vm, source_file, @@ -1173,7 +1215,10 @@ impl Node for ruff::StmtExpr { // constructor impl Node for ruff::StmtPass { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { range: _range } = self; + let Self { + node_index: _, + range: _range, + } = self; let node = NodeAst .into_ref_with_type(_vm, pyast::NodeStmtPass::static_type().to_owned()) .unwrap(); @@ -1187,6 +1232,7 @@ impl Node for ruff::StmtPass { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), range: range_from_object(_vm, source_file, _object, "Pass")?, }) } @@ -1194,7 +1240,10 @@ impl Node for ruff::StmtPass { // constructor impl Node for ruff::StmtBreak { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { range: _range } = self; + let Self { + node_index: _, + range: _range, + } = self; let node = NodeAst .into_ref_with_type(_vm, pyast::NodeStmtBreak::static_type().to_owned()) .unwrap(); @@ -1209,6 +1258,7 @@ impl Node for ruff::StmtBreak { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), range: range_from_object(_vm, source_file, _object, "Break")?, }) } @@ -1217,7 +1267,10 @@ impl Node for ruff::StmtBreak { // constructor impl Node for ruff::StmtContinue { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { - let Self { range: _range } = self; + let Self { + node_index: _, + range: _range, + } = self; let node = NodeAst .into_ref_with_type(_vm, pyast::NodeStmtContinue::static_type().to_owned()) .unwrap(); @@ -1231,6 +1284,7 @@ impl Node for ruff::StmtContinue { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), range: range_from_object(_vm, source_file, _object, "Continue")?, }) } diff --git a/vm/src/stdlib/ast/string.rs b/vm/src/stdlib/ast/string.rs index 5d8654270d4..f3df8d99262 100644 --- a/vm/src/stdlib/ast/string.rs +++ b/vm/src/stdlib/ast/string.rs @@ -5,24 +5,26 @@ fn ruff_fstring_value_into_iter( mut fstring_value: ruff::FStringValue, ) -> impl Iterator + 'static { let default = ruff::FStringPart::FString(ruff::FString { + node_index: Default::default(), range: Default::default(), elements: Default::default(), flags: ruff::FStringFlags::empty(), }); (0..fstring_value.as_slice().len()).map(move |i| { - let fstring_value = &mut fstring_value; - let tmp = fstring_value.into_iter().nth(i).unwrap(); + let tmp = fstring_value.iter_mut().nth(i).unwrap(); std::mem::replace(tmp, default.clone()) }) } fn ruff_fstring_element_into_iter( - mut fstring_element: ruff::FStringElements, -) -> impl Iterator + 'static { - let default = ruff::FStringElement::Literal(ruff::FStringLiteralElement { - range: Default::default(), - value: Default::default(), - }); + mut fstring_element: ruff::InterpolatedStringElements, +) -> impl Iterator + 'static { + let default = + ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement { + node_index: Default::default(), + range: Default::default(), + value: Default::default(), + }); (0..fstring_element.into_iter().len()).map(move |i| { let fstring_element = &mut fstring_element; let tmp = fstring_element.into_iter().nth(i).unwrap(); @@ -36,6 +38,7 @@ fn fstring_part_to_joined_str_part(fstring_part: ruff::FStringPart) -> Vec { vec![JoinedStrPart::Constant(Constant::new_str( value, @@ -47,27 +50,33 @@ fn fstring_part_to_joined_str_part(fstring_part: ruff::FStringPart) -> Vec ruff_fstring_element_into_iter(elements) .map(ruff_fstring_element_to_joined_str_part) .collect(), } } -fn ruff_fstring_element_to_joined_str_part(element: ruff::FStringElement) -> JoinedStrPart { +fn ruff_fstring_element_to_joined_str_part( + element: ruff::InterpolatedStringElement, +) -> JoinedStrPart { match element { - ruff::FStringElement::Literal(ruff::FStringLiteralElement { range, value }) => { - JoinedStrPart::Constant(Constant::new_str( - value, - ruff::str_prefix::StringLiteralPrefix::Empty, - range, - )) - } - ruff::FStringElement::Expression(ruff::FStringExpressionElement { + ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement { + range, + value, + node_index: _, + }) => JoinedStrPart::Constant(Constant::new_str( + value, + ruff::str_prefix::StringLiteralPrefix::Empty, + range, + )), + ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement { range, expression, debug_text: _, // TODO: What is this? conversion, format_spec, + node_index: _, }) => JoinedStrPart::FormattedValue(FormattedValue { value: expression, conversion, @@ -78,12 +87,16 @@ fn ruff_fstring_element_to_joined_str_part(element: ruff::FStringElement) -> Joi } fn ruff_format_spec_to_joined_str( - format_spec: Option>, + format_spec: Option>, ) -> Option> { match format_spec { None => None, Some(format_spec) => { - let ruff::FStringFormatSpec { range, elements } = *format_spec; + let ruff::InterpolatedStringFormatSpec { + range, + elements, + node_index: _, + } = *format_spec; let values: Vec<_> = ruff_fstring_element_into_iter(elements) .map(ruff_fstring_element_to_joined_str_part) .collect(); @@ -93,45 +106,37 @@ fn ruff_format_spec_to_joined_str( } } -fn ruff_fstring_element_to_ruff_fstring_part(element: ruff::FStringElement) -> ruff::FStringPart { +fn ruff_fstring_element_to_ruff_fstring_part( + element: ruff::InterpolatedStringElement, +) -> ruff::FStringPart { match element { - ruff::FStringElement::Literal(value) => { - let ruff::FStringLiteralElement { range, value } = value; - ruff::FStringPart::Literal(ruff::StringLiteral { + ruff::InterpolatedStringElement::Literal(value) => { + let ruff::InterpolatedStringLiteralElement { + node_index, range, value, - flags: ruff::StringLiteralFlags::empty(), - }) - } - ruff::FStringElement::Expression(value) => { - let ruff::FStringExpressionElement { - range, - expression, - debug_text, - conversion, - format_spec, } = value; - ruff::FStringPart::FString(ruff::FString { + ruff::FStringPart::Literal(ruff::StringLiteral { + node_index, range, - elements: vec![ruff::FStringElement::Expression( - ruff::FStringExpressionElement { - range, - expression, - debug_text, - conversion, - format_spec, - }, - )] - .into(), - flags: ruff::FStringFlags::empty(), + value, + flags: ruff::StringLiteralFlags::empty(), }) } + ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement { + range, .. + }) => ruff::FStringPart::FString(ruff::FString { + node_index: Default::default(), + range, + elements: vec![element].into(), + flags: ruff::FStringFlags::empty(), + }), } } fn joined_str_to_ruff_format_spec( joined_str: Option>, -) -> Option> { +) -> Option> { match joined_str { None => None, Some(joined_str) => { @@ -139,7 +144,8 @@ fn joined_str_to_ruff_format_spec( let elements: Vec<_> = Box::into_iter(values) .map(joined_str_part_to_ruff_fstring_element) .collect(); - let format_spec = ruff::FStringFormatSpec { + let format_spec = ruff::InterpolatedStringFormatSpec { + node_index: Default::default(), range, elements: elements.into(), }; @@ -158,10 +164,12 @@ impl JoinedStr { pub(super) fn into_expr(self) -> ruff::Expr { let Self { range, values } = self; ruff::Expr::FString(ruff::ExprFString { + node_index: Default::default(), range: Default::default(), value: match values.len() { // ruff represents an empty fstring like this: 0 => ruff::FStringValue::single(ruff::FString { + node_index: Default::default(), range, elements: vec![].into(), flags: ruff::FStringFlags::empty(), @@ -170,6 +178,7 @@ impl JoinedStr { Box::<[_]>::into_iter(values) .map(joined_str_part_to_ruff_fstring_element) .map(|element| ruff::FString { + node_index: Default::default(), range, elements: vec![element].into(), flags: ruff::FStringFlags::empty(), @@ -188,10 +197,11 @@ impl JoinedStr { } } -fn joined_str_part_to_ruff_fstring_element(part: JoinedStrPart) -> ruff::FStringElement { +fn joined_str_part_to_ruff_fstring_element(part: JoinedStrPart) -> ruff::InterpolatedStringElement { match part { JoinedStrPart::FormattedValue(value) => { - ruff::FStringElement::Expression(ruff::FStringExpressionElement { + ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement { + node_index: Default::default(), range: value.range, expression: value.value.clone(), debug_text: None, // TODO: What is this? @@ -200,7 +210,8 @@ fn joined_str_part_to_ruff_fstring_element(part: JoinedStrPart) -> ruff::FString }) } JoinedStrPart::Constant(value) => { - ruff::FStringElement::Literal(ruff::FStringLiteralElement { + ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement { + node_index: Default::default(), range: value.range, value: match value.value { ConstantLiteral::Str { value, .. } => value, @@ -344,7 +355,11 @@ pub(super) fn fstring_to_object( source_file: &SourceFile, expression: ruff::ExprFString, ) -> PyObjectRef { - let ruff::ExprFString { range, value } = expression; + let ruff::ExprFString { + range, + value, + node_index: _, + } = expression; let values: Vec<_> = ruff_fstring_value_into_iter(value) .flat_map(fstring_part_to_joined_str_part) .collect(); diff --git a/vm/src/stdlib/ast/type_parameters.rs b/vm/src/stdlib/ast/type_parameters.rs index 505cd04d284..017470f7e64 100644 --- a/vm/src/stdlib/ast/type_parameters.rs +++ b/vm/src/stdlib/ast/type_parameters.rs @@ -15,7 +15,11 @@ impl Node for ruff::TypeParams { let range = Option::zip(type_params.first(), type_params.last()) .map(|(first, last)| first.range().cover(last.range())) .unwrap_or_default(); - Ok(Self { type_params, range }) + Ok(Self { + node_index: Default::default(), + type_params, + range, + }) } fn is_none(&self) -> bool { @@ -70,6 +74,7 @@ impl Node for ruff::TypeParam { impl Node for ruff::TypeParamTypeVar { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, name, bound, range: _range, @@ -93,6 +98,7 @@ impl Node for ruff::TypeParamTypeVar { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), name: Node::ast_from_object( _vm, source_file, @@ -115,6 +121,7 @@ impl Node for ruff::TypeParamTypeVar { impl Node for ruff::TypeParamParamSpec { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, name, range: _range, default, @@ -141,6 +148,7 @@ impl Node for ruff::TypeParamParamSpec { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), name: Node::ast_from_object( _vm, source_file, @@ -160,6 +168,7 @@ impl Node for ruff::TypeParamParamSpec { impl Node for ruff::TypeParamTypeVarTuple { fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { + node_index: _, name, range: _range, default, @@ -189,6 +198,7 @@ impl Node for ruff::TypeParamTypeVarTuple { _object: PyObjectRef, ) -> PyResult { Ok(Self { + node_index: Default::default(), name: Node::ast_from_object( _vm, source_file, diff --git a/vm/src/vm/vm_new.rs b/vm/src/vm/vm_new.rs index cf8d682ec24..d0b78cfe5bd 100644 --- a/vm/src/vm/vm_new.rs +++ b/vm/src/vm/vm_new.rs @@ -321,7 +321,7 @@ impl VirtualMachine { error: ruff_python_parser::ParseErrorType::Lexical( ruff_python_parser::LexicalErrorType::FStringError( - ruff_python_parser::FStringErrorType::UnterminatedTripleQuotedString, + ruff_python_parser::InterpolatedStringErrorType::UnterminatedTripleQuotedString, ), ), .. @@ -412,7 +412,7 @@ impl VirtualMachine { fn get_statement(source: &str, loc: Option) -> Option { let line = source .split('\n') - .nth(loc?.row.to_zero_indexed())? + .nth(loc?.line.to_zero_indexed())? .to_owned(); Some(line + "\n") } diff --git a/wasm/lib/src/convert.rs b/wasm/lib/src/convert.rs index 9f4add07082..d1821f2e733 100644 --- a/wasm/lib/src/convert.rs +++ b/wasm/lib/src/convert.rs @@ -251,12 +251,12 @@ pub fn syntax_err(err: CompileError) -> SyntaxError { let _ = Reflect::set( &js_err, &"row".into(), - &(err.location().unwrap().row.get()).into(), + &(err.location().unwrap().line.get()).into(), ); let _ = Reflect::set( &js_err, &"col".into(), - &(err.location().unwrap().column.get()).into(), + &(err.location().unwrap().character_offset.get()).into(), ); // | ParseErrorType::UnrecognizedToken(Token::Dedent, _) let can_continue = matches!(