Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
export ruff types in compiler::core
  • Loading branch information
ShaharNaveh committed Jul 22, 2025
commit 2109e8bebe458a9dad409ff3ed22ec7eb5f4d268
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::*};
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 malachite_bigint::{BigInt, Sign};
use num_complex::Complex64;
use ruff_source_file::{OneIndexed, SourceLocation};
use rustpython_wtf8::Wtf8;
use std::convert::Infallible;

Expand Down