Skip to content
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: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,6 @@ inconsistent_struct_constructor = "warn"
manual_is_variant_and = "warn"
map_unwrap_or = "warn"
must_use_candidate = "warn"
redundant_else = "warn"
uninlined_format_args = "warn"
unnecessary_wraps = "warn"
66 changes: 32 additions & 34 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 @@ -1287,8 +1288,7 @@ impl Compiler {
let name = current_table.name.clone();
let typ = current_table.typ;
return Err(self.error(CodegenErrorType::SyntaxError(format!(
"no symbol table available in {} (type: {:?})",
name, typ
"no symbol table available in {name} (type: {typ:?})"
))));
}

Expand Down Expand Up @@ -1829,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 @@ -1842,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 @@ -3400,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 @@ -5258,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 @@ -5820,7 +5820,7 @@ impl Compiler {
0,
0,
0,
type_params_name,
&type_params_name,
)?;

// Set private name for name mangling
Expand Down Expand Up @@ -7956,21 +7956,20 @@ impl Compiler {
if let ast::Expr::Starred(_) = &element {
if seen_star {
return Err(self.error(CodegenErrorType::MultipleStarArgs));
} else {
seen_star = true;
let before = i;
let after = elts.len() - i - 1;
let (before, after) = (|| Some((before.to_u8()?, after.to_u8()?)))(
)
}

seen_star = true;
let before = i;
let after = elts.len() - i - 1;
let (before, after) = (|| Some((before.to_u8()?, after.to_u8()?)))()
.ok_or_else(|| {
self.error_ranged(
CodegenErrorType::TooManyStarUnpack,
target.range(),
)
})?;
let counts = bytecode::UnpackExArgs { before, after };
emit!(self, Instruction::UnpackEx { counts });
}
let counts = bytecode::UnpackExArgs { before, after };
emit!(self, Instruction::UnpackEx { counts });
}
}

Expand Down Expand Up @@ -9696,8 +9695,7 @@ impl Compiler {
let name = current_table.name.clone();
let typ = current_table.typ;
Err(self.error(CodegenErrorType::SyntaxError(format!(
"no symbol table available in {} (type: {:?})",
name, typ
"no symbol table available in {name} (type: {typ:?})"
))))
}
})();
Expand All @@ -9715,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 @@ -9786,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 @@ -9821,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 @@ -10028,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 @@ -12380,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 @@ -12418,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 @@ -12459,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
11 changes: 4 additions & 7 deletions crates/codegen/src/symboltable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2418,7 +2418,7 @@ impl SymbolTableBuilder {
};
if !seen_names.insert(name) {
return Err(SymbolTableError {
error: format!("duplicate type parameter '{}'", name),
error: format!("duplicate type parameter '{name}'"),
location: Some(
self.source_file
.to_source_code()
Expand All @@ -2431,8 +2431,7 @@ impl SymbolTableBuilder {
} else if default_seen {
return Err(SymbolTableError {
error: format!(
"non-default type parameter '{}' follows default type parameter",
name
"non-default type parameter '{name}' follows default type parameter"
),
location: Some(
self.source_file
Expand Down Expand Up @@ -2743,8 +2742,7 @@ impl SymbolTableBuilder {
{
return Err(SymbolTableError {
error: format!(
"assignment expression cannot rebind comprehension iteration variable '{}'",
mangled
"assignment expression cannot rebind comprehension iteration variable '{mangled}'"
),
location,
});
Expand Down Expand Up @@ -2868,8 +2866,7 @@ impl SymbolTableBuilder {
{
return Err(SymbolTableError {
error: format!(
"comprehension inner loop cannot rebind assignment expression target '{}'",
name
"comprehension inner loop cannot rebind assignment expression target '{name}'"
),
location,
});
Expand Down
32 changes: 17 additions & 15 deletions crates/common/src/cformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,21 +788,23 @@ impl<S> CFormatStrOrBytes<S> {
iter.next().unwrap();
literal.extend([second]);
continue;
} else {
if !literal.is_empty() {
parts.push((
part_index,
CFormatPart::Literal(core::mem::take(&mut literal)),
));
}
let spec = CFormatSpecKeyed::parse(iter).map_err(|err| CFormatError {
typ: err.0,
index: err.1,
})?;
parts.push((index, CFormatPart::Spec(spec)));
if let Some(&(index, _)) = iter.peek() {
part_index = index;
}
}

if !literal.is_empty() {
parts.push((
part_index,
CFormatPart::Literal(core::mem::take(&mut literal)),
));
}

let spec = CFormatSpecKeyed::parse(iter).map_err(|err| CFormatError {
typ: err.0,
index: err.1,
})?;

parts.push((index, CFormatPart::Spec(spec)));
if let Some(&(index, _)) = iter.peek() {
part_index = index;
}
} else {
return Err(CFormatError {
Expand Down
2 changes: 1 addition & 1 deletion crates/common/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ impl FormatSpec {
Ok("inf%".to_owned())
} else {
let capped = float::clamp_fmt_precision(precision);
let mut result = format!("{:.*}", capped, scaled);
let mut result = format!("{scaled:.capped$}");
// Pad with '0's up to the requested precision to match
// CPython byte-identically past the internal cap.
let missing = precision.saturating_sub(capped);
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl CompileError {
line: loc.line,
character_offset: loc.character_offset.saturating_add(1),
};
let msg = format!("'{}' was never closed", bracket_char);
let msg = format!("'{bracket_char}' was never closed");
is_unclosed_bracket = true;
(parser::ParseErrorType::OtherError(msg), loc, end_loc)
} else {
Expand Down
Loading
Loading