Skip to content

Commit 397a196

Browse files
committed
fix clippy
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
1 parent 783e45f commit 397a196

3 files changed

Lines changed: 44 additions & 22 deletions

File tree

‎compiler/codegen/src/compile.rs‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ use rustpython_compiler_core::{
4141
};
4242
use rustpython_compiler_source::SourceCode;
4343
// use rustpython_parser_core::source_code::{LineNumber, SourceLocation};
44-
use std::borrow::Cow;
4544
use crate::error::InternalError;
45+
use std::borrow::Cow;
4646

4747
pub(crate) type InternalResult<T> = Result<T, InternalError>;
4848
type CompileResult<T> = Result<T, CodegenError>;
@@ -213,27 +213,38 @@ macro_rules! emit {
213213
}
214214

215215
fn eprint_location(zelf: &Compiler<'_>) {
216-
let start = zelf.source_code.source_location(zelf.current_source_range.start());
217-
let end = zelf.source_code.source_location(zelf.current_source_range.end());
218-
eprintln!("LOCATION: {} from {}:{} to {}:{}", zelf.source_code.path.to_owned(), start.row, start.column, end.row, end.column);
216+
let start = zelf
217+
.source_code
218+
.source_location(zelf.current_source_range.start());
219+
let end = zelf
220+
.source_code
221+
.source_location(zelf.current_source_range.end());
222+
eprintln!(
223+
"LOCATION: {} from {}:{} to {}:{}",
224+
zelf.source_code.path.to_owned(),
225+
start.row,
226+
start.column,
227+
end.row,
228+
end.column
229+
);
219230
}
220231

221232
/// Better traceback for internal error
222233
fn unwrap_internal<T>(zelf: &Compiler<'_>, r: InternalResult<T>) -> T {
223234
if let Err(ref r_err) = r {
224235
eprintln!("=== CODEGEN PANIC INFO ===");
225236
eprintln!("This IS an internal error: {}", r_err);
226-
print_location(zelf);
237+
eprint_location(zelf);
227238
eprintln!("=== END PANIC INFO ===");
228239
}
229240
r.unwrap()
230241
}
231242

232243
fn compiler_unwrap_option<T>(zelf: &Compiler<'_>, o: Option<T>) -> T {
233-
if let None = o {
244+
if o.is_none() {
234245
eprintln!("=== CODEGEN PANIC INFO ===");
235246
eprintln!("This IS an internal error, an option was unwrapped during codegen");
236-
print_location(zelf);
247+
eprint_location(zelf);
237248
eprintln!("=== END PANIC INFO ===");
238249
}
239250
o.unwrap()
@@ -243,7 +254,7 @@ fn compiler_unwrap_option<T>(zelf: &Compiler<'_>, o: Option<T>) -> T {
243254
// if result.is_err() {
244255
// eprintln!("=== CODEGEN PANIC INFO ===");
245256
// eprintln!("This IS an internal error, an result was unwrapped during codegen");
246-
// print_location(zelf);
257+
// eprint_location(zelf);
247258
// eprintln!("=== END PANIC INFO ===");
248259
// }
249260
// result.unwrap()
@@ -579,9 +590,12 @@ impl Compiler<'_> {
579590
self.check_forbidden_name(&name, usage)?;
580591

581592
let symbol_table = self.symbol_table_stack.last().unwrap();
582-
let symbol = unwrap_internal(self, symbol_table.lookup(name.as_ref()).ok_or_else(||
583-
InternalError::MissingSymbol(name.to_string())
584-
));
593+
let symbol = unwrap_internal(
594+
self,
595+
symbol_table
596+
.lookup(name.as_ref())
597+
.ok_or_else(|| InternalError::MissingSymbol(name.to_string())),
598+
);
585599
let info = self.code_stack.last_mut().unwrap();
586600
let mut cache = &mut info.name_cache;
587601
enum NameOpType {
@@ -1515,9 +1529,12 @@ impl Compiler<'_> {
15151529
}
15161530
for var in &*code.freevars {
15171531
let table = self.symbol_table_stack.last().unwrap();
1518-
let symbol = unwrap_internal(self, table.lookup(var).ok_or_else(|| {
1519-
InternalError::MissingSymbol(var.to_owned())
1520-
}));
1532+
let symbol = unwrap_internal(
1533+
self,
1534+
table
1535+
.lookup(var)
1536+
.ok_or_else(|| InternalError::MissingSymbol(var.to_owned())),
1537+
);
15211538
let parent_code = self.code_stack.last().unwrap();
15221539
let vars = match symbol.scope {
15231540
SymbolScope::Free => &parent_code.freevar_cache,
@@ -1600,7 +1617,12 @@ impl Compiler<'_> {
16001617

16011618
// Check if the class is declared global
16021619
let symbol_table = self.symbol_table_stack.last().unwrap();
1603-
let symbol = unwrap_internal(self, symbol_table.lookup(name.as_ref()).ok_or_else(|| InternalError::MissingSymbol(name.to_owned())));
1620+
let symbol = unwrap_internal(
1621+
self,
1622+
symbol_table
1623+
.lookup(name.as_ref())
1624+
.ok_or_else(|| InternalError::MissingSymbol(name.to_owned())),
1625+
);
16041626
let mut global_path_prefix = Vec::new();
16051627
if symbol.scope == SymbolScope::GlobalExplicit {
16061628
global_path_prefix.append(&mut self.qualified_path);

‎compiler/codegen/src/error.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ impl Display for InternalError {
4747
match self {
4848
Self::StackOverflow => write!(f, "stack overflow"),
4949
Self::StackUnderflow => write!(f, "stack underflow"),
50-
Self::MissingSymbol(s) => write!(f, "The symbol '{s}' must be present in the symbol table, even when it is undefined in python.")
50+
Self::MissingSymbol(s) => write!(
51+
f,
52+
"The symbol '{s}' must be present in the symbol table, even when it is undefined in python."
53+
),
5154
}
5255
}
5356
}

‎compiler/codegen/src/ir.rs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::ops;
22

33
use crate::IndexSet;
4+
use crate::error::InternalError;
45
use ruff_source_file::{OneIndexed, SourceLocation};
56
use rustpython_compiler_core::bytecode::{
67
CodeFlags, CodeObject, CodeUnit, ConstantData, InstrDisplayContext, Instruction, Label, OpArg,
78
};
8-
use crate::error::InternalError;
99
// use rustpython_parser_core::source_code::{LineNumber, SourceLocation};
1010

1111
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@@ -245,10 +245,7 @@ impl CodeInfo {
245245
let instr_display = instr.display(display_arg, self);
246246
eprint!("{instr_display}: {depth} {effect:+} => ");
247247
}
248-
if effect < 0 && depth < effect.unsigned_abs() {
249-
panic!("The stack will underflow at {depth} with {effect} effect on {instr:?}");
250-
}
251-
let new_depth = depth.checked_add_signed(effect).ok_or_else(|| {
248+
let new_depth = depth.checked_add_signed(effect).ok_or({
252249
if effect < 0 {
253250
InternalError::StackUnderflow
254251
} else {
@@ -271,7 +268,7 @@ impl CodeInfo {
271268
)
272269
{
273270
let effect = instr.stack_effect(ins.arg, true);
274-
let target_depth = depth.checked_add_signed(effect).ok_or_else(|| {
271+
let target_depth = depth.checked_add_signed(effect).ok_or({
275272
if effect < 0 {
276273
InternalError::StackUnderflow
277274
} else {

0 commit comments

Comments
 (0)