Skip to content

Export ruff_source_file types in rustpython_compiler_core #6020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion compiler/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
12 changes: 5 additions & 7 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
};
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion compiler/codegen/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_source_file::SourceLocation;
use rustpython_compiler_core::SourceLocation;
use std::fmt::{self, Display};
use thiserror::Error;

Expand Down
20 changes: 15 additions & 5 deletions compiler/codegen/src/ir.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,23 +36,29 @@ impl BlockIdx {
self.0 as usize
}
}

impl ops::Index<BlockIdx> for [Block] {
type Output = Block;

fn index(&self, idx: BlockIdx) -> &Block {
&self[idx.idx()]
}
}

impl ops::IndexMut<BlockIdx> for [Block] {
fn index_mut(&mut self, idx: BlockIdx) -> &mut Block {
&mut self[idx.idx()]
}
}

impl ops::Index<BlockIdx> for Vec<Block> {
type Output = Block;

fn index(&self, idx: BlockIdx) -> &Block {
&self[idx.idx()]
}
}

impl ops::IndexMut<BlockIdx> for Vec<Block> {
fn index_mut(&mut self, idx: BlockIdx) -> &mut Block {
&mut self[idx.idx()]
Expand All @@ -74,6 +82,7 @@ pub struct Block {
pub instructions: Vec<InstructionInfo>,
pub next: BlockIdx,
}

impl Default for Block {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -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<CodeObject> {
if optimize > 0 {
Expand Down
4 changes: 1 addition & 3 deletions compiler/codegen/src/symboltable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 5 additions & 7 deletions compiler/codegen/src/unparse.rs
Original file line number Diff line number Diff line change
@@ -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,)*) => {
Expand Down
5 changes: 2 additions & 3 deletions compiler/core/src/bytecode.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 2 additions & 0 deletions compiler/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ pub mod marshal;
mod mode;

pub use mode::Mode;

pub use ruff_source_file::{LineIndex, OneIndexed, SourceFile, SourceFileBuilder, SourceLocation};
3 changes: 1 addition & 2 deletions compiler/core/src/marshal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::bytecode::*;
use crate::{OneIndexed, SourceLocation, bytecode::*};
use malachite_bigint::{BigInt, Sign};
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

❓ Verification inconclusive

Verify root-level re-export of OneIndexed / SourceLocation

marshal.rs now pulls these types from crate, so rustpython_compiler_core::lib.rs must pub use them.
Please confirm with a quick search:


🏁 Script executed:

#!/bin/bash
# Ensure the symbols are publicly exposed at crate root
rg -n $'^\\s*pub\\s+use\\s+ruff_source_file::(OneIndexed|SourceLocation)' compiler/core/src/lib.rs

Length of output: 96


Add missing re-exports of OneIndexed and SourceLocation at crate root

The import in marshal.rs (use crate::{OneIndexed, SourceLocation, …};) will fail because these types aren’t publicly re-exported from the crate root.

• File needing update:

  • compiler/core/src/lib.rs – add the following alongside the other pub use statements.
diff --git a/compiler/core/src/lib.rs b/compiler/core/src/lib.rs
index abc1234..def5678 100644
--- a/compiler/core/src/lib.rs
+++ b/compiler/core/src/lib.rs
@@
 pub use bytecode::*;
+pub use ruff_source_file::{OneIndexed, SourceLocation};
🤖 Prompt for AI Agents
In compiler/core/src/lib.rs near the top where other pub use statements are, add
public re-exports for OneIndexed and SourceLocation to make them accessible from
the crate root. This will fix the import failure in marshal.rs by allowing these
types to be imported publicly from the crate.

use num_complex::Complex64;
use ruff_source_file::{OneIndexed, SourceLocation};
use rustpython_wtf8::Wtf8;
use std::convert::Infallible;

Expand Down
1 change: 0 additions & 1 deletion vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/traceback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
7 changes: 7 additions & 0 deletions vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -81,6 +87,7 @@ pub mod utils;
pub mod version;
pub mod vm;
pub mod warn;

#[cfg(windows)]
pub mod windows;

Expand Down
4 changes: 3 additions & 1 deletion vm/src/stdlib/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/argument.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use ruff_source_file::SourceFile;
use rustpython_compiler_core::SourceFile;

pub(super) struct PositionalArguments {
pub range: TextRange,
Expand Down
5 changes: 2 additions & 3 deletions vm/src/stdlib/ast/basic.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/constant.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/elif_else_clause.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/exception.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use ruff_source_file::SourceFile;
use rustpython_compiler_core::SourceFile;

// sum
impl Node for ruff::ExceptHandler {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/module.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/node.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/operator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use ruff_source_file::SourceFile;
use rustpython_compiler_core::SourceFile;

// sum
impl Node for ruff::BoolOp {
Expand Down
3 changes: 1 addition & 2 deletions vm/src/stdlib/ast/other.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/parameter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use ruff_source_file::SourceFile;
use rustpython_compiler_core::SourceFile;

// product
impl Node for ruff::Parameters {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/pattern.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use ruff_source_file::SourceFile;
use rustpython_compiler_core::SourceFile;

// product
impl Node for ruff::MatchCase {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/statement.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/ast/type_ignore.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use ruff_source_file::SourceFile;
use rustpython_compiler_core::SourceFile;

pub(super) enum TypeIgnore {
TypeIgnore(TypeIgnoreTypeIgnore),
Expand Down
Loading
Loading