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
Prev Previous commit
Next Next commit
Fix little of needless_pass_by_value
  • Loading branch information
ShaharNaveh committed May 14, 2026
commit f33bfcc89768fba01f492c7949a0cb2de70119fa
43 changes: 22 additions & 21 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub fn compile_program(
) -> CompileResult<CodeObject> {
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, "<module>".to_owned());
let mut compiler = Compiler::new(opts, source_file, "<module>");
compiler.compile_program(ast, symbol_table)?;
let code = compiler.exit_scope();
trace!("Compilation completed: {code:?}");
Expand All @@ -304,7 +304,7 @@ pub fn compile_program_single(
) -> CompileResult<CodeObject> {
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, "<module>".to_owned());
let mut compiler = Compiler::new(opts, source_file, "<module>");
compiler.compile_program_single(&ast.body, symbol_table)?;
let code = compiler.exit_scope();
trace!("Compilation completed: {code:?}");
Expand All @@ -318,7 +318,7 @@ pub fn compile_block_expression(
) -> CompileResult<CodeObject> {
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, "<module>".to_owned());
let mut compiler = Compiler::new(opts, source_file, "<module>");
compiler.compile_block_expr(&ast.body, symbol_table)?;
let code = compiler.exit_scope();
trace!("Compilation completed: {code:?}");
Expand All @@ -332,7 +332,7 @@ pub fn compile_expression(
) -> CompileResult<CodeObject> {
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, "<module>".to_owned());
let mut compiler = Compiler::new(opts, source_file, "<module>");
compiler.compile_eval(ast, symbol_table)?;
let code = compiler.exit_scope();
Ok(code)
Expand Down Expand Up @@ -447,13 +447,14 @@ impl PatternContext {
}
}

#[derive(Clone, Copy, Eq, PartialEq)]
enum JumpOp {
Jump,
PopJumpIfFalse,
}

/// Type of collection to build in starunpack_helper
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum CollectionType {
Tuple,
List,
Expand Down Expand Up @@ -541,7 +542,7 @@ impl Compiler {
}
}

fn new(opts: CompileOpts, source_file: SourceFile, code_name: String) -> Self {
fn new(opts: CompileOpts, source_file: SourceFile, code_name: &str) -> Self {
let module_code = ir::CodeInfo {
// CPython convention: top-level module / interactive /
// expression code does not carry CO_NEWLOCALS or CO_OPTIMIZED.
Expand All @@ -558,8 +559,8 @@ impl Compiler {
current_block: BlockIdx::new(0),
annotations_blocks: None,
metadata: ir::CodeUnitMetadata {
name: code_name.clone(),
qualname: Some(code_name),
name: code_name.to_string(),
qualname: Some(code_name.to_string()),
consts: IndexSet::default(),
names: IndexSet::default(),
varnames: IndexSet::default(),
Expand Down Expand Up @@ -1828,7 +1829,7 @@ impl Compiler {
posonlyarg_count: u32,
arg_count: u32,
kwonlyarg_count: u32,
obj_name: String,
obj_name: &str,
) -> CompileResult<()> {
// First push the symbol table
let table = self.push_symbol_table()?;
Expand All @@ -1841,7 +1842,7 @@ impl Compiler {
let lineno = self.get_source_line_number().get();

// Call enter_scope which does most of the work
self.enter_scope(&obj_name, scope_type, key, lineno.to_u32())?;
self.enter_scope(obj_name, scope_type, key, lineno.to_u32())?;

// Override the values that push_output sets explicitly
// enter_scope sets default values based on scope_type, but push_output
Expand Down Expand Up @@ -3399,7 +3400,7 @@ impl Compiler {
parameters.posonlyargs.len().to_u32(),
(parameters.posonlyargs.len() + parameters.args.len()).to_u32(),
parameters.kwonlyargs.len().to_u32(),
name.to_owned(),
name,
)?;

let args_iter = core::iter::empty()
Expand Down Expand Up @@ -5257,7 +5258,7 @@ impl Compiler {
0,
num_typeparam_args as u32,
0,
type_params_name,
&type_params_name,
)?;

// TypeParams scope is function-like
Expand Down Expand Up @@ -5819,7 +5820,7 @@ impl Compiler {
0,
0,
0,
type_params_name,
&type_params_name,
)?;

// Set private name for name mangling
Expand Down Expand Up @@ -9712,14 +9713,14 @@ impl Compiler {
posonlyarg_count: u32,
arg_count: u32,
kwonlyarg_count: u32,
obj_name: String,
obj_name: &str,
) -> CompileResult<()> {
let scope_type = table.typ;
self.symbol_table_stack.push(table);

let key = self.symbol_table_stack.len() - 1;
let lineno = self.get_source_line_number().get();
self.enter_scope(&obj_name, scope_type, key, lineno.to_u32())?;
self.enter_scope(obj_name, scope_type, key, lineno.to_u32())?;

if let Some(info) = self.code_stack.last_mut() {
info.flags = flags | (info.flags & bytecode::CodeFlags::NESTED);
Expand Down Expand Up @@ -9783,7 +9784,7 @@ impl Compiler {
// and relies on the inlined path itself to handle GET_AITER /
// async-comprehension cleanup.
return self.compile_inlined_comprehension(
comp_table,
&comp_table,
init_collection,
generators,
compile_element,
Expand Down Expand Up @@ -9818,7 +9819,7 @@ impl Compiler {
// scope itself. Peek past those nested scopes so we can enter the
// correct comprehension table here, then let the real outermost
// iterator compile consume its nested scopes later in parent scope.
self.push_output_with_symbol_table(comp_table, flags, 1, 1, 0, name.to_owned())?;
self.push_output_with_symbol_table(comp_table, flags, 1, 1, 0, name)?;

// Set qualname for comprehension
self.set_qualname();
Expand Down Expand Up @@ -10025,7 +10026,7 @@ impl Compiler {
/// This generates bytecode inline without creating a new code object
fn compile_inlined_comprehension(
&mut self,
comp_table: SymbolTable,
comp_table: &SymbolTable,
init_collection: Option<AnyInstruction>,
generators: &[ast::Comprehension],
compile_element: &dyn Fn(&mut Self, usize) -> CompileResult<()>,
Expand Down Expand Up @@ -12377,7 +12378,7 @@ mod tests {
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_file, "<module>".to_owned());
let mut compiler = Compiler::new(opts, source_file, "<module>");
compiler.compile_program(&ast, symbol_table).unwrap();
compiler.exit_scope()
}
Expand Down Expand Up @@ -12415,7 +12416,7 @@ mod tests {
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_file, "<module>".to_owned());
let mut compiler = Compiler::new(opts, source_file, "<module>");
compiler.compile_program(&ast, symbol_table).unwrap();
let _table = compiler.pop_symbol_table();
let stack_top = compiler.code_stack.pop().unwrap();
Expand Down Expand Up @@ -12456,7 +12457,7 @@ mod tests {
let is_async = function.is_async;
let range = function.range();

let mut compiler = Compiler::new(opts, source_file, "<module>".to_owned());
let mut compiler = Compiler::new(opts, source_file, "<module>");
compiler.future_annotations = symbol_table.future_annotations;
compiler.symbol_table_stack.push(symbol_table);
compiler.set_source_range(range);
Expand Down
47 changes: 22 additions & 25 deletions crates/derive-impl/src/compile_bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub trait Compiler {
&self,
source: &str,
mode: Mode,
module_name: String,
module_name: &str,
) -> Result<CodeObject, Box<dyn core::error::Error>>;
}

Expand All @@ -61,7 +61,7 @@ impl CompilationSource {
&self,
source: &str,
mode: Mode,
module_name: String,
module_name: &str,
compiler: &dyn Compiler,
origin: F,
) -> Result<CodeObject, Diagnostic> {
Expand All @@ -76,15 +76,15 @@ impl CompilationSource {
fn compile(
&self,
mode: Mode,
module_name: String,
module_name: &str,
compiler: &dyn Compiler,
) -> Result<HashMap<String, CompiledModule>, Diagnostic> {
match &self.kind {
CompilationSourceKind::Dir { base, rel_path } => {
self.compile_dir(base, &base.join(rel_path), String::new(), mode, compiler)
self.compile_dir(base, &base.join(rel_path), "", mode, compiler)
}
_ => Ok(hashmap! {
module_name.clone() => CompiledModule {
module_name.to_string() => CompiledModule {
code: self.compile_single(mode, module_name, compiler)?,
package: false,
},
Expand All @@ -95,7 +95,7 @@ impl CompilationSource {
fn compile_single(
&self,
mode: Mode,
module_name: String,
module_name: &str,
compiler: &dyn Compiler,
) -> Result<CodeObject, Diagnostic> {
match &self.kind {
Expand Down Expand Up @@ -126,7 +126,7 @@ impl CompilationSource {
&self,
base: &Path,
path: &Path,
parent: String,
parent: &str,
mode: Mode,
compiler: &dyn Compiler,
) -> Result<HashMap<String, CompiledModule>, Diagnostic> {
Expand All @@ -152,26 +152,21 @@ impl CompilationSource {
Diagnostic::spans_error(self.span, format!("Invalid UTF-8 in file name {path:?}"))
})?;
if path.is_dir() {
code_map.extend(self.compile_dir(
base,
&path,
if parent.is_empty() {
file_name.to_string()
} else {
format!("{parent}.{file_name}")
},
mode,
compiler,
)?);
let nparent = if parent.is_empty() {
file_name.to_string()
} else {
format!("{parent}.{file_name}")
};
code_map.extend(self.compile_dir(base, &path, &nparent, mode, compiler)?);
} else if file_name.ends_with(".py") {
let stem = path.file_stem().unwrap().to_str().unwrap();
let is_init = stem == "__init__";
let module_name = if is_init {
parent.clone()
parent
} else if parent.is_empty() {
stem.to_owned()
stem
} else {
format!("{parent}.{stem}")
&format!("{parent}.{stem}")
};

let compile_path = |src_path: &Path| {
Expand All @@ -181,7 +176,7 @@ impl CompilationSource {
format!("Error reading file {path:?}: {err}"),
)
})?;
self.compile_string(&source, mode, module_name.clone(), compiler, || {
self.compile_string(&source, mode, module_name, compiler, || {
path.strip_prefix(base).ok().unwrap_or(&path).display()
})
};
Expand Down Expand Up @@ -211,7 +206,7 @@ impl CompilationSource {
};

code_map.insert(
module_name,
module_name.to_string(),
CompiledModule {
code,
package: is_init,
Expand Down Expand Up @@ -343,7 +338,7 @@ pub(crate) fn impl_py_compile(
let crate_name = args.crate_name;
let code = args
.source
.compile_single(args.mode, args.module_name, compiler)?;
.compile_single(args.mode, &args.module_name, compiler)?;

let frozen = frozen::FrozenCodeObject::encode(&code);
let bytes = LitByteStr::new(&frozen.bytes, Span::call_site());
Expand All @@ -362,7 +357,9 @@ pub(crate) fn impl_py_freeze(
let args = PyCompileArgs::parse(input, true)?;

let crate_name = args.crate_name;
let code_map = args.source.compile(args.mode, args.module_name, compiler)?;
let code_map = args
.source
.compile(args.mode, &args.module_name, compiler)?;

let data = frozen::FrozenLib::encode(code_map.iter().map(|(k, v)| {
let v = frozen::FrozenModule {
Expand Down
2 changes: 1 addition & 1 deletion crates/derive-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn pyexception(attr: PunctuatedNestedMeta, item: Item) -> TokenStream {
if matches!(item, syn::Item::Impl(_)) {
pyclass::impl_pyexception_impl(attr, item)
} else {
result_to_tokens(pyclass::impl_pyexception(attr, item))
result_to_tokens(pyclass::impl_pyexception(attr, &item))
}
}

Expand Down
Loading
Loading