From 1ff541b70c4864b9d7a1054d6892d573a6c48a1b Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Mon, 21 Jul 2025 14:51:01 +0200 Subject: [PATCH 1/5] Replace `compiler::source`` with ruff --- Cargo.lock | 11 - Cargo.toml | 17 +- compiler/Cargo.toml | 4 +- compiler/codegen/Cargo.toml | 4 - compiler/codegen/src/compile.rs | 191 ++++----- compiler/codegen/src/symboltable.rs | 51 +-- compiler/codegen/src/unparse.rs | 17 +- compiler/source/Cargo.toml | 16 - compiler/source/src/lib.rs | 42 -- compiler/src/lib.rs | 47 +-- vm/Cargo.toml | 4 - vm/src/builtins/code.rs | 4 +- vm/src/builtins/traceback.rs | 12 +- vm/src/frame.rs | 2 +- vm/src/lib.rs | 1 - vm/src/stdlib/ast.rs | 43 +- vm/src/stdlib/ast/argument.rs | 17 +- vm/src/stdlib/ast/basic.rs | 13 +- vm/src/stdlib/ast/constant.rs | 53 +-- vm/src/stdlib/ast/elif_else_clause.rs | 25 +- vm/src/stdlib/ast/exception.rs | 30 +- vm/src/stdlib/ast/expression.rs | 549 +++++++++++++------------- vm/src/stdlib/ast/module.rs | 67 ++-- vm/src/stdlib/ast/node.rs | 38 +- vm/src/stdlib/ast/operator.rs | 17 +- vm/src/stdlib/ast/other.rs | 45 +-- vm/src/stdlib/ast/parameter.rs | 91 ++--- vm/src/stdlib/ast/pattern.rs | 189 ++++----- vm/src/stdlib/ast/statement.rs | 517 ++++++++++++------------ vm/src/stdlib/ast/string.rs | 48 +-- vm/src/stdlib/ast/type_ignore.rs | 17 +- vm/src/stdlib/ast/type_parameters.rs | 82 ++-- vm/src/vm/vm_new.rs | 2 +- 33 files changed, 1124 insertions(+), 1142 deletions(-) delete mode 100644 compiler/source/Cargo.toml delete mode 100644 compiler/source/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 50ec28b1ed..d9ef9c78b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2278,7 +2278,6 @@ dependencies = [ "ruff_source_file", "ruff_text_size", "rustpython-compiler-core", - "rustpython-compiler-source", "rustpython-literal", "rustpython-wtf8", "thiserror 2.0.12", @@ -2325,7 +2324,6 @@ dependencies = [ "ruff_text_size", "rustpython-codegen", "rustpython-compiler-core", - "rustpython-compiler-source", "thiserror 2.0.12", ] @@ -2343,14 +2341,6 @@ dependencies = [ "serde", ] -[[package]] -name = "rustpython-compiler-source" -version = "0.4.0" -dependencies = [ - "ruff_source_file", - "ruff_text_size", -] - [[package]] name = "rustpython-derive" version = "0.4.0" @@ -2561,7 +2551,6 @@ dependencies = [ "rustpython-common", "rustpython-compiler", "rustpython-compiler-core", - "rustpython-compiler-source", "rustpython-derive", "rustpython-jit", "rustpython-literal", diff --git a/Cargo.toml b/Cargo.toml index 440855aba5..77ecb2a769 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -117,8 +117,20 @@ template = "installer-config/installer.wxs" [workspace] resolver = "2" members = [ - "compiler", "compiler/core", "compiler/codegen", "compiler/literal", "compiler/source", - ".", "common", "derive", "jit", "vm", "vm/sre_engine", "pylib", "stdlib", "derive-impl", "wtf8", + "compiler", + "compiler/core", + "compiler/codegen", + "compiler/literal", + ".", + "common", + "derive", + "jit", + "vm", + "vm/sre_engine", + "pylib", + "stdlib", + "derive-impl", + "wtf8", "wasm/lib", ] @@ -131,7 +143,6 @@ repository = "https://github.com/RustPython/RustPython" license = "MIT" [workspace.dependencies] -rustpython-compiler-source = { path = "compiler/source" } rustpython-compiler-core = { path = "compiler/core", version = "0.4.0" } rustpython-compiler = { path = "compiler", version = "0.4.0" } rustpython-codegen = { path = "compiler/codegen", version = "0.4.0" } diff --git a/compiler/Cargo.toml b/compiler/Cargo.toml index 39b7c54beb..2ecc06d59a 100644 --- a/compiler/Cargo.toml +++ b/compiler/Cargo.toml @@ -11,8 +11,6 @@ license.workspace = true [dependencies] rustpython-codegen = { workspace = true } rustpython-compiler-core = { workspace = true } -rustpython-compiler-source = { workspace = true } -# rustpython-parser = { workspace = true } ruff_python_parser = { workspace = true } ruff_python_ast = { workspace = true } ruff_source_file = { workspace = true } @@ -23,4 +21,4 @@ thiserror = { workspace = true } rand = { workspace = true } [lints] -workspace = true \ No newline at end of file +workspace = true diff --git a/compiler/codegen/Cargo.toml b/compiler/codegen/Cargo.toml index 479b0b29f6..6093e522e4 100644 --- a/compiler/codegen/Cargo.toml +++ b/compiler/codegen/Cargo.toml @@ -8,12 +8,8 @@ rust-version.workspace = true repository.workspace = true license.workspace = true - [dependencies] -# rustpython-ast = { workspace = true, features=["unparse", "constant-optimization"] } -# rustpython-parser-core = { workspace = true } rustpython-compiler-core = { workspace = true } -rustpython-compiler-source = {workspace = true } rustpython-literal = {workspace = true } rustpython-wtf8 = { workspace = true } ruff_python_ast = { workspace = true } diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index 99f14bea5b..3119927851 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -11,11 +11,37 @@ use crate::{ IndexMap, IndexSet, ToPythonName, - error::{CodegenError, CodegenErrorType, PatternUnreachableReason}, + error::{CodegenError, CodegenErrorType, InternalError, PatternUnreachableReason}, ir::{self, BlockIdx}, symboltable::{self, CompilerScope, SymbolFlags, SymbolScope, SymbolTable}, unparse::unparse_expr, }; +use itertools::Itertools; +use malachite_bigint::BigInt; +use num_complex::Complex; +use num_traits::{Num, ToPrimitive}; +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, PatternMatchOr, PatternMatchSequence, PatternMatchSingleton, + PatternMatchStar, PatternMatchValue, Singleton, Stmt, StmtExpr, TypeParam, TypeParamParamSpec, + TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, UnaryOp, WithItem, +}; +use ruff_source_file::{OneIndexed, SourceFile}; +use ruff_text_size::{Ranged, TextRange}; +use rustpython_wtf8::Wtf8Buf; +// use rustpython_ast::located::{self as located_ast, Located}; +use rustpython_compiler_core::{ + Mode, + bytecode::{ + self, Arg as OpArgMarker, BinaryOperator, CodeObject, ComparisonOperator, ConstantData, + Instruction, OpArg, OpArgType, UnpackExArgs, + }, +}; +use std::borrow::Cow; const MAXBLOCKS: usize = 20; @@ -43,35 +69,6 @@ pub struct FBlockInfo { pub fb_exit: BlockIdx, // fb_datum is not needed in RustPython } -use itertools::Itertools; -use malachite_bigint::BigInt; -use num_complex::Complex; -use num_traits::{Num, ToPrimitive}; -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, PatternMatchOr, PatternMatchSequence, PatternMatchSingleton, - PatternMatchStar, PatternMatchValue, Singleton, Stmt, StmtExpr, TypeParam, TypeParamParamSpec, - TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, UnaryOp, WithItem, -}; -use ruff_source_file::OneIndexed; -use ruff_text_size::{Ranged, TextRange}; -use rustpython_wtf8::Wtf8Buf; -// use rustpython_ast::located::{self as located_ast, Located}; -use rustpython_compiler_core::{ - Mode, - bytecode::{ - self, Arg as OpArgMarker, BinaryOperator, CodeObject, ComparisonOperator, ConstantData, - Instruction, OpArg, OpArgType, UnpackExArgs, - }, -}; -use rustpython_compiler_source::SourceCode; -// use rustpython_parser_core::source_code::{LineNumber, SourceLocation}; -use crate::error::InternalError; -use std::borrow::Cow; pub(crate) type InternalResult = Result; type CompileResult = Result; @@ -97,10 +94,10 @@ fn is_forbidden_name(name: &str) -> bool { } /// Main structure holding the state of compilation. -struct Compiler<'src> { +struct Compiler { code_stack: Vec, symbol_table_stack: Vec, - source_code: SourceCode<'src>, + source_file: SourceFile, // current_source_location: SourceLocation, current_source_range: TextRange, done_with_future_stmts: DoneWithFuture, @@ -154,29 +151,29 @@ enum ComprehensionType { /// Compile an Mod produced from ruff parser pub fn compile_top( ast: ruff_python_ast::Mod, - source_code: SourceCode<'_>, + source_file: SourceFile, mode: Mode, opts: CompileOpts, ) -> CompileResult { match ast { ruff_python_ast::Mod::Module(module) => match mode { - Mode::Exec | Mode::Eval => compile_program(&module, source_code, opts), - Mode::Single => compile_program_single(&module, source_code, opts), - Mode::BlockExpr => compile_block_expression(&module, source_code, opts), + Mode::Exec | Mode::Eval => compile_program(&module, source_file, opts), + Mode::Single => compile_program_single(&module, source_file, opts), + Mode::BlockExpr => compile_block_expression(&module, source_file, opts), }, - ruff_python_ast::Mod::Expression(expr) => compile_expression(&expr, source_code, opts), + ruff_python_ast::Mod::Expression(expr) => compile_expression(&expr, source_file, opts), } } /// Compile a standard Python program to bytecode pub fn compile_program( ast: &ModModule, - source_code: SourceCode<'_>, + source_file: SourceFile, opts: CompileOpts, ) -> CompileResult { - let symbol_table = SymbolTable::scan_program(ast, source_code.clone()) - .map_err(|e| e.into_codegen_error(source_code.path.to_owned()))?; - let mut compiler = Compiler::new(opts, source_code, "".to_owned()); + let symbol_table = SymbolTable::scan_program(ast, source_file.clone()) + .map_err(|e| e.into_codegen_error(source_file.name().to_owned()))?; + let mut compiler = Compiler::new(opts, source_file, "".to_owned()); compiler.compile_program(ast, symbol_table)?; let code = compiler.exit_scope(); trace!("Compilation completed: {code:?}"); @@ -186,12 +183,12 @@ pub fn compile_program( /// Compile a Python program to bytecode for the context of a REPL pub fn compile_program_single( ast: &ModModule, - source_code: SourceCode<'_>, + source_file: SourceFile, opts: CompileOpts, ) -> CompileResult { - let symbol_table = SymbolTable::scan_program(ast, source_code.clone()) - .map_err(|e| e.into_codegen_error(source_code.path.to_owned()))?; - let mut compiler = Compiler::new(opts, source_code, "".to_owned()); + let symbol_table = SymbolTable::scan_program(ast, source_file.clone()) + .map_err(|e| e.into_codegen_error(source_file.name().to_owned()))?; + let mut compiler = Compiler::new(opts, source_file, "".to_owned()); compiler.compile_program_single(&ast.body, symbol_table)?; let code = compiler.exit_scope(); trace!("Compilation completed: {code:?}"); @@ -200,12 +197,12 @@ pub fn compile_program_single( pub fn compile_block_expression( ast: &ModModule, - source_code: SourceCode<'_>, + source_file: SourceFile, opts: CompileOpts, ) -> CompileResult { - let symbol_table = SymbolTable::scan_program(ast, source_code.clone()) - .map_err(|e| e.into_codegen_error(source_code.path.to_owned()))?; - let mut compiler = Compiler::new(opts, source_code, "".to_owned()); + let symbol_table = SymbolTable::scan_program(ast, source_file.clone()) + .map_err(|e| e.into_codegen_error(source_file.name().to_owned()))?; + let mut compiler = Compiler::new(opts, source_file, "".to_owned()); compiler.compile_block_expr(&ast.body, symbol_table)?; let code = compiler.exit_scope(); trace!("Compilation completed: {code:?}"); @@ -214,12 +211,12 @@ pub fn compile_block_expression( pub fn compile_expression( ast: &ModExpression, - source_code: SourceCode<'_>, + source_file: SourceFile, opts: CompileOpts, ) -> CompileResult { - let symbol_table = SymbolTable::scan_expr(ast, source_code.clone()) - .map_err(|e| e.into_codegen_error(source_code.path.to_owned()))?; - let mut compiler = Compiler::new(opts, source_code, "".to_owned()); + let symbol_table = SymbolTable::scan_expr(ast, source_file.clone()) + .map_err(|e| e.into_codegen_error(source_file.name().to_owned()))?; + let mut compiler = Compiler::new(opts, source_file, "".to_owned()); compiler.compile_eval(ast, symbol_table)?; let code = compiler.exit_scope(); Ok(code) @@ -240,16 +237,18 @@ macro_rules! emit { }; } -fn eprint_location(zelf: &Compiler<'_>) { +fn eprint_location(zelf: &Compiler) { let start = zelf - .source_code + .source_file + .to_source_code() .source_location(zelf.current_source_range.start()); let end = zelf - .source_code + .source_file + .to_source_code() .source_location(zelf.current_source_range.end()); eprintln!( "LOCATION: {} from {}:{} to {}:{}", - zelf.source_code.path.to_owned(), + zelf.source_file.name(), start.row, start.column, end.row, @@ -259,7 +258,7 @@ fn eprint_location(zelf: &Compiler<'_>) { /// Better traceback for internal error #[track_caller] -fn unwrap_internal(zelf: &Compiler<'_>, r: InternalResult) -> T { +fn unwrap_internal(zelf: &Compiler, r: InternalResult) -> T { if let Err(ref r_err) = r { eprintln!("=== CODEGEN PANIC INFO ==="); eprintln!("This IS an internal error: {r_err}"); @@ -269,7 +268,7 @@ fn unwrap_internal(zelf: &Compiler<'_>, r: InternalResult) -> T { r.unwrap() } -fn compiler_unwrap_option(zelf: &Compiler<'_>, o: Option) -> T { +fn compiler_unwrap_option(zelf: &Compiler, o: Option) -> T { if o.is_none() { eprintln!("=== CODEGEN PANIC INFO ==="); eprintln!("This IS an internal error, an option was unwrapped during codegen"); @@ -279,7 +278,7 @@ fn compiler_unwrap_option(zelf: &Compiler<'_>, o: Option) -> T { o.unwrap() } -// fn compiler_result_unwrap(zelf: &Compiler<'_>, result: Result) -> T { +// fn compiler_result_unwrap(zelf: &Compiler, result: Result) -> T { // if result.is_err() { // eprintln!("=== CODEGEN PANIC INFO ==="); // eprintln!("This IS an internal error, an result was unwrapped during codegen"); @@ -328,11 +327,19 @@ enum JumpOp { PopJumpIfFalse, } -impl<'src> Compiler<'src> { - fn new(opts: CompileOpts, source_code: SourceCode<'src>, code_name: String) -> Self { +/// Type of collection to build in starunpack_helper +#[derive(Debug, Clone, Copy, PartialEq)] +enum CollectionType { + Tuple, + List, + Set, +} + +impl Compiler { + fn new(opts: CompileOpts, source_file: SourceFile, code_name: String) -> Self { let module_code = ir::CodeInfo { flags: bytecode::CodeFlags::NEW_LOCALS, - source_path: source_code.path.to_owned(), + source_path: source_file.name().to_owned(), private: None, blocks: vec![ir::Block::default()], current_block: ir::BlockIdx(0), @@ -358,7 +365,7 @@ impl<'src> Compiler<'src> { Compiler { code_stack: vec![module_code], symbol_table_stack: Vec::new(), - source_code, + source_file, // current_source_location: SourceLocation::default(), current_source_range: TextRange::default(), done_with_future_stmts: DoneWithFuture::No, @@ -372,17 +379,7 @@ impl<'src> Compiler<'src> { in_annotation: false, } } -} -/// Type of collection to build in starunpack_helper -#[derive(Debug, Clone, Copy, PartialEq)] -enum CollectionType { - Tuple, - List, - Set, -} - -impl Compiler<'_> { /// Check if the slice is a two-element slice (no step) // = is_two_element_slice fn is_two_element_slice(slice: &Expr) -> bool { @@ -526,15 +523,20 @@ impl Compiler<'_> { Ok(()) } + fn error(&mut self, error: CodegenErrorType) -> CodegenError { self.error_ranged(error, self.current_source_range) } + fn error_ranged(&mut self, error: CodegenErrorType, range: TextRange) -> CodegenError { - let location = self.source_code.source_location(range.start()); + let location = self + .source_file + .to_source_code() + .source_location(range.start()); CodegenError { error, location: Some(location), - source_path: self.source_code.path.to_owned(), + source_path: self.source_file.name().to_owned(), } } @@ -637,7 +639,7 @@ impl Compiler<'_> { // Allocate a new compiler unit // In Rust, we'll create the structure directly - let source_path = self.source_code.path.to_owned(); + let source_path = self.source_file.name().to_owned(); // Lookup symbol table entry using key (_PySymtable_Lookup) let ste = if key < self.symbol_table_stack.len() { @@ -4034,7 +4036,7 @@ impl Compiler<'_> { fn compile_annotation(&mut self, annotation: &Expr) -> CompileResult<()> { if self.future_annotations { self.emit_load_const(ConstantData::Str { - value: unparse_expr(annotation, &self.source_code) + value: unparse_expr(annotation, &self.source_file) .to_string() .into(), }); @@ -4777,7 +4779,7 @@ impl Compiler<'_> { .value .iter() .map(|lit| { - let source = self.source_code.get_range(lit.range); + let source = self.source_file.slice(lit.range); crate::string_parser::parse_string_literal(source, lit.flags.into()) }) .collect(); @@ -5220,7 +5222,10 @@ impl Compiler<'_> { // Low level helper functions: fn _emit(&mut self, instr: Instruction, arg: OpArg, target: ir::BlockIdx) { let range = self.current_source_range; - let location = self.source_code.source_location(range.start()); + let location = self + .source_file + .to_source_code() + .source_location(range.start()); // TODO: insert source filename self.current_block().instructions.push(ir::InstructionInfo { instr, @@ -5311,7 +5316,8 @@ impl Compiler<'_> { } fn get_source_line_number(&mut self) -> OneIndexed { - self.source_code + self.source_file + .to_source_code() .line_index(self.current_source_range.start()) } @@ -5463,7 +5469,7 @@ impl Compiler<'_> { FStringPart::Literal(string) => { if string.value.contains(char::REPLACEMENT_CHARACTER) { // might have a surrogate literal; should reparse to be sure - let source = self.source_code.get_range(string.range); + let source = self.source_file.slice(string.range); let value = crate::string_parser::parse_string_literal(source, string.flags.into()); self.emit_load_const(ConstantData::Str { @@ -5496,7 +5502,7 @@ impl Compiler<'_> { FStringElement::Literal(string) => { if string.value.contains(char::REPLACEMENT_CHARACTER) { // might have a surrogate literal; should reparse to be sure - let source = self.source_code.get_range(string.range); + let source = self.source_file.slice(string.range); let value = crate::string_parser::parse_fstring_literal_element( source.into(), flags.into(), @@ -5515,7 +5521,7 @@ impl Compiler<'_> { if let Some(DebugText { leading, trailing }) = &fstring_expr.debug_text { let range = fstring_expr.expression.range(); - let source = self.source_code.get_range(range); + let source = self.source_file.slice(range); let text = [leading, source, trailing].concat(); self.emit_load_const(ConstantData::Str { value: text.into() }); @@ -5836,22 +5842,25 @@ mod ruff_tests { #[cfg(test)] mod tests { use super::*; + use ruff_source_file::SourceFileBuilder; fn compile_exec(source: &str) -> CodeObject { let opts = CompileOpts::default(); - let source_code = SourceCode::new("source_path", source); - let parsed = - ruff_python_parser::parse(source_code.text, ruff_python_parser::Mode::Module.into()) - .unwrap(); + let source_file = SourceFileBuilder::new("source_path", source).finish(); + let parsed = ruff_python_parser::parse( + source_file.source_text(), + ruff_python_parser::Mode::Module.into(), + ) + .unwrap(); let ast = parsed.into_syntax(); let ast = match ast { ruff_python_ast::Mod::Module(stmts) => stmts, _ => unreachable!(), }; - let symbol_table = SymbolTable::scan_program(&ast, source_code.clone()) - .map_err(|e| e.into_codegen_error(source_code.path.to_owned())) + let symbol_table = SymbolTable::scan_program(&ast, source_file.clone()) + .map_err(|e| e.into_codegen_error(source_file.name().to_owned())) .unwrap(); - let mut compiler = Compiler::new(opts, source_code, "".to_owned()); + let mut compiler = Compiler::new(opts, source_file, "".to_owned()); compiler.compile_program(&ast, symbol_table).unwrap(); compiler.exit_scope() } diff --git a/compiler/codegen/src/symboltable.rs b/compiler/codegen/src/symboltable.rs index fdfc83e797..ccf9aa4f31 100644 --- a/compiler/codegen/src/symboltable.rs +++ b/compiler/codegen/src/symboltable.rs @@ -18,8 +18,8 @@ use ruff_python_ast::{ PatternMatchMapping, PatternMatchOr, PatternMatchSequence, PatternMatchStar, PatternMatchValue, Stmt, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, }; +use ruff_source_file::{SourceFile, SourceLocation}; use ruff_text_size::{Ranged, TextRange}; -use rustpython_compiler_source::{SourceCode, SourceLocation}; // use rustpython_ast::{self as ast, located::Located}; // use rustpython_parser_core::source_code::{LineNumber, SourceLocation}; use std::{borrow::Cow, fmt}; @@ -75,20 +75,21 @@ impl SymbolTable { } } - pub fn scan_program( - program: &ModModule, - source_code: SourceCode<'_>, - ) -> SymbolTableResult { - let mut builder = SymbolTableBuilder::new(source_code); + pub fn scan_program(program: &ModModule, source_file: SourceFile) -> SymbolTableResult { + let mut builder = SymbolTableBuilder::new(source_file); builder.scan_statements(program.body.as_ref())?; builder.finish() } - pub fn scan_expr(expr: &ModExpression, source_code: SourceCode<'_>) -> SymbolTableResult { - let mut builder = SymbolTableBuilder::new(source_code); + pub fn scan_expr(expr: &ModExpression, source_file: SourceFile) -> SymbolTableResult { + let mut builder = SymbolTableBuilder::new(source_file); builder.scan_expression(expr.body.as_ref(), ExpressionContext::Load)?; builder.finish() } + + pub fn lookup(&self, name: &str) -> Option<&Symbol> { + self.symbols.get(name) + } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -216,12 +217,6 @@ impl SymbolTableError { type SymbolTableResult = Result; -impl SymbolTable { - pub fn lookup(&self, name: &str) -> Option<&Symbol> { - self.symbols.get(name) - } -} - impl std::fmt::Debug for SymbolTable { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( @@ -610,12 +605,12 @@ enum SymbolUsage { TypeParam, } -struct SymbolTableBuilder<'src> { +struct SymbolTableBuilder { class_name: Option, // Scope stack. tables: Vec, future_annotations: bool, - source_code: SourceCode<'src>, + source_file: SourceFile, // Current scope's varnames being collected (temporary storage) current_varnames: Vec, } @@ -633,21 +628,19 @@ enum ExpressionContext { IterDefinitionExp, } -impl<'src> SymbolTableBuilder<'src> { - fn new(source_code: SourceCode<'src>) -> Self { +impl SymbolTableBuilder { + fn new(source_file: SourceFile) -> Self { let mut this = Self { class_name: None, tables: vec![], future_annotations: false, - source_code, + source_file, current_varnames: Vec::new(), }; this.enter_scope("top", CompilerScope::Module, 0); this } -} -impl SymbolTableBuilder<'_> { fn finish(mut self) -> Result { assert_eq!(self.tables.len(), 1); let mut symbol_table = self.tables.pop().unwrap(); @@ -703,7 +696,10 @@ impl SymbolTableBuilder<'_> { } fn line_index_start(&self, range: TextRange) -> u32 { - self.source_code.line_index(range.start()).get() as _ + self.source_file + .to_source_code() + .line_index(range.start()) + .get() as _ } fn scan_statements(&mut self, statements: &[Stmt]) -> SymbolTableResult { @@ -1051,7 +1047,9 @@ impl SymbolTableBuilder<'_> { "{keyword} expression cannot be used within a type parameter" ), location: Some( - self.source_code.source_location(expression.range().start()), + self.source_file + .to_source_code() + .source_location(expression.range().start()), ), }); } @@ -1289,7 +1287,7 @@ impl SymbolTableBuilder<'_> { 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_code.source_location(target.range().start())), + location: Some(self.source_file.to_source_code().source_location(target.range().start())), }); } @@ -1569,7 +1567,10 @@ impl SymbolTableBuilder<'_> { role: SymbolUsage, range: TextRange, ) -> SymbolTableResult { - let location = self.source_code.source_location(range.start()); + let location = self + .source_file + .to_source_code() + .source_location(range.start()); 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 47e883da3a..81bd4dfe2d 100644 --- a/compiler/codegen/src/unparse.rs +++ b/compiler/codegen/src/unparse.rs @@ -1,6 +1,6 @@ use ruff_python_ast as ruff; +use ruff_source_file::SourceFile; use ruff_text_size::Ranged; -use rustpython_compiler_source::SourceCode; use rustpython_literal::escape::{AsciiEscape, UnicodeEscape}; use std::fmt::{self, Display as _}; @@ -29,28 +29,33 @@ mod precedence { struct Unparser<'a, 'b, 'c> { f: &'b mut fmt::Formatter<'a>, - source: &'c SourceCode<'c>, + source: &'c SourceFile, } + impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { - const fn new(f: &'b mut fmt::Formatter<'a>, source: &'c SourceCode<'c>) -> Self { + const fn new(f: &'b mut fmt::Formatter<'a>, source: &'c SourceFile) -> Self { Unparser { f, source } } fn p(&mut self, s: &str) -> fmt::Result { self.f.write_str(s) } + fn p_id(&mut self, s: &Identifier) -> fmt::Result { self.f.write_str(s.as_str()) } + fn p_if(&mut self, cond: bool, s: &str) -> fmt::Result { if cond { self.f.write_str(s)?; } Ok(()) } + fn p_delim(&mut self, first: &mut bool, s: &str) -> fmt::Result { self.p_if(!std::mem::take(first), s) } + fn write_fmt(&mut self, f: fmt::Arguments<'_>) -> fmt::Result { self.f.write_fmt(f) } @@ -525,7 +530,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { }); if let Some(ruff::DebugText { leading, trailing }) = debug_text { self.p(leading)?; - self.p(self.source.get_range(val.range()))?; + self.p(self.source.slice(val.range()))?; self.p(trailing)?; } let brace = if buffered.starts_with('{') { @@ -599,10 +604,10 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> { pub struct UnparseExpr<'a> { expr: &'a Expr, - source: &'a SourceCode<'a>, + source: &'a SourceFile, } -pub const fn unparse_expr<'a>(expr: &'a Expr, source: &'a SourceCode<'a>) -> UnparseExpr<'a> { +pub const fn unparse_expr<'a>(expr: &'a Expr, source: &'a SourceFile) -> UnparseExpr<'a> { UnparseExpr { expr, source } } diff --git a/compiler/source/Cargo.toml b/compiler/source/Cargo.toml deleted file mode 100644 index 373af47cf0..0000000000 --- a/compiler/source/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "rustpython-compiler-source" -description = "RustPython Source and Index" -version.workspace = true -authors.workspace = true -edition.workspace = true -rust-version.workspace = true -repository.workspace = true -license.workspace = true - -[dependencies] -ruff_source_file = { workspace = true } -ruff_text_size = { workspace = true } - -[lints] -workspace = true \ No newline at end of file diff --git a/compiler/source/src/lib.rs b/compiler/source/src/lib.rs deleted file mode 100644 index 2d967e218d..0000000000 --- a/compiler/source/src/lib.rs +++ /dev/null @@ -1,42 +0,0 @@ -pub use ruff_source_file::{LineIndex, OneIndexed as LineNumber, SourceLocation}; -use ruff_text_size::TextRange; -pub use ruff_text_size::TextSize; - -#[derive(Clone)] -pub struct SourceCode<'src> { - pub path: &'src str, - pub text: &'src str, - pub index: LineIndex, -} - -impl<'src> SourceCode<'src> { - pub fn new(path: &'src str, text: &'src str) -> Self { - let index = LineIndex::from_source_text(text); - Self { path, text, index } - } - - pub fn line_index(&self, offset: TextSize) -> LineNumber { - self.index.line_index(offset) - } - - pub fn source_location(&self, offset: TextSize) -> SourceLocation { - self.index.source_location(offset, self.text) - } - - pub fn get_range(&'src self, range: TextRange) -> &'src str { - &self.text[range.start().to_usize()..range.end().to_usize()] - } -} - -pub struct SourceCodeOwned { - pub path: String, - pub text: String, - pub index: LineIndex, -} - -impl SourceCodeOwned { - pub fn new(path: String, text: String) -> Self { - let index = LineIndex::from_source_text(&text); - Self { path, text, index } - } -} diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index 717593ac4b..7e6087dd36 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -1,16 +1,14 @@ -use ruff_source_file::SourceLocation; +use ruff_source_file::{SourceFile, SourceFileBuilder, SourceLocation}; use rustpython_codegen::{compile, symboltable}; pub use rustpython_codegen::compile::CompileOpts; pub use rustpython_compiler_core::{Mode, bytecode::CodeObject}; -use rustpython_compiler_source::SourceCode; // these modules are out of repository. re-exporting them here for convenience. pub use ruff_python_ast as ast; pub use ruff_python_parser as parser; pub use rustpython_codegen as codegen; pub use rustpython_compiler_core as core; -pub use rustpython_compiler_source as source; use thiserror::Error; #[derive(Error, Debug)] @@ -45,13 +43,15 @@ pub enum CompileError { } impl CompileError { - pub fn from_ruff_parse_error(error: parser::ParseError, source_code: &SourceCode<'_>) -> Self { - let location = source_code.source_location(error.location.start()); + 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()); Self::Parse(ParseError { error: error.error, raw_location: error.location, location, - source_path: source_code.path.to_owned(), + source_path: source_file.name().to_owned(), }) } @@ -97,8 +97,8 @@ pub fn compile( // break in a multiline string into just an LF in the parsed value #[cfg(windows)] let source = &source.replace("\r\n", "\n"); - let source_code = SourceCode::new(source_path, source); - _compile(source_code, mode, opts) + let source_file = SourceFileBuilder::new(source_path, source).finish(); + _compile(source_file, mode, opts) // let index = LineIndex::from_source_text(source); // let source_code = SourceCode::new(source, &index); // let mut locator = LinearLocator::new(source); @@ -117,7 +117,7 @@ pub fn compile( } fn _compile( - source_code: SourceCode<'_>, + source_file: SourceFile, mode: Mode, opts: CompileOpts, ) -> Result { @@ -128,10 +128,10 @@ fn _compile( // since these are only different in terms of compilation Mode::Single | Mode::BlockExpr => parser::Mode::Module, }; - let parsed = parser::parse(source_code.text, parser_mode.into()) - .map_err(|err| CompileError::from_ruff_parse_error(err, &source_code))?; + let parsed = parser::parse(source_file.source_text(), parser_mode.into()) + .map_err(|err| CompileError::from_ruff_parse_error(err, &source_file))?; let ast = parsed.into_syntax(); - compile::compile_top(ast, source_code, mode, opts).map_err(|e| e.into()) + compile::compile_top(ast, source_file, mode, opts).map_err(|e| e.into()) } pub fn compile_symtable( @@ -139,30 +139,33 @@ pub fn compile_symtable( mode: Mode, source_path: &str, ) -> Result { - let source_code = SourceCode::new(source_path, source); - _compile_symtable(source_code, mode) + let source_file = SourceFileBuilder::new(source_path, source).finish(); + _compile_symtable(source_file, mode) } pub fn _compile_symtable( - source_code: SourceCode<'_>, + source_file: SourceFile, mode: Mode, ) -> Result { let res = match mode { Mode::Exec | Mode::Single | Mode::BlockExpr => { - let ast = ruff_python_parser::parse_module(source_code.text) - .map_err(|e| CompileError::from_ruff_parse_error(e, &source_code))?; - symboltable::SymbolTable::scan_program(&ast.into_syntax(), source_code.clone()) + let ast = ruff_python_parser::parse_module(source_file.source_text()) + .map_err(|e| CompileError::from_ruff_parse_error(e, &source_file))?; + symboltable::SymbolTable::scan_program(&ast.into_syntax(), source_file.clone()) } Mode::Eval => { - let ast = ruff_python_parser::parse(source_code.text, parser::Mode::Expression.into()) - .map_err(|e| CompileError::from_ruff_parse_error(e, &source_code))?; + let ast = ruff_python_parser::parse( + source_file.source_text(), + parser::Mode::Expression.into(), + ) + .map_err(|e| CompileError::from_ruff_parse_error(e, &source_file))?; symboltable::SymbolTable::scan_expr( &ast.into_syntax().expect_expression(), - source_code.clone(), + source_file.clone(), ) } }; - res.map_err(|e| e.into_codegen_error(source_code.path.to_owned()).into()) + res.map_err(|e| e.into_codegen_error(source_file.name().to_owned()).into()) } #[test] diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 5a4b0df2a1..1592f1966f 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -37,11 +37,7 @@ ruff_python_ast = { workspace = true, optional = true } ruff_python_parser = { workspace = true } ruff_text_size = { workspace = true, optional = true } ruff_source_file = { workspace = true, optional = true } -# rustpython-ast = { workspace = true, optional = true } -# rustpython-parser = { workspace = true, optional = true } rustpython-compiler-core = { workspace = true } -rustpython-compiler-source = { workspace = true } -# rustpython-parser-core = { workspace = true } rustpython-literal = { workspace = true } rustpython-sre_engine = { workspace = true } diff --git a/vm/src/builtins/code.rs b/vm/src/builtins/code.rs index 59058df134..3c182b936c 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -11,11 +11,11 @@ use crate::{ convert::ToPyObject, frozen, function::{FuncArgs, OptionalArg}, - source::LineNumber, types::Representable, }; use malachite_bigint::BigInt; use num_traits::Zero; +use ruff_source_file::OneIndexed; use std::{borrow::Borrow, fmt, ops::Deref}; #[derive(FromArgs)] @@ -356,7 +356,7 @@ impl PyCode { }; let first_line_number = match args.co_firstlineno { - OptionalArg::Present(first_line_number) => LineNumber::new(first_line_number as _), + OptionalArg::Present(first_line_number) => OneIndexed::new(first_line_number as _), OptionalArg::Missing => self.code.first_line_number, }; diff --git a/vm/src/builtins/traceback.rs b/vm/src/builtins/traceback.rs index 05e9944e09..713dc17ba5 100644 --- a/vm/src/builtins/traceback.rs +++ b/vm/src/builtins/traceback.rs @@ -1,10 +1,10 @@ -use rustpython_common::lock::PyMutex; - use super::{PyType, PyTypeRef}; use crate::{ Context, Py, PyPayload, PyRef, PyResult, VirtualMachine, class::PyClassImpl, frame::FrameRef, - source::LineNumber, types::Constructor, + types::Constructor, }; +use ruff_source_file::OneIndexed; +use rustpython_common::lock::PyMutex; #[pyclass(module = false, name = "traceback", traverse)] #[derive(Debug)] @@ -14,7 +14,7 @@ pub struct PyTraceback { #[pytraverse(skip)] pub lasti: u32, #[pytraverse(skip)] - pub lineno: LineNumber, + pub lineno: OneIndexed, } pub type PyTracebackRef = PyRef; @@ -32,7 +32,7 @@ impl PyTraceback { next: Option>, frame: FrameRef, lasti: u32, - lineno: LineNumber, + lineno: OneIndexed, ) -> Self { Self { next: PyMutex::new(next), @@ -73,7 +73,7 @@ impl Constructor for PyTraceback { fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult { let (next, frame, lasti, lineno) = args; - let lineno = LineNumber::new(lineno) + let lineno = OneIndexed::new(lineno) .ok_or_else(|| vm.new_value_error("lineno must be positive".to_owned()))?; let tb = PyTraceback::new(next, frame, lasti, lineno); tb.into_ref_with_type(vm, cls).map(Into::into) diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 5730e83203..8fec94e537 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -16,12 +16,12 @@ use crate::{ function::{ArgMapping, Either, FuncArgs}, protocol::{PyIter, PyIterReturn}, scope::Scope, - source::SourceLocation, stdlib::{builtins, typing}, vm::{Context, PyMethod}, }; use indexmap::IndexMap; use itertools::Itertools; +use ruff_source_file::SourceLocation; use rustpython_common::wtf8::Wtf8Buf; #[cfg(feature = "threading")] use std::sync::atomic; diff --git a/vm/src/lib.rs b/vm/src/lib.rs index e854518dc2..b6656a8f52 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -84,7 +84,6 @@ pub mod warn; #[cfg(windows)] pub mod windows; -pub use self::compiler::source; pub use self::convert::{TryFromBorrowedObject, TryFromObject}; pub use self::object::{ AsObject, Py, PyAtomicRef, PyExact, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index cc111e4c05..5b07c35771 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -18,17 +18,15 @@ use crate::{ compiler::core::bytecode::OpArgType, compiler::{CompileError, ParseError}, convert::ToPyObject, - source::SourceCode, - source::SourceLocation, }; use node::Node; use ruff_python_ast as ruff; -use ruff_source_file::OneIndexed; +use ruff_source_file::{LineIndex, OneIndexed, SourceFile, SourceFileBuilder, SourceLocation}; use ruff_text_size::{Ranged, TextRange, TextSize}; -use rustpython_compiler_source::SourceCodeOwned; #[cfg(feature = "parser")] use ruff_python_parser as parser; + #[cfg(feature = "codegen")] use rustpython_codegen as codegen; @@ -126,12 +124,9 @@ impl Column { } } -fn text_range_to_source_range( - source_code: &SourceCodeOwned, - text_range: TextRange, -) -> PySourceRange { - let index = &source_code.index; - let source = &source_code.text; +fn text_range_to_source_range(source_file: &SourceFile, text_range: TextRange) -> PySourceRange { + let index = LineIndex::from_source_text(source_file.clone().source_text()); + let source = &source_file.source_text(); if source.is_empty() { return PySourceRange { @@ -165,7 +160,7 @@ fn text_range_to_source_range( fn range_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, name: &str, ) -> PyResult { @@ -185,12 +180,12 @@ fn range_from_object( }, }; - Ok(source_range_to_text_range(source_code, location)) + Ok(source_range_to_text_range(source_file, location)) } -fn source_range_to_text_range(source_code: &SourceCodeOwned, location: PySourceRange) -> TextRange { - let source = &source_code.text; - let index = &source_code.index; +fn source_range_to_text_range(source_file: &SourceFile, location: PySourceRange) -> TextRange { + let index = LineIndex::from_source_text(source_file.clone().source_text()); + let source = &source_file.source_text(); if source.is_empty() { return TextRange::new(TextSize::new(0), TextSize::new(0)); @@ -214,9 +209,9 @@ fn node_add_location( dict: &Py, range: TextRange, vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, ) { - let range = text_range_to_source_range(source_code, range); + let range = text_range_to_source_range(source_file, range); dict.set_item("lineno", vm.ctx.new_int(range.start.row.get()).into(), vm) .unwrap(); dict.set_item( @@ -241,12 +236,12 @@ pub(crate) fn parse( source: &str, mode: parser::Mode, ) -> Result { - let source_code = SourceCodeOwned::new("".to_owned(), source.to_owned()); + let source_file = SourceFileBuilder::new("".to_owned(), source.to_owned()).finish(); let top = parser::parse(source, mode.into()) .map_err(|parse_error| ParseError { error: parse_error.error, raw_location: parse_error.location, - location: text_range_to_source_range(&source_code, parse_error.location) + location: text_range_to_source_range(&source_file, parse_error.location) .start .to_source_location(), source_path: "".to_string(), @@ -256,7 +251,7 @@ pub(crate) fn parse( ruff::Mod::Module(m) => Mod::Module(m), ruff::Mod::Expression(e) => Mod::Expression(e), }; - Ok(top.ast_to_object(vm, &source_code)) + Ok(top.ast_to_object(vm, &source_file)) } #[cfg(feature = "codegen")] @@ -272,8 +267,8 @@ pub(crate) fn compile( opts.optimize = optimize; } - let source_code = SourceCodeOwned::new(filename.to_owned(), "".to_owned()); - let ast: Mod = Node::ast_from_object(vm, &source_code, object)?; + let source_file = SourceFileBuilder::new(filename.to_owned(), "".to_owned()).finish(); + 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 }) => { @@ -284,8 +279,8 @@ pub(crate) fn compile( }; // TODO: create a textual representation of the ast let text = ""; - let source_code = SourceCode::new(filename, text); - let code = codegen::compile::compile_top(ast, source_code, mode, opts) + let source_file = SourceFileBuilder::new(filename, text).finish(); + let code = codegen::compile::compile_top(ast, source_file, mode, opts) .map_err(|err| vm.new_syntax_error(&err.into(), None))?; // FIXME source Ok(vm.ctx.new_code(code).into()) } diff --git a/vm/src/stdlib/ast/argument.rs b/vm/src/stdlib/ast/argument.rs index dae2345f0a..e95199de3a 100644 --- a/vm/src/stdlib/ast/argument.rs +++ b/vm/src/stdlib/ast/argument.rs @@ -1,4 +1,5 @@ use super::*; +use ruff_source_file::SourceFile; pub(super) struct PositionalArguments { pub range: TextRange, @@ -6,17 +7,17 @@ pub(super) struct PositionalArguments { } impl Node for PositionalArguments { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { args, range: _ } = self; - BoxedSlice(args).ast_to_object(vm, source_code) + BoxedSlice(args).ast_to_object(vm, source_file) } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - let args: BoxedSlice<_> = Node::ast_from_object(vm, source_code, object)?; + let args: BoxedSlice<_> = Node::ast_from_object(vm, source_file, object)?; Ok(Self { args: args.0, range: TextRange::default(), // TODO @@ -30,18 +31,18 @@ pub(super) struct KeywordArguments { } impl Node for KeywordArguments { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { keywords, range: _ } = self; // TODO: use range - BoxedSlice(keywords).ast_to_object(vm, source_code) + BoxedSlice(keywords).ast_to_object(vm, source_file) } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - let keywords: BoxedSlice<_> = Node::ast_from_object(vm, source_code, object)?; + let keywords: BoxedSlice<_> = Node::ast_from_object(vm, source_file, object)?; Ok(Self { keywords: keywords.0, range: TextRange::default(), // TODO diff --git a/vm/src/stdlib/ast/basic.rs b/vm/src/stdlib/ast/basic.rs index c6ad8fa228..86011040b0 100644 --- a/vm/src/stdlib/ast/basic.rs +++ b/vm/src/stdlib/ast/basic.rs @@ -1,16 +1,17 @@ +use ruff_source_file::SourceFile; use rustpython_codegen::compile::ruff_int_to_bigint; use super::*; impl Node for ruff::Identifier { - fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let id = self.as_str(); vm.ctx.new_str(id).into() } fn ast_from_object( vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { let py_str = PyStrRef::try_from_object(vm, object)?; @@ -19,13 +20,13 @@ impl Node for ruff::Identifier { } impl Node for ruff::Int { - fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { vm.ctx.new_int(ruff_int_to_bigint(&self).unwrap()).into() } fn ast_from_object( vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { // FIXME: performance @@ -36,13 +37,13 @@ impl Node for ruff::Int { } impl Node for bool { - fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { vm.ctx.new_int(self as u8).into() } fn ast_from_object( vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { i32::try_from_object(vm, object).map(|i| i != 0) diff --git a/vm/src/stdlib/ast/constant.rs b/vm/src/stdlib/ast/constant.rs index a6bb598018..89f4ec17e5 100644 --- a/vm/src/stdlib/ast/constant.rs +++ b/vm/src/stdlib/ast/constant.rs @@ -1,6 +1,7 @@ use super::*; use crate::builtins::{PyComplex, PyFrozenSet, PyTuple}; use ruff::str_prefix::StringLiteralPrefix; +use ruff_source_file::SourceFile; #[derive(Debug)] pub(super) struct Constant { @@ -97,7 +98,7 @@ pub(crate) enum ConstantLiteral { // constructor impl Node for Constant { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { range, value } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprConstant::static_type().to_owned()) @@ -109,50 +110,50 @@ impl Node for Constant { } => vm.ctx.new_str("u").into(), _ => vm.ctx.none(), }; - let value = value.ast_to_object(vm, source_code); + let value = value.ast_to_object(vm, source_file); let dict = node.as_object().dict().unwrap(); dict.set_item("value", value, vm).unwrap(); dict.set_item("kind", kind, vm).unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { let value_object = get_node_field(vm, &object, "value", "Constant")?; - let value = Node::ast_from_object(vm, source_code, value_object)?; + let value = Node::ast_from_object(vm, source_file, value_object)?; Ok(Self { value, // kind: get_node_field_opt(_vm, &_object, "kind")? // .map(|obj| Node::ast_from_object(_vm, obj)) // .transpose()?, - range: range_from_object(vm, source_code, object, "Constant")?, + range: range_from_object(vm, source_file, object, "Constant")?, }) } } impl Node for ConstantLiteral { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { Self::None => vm.ctx.none(), Self::Bool(value) => vm.ctx.new_bool(value).to_pyobject(vm), Self::Str { value, .. } => vm.ctx.new_str(value).to_pyobject(vm), Self::Bytes(value) => vm.ctx.new_bytes(value.into()).to_pyobject(vm), - Self::Int(value) => value.ast_to_object(vm, source_code), + Self::Int(value) => value.ast_to_object(vm, source_file), Self::Tuple(value) => { let value = value .into_iter() - .map(|c| c.ast_to_object(vm, source_code)) + .map(|c| c.ast_to_object(vm, source_file)) .collect(); vm.ctx.new_tuple(value).to_pyobject(vm) } Self::FrozenSet(value) => PyFrozenSet::from_iter( vm, - value.into_iter().map(|c| c.ast_to_object(vm, source_code)), + value.into_iter().map(|c| c.ast_to_object(vm, source_file)), ) .unwrap() .into_pyobject(vm), @@ -167,7 +168,7 @@ impl Node for ConstantLiteral { fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, value_object: PyObjectRef, ) -> PyResult { let cls = value_object.class(); @@ -189,7 +190,7 @@ impl Node for ConstantLiteral { } else if cls.is(vm.ctx.types.bytes_type) { Self::Bytes(value_object.try_to_value::>(vm)?.into()) } else if cls.is(vm.ctx.types.int_type) { - Self::Int(Node::ast_from_object(vm, source_code, value_object)?) + Self::Int(Node::ast_from_object(vm, source_file, value_object)?) } else if cls.is(vm.ctx.types.tuple_type) { let tuple = value_object.downcast::().map_err(|obj| { vm.new_type_error(format!( @@ -201,7 +202,7 @@ impl Node for ConstantLiteral { let tuple = tuple .into_iter() .cloned() - .map(|object| Node::ast_from_object(vm, source_code, object)) + .map(|object| Node::ast_from_object(vm, source_file, object)) .collect::>()?; Self::Tuple(tuple) } else if cls.is(vm.ctx.types.frozenset_type) { @@ -209,7 +210,7 @@ impl Node for ConstantLiteral { let elements = set .elements() .into_iter() - .map(|object| Node::ast_from_object(vm, source_code, object)) + .map(|object| Node::ast_from_object(vm, source_file, object)) .collect::>()?; Self::FrozenSet(elements) } else if cls.is(vm.ctx.types.float_type) { @@ -329,7 +330,7 @@ fn constant_to_ruff_expr(value: Constant) -> ruff::Expr { pub(super) fn number_literal_to_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, constant: ruff::ExprNumberLiteral, ) -> PyObjectRef { let ruff::ExprNumberLiteral { range, value } = constant; @@ -338,12 +339,12 @@ pub(super) fn number_literal_to_object( ruff::Number::Float(n) => Constant::new_float(n, range), ruff::Number::Complex { real, imag } => Constant::new_complex(real, imag, range), }; - c.ast_to_object(vm, source_code) + c.ast_to_object(vm, source_file) } pub(super) fn string_literal_to_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, constant: ruff::ExprStringLiteral, ) -> PyObjectRef { let ruff::ExprStringLiteral { range, value } = constant; @@ -352,46 +353,46 @@ pub(super) fn string_literal_to_object( .next() .map_or(StringLiteralPrefix::Empty, |part| part.flags.prefix()); let c = Constant::new_str(value.to_str(), prefix, range); - c.ast_to_object(vm, source_code) + c.ast_to_object(vm, source_file) } pub(super) fn bytes_literal_to_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, constant: ruff::ExprBytesLiteral, ) -> PyObjectRef { let ruff::ExprBytesLiteral { 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_code) + c.ast_to_object(vm, source_file) } pub(super) fn boolean_literal_to_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, constant: ruff::ExprBooleanLiteral, ) -> PyObjectRef { let ruff::ExprBooleanLiteral { range, value } = constant; let c = Constant::new_bool(value, range); - c.ast_to_object(vm, source_code) + c.ast_to_object(vm, source_file) } pub(super) fn none_literal_to_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, constant: ruff::ExprNoneLiteral, ) -> PyObjectRef { let ruff::ExprNoneLiteral { range } = constant; let c = Constant::new_none(range); - c.ast_to_object(vm, source_code) + c.ast_to_object(vm, source_file) } pub(super) fn ellipsis_literal_to_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, constant: ruff::ExprEllipsisLiteral, ) -> PyObjectRef { let ruff::ExprEllipsisLiteral { range } = constant; let c = Constant::new_ellipsis(range); - c.ast_to_object(vm, source_code) + 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 13f6865eaf..a1e49ee525 100644 --- a/vm/src/stdlib/ast/elif_else_clause.rs +++ b/vm/src/stdlib/ast/elif_else_clause.rs @@ -1,56 +1,57 @@ use super::*; +use ruff_source_file::SourceFile; pub(super) fn ast_to_object( clause: ruff::ElifElseClause, mut rest: std::vec::IntoIter, vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, ) -> PyObjectRef { let ruff::ElifElseClause { range, test, body } = clause; let Some(test) = test else { assert!(rest.len() == 0); - return body.ast_to_object(vm, source_code); + return body.ast_to_object(vm, source_file); }; let node = NodeAst .into_ref_with_type(vm, pyast::NodeStmtIf::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("test", test.ast_to_object(vm, source_code), vm) + dict.set_item("test", test.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("body", body.ast_to_object(vm, source_code), vm) + dict.set_item("body", body.ast_to_object(vm, source_file), vm) .unwrap(); let orelse = if let Some(next) = rest.next() { if next.test.is_some() { vm.ctx - .new_list(vec![ast_to_object(next, rest, vm, source_code)]) + .new_list(vec![ast_to_object(next, rest, vm, source_file)]) .into() } else { - next.body.ast_to_object(vm, source_code) + next.body.ast_to_object(vm, source_file) } } else { vm.ctx.new_list(vec![]).into() }; dict.set_item("orelse", orelse, vm).unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } pub(super) fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - let test = Node::ast_from_object(vm, source_code, get_node_field(vm, &object, "test", "If")?)?; - let body = Node::ast_from_object(vm, source_code, get_node_field(vm, &object, "body", "If")?)?; + let test = Node::ast_from_object(vm, source_file, get_node_field(vm, &object, "test", "If")?)?; + let body = Node::ast_from_object(vm, source_file, get_node_field(vm, &object, "body", "If")?)?; let orelse: Vec = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "orelse", "If")?, )?; - let range = range_from_object(vm, source_code, object, "If")?; + let range = range_from_object(vm, source_file, object, "If")?; let elif_else_clauses = if let [ruff::Stmt::If(_)] = &*orelse { let Some(ruff::Stmt::If(ruff::StmtIf { diff --git a/vm/src/stdlib/ast/exception.rs b/vm/src/stdlib/ast/exception.rs index b8bf034a7b..446f7d8400 100644 --- a/vm/src/stdlib/ast/exception.rs +++ b/vm/src/stdlib/ast/exception.rs @@ -1,15 +1,16 @@ use super::*; +use ruff_source_file::SourceFile; // sum impl Node for ruff::ExceptHandler { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { - Self::ExceptHandler(cons) => cons.ast_to_object(vm, source_code), + Self::ExceptHandler(cons) => cons.ast_to_object(vm, source_file), } } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); @@ -17,7 +18,7 @@ impl Node for ruff::ExceptHandler { if _cls.is(pyast::NodeExceptHandlerExceptHandler::static_type()) { Self::ExceptHandler(ruff::ExceptHandlerExceptHandler::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else { @@ -29,9 +30,10 @@ impl Node for ruff::ExceptHandler { ) } } + // constructor impl Node for ruff::ExceptHandlerExceptHandler { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { type_, name, @@ -45,34 +47,34 @@ impl Node for ruff::ExceptHandlerExceptHandler { ) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("type", type_.ast_to_object(_vm, source_code), _vm) + dict.set_item("type", type_.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("name", name.ast_to_object(_vm, source_code), _vm) + dict.set_item("name", name.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("body", body.ast_to_object(_vm, source_code), _vm) + dict.set_item("body", body.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { type_: get_node_field_opt(_vm, &_object, "type")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, name: get_node_field_opt(_vm, &_object, "name")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, body: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "body", "ExceptHandler")?, )?, - range: range_from_object(_vm, source_code, _object, "ExceptHandler")?, + range: range_from_object(_vm, source_file, _object, "ExceptHandler")?, }) } } diff --git a/vm/src/stdlib/ast/expression.rs b/vm/src/stdlib/ast/expression.rs index 8999c2c92c..3b187fb413 100644 --- a/vm/src/stdlib/ast/expression.rs +++ b/vm/src/stdlib/ast/expression.rs @@ -1,47 +1,50 @@ use super::*; -use crate::stdlib::ast::argument::{merge_function_call_arguments, split_function_call_arguments}; -use crate::stdlib::ast::constant::Constant; -use crate::stdlib::ast::string::JoinedStr; +use crate::stdlib::ast::{ + argument::{merge_function_call_arguments, split_function_call_arguments}, + constant::Constant, + string::JoinedStr, +}; +use ruff_source_file::SourceFile; // sum impl Node for ruff::Expr { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { - Self::BoolOp(cons) => cons.ast_to_object(vm, source_code), - Self::Name(cons) => cons.ast_to_object(vm, source_code), - Self::BinOp(cons) => cons.ast_to_object(vm, source_code), - Self::UnaryOp(cons) => cons.ast_to_object(vm, source_code), - Self::Lambda(cons) => cons.ast_to_object(vm, source_code), - Self::If(cons) => cons.ast_to_object(vm, source_code), - Self::Dict(cons) => cons.ast_to_object(vm, source_code), - Self::Set(cons) => cons.ast_to_object(vm, source_code), - Self::ListComp(cons) => cons.ast_to_object(vm, source_code), - Self::SetComp(cons) => cons.ast_to_object(vm, source_code), - Self::DictComp(cons) => cons.ast_to_object(vm, source_code), - Self::Generator(cons) => cons.ast_to_object(vm, source_code), - Self::Await(cons) => cons.ast_to_object(vm, source_code), - Self::Yield(cons) => cons.ast_to_object(vm, source_code), - Self::YieldFrom(cons) => cons.ast_to_object(vm, source_code), - Self::Compare(cons) => cons.ast_to_object(vm, source_code), - Self::Call(cons) => cons.ast_to_object(vm, source_code), - Self::Attribute(cons) => cons.ast_to_object(vm, source_code), - Self::Subscript(cons) => cons.ast_to_object(vm, source_code), - Self::Starred(cons) => cons.ast_to_object(vm, source_code), - Self::List(cons) => cons.ast_to_object(vm, source_code), - Self::Tuple(cons) => cons.ast_to_object(vm, source_code), - Self::Slice(cons) => cons.ast_to_object(vm, source_code), - Self::NumberLiteral(cons) => constant::number_literal_to_object(vm, source_code, cons), - Self::StringLiteral(cons) => constant::string_literal_to_object(vm, source_code, cons), - Self::FString(cons) => string::fstring_to_object(vm, source_code, cons), - Self::BytesLiteral(cons) => constant::bytes_literal_to_object(vm, source_code, cons), + Self::BoolOp(cons) => cons.ast_to_object(vm, source_file), + Self::Name(cons) => cons.ast_to_object(vm, source_file), + Self::BinOp(cons) => cons.ast_to_object(vm, source_file), + Self::UnaryOp(cons) => cons.ast_to_object(vm, source_file), + Self::Lambda(cons) => cons.ast_to_object(vm, source_file), + Self::If(cons) => cons.ast_to_object(vm, source_file), + Self::Dict(cons) => cons.ast_to_object(vm, source_file), + Self::Set(cons) => cons.ast_to_object(vm, source_file), + Self::ListComp(cons) => cons.ast_to_object(vm, source_file), + Self::SetComp(cons) => cons.ast_to_object(vm, source_file), + Self::DictComp(cons) => cons.ast_to_object(vm, source_file), + Self::Generator(cons) => cons.ast_to_object(vm, source_file), + Self::Await(cons) => cons.ast_to_object(vm, source_file), + Self::Yield(cons) => cons.ast_to_object(vm, source_file), + Self::YieldFrom(cons) => cons.ast_to_object(vm, source_file), + Self::Compare(cons) => cons.ast_to_object(vm, source_file), + Self::Call(cons) => cons.ast_to_object(vm, source_file), + Self::Attribute(cons) => cons.ast_to_object(vm, source_file), + Self::Subscript(cons) => cons.ast_to_object(vm, source_file), + Self::Starred(cons) => cons.ast_to_object(vm, source_file), + Self::List(cons) => cons.ast_to_object(vm, source_file), + Self::Tuple(cons) => cons.ast_to_object(vm, source_file), + Self::Slice(cons) => cons.ast_to_object(vm, source_file), + 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::BytesLiteral(cons) => constant::bytes_literal_to_object(vm, source_file, cons), Self::BooleanLiteral(cons) => { - constant::boolean_literal_to_object(vm, source_code, cons) + constant::boolean_literal_to_object(vm, source_file, cons) } - Self::NoneLiteral(cons) => constant::none_literal_to_object(vm, source_code, cons), + Self::NoneLiteral(cons) => constant::none_literal_to_object(vm, source_file, cons), Self::EllipsisLiteral(cons) => { - constant::ellipsis_literal_to_object(vm, source_code, cons) + constant::ellipsis_literal_to_object(vm, source_file, cons) } - Self::Named(cons) => cons.ast_to_object(vm, source_code), + Self::Named(cons) => cons.ast_to_object(vm, source_file), Self::IpyEscapeCommand(_) => { unimplemented!("IPython escape command is not allowed in Python AST") } @@ -50,86 +53,86 @@ impl Node for ruff::Expr { fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { let cls = object.class(); Ok(if cls.is(pyast::NodeExprBoolOp::static_type()) { - Self::BoolOp(ruff::ExprBoolOp::ast_from_object(vm, source_code, object)?) + Self::BoolOp(ruff::ExprBoolOp::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprNamedExpr::static_type()) { - Self::Named(ruff::ExprNamed::ast_from_object(vm, source_code, object)?) + Self::Named(ruff::ExprNamed::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprBinOp::static_type()) { - Self::BinOp(ruff::ExprBinOp::ast_from_object(vm, source_code, object)?) + Self::BinOp(ruff::ExprBinOp::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprUnaryOp::static_type()) { - Self::UnaryOp(ruff::ExprUnaryOp::ast_from_object(vm, source_code, object)?) + Self::UnaryOp(ruff::ExprUnaryOp::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprLambda::static_type()) { - Self::Lambda(ruff::ExprLambda::ast_from_object(vm, source_code, object)?) + Self::Lambda(ruff::ExprLambda::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprIfExp::static_type()) { - Self::If(ruff::ExprIf::ast_from_object(vm, source_code, object)?) + Self::If(ruff::ExprIf::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprDict::static_type()) { - Self::Dict(ruff::ExprDict::ast_from_object(vm, source_code, object)?) + Self::Dict(ruff::ExprDict::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprSet::static_type()) { - Self::Set(ruff::ExprSet::ast_from_object(vm, source_code, object)?) + Self::Set(ruff::ExprSet::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprListComp::static_type()) { Self::ListComp(ruff::ExprListComp::ast_from_object( vm, - source_code, + source_file, object, )?) } else if cls.is(pyast::NodeExprSetComp::static_type()) { - Self::SetComp(ruff::ExprSetComp::ast_from_object(vm, source_code, object)?) + Self::SetComp(ruff::ExprSetComp::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprDictComp::static_type()) { Self::DictComp(ruff::ExprDictComp::ast_from_object( vm, - source_code, + source_file, object, )?) } else if cls.is(pyast::NodeExprGeneratorExp::static_type()) { Self::Generator(ruff::ExprGenerator::ast_from_object( vm, - source_code, + source_file, object, )?) } else if cls.is(pyast::NodeExprAwait::static_type()) { - Self::Await(ruff::ExprAwait::ast_from_object(vm, source_code, object)?) + Self::Await(ruff::ExprAwait::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprYield::static_type()) { - Self::Yield(ruff::ExprYield::ast_from_object(vm, source_code, object)?) + Self::Yield(ruff::ExprYield::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprYieldFrom::static_type()) { Self::YieldFrom(ruff::ExprYieldFrom::ast_from_object( vm, - source_code, + source_file, object, )?) } else if cls.is(pyast::NodeExprCompare::static_type()) { - Self::Compare(ruff::ExprCompare::ast_from_object(vm, source_code, object)?) + Self::Compare(ruff::ExprCompare::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprCall::static_type()) { - Self::Call(ruff::ExprCall::ast_from_object(vm, source_code, object)?) + Self::Call(ruff::ExprCall::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprAttribute::static_type()) { Self::Attribute(ruff::ExprAttribute::ast_from_object( vm, - source_code, + source_file, object, )?) } else if cls.is(pyast::NodeExprSubscript::static_type()) { Self::Subscript(ruff::ExprSubscript::ast_from_object( vm, - source_code, + source_file, object, )?) } else if cls.is(pyast::NodeExprStarred::static_type()) { - Self::Starred(ruff::ExprStarred::ast_from_object(vm, source_code, object)?) + Self::Starred(ruff::ExprStarred::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprName::static_type()) { - Self::Name(ruff::ExprName::ast_from_object(vm, source_code, object)?) + Self::Name(ruff::ExprName::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprList::static_type()) { - Self::List(ruff::ExprList::ast_from_object(vm, source_code, object)?) + Self::List(ruff::ExprList::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprTuple::static_type()) { - Self::Tuple(ruff::ExprTuple::ast_from_object(vm, source_code, object)?) + Self::Tuple(ruff::ExprTuple::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprSlice::static_type()) { - Self::Slice(ruff::ExprSlice::ast_from_object(vm, source_code, object)?) + Self::Slice(ruff::ExprSlice::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeExprConstant::static_type()) { - Constant::ast_from_object(vm, source_code, object)?.into_expr() + Constant::ast_from_object(vm, source_file, object)?.into_expr() } else if cls.is(pyast::NodeExprJoinedStr::static_type()) { - JoinedStr::ast_from_object(vm, source_code, object)?.into_expr() + JoinedStr::ast_from_object(vm, source_file, object)?.into_expr() } else { return Err(vm.new_type_error(format!( "expected some sort of expr, but got {}", @@ -141,44 +144,44 @@ impl Node for ruff::Expr { // constructor impl Node for ruff::ExprBoolOp { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { op, values, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprBoolOp::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("op", op.ast_to_object(vm, source_code), vm) + dict.set_item("op", op.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("values", values.ast_to_object(vm, source_code), vm) + dict.set_item("values", values.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { op: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "op", "BoolOp")?, )?, values: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "values", "BoolOp")?, )?, - range: range_from_object(vm, source_code, object, "BoolOp")?, + range: range_from_object(vm, source_file, object, "BoolOp")?, }) } } // constructor impl Node for ruff::ExprNamed { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { target, value, @@ -188,38 +191,38 @@ impl Node for ruff::ExprNamed { .into_ref_with_type(vm, pyast::NodeExprNamedExpr::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("target", target.ast_to_object(vm, source_code), vm) + dict.set_item("target", target.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("value", value.ast_to_object(vm, source_code), vm) + dict.set_item("value", value.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { target: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "target", "NamedExpr")?, )?, value: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "value", "NamedExpr")?, )?, - range: range_from_object(vm, source_code, object, "NamedExpr")?, + range: range_from_object(vm, source_file, object, "NamedExpr")?, }) } } // constructor impl Node for ruff::ExprBinOp { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { left, op, @@ -230,81 +233,81 @@ impl Node for ruff::ExprBinOp { .into_ref_with_type(vm, pyast::NodeExprBinOp::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("left", left.ast_to_object(vm, source_code), vm) + dict.set_item("left", left.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("op", op.ast_to_object(vm, source_code), vm) + dict.set_item("op", op.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("right", right.ast_to_object(vm, source_code), vm) + dict.set_item("right", right.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { left: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "left", "BinOp")?, )?, op: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "op", "BinOp")?, )?, right: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "right", "BinOp")?, )?, - range: range_from_object(vm, source_code, object, "BinOp")?, + range: range_from_object(vm, source_file, object, "BinOp")?, }) } } // constructor impl Node for ruff::ExprUnaryOp { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { op, operand, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprUnaryOp::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("op", op.ast_to_object(vm, source_code), vm) + dict.set_item("op", op.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("operand", operand.ast_to_object(vm, source_code), vm) + dict.set_item("operand", operand.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { op: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "op", "UnaryOp")?, )?, operand: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "operand", "UnaryOp")?, )?, - range: range_from_object(vm, source_code, object, "UnaryOp")?, + range: range_from_object(vm, source_file, object, "UnaryOp")?, }) } } // constructor impl Node for ruff::ExprLambda { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { parameters, body, @@ -314,38 +317,38 @@ impl Node for ruff::ExprLambda { .into_ref_with_type(vm, pyast::NodeExprLambda::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("args", parameters.ast_to_object(vm, source_code), vm) + dict.set_item("args", parameters.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("body", body.ast_to_object(vm, source_code), vm) + dict.set_item("body", body.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, _range, vm, source_code); + node_add_location(&dict, _range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { parameters: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "args", "Lambda")?, )?, body: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "body", "Lambda")?, )?, - range: range_from_object(vm, source_code, object, "Lambda")?, + range: range_from_object(vm, source_file, object, "Lambda")?, }) } } // constructor impl Node for ruff::ExprIf { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { test, body, @@ -356,45 +359,45 @@ impl Node for ruff::ExprIf { .into_ref_with_type(vm, pyast::NodeExprIfExp::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("test", test.ast_to_object(vm, source_code), vm) + dict.set_item("test", test.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("body", body.ast_to_object(vm, source_code), vm) + dict.set_item("body", body.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("orelse", orelse.ast_to_object(vm, source_code), vm) + dict.set_item("orelse", orelse.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { test: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "test", "IfExp")?, )?, body: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "body", "IfExp")?, )?, orelse: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "orelse", "IfExp")?, )?, - range: range_from_object(vm, source_code, object, "IfExp")?, + range: range_from_object(vm, source_file, object, "IfExp")?, }) } } // constructor impl Node for ruff::ExprDict { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { items, range } = self; let (keys, values) = items @@ -408,27 +411,27 @@ impl Node for ruff::ExprDict { .into_ref_with_type(vm, pyast::NodeExprDict::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("keys", keys.ast_to_object(vm, source_code), vm) + dict.set_item("keys", keys.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("values", values.ast_to_object(vm, source_code), vm) + dict.set_item("values", values.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { let keys: Vec> = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "keys", "Dict")?, )?; let values: Vec<_> = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "values", "Dict")?, )?; let items = keys @@ -438,43 +441,43 @@ impl Node for ruff::ExprDict { .collect(); Ok(Self { items, - range: range_from_object(vm, source_code, object, "Dict")?, + range: range_from_object(vm, source_file, object, "Dict")?, }) } } // constructor impl Node for ruff::ExprSet { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { elts, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprSet::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("elts", elts.ast_to_object(vm, source_code), vm) + dict.set_item("elts", elts.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { elts: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "elts", "Set")?, )?, - range: range_from_object(vm, source_code, object, "Set")?, + range: range_from_object(vm, source_file, object, "Set")?, }) } } // constructor impl Node for ruff::ExprListComp { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { elt, generators, @@ -484,38 +487,38 @@ impl Node for ruff::ExprListComp { .into_ref_with_type(vm, pyast::NodeExprListComp::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("elt", elt.ast_to_object(vm, source_code), vm) + dict.set_item("elt", elt.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("generators", generators.ast_to_object(vm, source_code), vm) + dict.set_item("generators", generators.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { elt: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "elt", "ListComp")?, )?, generators: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "generators", "ListComp")?, )?, - range: range_from_object(vm, source_code, object, "ListComp")?, + range: range_from_object(vm, source_file, object, "ListComp")?, }) } } // constructor impl Node for ruff::ExprSetComp { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { elt, generators, @@ -525,38 +528,38 @@ impl Node for ruff::ExprSetComp { .into_ref_with_type(vm, pyast::NodeExprSetComp::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("elt", elt.ast_to_object(vm, source_code), vm) + dict.set_item("elt", elt.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("generators", generators.ast_to_object(vm, source_code), vm) + dict.set_item("generators", generators.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { elt: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "elt", "SetComp")?, )?, generators: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "generators", "SetComp")?, )?, - range: range_from_object(vm, source_code, object, "SetComp")?, + range: range_from_object(vm, source_file, object, "SetComp")?, }) } } // constructor impl Node for ruff::ExprDictComp { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { key, value, @@ -567,45 +570,45 @@ impl Node for ruff::ExprDictComp { .into_ref_with_type(vm, pyast::NodeExprDictComp::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("key", key.ast_to_object(vm, source_code), vm) + dict.set_item("key", key.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("value", value.ast_to_object(vm, source_code), vm) + dict.set_item("value", value.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("generators", generators.ast_to_object(vm, source_code), vm) + dict.set_item("generators", generators.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { key: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "key", "DictComp")?, )?, value: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "value", "DictComp")?, )?, generators: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "generators", "DictComp")?, )?, - range: range_from_object(vm, source_code, object, "DictComp")?, + range: range_from_object(vm, source_file, object, "DictComp")?, }) } } // constructor impl Node for ruff::ExprGenerator { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { elt, generators, @@ -616,31 +619,31 @@ impl Node for ruff::ExprGenerator { .into_ref_with_type(vm, pyast::NodeExprGeneratorExp::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("elt", elt.ast_to_object(vm, source_code), vm) + dict.set_item("elt", elt.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("generators", generators.ast_to_object(vm, source_code), vm) + dict.set_item("generators", generators.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { elt: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "elt", "GeneratorExp")?, )?, generators: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "generators", "GeneratorExp")?, )?, - range: range_from_object(vm, source_code, object, "GeneratorExp")?, + range: range_from_object(vm, source_file, object, "GeneratorExp")?, // TODO: Is this correct? parenthesized: true, }) @@ -649,94 +652,94 @@ impl Node for ruff::ExprGenerator { // constructor impl Node for ruff::ExprAwait { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { value, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprAwait::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("value", value.ast_to_object(vm, source_code), vm) + dict.set_item("value", value.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { value: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "value", "Await")?, )?, - range: range_from_object(vm, source_code, object, "Await")?, + range: range_from_object(vm, source_file, object, "Await")?, }) } } // constructor impl Node for ruff::ExprYield { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { value, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprYield::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("value", value.ast_to_object(vm, source_code), vm) + dict.set_item("value", value.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { value: get_node_field_opt(vm, &object, "value")? - .map(|obj| Node::ast_from_object(vm, source_code, obj)) + .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, - range: range_from_object(vm, source_code, object, "Yield")?, + range: range_from_object(vm, source_file, object, "Yield")?, }) } } // constructor impl Node for ruff::ExprYieldFrom { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { value, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprYieldFrom::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("value", value.ast_to_object(vm, source_code), vm) + dict.set_item("value", value.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { value: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "value", "YieldFrom")?, )?, - range: range_from_object(vm, source_code, object, "YieldFrom")?, + range: range_from_object(vm, source_file, object, "YieldFrom")?, }) } } // constructor impl Node for ruff::ExprCompare { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { left, ops, @@ -747,35 +750,35 @@ impl Node for ruff::ExprCompare { .into_ref_with_type(vm, pyast::NodeExprCompare::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("left", left.ast_to_object(vm, source_code), vm) + dict.set_item("left", left.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("ops", BoxedSlice(ops).ast_to_object(vm, source_code), vm) + dict.set_item("ops", BoxedSlice(ops).ast_to_object(vm, source_file), vm) .unwrap(); dict.set_item( "comparators", - BoxedSlice(comparators).ast_to_object(vm, source_code), + BoxedSlice(comparators).ast_to_object(vm, source_file), vm, ) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { left: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "left", "Compare")?, )?, ops: { let ops: BoxedSlice<_> = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "ops", "Compare")?, )?; ops.0 @@ -783,19 +786,19 @@ impl Node for ruff::ExprCompare { comparators: { let comparators: BoxedSlice<_> = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "comparators", "Compare")?, )?; comparators.0 }, - range: range_from_object(vm, source_code, object, "Compare")?, + range: range_from_object(vm, source_file, object, "Compare")?, }) } } // constructor impl Node for ruff::ExprCall { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { func, arguments, @@ -806,55 +809,55 @@ impl Node for ruff::ExprCall { .into_ref_with_type(vm, pyast::NodeExprCall::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("func", func.ast_to_object(vm, source_code), vm) + dict.set_item("func", func.ast_to_object(vm, source_file), vm) .unwrap(); dict.set_item( "args", - positional_arguments.ast_to_object(vm, source_code), + positional_arguments.ast_to_object(vm, source_file), vm, ) .unwrap(); dict.set_item( "keywords", - keyword_arguments.ast_to_object(vm, source_code), + keyword_arguments.ast_to_object(vm, source_file), vm, ) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { func: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "func", "Call")?, )?, arguments: merge_function_call_arguments( Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "args", "Call")?, )?, Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "keywords", "Call")?, )?, ), - range: range_from_object(vm, source_code, object, "Call")?, + range: range_from_object(vm, source_file, object, "Call")?, }) } } // constructor impl Node for ruff::ExprAttribute { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { value, attr, @@ -865,45 +868,45 @@ impl Node for ruff::ExprAttribute { .into_ref_with_type(vm, pyast::NodeExprAttribute::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("value", value.ast_to_object(vm, source_code), vm) + dict.set_item("value", value.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("attr", attr.ast_to_object(vm, source_code), vm) + dict.set_item("attr", attr.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("ctx", ctx.ast_to_object(vm, source_code), vm) + dict.set_item("ctx", ctx.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { value: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "value", "Attribute")?, )?, attr: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "attr", "Attribute")?, )?, ctx: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "ctx", "Attribute")?, )?, - range: range_from_object(vm, source_code, object, "Attribute")?, + range: range_from_object(vm, source_file, object, "Attribute")?, }) } } // constructor impl Node for ruff::ExprSubscript { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { value, slice, @@ -914,149 +917,149 @@ impl Node for ruff::ExprSubscript { .into_ref_with_type(vm, pyast::NodeExprSubscript::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("value", value.ast_to_object(vm, source_code), vm) + dict.set_item("value", value.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("slice", slice.ast_to_object(vm, source_code), vm) + dict.set_item("slice", slice.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("ctx", ctx.ast_to_object(vm, source_code), vm) + dict.set_item("ctx", ctx.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, _range, vm, source_code); + node_add_location(&dict, _range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { value: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "value", "Subscript")?, )?, slice: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "slice", "Subscript")?, )?, ctx: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "ctx", "Subscript")?, )?, - range: range_from_object(vm, source_code, object, "Subscript")?, + range: range_from_object(vm, source_file, object, "Subscript")?, }) } } // constructor impl Node for ruff::ExprStarred { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { value, ctx, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprStarred::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("value", value.ast_to_object(vm, source_code), vm) + dict.set_item("value", value.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("ctx", ctx.ast_to_object(vm, source_code), vm) + dict.set_item("ctx", ctx.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { value: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "value", "Starred")?, )?, ctx: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "ctx", "Starred")?, )?, - range: range_from_object(vm, source_code, object, "Starred")?, + range: range_from_object(vm, source_file, object, "Starred")?, }) } } // constructor impl Node for ruff::ExprName { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { id, ctx, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprName::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); dict.set_item("id", id.to_pyobject(vm), vm).unwrap(); - dict.set_item("ctx", ctx.ast_to_object(vm, source_code), vm) + dict.set_item("ctx", ctx.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { - id: Node::ast_from_object(vm, source_code, get_node_field(vm, &object, "id", "Name")?)?, + id: Node::ast_from_object(vm, source_file, get_node_field(vm, &object, "id", "Name")?)?, ctx: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "ctx", "Name")?, )?, - range: range_from_object(vm, source_code, object, "Name")?, + range: range_from_object(vm, source_file, object, "Name")?, }) } } // constructor impl Node for ruff::ExprList { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { elts, ctx, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprList::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("elts", elts.ast_to_object(vm, source_code), vm) + dict.set_item("elts", elts.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("ctx", ctx.ast_to_object(vm, source_code), vm) + dict.set_item("ctx", ctx.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { elts: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "elts", "List")?, )?, ctx: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "ctx", "List")?, )?, - range: range_from_object(vm, source_code, object, "List")?, + range: range_from_object(vm, source_file, object, "List")?, }) } } // constructor impl Node for ruff::ExprTuple { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { elts, ctx, @@ -1067,31 +1070,31 @@ impl Node for ruff::ExprTuple { .into_ref_with_type(vm, pyast::NodeExprTuple::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("elts", elts.ast_to_object(vm, source_code), vm) + dict.set_item("elts", elts.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("ctx", ctx.ast_to_object(vm, source_code), vm) + dict.set_item("ctx", ctx.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, _range, vm, source_code); + node_add_location(&dict, _range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { elts: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "elts", "Tuple")?, )?, ctx: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "ctx", "Tuple")?, )?, - range: range_from_object(vm, source_code, object, "Tuple")?, + range: range_from_object(vm, source_file, object, "Tuple")?, parenthesized: true, // TODO: is this correct? }) } @@ -1099,7 +1102,7 @@ impl Node for ruff::ExprTuple { // constructor impl Node for ruff::ExprSlice { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { lower, upper, @@ -1110,39 +1113,39 @@ impl Node for ruff::ExprSlice { .into_ref_with_type(vm, pyast::NodeExprSlice::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("lower", lower.ast_to_object(vm, source_code), vm) + dict.set_item("lower", lower.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("upper", upper.ast_to_object(vm, source_code), vm) + dict.set_item("upper", upper.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("step", step.ast_to_object(vm, source_code), vm) + dict.set_item("step", step.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, _range, vm, source_code); + node_add_location(&dict, _range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { lower: get_node_field_opt(vm, &object, "lower")? - .map(|obj| Node::ast_from_object(vm, source_code, obj)) + .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, upper: get_node_field_opt(vm, &object, "upper")? - .map(|obj| Node::ast_from_object(vm, source_code, obj)) + .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, step: get_node_field_opt(vm, &object, "step")? - .map(|obj| Node::ast_from_object(vm, source_code, obj)) + .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, - range: range_from_object(vm, source_code, object, "Slice")?, + range: range_from_object(vm, source_file, object, "Slice")?, }) } } // sum impl Node for ruff::ExprContext { - fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let node_type = match self { Self::Load => pyast::NodeExprContextLoad::static_type(), Self::Store => pyast::NodeExprContextStore::static_type(), @@ -1159,7 +1162,7 @@ impl Node for ruff::ExprContext { fn ast_from_object( vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { let _cls = object.class(); @@ -1180,7 +1183,7 @@ impl Node for ruff::ExprContext { // product impl Node for ruff::Comprehension { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { target, iter, @@ -1192,41 +1195,41 @@ impl Node for ruff::Comprehension { .into_ref_with_type(vm, pyast::NodeComprehension::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("target", target.ast_to_object(vm, source_code), vm) + dict.set_item("target", target.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("iter", iter.ast_to_object(vm, source_code), vm) + dict.set_item("iter", iter.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("ifs", ifs.ast_to_object(vm, source_code), vm) + dict.set_item("ifs", ifs.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("is_async", is_async.ast_to_object(vm, source_code), vm) + dict.set_item("is_async", is_async.ast_to_object(vm, source_file), vm) .unwrap(); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { target: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "target", "comprehension")?, )?, iter: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "iter", "comprehension")?, )?, ifs: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "ifs", "comprehension")?, )?, is_async: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "is_async", "comprehension")?, )?, range: Default::default(), diff --git a/vm/src/stdlib/ast/module.rs b/vm/src/stdlib/ast/module.rs index 409d836808..4a3bc4fd56 100644 --- a/vm/src/stdlib/ast/module.rs +++ b/vm/src/stdlib/ast/module.rs @@ -1,5 +1,6 @@ use super::*; use crate::stdlib::ast::type_ignore::TypeIgnore; +use ruff_source_file::SourceFile; /// Represents the different types of Python module structures. /// @@ -25,33 +26,33 @@ pub(super) enum Mod { // sum impl Node for Mod { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { - Self::Module(cons) => cons.ast_to_object(vm, source_code), - Self::Interactive(cons) => cons.ast_to_object(vm, source_code), - Self::Expression(cons) => cons.ast_to_object(vm, source_code), - Self::FunctionType(cons) => cons.ast_to_object(vm, source_code), + Self::Module(cons) => cons.ast_to_object(vm, source_file), + Self::Interactive(cons) => cons.ast_to_object(vm, source_file), + Self::Expression(cons) => cons.ast_to_object(vm, source_file), + Self::FunctionType(cons) => cons.ast_to_object(vm, source_file), } } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { let cls = object.class(); Ok(if cls.is(pyast::NodeModModule::static_type()) { - Self::Module(ruff::ModModule::ast_from_object(vm, source_code, object)?) + Self::Module(ruff::ModModule::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeModInteractive::static_type()) { - Self::Interactive(ModInteractive::ast_from_object(vm, source_code, object)?) + Self::Interactive(ModInteractive::ast_from_object(vm, source_file, object)?) } else if cls.is(pyast::NodeModExpression::static_type()) { Self::Expression(ruff::ModExpression::ast_from_object( vm, - source_code, + source_file, object, )?) } else if cls.is(pyast::NodeModFunctionType::static_type()) { - Self::FunctionType(ModFunctionType::ast_from_object(vm, source_code, object)?) + Self::FunctionType(ModFunctionType::ast_from_object(vm, source_file, object)?) } else { return Err(vm.new_type_error(format!( "expected some sort of mod, but got {}", @@ -63,7 +64,7 @@ impl Node for Mod { // constructor impl Node for ruff::ModModule { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { body, // type_ignores, @@ -73,30 +74,30 @@ impl Node for ruff::ModModule { .into_ref_with_type(vm, pyast::NodeModModule::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("body", body.ast_to_object(vm, source_code), vm) + dict.set_item("body", body.ast_to_object(vm, source_file), vm) .unwrap(); // TODO: Improve ruff API // ruff ignores type_ignore comments currently. let type_ignores: Vec = vec![]; dict.set_item( "type_ignores", - type_ignores.ast_to_object(vm, source_code), + type_ignores.ast_to_object(vm, source_file), vm, ) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { body: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "body", "Module")?, )?, // type_ignores: Node::ast_from_object( @@ -115,27 +116,27 @@ pub(super) struct ModInteractive { // constructor impl Node for ModInteractive { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { body, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeModInteractive::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("body", body.ast_to_object(vm, source_code), vm) + dict.set_item("body", body.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { body: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "body", "Interactive")?, )?, range: Default::default(), @@ -145,27 +146,27 @@ impl Node for ModInteractive { // constructor impl Node for ruff::ModExpression { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { body, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeModExpression::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("body", body.ast_to_object(vm, source_code), vm) + dict.set_item("body", body.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { body: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "body", "Expression")?, )?, range: Default::default(), @@ -181,7 +182,7 @@ pub(super) struct ModFunctionType { // constructor impl Node for ModFunctionType { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { argtypes, returns, @@ -193,33 +194,33 @@ impl Node for ModFunctionType { let dict = node.as_object().dict().unwrap(); dict.set_item( "argtypes", - BoxedSlice(argtypes).ast_to_object(vm, source_code), + BoxedSlice(argtypes).ast_to_object(vm, source_file), vm, ) .unwrap(); - dict.set_item("returns", returns.ast_to_object(vm, source_code), vm) + dict.set_item("returns", returns.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { argtypes: { let argtypes: BoxedSlice<_> = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "argtypes", "FunctionType")?, )?; argtypes.0 }, returns: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "returns", "FunctionType")?, )?, range: Default::default(), diff --git a/vm/src/stdlib/ast/node.rs b/vm/src/stdlib/ast/node.rs index bf56f6683d..c688a95a72 100644 --- a/vm/src/stdlib/ast/node.rs +++ b/vm/src/stdlib/ast/node.rs @@ -1,11 +1,11 @@ use crate::{PyObjectRef, PyResult, VirtualMachine}; -use rustpython_compiler_source::SourceCodeOwned; +use ruff_source_file::SourceFile; pub(crate) trait Node: Sized { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef; + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef; fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult; @@ -16,11 +16,11 @@ pub(crate) trait Node: Sized { } impl Node for Vec { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { vm.ctx .new_list( self.into_iter() - .map(|node| node.ast_to_object(vm, source_code)) + .map(|node| node.ast_to_object(vm, source_file)) .collect(), ) .into() @@ -28,24 +28,24 @@ impl Node for Vec { fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - vm.extract_elements_with(&object, |obj| Node::ast_from_object(vm, source_code, obj)) + vm.extract_elements_with(&object, |obj| Node::ast_from_object(vm, source_file, obj)) } } impl Node for Box { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { - (*self).ast_to_object(vm, source_code) + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { + (*self).ast_to_object(vm, source_file) } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - T::ast_from_object(vm, source_code, object).map(Self::new) + T::ast_from_object(vm, source_file, object).map(Self::new) } fn is_none(&self) -> bool { @@ -54,22 +54,22 @@ impl Node for Box { } impl Node for Option { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { - Some(node) => node.ast_to_object(vm, source_code), + Some(node) => node.ast_to_object(vm, source_file), None => vm.ctx.none(), } } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { if vm.is_none(&object) { Ok(None) } else { - let x = T::ast_from_object(vm, source_code, object)?; + let x = T::ast_from_object(vm, source_file, object)?; Ok((!x.is_none()).then_some(x)) } } @@ -78,17 +78,17 @@ impl Node for Option { pub(super) struct BoxedSlice(pub(super) Box<[T]>); impl Node for BoxedSlice { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { - self.0.into_vec().ast_to_object(vm, source_code) + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { + self.0.into_vec().ast_to_object(vm, source_file) } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self( - as Node>::ast_from_object(vm, source_code, object)?.into_boxed_slice(), + as Node>::ast_from_object(vm, source_file, object)?.into_boxed_slice(), )) } } diff --git a/vm/src/stdlib/ast/operator.rs b/vm/src/stdlib/ast/operator.rs index fbb2af68c5..2ba03b4835 100644 --- a/vm/src/stdlib/ast/operator.rs +++ b/vm/src/stdlib/ast/operator.rs @@ -1,8 +1,9 @@ use super::*; +use ruff_source_file::SourceFile; // sum impl Node for ruff::BoolOp { - fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let node_type = match self { Self::And => pyast::NodeBoolOpAnd::static_type(), Self::Or => pyast::NodeBoolOpOr::static_type(), @@ -15,7 +16,7 @@ impl Node for ruff::BoolOp { fn ast_from_object( _vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); @@ -34,7 +35,7 @@ impl Node for ruff::BoolOp { // sum impl Node for ruff::Operator { - fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let node_type = match self { Self::Add => pyast::NodeOperatorAdd::static_type(), Self::Sub => pyast::NodeOperatorSub::static_type(), @@ -58,7 +59,7 @@ impl Node for ruff::Operator { fn ast_from_object( _vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); @@ -99,7 +100,7 @@ impl Node for ruff::Operator { // sum impl Node for ruff::UnaryOp { - fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let node_type = match self { Self::Invert => pyast::NodeUnaryOpInvert::static_type(), Self::Not => pyast::NodeUnaryOpNot::static_type(), @@ -114,7 +115,7 @@ impl Node for ruff::UnaryOp { fn ast_from_object( _vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); @@ -137,7 +138,7 @@ impl Node for ruff::UnaryOp { // sum impl Node for ruff::CmpOp { - fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { let node_type = match self { Self::Eq => pyast::NodeCmpOpEq::static_type(), Self::NotEq => pyast::NodeCmpOpNotEq::static_type(), @@ -158,7 +159,7 @@ impl Node for ruff::CmpOp { fn ast_from_object( _vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); diff --git a/vm/src/stdlib/ast/other.rs b/vm/src/stdlib/ast/other.rs index 9003584896..476f5b7c31 100644 --- a/vm/src/stdlib/ast/other.rs +++ b/vm/src/stdlib/ast/other.rs @@ -1,15 +1,16 @@ use super::*; use num_traits::ToPrimitive; +use ruff_source_file::SourceFile; use rustpython_compiler_core::bytecode; impl Node for ruff::ConversionFlag { - fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { vm.ctx.new_int(self as u8).into() } fn ast_from_object( vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { i32::try_from_object(vm, object)? @@ -27,13 +28,13 @@ impl Node for ruff::ConversionFlag { // /// This is just a string, not strictly an AST node. But it makes AST conversions easier. impl Node for ruff::name::Name { - fn ast_to_object(self, vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { vm.ctx.new_str(self.as_str()).to_pyobject(vm) } fn ast_from_object( vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { match object.downcast::() { @@ -44,16 +45,16 @@ impl Node for ruff::name::Name { } impl Node for ruff::Decorator { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { - ruff::Expr::ast_to_object(self.expression, vm, source_code) + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { + ruff::Expr::ast_to_object(self.expression, vm, source_file) } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - let expression = ruff::Expr::ast_from_object(vm, source_code, object)?; + let expression = ruff::Expr::ast_from_object(vm, source_file, object)?; let range = expression.range(); Ok(Self { expression, range }) } @@ -61,7 +62,7 @@ impl Node for ruff::Decorator { // product impl Node for ruff::Alias { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { name, asname, @@ -71,36 +72,36 @@ impl Node for ruff::Alias { .into_ref_with_type(vm, pyast::NodeAlias::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("name", name.ast_to_object(vm, source_code), vm) + dict.set_item("name", name.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("asname", asname.ast_to_object(vm, source_code), vm) + dict.set_item("asname", asname.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, _range, vm, source_code); + node_add_location(&dict, _range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { name: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "name", "alias")?, )?, asname: get_node_field_opt(vm, &object, "asname")? - .map(|obj| Node::ast_from_object(vm, source_code, obj)) + .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, - range: range_from_object(vm, source_code, object, "alias")?, + range: range_from_object(vm, source_file, object, "alias")?, }) } } // product impl Node for ruff::WithItem { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { context_expr, optional_vars, @@ -112,13 +113,13 @@ impl Node for ruff::WithItem { let dict = node.as_object().dict().unwrap(); dict.set_item( "context_expr", - context_expr.ast_to_object(vm, source_code), + context_expr.ast_to_object(vm, source_file), vm, ) .unwrap(); dict.set_item( "optional_vars", - optional_vars.ast_to_object(vm, source_code), + optional_vars.ast_to_object(vm, source_file), vm, ) .unwrap(); @@ -127,17 +128,17 @@ impl Node for ruff::WithItem { fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { context_expr: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "context_expr", "withitem")?, )?, optional_vars: get_node_field_opt(vm, &object, "optional_vars")? - .map(|obj| Node::ast_from_object(vm, source_code, obj)) + .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, range: Default::default(), }) diff --git a/vm/src/stdlib/ast/parameter.rs b/vm/src/stdlib/ast/parameter.rs index b8bbfc9705..8131c819ac 100644 --- a/vm/src/stdlib/ast/parameter.rs +++ b/vm/src/stdlib/ast/parameter.rs @@ -1,8 +1,9 @@ use super::*; +use ruff_source_file::SourceFile; // product impl Node for ruff::Parameters { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { posonlyargs, args, @@ -20,60 +21,60 @@ impl Node for ruff::Parameters { let dict = node.as_object().dict().unwrap(); dict.set_item( "posonlyargs", - posonlyargs.ast_to_object(vm, source_code), + posonlyargs.ast_to_object(vm, source_file), vm, ) .unwrap(); - dict.set_item("args", args.ast_to_object(vm, source_code), vm) + dict.set_item("args", args.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("vararg", vararg.ast_to_object(vm, source_code), vm) + dict.set_item("vararg", vararg.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("kwonlyargs", kwonlyargs.ast_to_object(vm, source_code), vm) + dict.set_item("kwonlyargs", kwonlyargs.ast_to_object(vm, source_file), vm) .unwrap(); dict.set_item( "kw_defaults", - kw_defaults.ast_to_object(vm, source_code), + kw_defaults.ast_to_object(vm, source_file), vm, ) .unwrap(); - dict.set_item("kwarg", kwarg.ast_to_object(vm, source_code), vm) + dict.set_item("kwarg", kwarg.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("defaults", defaults.ast_to_object(vm, source_code), vm) + dict.set_item("defaults", defaults.ast_to_object(vm, source_file), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { let kwonlyargs = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "kwonlyargs", "arguments")?, )?; let kw_defaults = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "kw_defaults", "arguments")?, )?; let kwonlyargs = merge_keyword_parameter_defaults(kwonlyargs, kw_defaults); let posonlyargs = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "posonlyargs", "arguments")?, )?; let args = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "args", "arguments")?, )?; let defaults = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "defaults", "arguments")?, )?; let (posonlyargs, args) = merge_positional_parameter_defaults(posonlyargs, args, defaults); @@ -82,11 +83,11 @@ impl Node for ruff::Parameters { posonlyargs, args, vararg: get_node_field_opt(vm, &object, "vararg")? - .map(|obj| Node::ast_from_object(vm, source_code, obj)) + .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, kwonlyargs, kwarg: get_node_field_opt(vm, &object, "kwarg")? - .map(|obj| Node::ast_from_object(vm, source_code, obj)) + .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, range: Default::default(), }) @@ -99,7 +100,7 @@ impl Node for ruff::Parameters { // product impl Node for ruff::Parameter { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { name, annotation, @@ -114,45 +115,45 @@ impl Node for ruff::Parameter { .into_ref_with_type(_vm, pyast::NodeArg::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("arg", name.ast_to_object(_vm, source_code), _vm) + dict.set_item("arg", name.ast_to_object(_vm, source_file), _vm) .unwrap(); dict.set_item( "annotation", - annotation.ast_to_object(_vm, source_code), + annotation.ast_to_object(_vm, source_file), _vm, ) .unwrap(); // dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm) // .unwrap(); - node_add_location(&dict, range, _vm, source_code); + node_add_location(&dict, range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { name: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "arg", "arg")?, )?, annotation: get_node_field_opt(_vm, &_object, "annotation")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, // type_comment: get_node_field_opt(_vm, &_object, "type_comment")? // .map(|obj| Node::ast_from_object(_vm, obj)) // .transpose()?, - range: range_from_object(_vm, source_code, _object, "arg")?, + range: range_from_object(_vm, source_file, _object, "arg")?, }) } } // product impl Node for ruff::Keyword { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { arg, value, @@ -162,28 +163,28 @@ impl Node for ruff::Keyword { .into_ref_with_type(_vm, pyast::NodeKeyword::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("arg", arg.ast_to_object(_vm, source_code), _vm) + dict.set_item("arg", arg.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("value", value.ast_to_object(_vm, source_code), _vm) + dict.set_item("value", value.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { arg: get_node_field_opt(_vm, &_object, "arg")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, value: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "value", "keyword")?, )?, - range: range_from_object(_vm, source_code, _object, "keyword")?, + range: range_from_object(_vm, source_file, _object, "keyword")?, }) } } @@ -194,16 +195,16 @@ struct PositionalParameters { } impl Node for PositionalParameters { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { - BoxedSlice(self.args).ast_to_object(vm, source_code) + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { + BoxedSlice(self.args).ast_to_object(vm, source_file) } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - let args: BoxedSlice<_> = Node::ast_from_object(vm, source_code, object)?; + let args: BoxedSlice<_> = Node::ast_from_object(vm, source_file, object)?; Ok(Self { args: args.0, _range: TextRange::default(), // TODO @@ -217,16 +218,16 @@ struct KeywordParameters { } impl Node for KeywordParameters { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { - BoxedSlice(self.keywords).ast_to_object(vm, source_code) + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { + BoxedSlice(self.keywords).ast_to_object(vm, source_file) } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - let keywords: BoxedSlice<_> = Node::ast_from_object(vm, source_code, object)?; + let keywords: BoxedSlice<_> = Node::ast_from_object(vm, source_file, object)?; Ok(Self { keywords: keywords.0, _range: TextRange::default(), // TODO @@ -240,16 +241,16 @@ struct ParameterDefaults { } impl Node for ParameterDefaults { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { - BoxedSlice(self.defaults).ast_to_object(vm, source_code) + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { + BoxedSlice(self.defaults).ast_to_object(vm, source_file) } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - let defaults: BoxedSlice<_> = Node::ast_from_object(vm, source_code, object)?; + let defaults: BoxedSlice<_> = Node::ast_from_object(vm, source_file, object)?; Ok(Self { defaults: defaults.0, _range: TextRange::default(), // TODO diff --git a/vm/src/stdlib/ast/pattern.rs b/vm/src/stdlib/ast/pattern.rs index 7057309989..af14bb609d 100644 --- a/vm/src/stdlib/ast/pattern.rs +++ b/vm/src/stdlib/ast/pattern.rs @@ -1,8 +1,9 @@ use super::*; +use ruff_source_file::SourceFile; // product impl Node for ruff::MatchCase { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { pattern, guard, @@ -13,32 +14,32 @@ impl Node for ruff::MatchCase { .into_ref_with_type(_vm, pyast::NodeMatchCase::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("pattern", pattern.ast_to_object(_vm, source_code), _vm) + dict.set_item("pattern", pattern.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("guard", guard.ast_to_object(_vm, source_code), _vm) + dict.set_item("guard", guard.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("body", body.ast_to_object(_vm, source_code), _vm) + dict.set_item("body", body.ast_to_object(_vm, source_file), _vm) .unwrap(); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { pattern: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "pattern", "match_case")?, )?, guard: get_node_field_opt(_vm, &_object, "guard")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, body: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "body", "match_case")?, )?, range: Default::default(), @@ -48,70 +49,70 @@ impl Node for ruff::MatchCase { // sum impl Node for ruff::Pattern { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { - Self::MatchValue(cons) => cons.ast_to_object(vm, source_code), - Self::MatchSingleton(cons) => cons.ast_to_object(vm, source_code), - Self::MatchSequence(cons) => cons.ast_to_object(vm, source_code), - Self::MatchMapping(cons) => cons.ast_to_object(vm, source_code), - Self::MatchClass(cons) => cons.ast_to_object(vm, source_code), - Self::MatchStar(cons) => cons.ast_to_object(vm, source_code), - Self::MatchAs(cons) => cons.ast_to_object(vm, source_code), - Self::MatchOr(cons) => cons.ast_to_object(vm, source_code), + Self::MatchValue(cons) => cons.ast_to_object(vm, source_file), + Self::MatchSingleton(cons) => cons.ast_to_object(vm, source_file), + Self::MatchSequence(cons) => cons.ast_to_object(vm, source_file), + Self::MatchMapping(cons) => cons.ast_to_object(vm, source_file), + Self::MatchClass(cons) => cons.ast_to_object(vm, source_file), + Self::MatchStar(cons) => cons.ast_to_object(vm, source_file), + Self::MatchAs(cons) => cons.ast_to_object(vm, source_file), + Self::MatchOr(cons) => cons.ast_to_object(vm, source_file), } } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); Ok(if _cls.is(pyast::NodePatternMatchValue::static_type()) { Self::MatchValue(ruff::PatternMatchValue::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchSingleton::static_type()) { Self::MatchSingleton(ruff::PatternMatchSingleton::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchSequence::static_type()) { Self::MatchSequence(ruff::PatternMatchSequence::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchMapping::static_type()) { Self::MatchMapping(ruff::PatternMatchMapping::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchClass::static_type()) { Self::MatchClass(ruff::PatternMatchClass::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchStar::static_type()) { Self::MatchStar(ruff::PatternMatchStar::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchAs::static_type()) { Self::MatchAs(ruff::PatternMatchAs::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodePatternMatchOr::static_type()) { Self::MatchOr(ruff::PatternMatchOr::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else { @@ -124,7 +125,7 @@ impl Node for ruff::Pattern { } // constructor impl Node for ruff::PatternMatchValue { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { value, range: _range, @@ -133,31 +134,31 @@ impl Node for ruff::PatternMatchValue { .into_ref_with_type(_vm, pyast::NodePatternMatchValue::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("value", value.ast_to_object(_vm, source_code), _vm) + dict.set_item("value", value.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { value: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "value", "MatchValue")?, )?, - range: range_from_object(_vm, source_code, _object, "MatchValue")?, + range: range_from_object(_vm, source_file, _object, "MatchValue")?, }) } } // constructor impl Node for ruff::PatternMatchSingleton { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { value, range: _range, @@ -169,36 +170,36 @@ impl Node for ruff::PatternMatchSingleton { ) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("value", value.ast_to_object(_vm, source_code), _vm) + dict.set_item("value", value.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { value: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "value", "MatchSingleton")?, )?, - range: range_from_object(_vm, source_code, _object, "MatchSingleton")?, + range: range_from_object(_vm, source_file, _object, "MatchSingleton")?, }) } } impl Node for ruff::Singleton { - fn ast_to_object(self, _vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { todo!() } fn ast_from_object( _vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { todo!() @@ -207,7 +208,7 @@ impl Node for ruff::Singleton { // constructor impl Node for ruff::PatternMatchSequence { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { patterns, range: _range, @@ -219,31 +220,31 @@ impl Node for ruff::PatternMatchSequence { ) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("patterns", patterns.ast_to_object(_vm, source_code), _vm) + dict.set_item("patterns", patterns.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { patterns: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "patterns", "MatchSequence")?, )?, - range: range_from_object(_vm, source_code, _object, "MatchSequence")?, + range: range_from_object(_vm, source_file, _object, "MatchSequence")?, }) } } // constructor impl Node for ruff::PatternMatchMapping { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { keys, patterns, @@ -257,43 +258,43 @@ impl Node for ruff::PatternMatchMapping { ) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("keys", keys.ast_to_object(_vm, source_code), _vm) + dict.set_item("keys", keys.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("patterns", patterns.ast_to_object(_vm, source_code), _vm) + dict.set_item("patterns", patterns.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("rest", rest.ast_to_object(_vm, source_code), _vm) + dict.set_item("rest", rest.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { keys: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "keys", "MatchMapping")?, )?, patterns: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "patterns", "MatchMapping")?, )?, rest: get_node_field_opt(_vm, &_object, "rest")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, - range: range_from_object(_vm, source_code, _object, "MatchMapping")?, + range: range_from_object(_vm, source_file, _object, "MatchMapping")?, }) } } // constructor impl Node for ruff::PatternMatchClass { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { cls, arguments, @@ -304,40 +305,40 @@ impl Node for ruff::PatternMatchClass { .into_ref_with_type(vm, pyast::NodePatternMatchClass::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("cls", cls.ast_to_object(vm, source_code), vm) + dict.set_item("cls", cls.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("patterns", patterns.ast_to_object(vm, source_code), vm) + dict.set_item("patterns", patterns.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("kwd_attrs", kwd_attrs.ast_to_object(vm, source_code), vm) + dict.set_item("kwd_attrs", kwd_attrs.ast_to_object(vm, source_file), vm) .unwrap(); dict.set_item( "kwd_patterns", - kwd_patterns.ast_to_object(vm, source_code), + kwd_patterns.ast_to_object(vm, source_file), vm, ) .unwrap(); - node_add_location(&dict, _range, vm, source_code); + node_add_location(&dict, _range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { let patterns = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "patterns", "MatchClass")?, )?; let kwd_attrs = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "kwd_attrs", "MatchClass")?, )?; let kwd_patterns = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "kwd_patterns", "MatchClass")?, )?; let (patterns, keywords) = merge_pattern_match_class(patterns, kwd_attrs, kwd_patterns); @@ -345,10 +346,10 @@ impl Node for ruff::PatternMatchClass { Ok(Self { cls: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "cls", "MatchClass")?, )?, - range: range_from_object(vm, source_code, object, "MatchClass")?, + range: range_from_object(vm, source_file, object, "MatchClass")?, arguments: ruff::PatternArguments { range: Default::default(), patterns, @@ -363,13 +364,13 @@ struct PatternMatchClassPatterns { } impl Node for PatternMatchClassPatterns { - fn ast_to_object(self, _vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { todo!() } fn ast_from_object( _vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { todo!() @@ -381,13 +382,13 @@ struct PatternMatchClassKeywordAttributes { } impl Node for PatternMatchClassKeywordAttributes { - fn ast_to_object(self, _vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { todo!() } fn ast_from_object( _vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { todo!() @@ -399,13 +400,13 @@ struct PatternMatchClassKeywordPatterns { } impl Node for PatternMatchClassKeywordPatterns { - fn ast_to_object(self, _vm: &VirtualMachine, _source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { todo!() } fn ast_from_object( _vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { todo!() @@ -413,7 +414,7 @@ impl Node for PatternMatchClassKeywordPatterns { } // constructor impl Node for ruff::PatternMatchStar { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { name, range: _range, @@ -422,29 +423,29 @@ impl Node for ruff::PatternMatchStar { .into_ref_with_type(_vm, pyast::NodePatternMatchStar::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("name", name.ast_to_object(_vm, source_code), _vm) + dict.set_item("name", name.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { name: get_node_field_opt(_vm, &_object, "name")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, - range: range_from_object(_vm, source_code, _object, "MatchStar")?, + range: range_from_object(_vm, source_file, _object, "MatchStar")?, }) } } // constructor impl Node for ruff::PatternMatchAs { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { pattern, name, @@ -454,34 +455,34 @@ impl Node for ruff::PatternMatchAs { .into_ref_with_type(_vm, pyast::NodePatternMatchAs::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("pattern", pattern.ast_to_object(_vm, source_code), _vm) + dict.set_item("pattern", pattern.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("name", name.ast_to_object(_vm, source_code), _vm) + dict.set_item("name", name.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { pattern: get_node_field_opt(_vm, &_object, "pattern")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, name: get_node_field_opt(_vm, &_object, "name")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, - range: range_from_object(_vm, source_code, _object, "MatchAs")?, + range: range_from_object(_vm, source_file, _object, "MatchAs")?, }) } } // constructor impl Node for ruff::PatternMatchOr { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { patterns, range: _range, @@ -490,23 +491,23 @@ impl Node for ruff::PatternMatchOr { .into_ref_with_type(_vm, pyast::NodePatternMatchOr::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("patterns", patterns.ast_to_object(_vm, source_code), _vm) + dict.set_item("patterns", patterns.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { patterns: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "patterns", "MatchOr")?, )?, - range: range_from_object(_vm, source_code, _object, "MatchOr")?, + range: range_from_object(_vm, source_file, _object, "MatchOr")?, }) } } diff --git a/vm/src/stdlib/ast/statement.rs b/vm/src/stdlib/ast/statement.rs index 3636c4ab6d..1ac07c4034 100644 --- a/vm/src/stdlib/ast/statement.rs +++ b/vm/src/stdlib/ast/statement.rs @@ -1,33 +1,35 @@ use super::*; use crate::stdlib::ast::argument::{merge_class_def_args, split_class_def_args}; +use ruff_source_file::SourceFile; + // sum impl Node for ruff::Stmt { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { - Self::FunctionDef(cons) => cons.ast_to_object(vm, source_code), - Self::ClassDef(cons) => cons.ast_to_object(vm, source_code), - Self::Return(cons) => cons.ast_to_object(vm, source_code), - Self::Delete(cons) => cons.ast_to_object(vm, source_code), - Self::Assign(cons) => cons.ast_to_object(vm, source_code), - Self::TypeAlias(cons) => cons.ast_to_object(vm, source_code), - Self::AugAssign(cons) => cons.ast_to_object(vm, source_code), - Self::AnnAssign(cons) => cons.ast_to_object(vm, source_code), - Self::For(cons) => cons.ast_to_object(vm, source_code), - Self::While(cons) => cons.ast_to_object(vm, source_code), - Self::If(cons) => cons.ast_to_object(vm, source_code), - Self::With(cons) => cons.ast_to_object(vm, source_code), - Self::Match(cons) => cons.ast_to_object(vm, source_code), - Self::Raise(cons) => cons.ast_to_object(vm, source_code), - Self::Try(cons) => cons.ast_to_object(vm, source_code), - Self::Assert(cons) => cons.ast_to_object(vm, source_code), - Self::Import(cons) => cons.ast_to_object(vm, source_code), - Self::ImportFrom(cons) => cons.ast_to_object(vm, source_code), - Self::Global(cons) => cons.ast_to_object(vm, source_code), - Self::Nonlocal(cons) => cons.ast_to_object(vm, source_code), - Self::Expr(cons) => cons.ast_to_object(vm, source_code), - Self::Pass(cons) => cons.ast_to_object(vm, source_code), - Self::Break(cons) => cons.ast_to_object(vm, source_code), - Self::Continue(cons) => cons.ast_to_object(vm, source_code), + Self::FunctionDef(cons) => cons.ast_to_object(vm, source_file), + Self::ClassDef(cons) => cons.ast_to_object(vm, source_file), + Self::Return(cons) => cons.ast_to_object(vm, source_file), + Self::Delete(cons) => cons.ast_to_object(vm, source_file), + Self::Assign(cons) => cons.ast_to_object(vm, source_file), + Self::TypeAlias(cons) => cons.ast_to_object(vm, source_file), + Self::AugAssign(cons) => cons.ast_to_object(vm, source_file), + Self::AnnAssign(cons) => cons.ast_to_object(vm, source_file), + Self::For(cons) => cons.ast_to_object(vm, source_file), + Self::While(cons) => cons.ast_to_object(vm, source_file), + Self::If(cons) => cons.ast_to_object(vm, source_file), + Self::With(cons) => cons.ast_to_object(vm, source_file), + Self::Match(cons) => cons.ast_to_object(vm, source_file), + Self::Raise(cons) => cons.ast_to_object(vm, source_file), + Self::Try(cons) => cons.ast_to_object(vm, source_file), + Self::Assert(cons) => cons.ast_to_object(vm, source_file), + Self::Import(cons) => cons.ast_to_object(vm, source_file), + Self::ImportFrom(cons) => cons.ast_to_object(vm, source_file), + Self::Global(cons) => cons.ast_to_object(vm, source_file), + Self::Nonlocal(cons) => cons.ast_to_object(vm, source_file), + Self::Expr(cons) => cons.ast_to_object(vm, source_file), + Self::Pass(cons) => cons.ast_to_object(vm, source_file), + Self::Break(cons) => cons.ast_to_object(vm, source_file), + Self::Continue(cons) => cons.ast_to_object(vm, source_file), Self::IpyEscapeCommand(_) => { unimplemented!("IPython escape command is not allowed in Python AST") } @@ -37,124 +39,124 @@ impl Node for ruff::Stmt { #[allow(clippy::if_same_then_else)] fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); Ok(if _cls.is(pyast::NodeStmtFunctionDef::static_type()) { Self::FunctionDef(ruff::StmtFunctionDef::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtAsyncFunctionDef::static_type()) { Self::FunctionDef(ruff::StmtFunctionDef::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtClassDef::static_type()) { Self::ClassDef(ruff::StmtClassDef::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtReturn::static_type()) { Self::Return(ruff::StmtReturn::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtDelete::static_type()) { Self::Delete(ruff::StmtDelete::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtAssign::static_type()) { Self::Assign(ruff::StmtAssign::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtTypeAlias::static_type()) { Self::TypeAlias(ruff::StmtTypeAlias::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtAugAssign::static_type()) { Self::AugAssign(ruff::StmtAugAssign::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtAnnAssign::static_type()) { Self::AnnAssign(ruff::StmtAnnAssign::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtFor::static_type()) { - Self::For(ruff::StmtFor::ast_from_object(_vm, source_code, _object)?) + Self::For(ruff::StmtFor::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtAsyncFor::static_type()) { - Self::For(ruff::StmtFor::ast_from_object(_vm, source_code, _object)?) + Self::For(ruff::StmtFor::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtWhile::static_type()) { - Self::While(ruff::StmtWhile::ast_from_object(_vm, source_code, _object)?) + Self::While(ruff::StmtWhile::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtIf::static_type()) { - Self::If(ruff::StmtIf::ast_from_object(_vm, source_code, _object)?) + Self::If(ruff::StmtIf::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtWith::static_type()) { - Self::With(ruff::StmtWith::ast_from_object(_vm, source_code, _object)?) + Self::With(ruff::StmtWith::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtAsyncWith::static_type()) { - Self::With(ruff::StmtWith::ast_from_object(_vm, source_code, _object)?) + Self::With(ruff::StmtWith::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtMatch::static_type()) { - Self::Match(ruff::StmtMatch::ast_from_object(_vm, source_code, _object)?) + Self::Match(ruff::StmtMatch::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtRaise::static_type()) { - Self::Raise(ruff::StmtRaise::ast_from_object(_vm, source_code, _object)?) + Self::Raise(ruff::StmtRaise::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtTry::static_type()) { - Self::Try(ruff::StmtTry::ast_from_object(_vm, source_code, _object)?) + Self::Try(ruff::StmtTry::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtTryStar::static_type()) { - Self::Try(ruff::StmtTry::ast_from_object(_vm, source_code, _object)?) + Self::Try(ruff::StmtTry::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtAssert::static_type()) { Self::Assert(ruff::StmtAssert::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtImport::static_type()) { Self::Import(ruff::StmtImport::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtImportFrom::static_type()) { Self::ImportFrom(ruff::StmtImportFrom::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtGlobal::static_type()) { Self::Global(ruff::StmtGlobal::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtNonlocal::static_type()) { Self::Nonlocal(ruff::StmtNonlocal::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeStmtExpr::static_type()) { - Self::Expr(ruff::StmtExpr::ast_from_object(_vm, source_code, _object)?) + Self::Expr(ruff::StmtExpr::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtPass::static_type()) { - Self::Pass(ruff::StmtPass::ast_from_object(_vm, source_code, _object)?) + Self::Pass(ruff::StmtPass::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtBreak::static_type()) { - Self::Break(ruff::StmtBreak::ast_from_object(_vm, source_code, _object)?) + Self::Break(ruff::StmtBreak::ast_from_object(_vm, source_file, _object)?) } else if _cls.is(pyast::NodeStmtContinue::static_type()) { Self::Continue(ruff::StmtContinue::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else { @@ -165,9 +167,10 @@ impl Node for ruff::Stmt { }) } } + // constructor impl Node for ruff::StmtFunctionDef { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { name, parameters, @@ -190,33 +193,33 @@ impl Node for ruff::StmtFunctionDef { let dict = node.as_object().dict().unwrap(); dict.set_item("name", vm.ctx.new_str(name.as_str()).to_pyobject(vm), vm) .unwrap(); - dict.set_item("args", parameters.ast_to_object(vm, source_code), vm) + dict.set_item("args", parameters.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("body", body.ast_to_object(vm, source_code), vm) + dict.set_item("body", body.ast_to_object(vm, source_file), vm) .unwrap(); dict.set_item( "decorator_list", - decorator_list.ast_to_object(vm, source_code), + decorator_list.ast_to_object(vm, source_file), vm, ) .unwrap(); - dict.set_item("returns", returns.ast_to_object(vm, source_code), vm) + dict.set_item("returns", returns.ast_to_object(vm, source_file), vm) .unwrap(); // TODO: Ruff ignores type_comment during parsing // dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm) // .unwrap(); dict.set_item( "type_params", - type_params.ast_to_object(vm, source_code), + type_params.ast_to_object(vm, source_file), vm, ) .unwrap(); - node_add_location(&dict, _range, vm, source_code); + node_add_location(&dict, _range, vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); @@ -224,26 +227,26 @@ impl Node for ruff::StmtFunctionDef { Ok(Self { name: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "name", "FunctionDef")?, )?, parameters: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "args", "FunctionDef")?, )?, body: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "body", "FunctionDef")?, )?, decorator_list: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "decorator_list", "FunctionDef")?, )?, returns: get_node_field_opt(_vm, &_object, "returns")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, // TODO: Ruff ignores type_comment during parsing // type_comment: get_node_field_opt(_vm, &_object, "type_comment")? @@ -251,17 +254,18 @@ impl Node for ruff::StmtFunctionDef { // .transpose()?, type_params: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "type_params", "FunctionDef")?, )?, - range: range_from_object(_vm, source_code, _object, "FunctionDef")?, + range: range_from_object(_vm, source_file, _object, "FunctionDef")?, is_async, }) } } + // constructor impl Node for ruff::StmtClassDef { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { name, arguments, @@ -275,73 +279,73 @@ impl Node for ruff::StmtClassDef { .into_ref_with_type(_vm, pyast::NodeStmtClassDef::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("name", name.ast_to_object(_vm, source_code), _vm) + dict.set_item("name", name.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("bases", bases.ast_to_object(_vm, source_code), _vm) + dict.set_item("bases", bases.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("keywords", keywords.ast_to_object(_vm, source_code), _vm) + dict.set_item("keywords", keywords.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("body", body.ast_to_object(_vm, source_code), _vm) + dict.set_item("body", body.ast_to_object(_vm, source_file), _vm) .unwrap(); dict.set_item( "decorator_list", - decorator_list.ast_to_object(_vm, source_code), + decorator_list.ast_to_object(_vm, source_file), _vm, ) .unwrap(); dict.set_item( "type_params", - type_params.ast_to_object(_vm, source_code), + type_params.ast_to_object(_vm, source_file), _vm, ) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let bases = Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "bases", "ClassDef")?, )?; let keywords = Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "keywords", "ClassDef")?, )?; Ok(Self { name: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "name", "ClassDef")?, )?, arguments: merge_class_def_args(bases, keywords), body: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "body", "ClassDef")?, )?, decorator_list: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "decorator_list", "ClassDef")?, )?, type_params: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "type_params", "ClassDef")?, )?, - range: range_from_object(_vm, source_code, _object, "ClassDef")?, + range: range_from_object(_vm, source_file, _object, "ClassDef")?, }) } } // constructor impl Node for ruff::StmtReturn { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { value, range: _range, @@ -350,27 +354,27 @@ impl Node for ruff::StmtReturn { .into_ref_with_type(_vm, pyast::NodeStmtReturn::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("value", value.ast_to_object(_vm, source_code), _vm) + dict.set_item("value", value.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { value: get_node_field_opt(_vm, &_object, "value")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, - range: range_from_object(_vm, source_code, _object, "Return")?, + range: range_from_object(_vm, source_file, _object, "Return")?, }) } } // constructor impl Node for ruff::StmtDelete { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { targets, range: _range, @@ -379,29 +383,30 @@ impl Node for ruff::StmtDelete { .into_ref_with_type(_vm, pyast::NodeStmtDelete::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("targets", targets.ast_to_object(_vm, source_code), _vm) + dict.set_item("targets", targets.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { targets: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "targets", "Delete")?, )?, - range: range_from_object(_vm, source_code, _object, "Delete")?, + range: range_from_object(_vm, source_file, _object, "Delete")?, }) } } + // constructor impl Node for ruff::StmtAssign { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { targets, value, @@ -412,41 +417,42 @@ impl Node for ruff::StmtAssign { .into_ref_with_type(vm, pyast::NodeStmtAssign::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("targets", targets.ast_to_object(vm, source_code), vm) + dict.set_item("targets", targets.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("value", value.ast_to_object(vm, source_code), vm) + dict.set_item("value", value.ast_to_object(vm, source_file), vm) .unwrap(); // TODO dict.set_item("type_comment", vm.ctx.none(), vm).unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { targets: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "targets", "Assign")?, )?, value: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "value", "Assign")?, )?, // type_comment: get_node_field_opt(_vm, &_object, "type_comment")? // .map(|obj| Node::ast_from_object(_vm, obj)) // .transpose()?, - range: range_from_object(vm, source_code, object, "Assign")?, + range: range_from_object(vm, source_file, object, "Assign")?, }) } } + // constructor impl Node for ruff::StmtTypeAlias { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { name, type_params, @@ -457,47 +463,49 @@ impl Node for ruff::StmtTypeAlias { .into_ref_with_type(_vm, pyast::NodeStmtTypeAlias::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("name", name.ast_to_object(_vm, source_code), _vm) + dict.set_item("name", name.ast_to_object(_vm, source_file), _vm) .unwrap(); dict.set_item( "type_params", - type_params.ast_to_object(_vm, source_code), + type_params.ast_to_object(_vm, source_file), _vm, ) .unwrap(); - dict.set_item("value", value.ast_to_object(_vm, source_code), _vm) + dict.set_item("value", value.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } + fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { name: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "name", "TypeAlias")?, )?, type_params: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "type_params", "TypeAlias")?, )?, value: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "value", "TypeAlias")?, )?, - range: range_from_object(_vm, source_code, _object, "TypeAlias")?, + range: range_from_object(_vm, source_file, _object, "TypeAlias")?, }) } } + // constructor impl Node for ruff::StmtAugAssign { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { target, op, @@ -508,43 +516,44 @@ impl Node for ruff::StmtAugAssign { .into_ref_with_type(_vm, pyast::NodeStmtAugAssign::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("target", target.ast_to_object(_vm, source_code), _vm) + dict.set_item("target", target.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("op", op.ast_to_object(_vm, source_code), _vm) + dict.set_item("op", op.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("value", value.ast_to_object(_vm, source_code), _vm) + dict.set_item("value", value.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { target: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "target", "AugAssign")?, )?, op: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "op", "AugAssign")?, )?, value: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "value", "AugAssign")?, )?, - range: range_from_object(_vm, source_code, _object, "AugAssign")?, + range: range_from_object(_vm, source_file, _object, "AugAssign")?, }) } } + // constructor impl Node for ruff::StmtAnnAssign { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { target, annotation, @@ -556,52 +565,53 @@ impl Node for ruff::StmtAnnAssign { .into_ref_with_type(_vm, pyast::NodeStmtAnnAssign::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("target", target.ast_to_object(_vm, source_code), _vm) + dict.set_item("target", target.ast_to_object(_vm, source_file), _vm) .unwrap(); dict.set_item( "annotation", - annotation.ast_to_object(_vm, source_code), + annotation.ast_to_object(_vm, source_file), _vm, ) .unwrap(); - dict.set_item("value", value.ast_to_object(_vm, source_code), _vm) + dict.set_item("value", value.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("simple", simple.ast_to_object(_vm, source_code), _vm) + dict.set_item("simple", simple.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { target: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "target", "AnnAssign")?, )?, annotation: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "annotation", "AnnAssign")?, )?, value: get_node_field_opt(_vm, &_object, "value")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, simple: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "simple", "AnnAssign")?, )?, - range: range_from_object(_vm, source_code, _object, "AnnAssign")?, + range: range_from_object(_vm, source_file, _object, "AnnAssign")?, }) } } + // constructor impl Node for ruff::StmtFor { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { is_async, target, @@ -620,22 +630,23 @@ impl Node for ruff::StmtFor { let node = NodeAst.into_ref_with_type(_vm, cls).unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("target", target.ast_to_object(_vm, source_code), _vm) + dict.set_item("target", target.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("iter", iter.ast_to_object(_vm, source_code), _vm) + dict.set_item("iter", iter.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("body", body.ast_to_object(_vm, source_code), _vm) + dict.set_item("body", body.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("orelse", orelse.ast_to_object(_vm, source_code), _vm) + dict.set_item("orelse", orelse.ast_to_object(_vm, source_file), _vm) .unwrap(); // dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm) // .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } + fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); @@ -647,35 +658,36 @@ impl Node for ruff::StmtFor { Ok(Self { target: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "target", "For")?, )?, iter: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "iter", "For")?, )?, body: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "body", "For")?, )?, orelse: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "orelse", "For")?, )?, // type_comment: get_node_field_opt(_vm, &_object, "type_comment")? // .map(|obj| Node::ast_from_object(_vm, obj)) // .transpose()?, - range: range_from_object(_vm, source_code, _object, "For")?, + range: range_from_object(_vm, source_file, _object, "For")?, is_async, }) } } + // constructor impl Node for ruff::StmtWhile { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { test, body, @@ -686,43 +698,44 @@ impl Node for ruff::StmtWhile { .into_ref_with_type(_vm, pyast::NodeStmtWhile::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("test", test.ast_to_object(_vm, source_code), _vm) + dict.set_item("test", test.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("body", body.ast_to_object(_vm, source_code), _vm) + dict.set_item("body", body.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("orelse", orelse.ast_to_object(_vm, source_code), _vm) + dict.set_item("orelse", orelse.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } + fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { test: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "test", "While")?, )?, body: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "body", "While")?, )?, orelse: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "orelse", "While")?, )?, - range: range_from_object(_vm, source_code, _object, "While")?, + range: range_from_object(_vm, source_file, _object, "While")?, }) } } // constructor impl Node for ruff::StmtIf { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { test, body, @@ -737,20 +750,20 @@ impl Node for ruff::StmtIf { }, elif_else_clauses.into_iter(), _vm, - source_code, + source_file, ) } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { - elif_else_clause::ast_from_object(vm, source_code, object) + elif_else_clause::ast_from_object(vm, source_file, object) } } // constructor impl Node for ruff::StmtWith { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { is_async, items, @@ -767,18 +780,18 @@ impl Node for ruff::StmtWith { let node = NodeAst.into_ref_with_type(_vm, cls).unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("items", items.ast_to_object(_vm, source_code), _vm) + dict.set_item("items", items.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("body", body.ast_to_object(_vm, source_code), _vm) + dict.set_item("body", body.ast_to_object(_vm, source_file), _vm) .unwrap(); // dict.set_item("type_comment", type_comment.ast_to_object(_vm), _vm) // .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); @@ -790,25 +803,25 @@ impl Node for ruff::StmtWith { Ok(Self { items: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "items", "With")?, )?, body: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "body", "With")?, )?, // type_comment: get_node_field_opt(_vm, &_object, "type_comment")? // .map(|obj| Node::ast_from_object(_vm, obj)) // .transpose()?, - range: range_from_object(_vm, source_code, _object, "With")?, + range: range_from_object(_vm, source_file, _object, "With")?, is_async, }) } } // constructor impl Node for ruff::StmtMatch { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { subject, cases, @@ -818,36 +831,36 @@ impl Node for ruff::StmtMatch { .into_ref_with_type(_vm, pyast::NodeStmtMatch::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("subject", subject.ast_to_object(_vm, source_code), _vm) + dict.set_item("subject", subject.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("cases", cases.ast_to_object(_vm, source_code), _vm) + dict.set_item("cases", cases.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { subject: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "subject", "Match")?, )?, cases: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "cases", "Match")?, )?, - range: range_from_object(_vm, source_code, _object, "Match")?, + range: range_from_object(_vm, source_file, _object, "Match")?, }) } } // constructor impl Node for ruff::StmtRaise { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { exc, cause, @@ -857,32 +870,32 @@ impl Node for ruff::StmtRaise { .into_ref_with_type(_vm, pyast::NodeStmtRaise::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("exc", exc.ast_to_object(_vm, source_code), _vm) + dict.set_item("exc", exc.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("cause", cause.ast_to_object(_vm, source_code), _vm) + dict.set_item("cause", cause.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { exc: get_node_field_opt(_vm, &_object, "exc")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, cause: get_node_field_opt(_vm, &_object, "cause")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, - range: range_from_object(_vm, source_code, _object, "Raise")?, + range: range_from_object(_vm, source_file, _object, "Raise")?, }) } } // constructor impl Node for ruff::StmtTry { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { body, handlers, @@ -902,20 +915,20 @@ impl Node for ruff::StmtTry { let node = NodeAst.into_ref_with_type(_vm, cls).unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("body", body.ast_to_object(_vm, source_code), _vm) + dict.set_item("body", body.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("handlers", handlers.ast_to_object(_vm, source_code), _vm) + dict.set_item("handlers", handlers.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("orelse", orelse.ast_to_object(_vm, source_code), _vm) + dict.set_item("orelse", orelse.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("finalbody", finalbody.ast_to_object(_vm, source_code), _vm) + dict.set_item("finalbody", finalbody.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); @@ -929,32 +942,32 @@ impl Node for ruff::StmtTry { Ok(Self { body: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "body", "Try")?, )?, handlers: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "handlers", "Try")?, )?, orelse: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "orelse", "Try")?, )?, finalbody: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "finalbody", "Try")?, )?, - range: range_from_object(_vm, source_code, _object, "Try")?, + range: range_from_object(_vm, source_file, _object, "Try")?, is_star, }) } } // constructor impl Node for ruff::StmtAssert { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { test, msg, @@ -964,34 +977,34 @@ impl Node for ruff::StmtAssert { .into_ref_with_type(_vm, pyast::NodeStmtAssert::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("test", test.ast_to_object(_vm, source_code), _vm) + dict.set_item("test", test.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("msg", msg.ast_to_object(_vm, source_code), _vm) + dict.set_item("msg", msg.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { test: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "test", "Assert")?, )?, msg: get_node_field_opt(_vm, &_object, "msg")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, - range: range_from_object(_vm, source_code, _object, "Assert")?, + range: range_from_object(_vm, source_file, _object, "Assert")?, }) } } // constructor impl Node for ruff::StmtImport { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { names, range: _range, @@ -1000,29 +1013,29 @@ impl Node for ruff::StmtImport { .into_ref_with_type(_vm, pyast::NodeStmtImport::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("names", names.ast_to_object(_vm, source_code), _vm) + dict.set_item("names", names.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { names: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "names", "Import")?, )?, - range: range_from_object(_vm, source_code, _object, "Import")?, + range: range_from_object(_vm, source_file, _object, "Import")?, }) } } // constructor impl Node for ruff::StmtImportFrom { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { module, names, @@ -1033,40 +1046,40 @@ impl Node for ruff::StmtImportFrom { .into_ref_with_type(vm, pyast::NodeStmtImportFrom::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("module", module.ast_to_object(vm, source_code), vm) + dict.set_item("module", module.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("names", names.ast_to_object(vm, source_code), vm) + dict.set_item("names", names.ast_to_object(vm, source_file), vm) .unwrap(); dict.set_item("level", vm.ctx.new_int(level).to_pyobject(vm), vm) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { module: get_node_field_opt(vm, &_object, "module")? - .map(|obj| Node::ast_from_object(vm, source_code, obj)) + .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, names: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &_object, "names", "ImportFrom")?, )?, level: get_node_field(vm, &_object, "level", "ImportFrom")? .downcast_exact::(vm) .unwrap() .try_to_primitive::(vm)?, - range: range_from_object(vm, source_code, _object, "ImportFrom")?, + range: range_from_object(vm, source_file, _object, "ImportFrom")?, }) } } // constructor impl Node for ruff::StmtGlobal { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { names, range: _range, @@ -1075,29 +1088,29 @@ impl Node for ruff::StmtGlobal { .into_ref_with_type(_vm, pyast::NodeStmtGlobal::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("names", names.ast_to_object(_vm, source_code), _vm) + dict.set_item("names", names.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { names: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "names", "Global")?, )?, - range: range_from_object(_vm, source_code, _object, "Global")?, + range: range_from_object(_vm, source_file, _object, "Global")?, }) } } // constructor impl Node for ruff::StmtNonlocal { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { names, range: _range, @@ -1106,29 +1119,29 @@ impl Node for ruff::StmtNonlocal { .into_ref_with_type(_vm, pyast::NodeStmtNonlocal::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("names", names.ast_to_object(_vm, source_code), _vm) + dict.set_item("names", names.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { names: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "names", "Nonlocal")?, )?, - range: range_from_object(_vm, source_code, _object, "Nonlocal")?, + range: range_from_object(_vm, source_file, _object, "Nonlocal")?, }) } } // constructor impl Node for ruff::StmtExpr { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { value, range: _range, @@ -1137,86 +1150,88 @@ impl Node for ruff::StmtExpr { .into_ref_with_type(_vm, pyast::NodeStmtExpr::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("value", value.ast_to_object(_vm, source_code), _vm) + dict.set_item("value", value.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { value: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "value", "Expr")?, )?, - range: range_from_object(_vm, source_code, _object, "Expr")?, + range: range_from_object(_vm, source_file, _object, "Expr")?, }) } } // constructor impl Node for ruff::StmtPass { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { range: _range } = self; let node = NodeAst .into_ref_with_type(_vm, pyast::NodeStmtPass::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { - range: range_from_object(_vm, source_code, _object, "Pass")?, + range: range_from_object(_vm, source_file, _object, "Pass")?, }) } } // constructor impl Node for ruff::StmtBreak { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { range: _range } = self; let node = NodeAst .into_ref_with_type(_vm, pyast::NodeStmtBreak::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } + fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { - range: range_from_object(_vm, source_code, _object, "Break")?, + range: range_from_object(_vm, source_file, _object, "Break")?, }) } } + // constructor impl Node for ruff::StmtContinue { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { range: _range } = self; let node = NodeAst .into_ref_with_type(_vm, pyast::NodeStmtContinue::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { - range: range_from_object(_vm, source_code, _object, "Continue")?, + 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 7b1e71a533..5d8654270d 100644 --- a/vm/src/stdlib/ast/string.rs +++ b/vm/src/stdlib/ast/string.rs @@ -213,7 +213,7 @@ fn joined_str_part_to_ruff_fstring_element(part: JoinedStrPart) -> ruff::FString // constructor impl Node for JoinedStr { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { values, range } = self; let node = NodeAst .into_ref_with_type(vm, pyast::NodeExprJoinedStr::static_type().to_owned()) @@ -221,26 +221,26 @@ impl Node for JoinedStr { let dict = node.as_object().dict().unwrap(); dict.set_item( "values", - BoxedSlice(values).ast_to_object(vm, source_code), + BoxedSlice(values).ast_to_object(vm, source_file), vm, ) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { let values: BoxedSlice<_> = Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "values", "JoinedStr")?, )?; Ok(Self { values: values.0, - range: range_from_object(vm, source_code, object, "JoinedStr")?, + range: range_from_object(vm, source_file, object, "JoinedStr")?, }) } } @@ -253,28 +253,28 @@ pub(super) enum JoinedStrPart { // constructor impl Node for JoinedStrPart { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { - Self::FormattedValue(value) => value.ast_to_object(vm, source_code), - Self::Constant(value) => value.ast_to_object(vm, source_code), + Self::FormattedValue(value) => value.ast_to_object(vm, source_file), + Self::Constant(value) => value.ast_to_object(vm, source_file), } } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { let cls = object.class(); if cls.is(pyast::NodeExprFormattedValue::static_type()) { Ok(Self::FormattedValue(Node::ast_from_object( vm, - source_code, + source_file, object, )?)) } else { Ok(Self::Constant(Node::ast_from_object( vm, - source_code, + source_file, object, )?)) } @@ -291,7 +291,7 @@ pub(super) struct FormattedValue { // constructor impl Node for FormattedValue { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { value, conversion, @@ -302,46 +302,46 @@ impl Node for FormattedValue { .into_ref_with_type(vm, pyast::NodeExprFormattedValue::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("value", value.ast_to_object(vm, source_code), vm) + dict.set_item("value", value.ast_to_object(vm, source_file), vm) .unwrap(); - dict.set_item("conversion", conversion.ast_to_object(vm, source_code), vm) + dict.set_item("conversion", conversion.ast_to_object(vm, source_file), vm) .unwrap(); dict.set_item( "format_spec", - format_spec.ast_to_object(vm, source_code), + format_spec.ast_to_object(vm, source_file), vm, ) .unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { value: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "value", "FormattedValue")?, )?, conversion: Node::ast_from_object( vm, - source_code, + source_file, get_node_field(vm, &object, "conversion", "FormattedValue")?, )?, format_spec: get_node_field_opt(vm, &object, "format_spec")? - .map(|obj| Node::ast_from_object(vm, source_code, obj)) + .map(|obj| Node::ast_from_object(vm, source_file, obj)) .transpose()?, - range: range_from_object(vm, source_code, object, "FormattedValue")?, + range: range_from_object(vm, source_file, object, "FormattedValue")?, }) } } pub(super) fn fstring_to_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, expression: ruff::ExprFString, ) -> PyObjectRef { let ruff::ExprFString { range, value } = expression; @@ -350,5 +350,5 @@ pub(super) fn fstring_to_object( .collect(); let values = values.into_boxed_slice(); let c = JoinedStr { range, values }; - c.ast_to_object(vm, source_code) + c.ast_to_object(vm, source_file) } diff --git a/vm/src/stdlib/ast/type_ignore.rs b/vm/src/stdlib/ast/type_ignore.rs index a247302fa6..de122d013c 100644 --- a/vm/src/stdlib/ast/type_ignore.rs +++ b/vm/src/stdlib/ast/type_ignore.rs @@ -1,4 +1,5 @@ use super::*; +use ruff_source_file::SourceFile; pub(super) enum TypeIgnore { TypeIgnore(TypeIgnoreTypeIgnore), @@ -6,21 +7,21 @@ pub(super) enum TypeIgnore { // sum impl Node for TypeIgnore { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { - Self::TypeIgnore(cons) => cons.ast_to_object(vm, source_code), + Self::TypeIgnore(cons) => cons.ast_to_object(vm, source_file), } } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); Ok(if _cls.is(pyast::NodeTypeIgnoreTypeIgnore::static_type()) { Self::TypeIgnore(TypeIgnoreTypeIgnore::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else { @@ -40,7 +41,7 @@ pub(super) struct TypeIgnoreTypeIgnore { // constructor impl Node for TypeIgnoreTypeIgnore { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { lineno, tag, range } = self; let node = NodeAst .into_ref_with_type( @@ -51,13 +52,13 @@ impl Node for TypeIgnoreTypeIgnore { let dict = node.as_object().dict().unwrap(); dict.set_item("lineno", lineno.to_pyobject(vm), vm).unwrap(); dict.set_item("tag", tag.to_pyobject(vm), vm).unwrap(); - node_add_location(&dict, range, vm, source_code); + node_add_location(&dict, range, vm, source_file); node.into() } fn ast_from_object( vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, object: PyObjectRef, ) -> PyResult { Ok(Self { @@ -67,7 +68,7 @@ impl Node for TypeIgnoreTypeIgnore { tag: get_node_field(vm, &object, "tag", "TypeIgnore")? .downcast_exact(vm) .unwrap(), - range: range_from_object(vm, source_code, object, "TypeIgnore")?, + range: range_from_object(vm, source_file, object, "TypeIgnore")?, }) } } diff --git a/vm/src/stdlib/ast/type_parameters.rs b/vm/src/stdlib/ast/type_parameters.rs index 1856f2c5b8..bb6e2724d2 100644 --- a/vm/src/stdlib/ast/type_parameters.rs +++ b/vm/src/stdlib/ast/type_parameters.rs @@ -1,16 +1,17 @@ use super::*; +use ruff_source_file::SourceFile; impl Node for ruff::TypeParams { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { - self.type_params.ast_to_object(vm, source_code) + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { + self.type_params.ast_to_object(vm, source_file) } fn ast_from_object( _vm: &VirtualMachine, - _source_code: &SourceCodeOwned, + _source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { - let type_params: Vec = Node::ast_from_object(_vm, _source_code, _object)?; + let type_params: Vec = Node::ast_from_object(_vm, _source_file, _object)?; let range = Option::zip(type_params.first(), type_params.last()) .map(|(first, last)| first.range().cover(last.range())) .unwrap_or_default(); @@ -21,38 +22,39 @@ impl Node for ruff::TypeParams { self.type_params.is_empty() } } + // sum impl Node for ruff::TypeParam { - fn ast_to_object(self, vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { match self { - Self::TypeVar(cons) => cons.ast_to_object(vm, source_code), - Self::ParamSpec(cons) => cons.ast_to_object(vm, source_code), - Self::TypeVarTuple(cons) => cons.ast_to_object(vm, source_code), + Self::TypeVar(cons) => cons.ast_to_object(vm, source_file), + Self::ParamSpec(cons) => cons.ast_to_object(vm, source_file), + Self::TypeVarTuple(cons) => cons.ast_to_object(vm, source_file), } } fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { let _cls = _object.class(); Ok(if _cls.is(pyast::NodeTypeParamTypeVar::static_type()) { Self::TypeVar(ruff::TypeParamTypeVar::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeTypeParamParamSpec::static_type()) { Self::ParamSpec(ruff::TypeParamParamSpec::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else if _cls.is(pyast::NodeTypeParamTypeVarTuple::static_type()) { Self::TypeVarTuple(ruff::TypeParamTypeVarTuple::ast_from_object( _vm, - source_code, + source_file, _object, )?) } else { @@ -63,9 +65,10 @@ impl Node for ruff::TypeParam { }) } } + // constructor impl Node for ruff::TypeParamTypeVar { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { name, bound, @@ -76,39 +79,41 @@ impl Node for ruff::TypeParamTypeVar { .into_ref_with_type(_vm, pyast::NodeTypeParamTypeVar::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("name", name.ast_to_object(_vm, source_code), _vm) + dict.set_item("name", name.ast_to_object(_vm, source_file), _vm) .unwrap(); - dict.set_item("bound", bound.ast_to_object(_vm, source_code), _vm) + dict.set_item("bound", bound.ast_to_object(_vm, source_file), _vm) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } + fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { name: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "name", "TypeVar")?, )?, bound: get_node_field_opt(_vm, &_object, "bound")? - .map(|obj| Node::ast_from_object(_vm, source_code, obj)) + .map(|obj| Node::ast_from_object(_vm, source_file, obj)) .transpose()?, default: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "default_value", "TypeVar")?, )?, - range: range_from_object(_vm, source_code, _object, "TypeVar")?, + range: range_from_object(_vm, source_file, _object, "TypeVar")?, }) } } + // constructor impl Node for ruff::TypeParamParamSpec { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { name, range: _range, @@ -118,40 +123,42 @@ impl Node for ruff::TypeParamParamSpec { .into_ref_with_type(_vm, pyast::NodeTypeParamParamSpec::static_type().to_owned()) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("name", name.ast_to_object(_vm, source_code), _vm) + dict.set_item("name", name.ast_to_object(_vm, source_file), _vm) .unwrap(); dict.set_item( "default_value", - default.ast_to_object(_vm, source_code), + default.ast_to_object(_vm, source_file), _vm, ) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } + fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { name: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "name", "ParamSpec")?, )?, default: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "default_value", "ParamSpec")?, )?, - range: range_from_object(_vm, source_code, _object, "ParamSpec")?, + range: range_from_object(_vm, source_file, _object, "ParamSpec")?, }) } } + // constructor impl Node for ruff::TypeParamTypeVarTuple { - fn ast_to_object(self, _vm: &VirtualMachine, source_code: &SourceCodeOwned) -> PyObjectRef { + fn ast_to_object(self, _vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { let Self { name, range: _range, @@ -164,34 +171,35 @@ impl Node for ruff::TypeParamTypeVarTuple { ) .unwrap(); let dict = node.as_object().dict().unwrap(); - dict.set_item("name", name.ast_to_object(_vm, source_code), _vm) + dict.set_item("name", name.ast_to_object(_vm, source_file), _vm) .unwrap(); dict.set_item( "default_value", - default.ast_to_object(_vm, source_code), + default.ast_to_object(_vm, source_file), _vm, ) .unwrap(); - node_add_location(&dict, _range, _vm, source_code); + node_add_location(&dict, _range, _vm, source_file); node.into() } + fn ast_from_object( _vm: &VirtualMachine, - source_code: &SourceCodeOwned, + source_file: &SourceFile, _object: PyObjectRef, ) -> PyResult { Ok(Self { name: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "name", "TypeVarTuple")?, )?, default: Node::ast_from_object( _vm, - source_code, + source_file, get_node_field(_vm, &_object, "default_value", "TypeVarTuple")?, )?, - range: range_from_object(_vm, source_code, _object, "TypeVarTuple")?, + range: range_from_object(_vm, source_file, _object, "TypeVarTuple")?, }) } } diff --git a/vm/src/vm/vm_new.rs b/vm/src/vm/vm_new.rs index 7673620c00..b66a34f8b1 100644 --- a/vm/src/vm/vm_new.rs +++ b/vm/src/vm/vm_new.rs @@ -286,7 +286,7 @@ impl VirtualMachine { source: Option<&str>, allow_incomplete: bool, ) -> PyBaseExceptionRef { - use crate::source::SourceLocation; + use ruff_source_file::SourceLocation; let syntax_error_type = match &error { #[cfg(feature = "parser")] From 7f2620b35d76e90164d945556adaa860c5dba25c Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Mon, 21 Jul 2025 18:57:09 +0200 Subject: [PATCH 2/5] Require `ruff_source_file` --- vm/Cargo.toml | 4 ++-- vm/src/compiler.rs | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 1592f1966f..272038e046 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -20,7 +20,7 @@ freeze-stdlib = ["encodings"] jit = ["rustpython-jit"] threading = ["rustpython-common/threading"] compiler = ["parser", "codegen", "rustpython-compiler"] -ast = ["ruff_python_ast", "ruff_text_size", "ruff_source_file"] +ast = ["ruff_python_ast", "ruff_text_size"] codegen = ["rustpython-codegen", "ast"] parser = ["ast"] serde = ["dep:serde"] @@ -36,7 +36,7 @@ rustpython-jit = { workspace = true, optional = true } ruff_python_ast = { workspace = true, optional = true } ruff_python_parser = { workspace = true } ruff_text_size = { workspace = true, optional = true } -ruff_source_file = { workspace = true, optional = true } +ruff_source_file = { workspace = true } rustpython-compiler-core = { workspace = true } rustpython-literal = { workspace = true } rustpython-sre_engine = { workspace = true } diff --git a/vm/src/compiler.rs b/vm/src/compiler.rs index 84fea452d1..25fa33302a 100644 --- a/vm/src/compiler.rs +++ b/vm/src/compiler.rs @@ -1,11 +1,9 @@ #[cfg(feature = "codegen")] pub use rustpython_codegen::CompileOpts; + #[cfg(feature = "compiler")] pub use rustpython_compiler::*; -#[cfg(not(feature = "compiler"))] -pub use rustpython_compiler_source as source; - #[cfg(not(feature = "compiler"))] pub use rustpython_compiler_core::Mode; From af382ddf1fea267538c9724e57a9f15b0e695b92 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Mon, 21 Jul 2025 19:19:31 +0200 Subject: [PATCH 3/5] Fix for windows --- compiler/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index 7e6087dd36..111b9aec2e 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -96,7 +96,10 @@ pub fn compile( // TODO: do this less hacky; ruff's parser should translate a CRLF line // break in a multiline string into just an LF in the parsed value #[cfg(windows)] - let source = &source.replace("\r\n", "\n"); + let source = source.replace("\r\n", "\n"); + #[cfg(windows)] + let source = source.as_str(); + let source_file = SourceFileBuilder::new(source_path, source).finish(); _compile(source_file, mode, opts) // let index = LineIndex::from_source_text(source); From d82ede7f9f332638d7c4627cbddc3f059ad4f960 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 22 Jul 2025 09:33:55 +0200 Subject: [PATCH 4/5] Restore `compiler::source` --- compiler/source/Cargo.toml | 16 +++++++++++++++ compiler/source/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 compiler/source/Cargo.toml create mode 100644 compiler/source/src/lib.rs diff --git a/compiler/source/Cargo.toml b/compiler/source/Cargo.toml new file mode 100644 index 0000000000..373af47cf0 --- /dev/null +++ b/compiler/source/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "rustpython-compiler-source" +description = "RustPython Source and Index" +version.workspace = true +authors.workspace = true +edition.workspace = true +rust-version.workspace = true +repository.workspace = true +license.workspace = true + +[dependencies] +ruff_source_file = { workspace = true } +ruff_text_size = { workspace = true } + +[lints] +workspace = true \ No newline at end of file diff --git a/compiler/source/src/lib.rs b/compiler/source/src/lib.rs new file mode 100644 index 0000000000..2d967e218d --- /dev/null +++ b/compiler/source/src/lib.rs @@ -0,0 +1,42 @@ +pub use ruff_source_file::{LineIndex, OneIndexed as LineNumber, SourceLocation}; +use ruff_text_size::TextRange; +pub use ruff_text_size::TextSize; + +#[derive(Clone)] +pub struct SourceCode<'src> { + pub path: &'src str, + pub text: &'src str, + pub index: LineIndex, +} + +impl<'src> SourceCode<'src> { + pub fn new(path: &'src str, text: &'src str) -> Self { + let index = LineIndex::from_source_text(text); + Self { path, text, index } + } + + pub fn line_index(&self, offset: TextSize) -> LineNumber { + self.index.line_index(offset) + } + + pub fn source_location(&self, offset: TextSize) -> SourceLocation { + self.index.source_location(offset, self.text) + } + + pub fn get_range(&'src self, range: TextRange) -> &'src str { + &self.text[range.start().to_usize()..range.end().to_usize()] + } +} + +pub struct SourceCodeOwned { + pub path: String, + pub text: String, + pub index: LineIndex, +} + +impl SourceCodeOwned { + pub fn new(path: String, text: String) -> Self { + let index = LineIndex::from_source_text(&text); + Self { path, text, index } + } +} From 776e57ea78cb2d7c3edae77e25d300ba973e8fad Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Tue, 22 Jul 2025 09:35:51 +0200 Subject: [PATCH 5/5] Deprecate `compiler::source` --- compiler/source/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/source/Cargo.toml b/compiler/source/Cargo.toml index 373af47cf0..068d31e87e 100644 --- a/compiler/source/Cargo.toml +++ b/compiler/source/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rustpython-compiler-source" -description = "RustPython Source and Index" -version.workspace = true +description = "(DEPRECATED) RustPython Source and Index" +version = "0.5.0+deprecated" authors.workspace = true edition.workspace = true rust-version.workspace = true @@ -13,4 +13,4 @@ ruff_source_file = { workspace = true } ruff_text_size = { workspace = true } [lints] -workspace = true \ No newline at end of file +workspace = true