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
23 changes: 17 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -335,17 +335,24 @@ if_not_else = "allow" # Not always more readable
single_match_else = "allow"
similar_names = "allow"

# restriction lints
alloc_instead_of_core = "warn"
std_instead_of_alloc = "warn"
std_instead_of_core = "warn"
format_collect = "warn"
from_iter_instead_of_collect = "warn"
inefficient_to_string = "warn"
redundant_clone = "warn"

# nursery lints to enforce gradually
debug_assert_with_mut_call = "warn"
unused_peekable = "warn"
derive_partial_eq_without_eq = "warn"
imprecise_flops = "warn"
or_fun_call = "warn"
unnested_or_patterns = "warn"
redundant_clone = "warn"
search_is_some = "warn"
single_option_map = "warn"
trait_duplication_in_bounds = "warn"
unused_peekable = "warn"
unused_rounding = "warn"
use_self = "warn"
useless_let_if_seq = "warn"

# pedantic lints to enforce gradually
cloned_instead_of_copied = "warn"
Expand All @@ -355,10 +362,14 @@ explicit_into_iter_loop = "warn"
explicit_iter_loop = "warn"
filter_map_next = "warn"
flat_map_option = "warn"
format_collect = "warn"
from_iter_instead_of_collect = "warn"
inconsistent_struct_constructor = "warn"
inefficient_to_string = "warn"
manual_is_variant_and = "warn"
map_unwrap_or = "warn"
must_use_candidate = "warn"
redundant_else = "warn"
uninlined_format_args = "warn"
unnecessary_wraps = "warn"
unnested_or_patterns = "warn"
8 changes: 4 additions & 4 deletions crates/capi/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ impl FfiResult<isize> for usize {
}

impl FfiResult for c_long {
const ERR_VALUE: c_long = -1;
const ERR_VALUE: Self = -1;

fn into_output(self, _vm: &VirtualMachine) -> c_long {
fn into_output(self, _vm: &VirtualMachine) -> Self {
self
}
}

impl FfiResult for c_double {
const ERR_VALUE: c_double = -1.0;
const ERR_VALUE: Self = -1.0;

fn into_output(self, _vm: &VirtualMachine) -> c_double {
fn into_output(self, _vm: &VirtualMachine) -> Self {
self
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ impl ExprExt for ast::Expr {
fn is_constant(&self) -> bool {
matches!(
self,
ast::Expr::NumberLiteral(_)
| ast::Expr::StringLiteral(_)
| ast::Expr::BytesLiteral(_)
| ast::Expr::NoneLiteral(_)
| ast::Expr::BooleanLiteral(_)
| ast::Expr::EllipsisLiteral(_)
Self::NumberLiteral(_)
| Self::StringLiteral(_)
| Self::BytesLiteral(_)
| Self::NoneLiteral(_)
| Self::BooleanLiteral(_)
| Self::EllipsisLiteral(_)
)
}

fn is_constant_slice(&self) -> bool {
match self {
ast::Expr::Slice(s) => {
Self::Slice(s) => {
let lower_const =
s.lower.is_none() || s.lower.as_deref().is_some_and(|e| e.is_constant());
let upper_const =
Expand All @@ -76,7 +76,7 @@ impl ExprExt for ast::Expr {
}

fn should_use_slice_optimization(&self) -> bool {
!self.is_constant_slice() && matches!(self, ast::Expr::Slice(s) if s.step.is_none())
!self.is_constant_slice() && matches!(self, Self::Slice(s) if s.step.is_none())
}
}

Expand Down
22 changes: 18 additions & 4 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,20 +1231,23 @@ impl CodeInfo {
op: oparg::BinaryOperator,
) -> Option<ConstantData> {
use oparg::BinaryOperator as BinOp;

fn repeat_wtf8(value: &Wtf8Buf, n: usize) -> Wtf8Buf {
let mut result = Wtf8Buf::with_capacity(value.len().saturating_mul(n));
for _ in 0..n {
result.push_wtf8(value);
}
result
}

fn checked_repeat_count(n: &BigInt, item_size: usize) -> Option<usize> {
let n = n.to_isize()?;
if item_size != 0 && (n < 0 || n as usize > MAX_STR_SIZE / item_size) {
return None;
}
Some(n.max(0) as usize)
}

fn eval_complex_binop(
left: Complex<f64>,
right: Complex<f64>,
Expand All @@ -1259,17 +1262,18 @@ impl CodeInfo {
BinOp::Add => left + right,
BinOp::Subtract => {
let re = left.re - right.re;
let mut im = left.im - right.im;
// Preserve CPython's signed-zero behavior for real-zero
// minus zero-complex expressions such as `0 - 0j`.
if left.re == 0.0
let im = if left.re == 0.0
&& left.im == 0.0
&& right.re == 0.0
&& right.im == 0.0
&& !right.im.is_sign_negative()
{
im = -0.0;
}
-0.0
} else {
left.im - right.im
};
Complex::new(re, im)
}
BinOp::Multiply => left * right,
Expand All @@ -1284,12 +1288,14 @@ impl CodeInfo {
if right.im != 0.0 || right.re < 0.0 {
return None;
}

return complex_const(if right.re == 0.0 {
Complex::new(1.0, 0.0)
} else {
Complex::new(0.0, 0.0)
});
}

if right.im == 0.0
&& right.re.fract() == 0.0
&& right.re >= f64::from(i32::MIN)
Expand All @@ -1304,6 +1310,7 @@ impl CodeInfo {
};
complex_const(value)
}

fn float_div_mod(left: f64, right: f64) -> Option<(f64, f64)> {
if right == 0.0 {
return None;
Expand All @@ -1330,6 +1337,7 @@ impl CodeInfo {

Some((floordiv, modulo))
}

fn constant_as_index(value: &ConstantData) -> Option<i64> {
match value {
ConstantData::Integer { value } => value.to_i64().or_else(|| {
Expand All @@ -1343,12 +1351,14 @@ impl CodeInfo {
_ => None,
}
}

fn slice_bound(value: &ConstantData) -> Option<Option<i64>> {
match value {
ConstantData::None => Some(None),
_ => constant_as_index(value).map(Some),
}
}

fn adjusted_slice_indices(len: usize, slice: &[ConstantData; 3]) -> Option<Vec<usize>> {
let len = i64::try_from(len).ok()?;
let start = slice_bound(&slice[0])?;
Expand Down Expand Up @@ -1393,6 +1403,7 @@ impl CodeInfo {
}
Some(indices)
}

fn adjusted_const_index(len: usize, index: &ConstantData) -> Option<usize> {
let len = i64::try_from(len).ok()?;
let index = constant_as_index(index)?;
Expand All @@ -1406,6 +1417,7 @@ impl CodeInfo {
}
usize::try_from(index).ok()
}

fn eval_const_subscript(
container: &ConstantData,
index: &ConstantData,
Expand Down Expand Up @@ -1472,9 +1484,11 @@ impl CodeInfo {
_ => None,
}
}

if matches!(op, BinOp::Subscr) {
return eval_const_subscript(left, right);
}

match (left, right) {
(ConstantData::Integer { value: l }, ConstantData::Integer { value: r }) => {
let result = match op {
Expand Down
6 changes: 3 additions & 3 deletions crates/codegen/src/symboltable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct SymbolTable {

/// A list of sub-scopes in the order as found in the
/// AST nodes.
pub sub_tables: Vec<SymbolTable>,
pub sub_tables: Vec<Self>,

/// Cursor pointing to the next sub-table to consume during compilation.
pub next_sub_table: usize,
Expand All @@ -63,7 +63,7 @@ pub struct SymbolTable {

/// PEP 649: Reference to annotation scope for this block
/// Annotations are compiled as a separate `__annotate__` function
pub annotation_block: Option<Box<SymbolTable>>,
pub annotation_block: Option<Box<Self>>,

/// True only for deferred function/class/module annotation scopes that
/// should resolve outer names as if they were siblings of the owning
Expand Down Expand Up @@ -177,7 +177,7 @@ pub enum SymbolScope {
}

bitflags! {
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct SymbolFlags: u16 {
const REFERENCED = 0x001; // USE
const ASSIGNED = 0x002; // DEF_LOCAL
Expand Down
26 changes: 13 additions & 13 deletions crates/common/src/cformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustpython_literal::{float, format::Case};

use crate::wtf8::{CodePoint, Wtf8, Wtf8Buf};

#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CFormatErrorType {
UnmatchedKeyParentheses,
MissingModuloSign,
Expand All @@ -27,7 +27,7 @@ pub enum CFormatErrorType {
// also contains how many chars the parsing function consumed
pub type ParsingError = (CFormatErrorType, usize);

#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CFormatError {
pub typ: CFormatErrorType, // FIXME
pub index: usize,
Expand All @@ -54,7 +54,7 @@ impl fmt::Display for CFormatError {

pub type CFormatConversion = super::format::FormatConversion;

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[repr(u8)]
pub enum CNumberType {
DecimalD = b'd',
Expand All @@ -65,7 +65,7 @@ pub enum CNumberType {
HexUpper = b'X',
}

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[repr(u8)]
pub enum CFloatType {
ExponentLower = b'e',
Expand All @@ -87,13 +87,13 @@ impl CFloatType {
}
}

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[repr(u8)]
pub enum CCharacterType {
Character = b'c',
}

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CFormatType {
Number(CNumberType),
Float(CFloatType),
Expand All @@ -113,7 +113,7 @@ impl CFormatType {
}
}

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CFormatPrecision {
Quantity(CFormatQuantity),
Dot,
Expand All @@ -126,7 +126,7 @@ impl From<CFormatQuantity> for CFormatPrecision {
}

bitflags! {
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct CConversionFlags: u32 {
const ALTERNATE_FORM = 0b0000_0001;
const ZERO_PAD = 0b0000_0010;
Expand All @@ -150,7 +150,7 @@ impl CConversionFlags {
}
}

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CFormatQuantity {
Amount(usize),
FromValuesTuple,
Expand Down Expand Up @@ -254,7 +254,7 @@ impl FormatChar for u8 {
}
}

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CFormatSpec {
pub flags: CConversionFlags,
pub min_field_width: Option<CFormatQuantity>,
Expand All @@ -263,7 +263,7 @@ pub struct CFormatSpec {
// chars_consumed: usize,
}

#[derive(Debug, PartialEq)]
#[derive(Debug, Eq, PartialEq)]
pub struct CFormatSpecKeyed<T> {
pub mapping_key: Option<T>,
pub spec: CFormatSpec,
Expand Down Expand Up @@ -718,7 +718,7 @@ where
Some(contained_text)
}

#[derive(Debug, PartialEq)]
#[derive(Debug, Eq, PartialEq)]
pub enum CFormatPart<T> {
Literal(T),
Spec(CFormatSpecKeyed<T>),
Expand All @@ -739,7 +739,7 @@ impl<T> CFormatPart<T> {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, Eq, PartialEq)]
pub struct CFormatStrOrBytes<S> {
parts: Vec<(usize, CFormatPart<S>)>,
}
Expand Down
Loading
Loading