diff --git a/Cargo.lock b/Cargo.lock index d9ef9c78b3c..5f240f42618 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2275,7 +2275,6 @@ dependencies = [ "num-traits", "ruff_python_ast", "ruff_python_parser", - "ruff_source_file", "ruff_text_size", "rustpython-compiler-core", "rustpython-literal", @@ -2544,7 +2543,6 @@ dependencies = [ "result-like", "ruff_python_ast", "ruff_python_parser", - "ruff_source_file", "ruff_text_size", "rustix", "rustpython-codegen", diff --git a/compiler/codegen/Cargo.toml b/compiler/codegen/Cargo.toml index 6093e522e43..ce7e8d74f59 100644 --- a/compiler/codegen/Cargo.toml +++ b/compiler/codegen/Cargo.toml @@ -14,7 +14,6 @@ rustpython-literal = {workspace = true } rustpython-wtf8 = { workspace = true } ruff_python_ast = { workspace = true } ruff_text_size = { workspace = true } -ruff_source_file = { workspace = true } ahash = { workspace = true } bitflags = { workspace = true } diff --git a/compiler/codegen/src/compile.rs b/compiler/codegen/src/compile.rs index 31199278517..7a85c1272b0 100644 --- a/compiler/codegen/src/compile.rs +++ b/compiler/codegen/src/compile.rs @@ -30,17 +30,15 @@ use ruff_python_ast::{ 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, + Mode, OneIndexed, SourceFile, SourceLocation, bytecode::{ self, Arg as OpArgMarker, BinaryOperator, CodeObject, ComparisonOperator, ConstantData, Instruction, OpArg, OpArgType, UnpackExArgs, }, }; +use rustpython_wtf8::Wtf8Buf; use std::borrow::Cow; const MAXBLOCKS: usize = 20; @@ -631,7 +629,7 @@ impl Compiler { lineno: u32, ) -> CompileResult<()> { // Create location - let location = ruff_source_file::SourceLocation { + let location = SourceLocation { row: OneIndexed::new(lineno as usize).unwrap_or(OneIndexed::MIN), column: OneIndexed::new(1).unwrap(), }; @@ -769,7 +767,7 @@ impl Compiler { // Emit RESUME instruction let _resume_loc = if scope_type == CompilerScope::Module { // Module scope starts with lineno 0 - ruff_source_file::SourceLocation { + SourceLocation { row: OneIndexed::MIN, column: OneIndexed::MIN, } @@ -5842,7 +5840,7 @@ mod ruff_tests { #[cfg(test)] mod tests { use super::*; - use ruff_source_file::SourceFileBuilder; + use rustpython_compiler_core::SourceFileBuilder; fn compile_exec(source: &str) -> CodeObject { let opts = CompileOpts::default(); diff --git a/compiler/codegen/src/error.rs b/compiler/codegen/src/error.rs index 5e0ac12934b..a0e36bf29ae 100644 --- a/compiler/codegen/src/error.rs +++ b/compiler/codegen/src/error.rs @@ -1,4 +1,4 @@ -use ruff_source_file::SourceLocation; +use rustpython_compiler_core::SourceLocation; use std::fmt::{self, Display}; use thiserror::Error; diff --git a/compiler/codegen/src/ir.rs b/compiler/codegen/src/ir.rs index ae4eef9cc96..1cc59dd656a 100644 --- a/compiler/codegen/src/ir.rs +++ b/compiler/codegen/src/ir.rs @@ -1,10 +1,12 @@ use std::ops; -use crate::error::InternalError; -use crate::{IndexMap, IndexSet}; -use ruff_source_file::{OneIndexed, SourceLocation}; -use rustpython_compiler_core::bytecode::{ - CodeFlags, CodeObject, CodeUnit, ConstantData, InstrDisplayContext, Instruction, Label, OpArg, +use crate::{IndexMap, IndexSet, error::InternalError}; +use rustpython_compiler_core::{ + OneIndexed, SourceLocation, + bytecode::{ + CodeFlags, CodeObject, CodeUnit, ConstantData, InstrDisplayContext, Instruction, Label, + OpArg, + }, }; /// Metadata for a code unit @@ -34,23 +36,29 @@ impl BlockIdx { self.0 as usize } } + impl ops::Index for [Block] { type Output = Block; + fn index(&self, idx: BlockIdx) -> &Block { &self[idx.idx()] } } + impl ops::IndexMut for [Block] { fn index_mut(&mut self, idx: BlockIdx) -> &mut Block { &mut self[idx.idx()] } } + impl ops::Index for Vec { type Output = Block; + fn index(&self, idx: BlockIdx) -> &Block { &self[idx.idx()] } } + impl ops::IndexMut for Vec { fn index_mut(&mut self, idx: BlockIdx) -> &mut Block { &mut self[idx.idx()] @@ -74,6 +82,7 @@ pub struct Block { pub instructions: Vec, pub next: BlockIdx, } + impl Default for Block { fn default() -> Self { Self { @@ -105,6 +114,7 @@ pub struct CodeInfo { // Reference to the symbol table for this scope pub symbol_table_index: usize, } + impl CodeInfo { pub fn finalize_code(mut self, optimize: u8) -> crate::InternalResult { if optimize > 0 { diff --git a/compiler/codegen/src/symboltable.rs b/compiler/codegen/src/symboltable.rs index ccf9aa4f310..7bed678aaf5 100644 --- a/compiler/codegen/src/symboltable.rs +++ b/compiler/codegen/src/symboltable.rs @@ -18,10 +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_ast::{self as ast, located::Located}; -// use rustpython_parser_core::source_code::{LineNumber, SourceLocation}; +use rustpython_compiler_core::{SourceFile, SourceLocation}; use std::{borrow::Cow, fmt}; /// Captures all symbols in the current scope, and has a list of sub-scopes in this scope. diff --git a/compiler/codegen/src/unparse.rs b/compiler/codegen/src/unparse.rs index 81bd4dfe2d6..afabb15b217 100644 --- a/compiler/codegen/src/unparse.rs +++ b/compiler/codegen/src/unparse.rs @@ -1,14 +1,12 @@ -use ruff_python_ast as ruff; -use ruff_source_file::SourceFile; +use ruff_python_ast::{ + self as ruff, Arguments, BoolOp, Comprehension, ConversionFlag, Expr, Identifier, Operator, + Parameter, ParameterWithDefault, Parameters, +}; use ruff_text_size::Ranged; +use rustpython_compiler_core::SourceFile; use rustpython_literal::escape::{AsciiEscape, UnicodeEscape}; use std::fmt::{self, Display as _}; -use ruff::{ - Arguments, BoolOp, Comprehension, ConversionFlag, Expr, Identifier, Operator, Parameter, - ParameterWithDefault, Parameters, -}; - mod precedence { macro_rules! precedence { ($($op:ident,)*) => { diff --git a/compiler/core/src/bytecode.rs b/compiler/core/src/bytecode.rs index 6e52dd7d548..d22ccd697fc 100644 --- a/compiler/core/src/bytecode.rs +++ b/compiler/core/src/bytecode.rs @@ -1,14 +1,13 @@ //! Implement python as a virtual machine with bytecode. This module //! implements bytecode structure. +use crate::{OneIndexed, SourceLocation}; use bitflags::bitflags; use itertools::Itertools; use malachite_bigint::BigInt; use num_complex::Complex64; -use ruff_source_file::{OneIndexed, SourceLocation}; use rustpython_wtf8::{Wtf8, Wtf8Buf}; -use std::marker::PhantomData; -use std::{collections::BTreeSet, fmt, hash, mem}; +use std::{collections::BTreeSet, fmt, hash, marker::PhantomData, mem}; #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] #[repr(i8)] diff --git a/compiler/core/src/lib.rs b/compiler/core/src/lib.rs index 75fde66684e..0ce4a9defb1 100644 --- a/compiler/core/src/lib.rs +++ b/compiler/core/src/lib.rs @@ -7,3 +7,5 @@ pub mod marshal; mod mode; pub use mode::Mode; + +pub use ruff_source_file::{LineIndex, OneIndexed, SourceFile, SourceFileBuilder, SourceLocation}; diff --git a/compiler/core/src/marshal.rs b/compiler/core/src/marshal.rs index fdbae7ec30b..c3b482631d0 100644 --- a/compiler/core/src/marshal.rs +++ b/compiler/core/src/marshal.rs @@ -1,7 +1,6 @@ -use crate::bytecode::*; +use crate::{OneIndexed, SourceLocation, bytecode::*}; use malachite_bigint::{BigInt, Sign}; use num_complex::Complex64; -use ruff_source_file::{OneIndexed, SourceLocation}; use rustpython_wtf8::Wtf8; use std::convert::Infallible; diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 272038e046c..3920c015009 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -36,7 +36,6 @@ 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 } rustpython-compiler-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 3c182b936c7..43b6380d73e 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -15,7 +15,7 @@ use crate::{ }; use malachite_bigint::BigInt; use num_traits::Zero; -use ruff_source_file::OneIndexed; +use rustpython_compiler_core::OneIndexed; use std::{borrow::Borrow, fmt, ops::Deref}; #[derive(FromArgs)] diff --git a/vm/src/builtins/traceback.rs b/vm/src/builtins/traceback.rs index 713dc17ba57..683bbf16548 100644 --- a/vm/src/builtins/traceback.rs +++ b/vm/src/builtins/traceback.rs @@ -3,8 +3,8 @@ use crate::{ Context, Py, PyPayload, PyRef, PyResult, VirtualMachine, class::PyClassImpl, frame::FrameRef, types::Constructor, }; -use ruff_source_file::OneIndexed; use rustpython_common::lock::PyMutex; +use rustpython_compiler_core::OneIndexed; #[pyclass(module = false, name = "traceback", traverse)] #[derive(Debug)] diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 8fec94e537d..b3954b6f44c 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -21,8 +21,8 @@ use crate::{ }; use indexmap::IndexMap; use itertools::Itertools; -use ruff_source_file::SourceLocation; use rustpython_common::wtf8::Wtf8Buf; +use rustpython_compiler_core::SourceLocation; #[cfg(feature = "threading")] use std::sync::atomic; use std::{fmt, iter::zip}; diff --git a/vm/src/lib.rs b/vm/src/lib.rs index b6656a8f520..94147345a6b 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -51,8 +51,10 @@ pub mod compiler; pub mod convert; mod coroutine; mod dict_inner; + #[cfg(feature = "rustpython-compiler")] pub mod eval; + pub mod exceptions; pub mod format; pub mod frame; @@ -61,13 +63,17 @@ pub mod import; mod intern; pub mod iter; pub mod object; + #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] pub mod ospath; + pub mod prelude; pub mod protocol; pub mod py_io; + #[cfg(feature = "serde")] pub mod py_serde; + pub mod readline; pub mod recursion; pub mod scope; @@ -81,6 +87,7 @@ pub mod utils; pub mod version; pub mod vm; pub mod warn; + #[cfg(windows)] pub mod windows; diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index 5b07c35771a..1533f903c5c 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -21,8 +21,10 @@ use crate::{ }; use node::Node; use ruff_python_ast as ruff; -use ruff_source_file::{LineIndex, OneIndexed, SourceFile, SourceFileBuilder, SourceLocation}; use ruff_text_size::{Ranged, TextRange, TextSize}; +use rustpython_compiler_core::{ + LineIndex, OneIndexed, SourceFile, SourceFileBuilder, SourceLocation, +}; #[cfg(feature = "parser")] use ruff_python_parser as parser; diff --git a/vm/src/stdlib/ast/argument.rs b/vm/src/stdlib/ast/argument.rs index e95199de3ad..a1207f2c054 100644 --- a/vm/src/stdlib/ast/argument.rs +++ b/vm/src/stdlib/ast/argument.rs @@ -1,5 +1,5 @@ use super::*; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; pub(super) struct PositionalArguments { pub range: TextRange, diff --git a/vm/src/stdlib/ast/basic.rs b/vm/src/stdlib/ast/basic.rs index 86011040b05..d8565029d6c 100644 --- a/vm/src/stdlib/ast/basic.rs +++ b/vm/src/stdlib/ast/basic.rs @@ -1,7 +1,6 @@ -use ruff_source_file::SourceFile; -use rustpython_codegen::compile::ruff_int_to_bigint; - use super::*; +use rustpython_codegen::compile::ruff_int_to_bigint; +use rustpython_compiler_core::SourceFile; impl Node for ruff::Identifier { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { diff --git a/vm/src/stdlib/ast/constant.rs b/vm/src/stdlib/ast/constant.rs index 89f4ec17e5e..b6c3e9596ba 100644 --- a/vm/src/stdlib/ast/constant.rs +++ b/vm/src/stdlib/ast/constant.rs @@ -1,7 +1,7 @@ use super::*; use crate::builtins::{PyComplex, PyFrozenSet, PyTuple}; use ruff::str_prefix::StringLiteralPrefix; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; #[derive(Debug)] pub(super) struct Constant { diff --git a/vm/src/stdlib/ast/elif_else_clause.rs b/vm/src/stdlib/ast/elif_else_clause.rs index a1e49ee5250..2b427b1ec13 100644 --- a/vm/src/stdlib/ast/elif_else_clause.rs +++ b/vm/src/stdlib/ast/elif_else_clause.rs @@ -1,5 +1,5 @@ use super::*; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; pub(super) fn ast_to_object( clause: ruff::ElifElseClause, diff --git a/vm/src/stdlib/ast/exception.rs b/vm/src/stdlib/ast/exception.rs index 446f7d84003..df6b391aa1d 100644 --- a/vm/src/stdlib/ast/exception.rs +++ b/vm/src/stdlib/ast/exception.rs @@ -1,5 +1,5 @@ use super::*; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; // sum impl Node for ruff::ExceptHandler { diff --git a/vm/src/stdlib/ast/expression.rs b/vm/src/stdlib/ast/expression.rs index 3b187fb4131..31b65bd13a9 100644 --- a/vm/src/stdlib/ast/expression.rs +++ b/vm/src/stdlib/ast/expression.rs @@ -4,7 +4,7 @@ use crate::stdlib::ast::{ constant::Constant, string::JoinedStr, }; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; // sum impl Node for ruff::Expr { diff --git a/vm/src/stdlib/ast/module.rs b/vm/src/stdlib/ast/module.rs index 4a3bc4fd56b..33ee4c567b1 100644 --- a/vm/src/stdlib/ast/module.rs +++ b/vm/src/stdlib/ast/module.rs @@ -1,6 +1,6 @@ use super::*; use crate::stdlib::ast::type_ignore::TypeIgnore; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; /// Represents the different types of Python module structures. /// diff --git a/vm/src/stdlib/ast/node.rs b/vm/src/stdlib/ast/node.rs index c688a95a728..6f74e71511f 100644 --- a/vm/src/stdlib/ast/node.rs +++ b/vm/src/stdlib/ast/node.rs @@ -1,5 +1,5 @@ use crate::{PyObjectRef, PyResult, VirtualMachine}; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; pub(crate) trait Node: Sized { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef; diff --git a/vm/src/stdlib/ast/operator.rs b/vm/src/stdlib/ast/operator.rs index 2ba03b48357..c394152da2c 100644 --- a/vm/src/stdlib/ast/operator.rs +++ b/vm/src/stdlib/ast/operator.rs @@ -1,5 +1,5 @@ use super::*; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; // sum impl Node for ruff::BoolOp { diff --git a/vm/src/stdlib/ast/other.rs b/vm/src/stdlib/ast/other.rs index 476f5b7c315..eddbef97482 100644 --- a/vm/src/stdlib/ast/other.rs +++ b/vm/src/stdlib/ast/other.rs @@ -1,7 +1,6 @@ use super::*; use num_traits::ToPrimitive; -use ruff_source_file::SourceFile; -use rustpython_compiler_core::bytecode; +use rustpython_compiler_core::{SourceFile, bytecode}; impl Node for ruff::ConversionFlag { fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef { diff --git a/vm/src/stdlib/ast/parameter.rs b/vm/src/stdlib/ast/parameter.rs index 8131c819ac5..347b3b4ec0c 100644 --- a/vm/src/stdlib/ast/parameter.rs +++ b/vm/src/stdlib/ast/parameter.rs @@ -1,5 +1,5 @@ use super::*; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; // product impl Node for ruff::Parameters { diff --git a/vm/src/stdlib/ast/pattern.rs b/vm/src/stdlib/ast/pattern.rs index af14bb609d1..0c484d6787a 100644 --- a/vm/src/stdlib/ast/pattern.rs +++ b/vm/src/stdlib/ast/pattern.rs @@ -1,5 +1,5 @@ use super::*; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; // product impl Node for ruff::MatchCase { diff --git a/vm/src/stdlib/ast/statement.rs b/vm/src/stdlib/ast/statement.rs index 1ac07c40349..fab7ea76961 100644 --- a/vm/src/stdlib/ast/statement.rs +++ b/vm/src/stdlib/ast/statement.rs @@ -1,6 +1,6 @@ use super::*; use crate::stdlib::ast::argument::{merge_class_def_args, split_class_def_args}; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; // sum impl Node for ruff::Stmt { diff --git a/vm/src/stdlib/ast/type_ignore.rs b/vm/src/stdlib/ast/type_ignore.rs index de122d013ce..de929fcf623 100644 --- a/vm/src/stdlib/ast/type_ignore.rs +++ b/vm/src/stdlib/ast/type_ignore.rs @@ -1,5 +1,5 @@ use super::*; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; pub(super) enum TypeIgnore { TypeIgnore(TypeIgnoreTypeIgnore), diff --git a/vm/src/stdlib/ast/type_parameters.rs b/vm/src/stdlib/ast/type_parameters.rs index bb6e2724d2d..505cd04d284 100644 --- a/vm/src/stdlib/ast/type_parameters.rs +++ b/vm/src/stdlib/ast/type_parameters.rs @@ -1,5 +1,5 @@ use super::*; -use ruff_source_file::SourceFile; +use rustpython_compiler_core::SourceFile; impl Node for ruff::TypeParams { fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef { diff --git a/vm/src/vm/vm_new.rs b/vm/src/vm/vm_new.rs index b66a34f8b12..671f5df34d5 100644 --- a/vm/src/vm/vm_new.rs +++ b/vm/src/vm/vm_new.rs @@ -12,6 +12,7 @@ use crate::{ scope::Scope, vm::VirtualMachine, }; +use rustpython_compiler_core::SourceLocation; macro_rules! define_exception_fn { ( @@ -286,8 +287,6 @@ impl VirtualMachine { source: Option<&str>, allow_incomplete: bool, ) -> PyBaseExceptionRef { - use ruff_source_file::SourceLocation; - let syntax_error_type = match &error { #[cfg(feature = "parser")] // FIXME: this condition will cause TabError even when the matching actual error is IndentationError