1
0
forked from M-Labs/nac3

meta: Restrict number of allowed lints

This commit is contained in:
David Mak 2024-06-12 15:13:09 +08:00
parent 40a3bded36
commit 23974feae7
33 changed files with 473 additions and 465 deletions

View File

@ -1 +1 @@
doc-valid-idents = ["NumPy", ".."]
doc-valid-idents = ["CPython", "NumPy", ".."]

View File

@ -260,7 +260,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> {
let start_expr = Located {
// location does not matter at this point
location: stmt.location,
node: ExprKind::Name { id: start, ctx: name_ctx.clone() },
node: ExprKind::Name { id: start, ctx: *name_ctx },
custom: Some(ctx.primitives.int64),
};
let start = self
@ -275,7 +275,7 @@ impl<'b> CodeGenerator for ArtiqCodeGenerator<'b> {
let end_expr = Located {
// location does not matter at this point
location: stmt.location,
node: ExprKind::Name { id: end, ctx: name_ctx.clone() },
node: ExprKind::Name { id: end, ctx: *name_ctx },
custom: Some(ctx.primitives.int64),
};
let end = self.gen_store_target(ctx, &end_expr, Some("end.addr"))?.unwrap();
@ -442,7 +442,7 @@ fn rpc_codegen_callback_fn<'ctx>(
format!("tagptr{}", fun.1 .0).as_str(),
);
tag_arr_ptr.set_initializer(&int8.const_array(
&tag.iter().map(|v| int8.const_int(*v as u64, false)).collect::<Vec<_>>(),
&tag.iter().map(|v| int8.const_int(u64::from(*v), false)).collect::<Vec<_>>(),
));
tag_arr_ptr.set_linkage(Linkage::Private);
let tag_ptr = ctx.module.add_global(tag_ptr_type, None, &hash);

View File

@ -1,7 +1,13 @@
#![deny(clippy::all)]
#![deny(
future_incompatible,
let_underscore,
nonstandard_style,
rust_2024_compatibility,
clippy::all
)]
#![warn(clippy::pedantic)]
#![allow(
clippy::cast_lossless,
unsafe_op_in_unsafe_fn,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::enum_glob_use,

View File

@ -125,7 +125,7 @@ impl StaticValue for PythonValue {
);
global.set_constant(true);
global.set_initializer(&ctx.ctx.const_struct(
&[ctx.ctx.i32_type().const_int(id as u64, false).into()],
&[ctx.ctx.i32_type().const_int(u64::from(id), false).into()],
false,
));
Ok(global.as_pointer_value().into())
@ -146,10 +146,14 @@ impl StaticValue for PythonValue {
return Ok(match val {
PrimitiveValue::I32(val) => ctx.ctx.i32_type().const_int(*val as u64, false).into(),
PrimitiveValue::I64(val) => ctx.ctx.i64_type().const_int(*val as u64, false).into(),
PrimitiveValue::U32(val) => ctx.ctx.i32_type().const_int(*val as u64, false).into(),
PrimitiveValue::U32(val) => {
ctx.ctx.i32_type().const_int(u64::from(*val), false).into()
}
PrimitiveValue::U64(val) => ctx.ctx.i64_type().const_int(*val, false).into(),
PrimitiveValue::F64(val) => ctx.ctx.f64_type().const_float(*val).into(),
PrimitiveValue::Bool(val) => ctx.ctx.i8_type().const_int(*val as u64, false).into(),
PrimitiveValue::Bool(val) => {
ctx.ctx.i8_type().const_int(u64::from(*val), false).into()
}
});
}
if let Some(global) = ctx.module.get_global(&self.id.to_string()) {
@ -864,7 +868,7 @@ impl InnerResolver {
} else if ty_id == self.primitive_ids.uint32 {
let val: u32 = obj.extract().unwrap();
self.id_to_primitive.write().insert(id, PrimitiveValue::U32(val));
Ok(Some(ctx.ctx.i32_type().const_int(val as u64, false).into()))
Ok(Some(ctx.ctx.i32_type().const_int(u64::from(val), false).into()))
} else if ty_id == self.primitive_ids.uint64 {
let val: u64 = obj.extract().unwrap();
self.id_to_primitive.write().insert(id, PrimitiveValue::U64(val));
@ -872,7 +876,7 @@ impl InnerResolver {
} else if ty_id == self.primitive_ids.bool {
let val: bool = obj.extract().unwrap();
self.id_to_primitive.write().insert(id, PrimitiveValue::Bool(val));
Ok(Some(ctx.ctx.i8_type().const_int(val as u64, false).into()))
Ok(Some(ctx.ctx.i8_type().const_int(u64::from(val), false).into()))
} else if ty_id == self.primitive_ids.float || ty_id == self.primitive_ids.float64 {
let val: f64 = obj.extract().unwrap();
self.id_to_primitive.write().insert(id, PrimitiveValue::F64(val));

View File

@ -15,7 +15,7 @@ lazy_static! {
}
thread_local! {
static LOCAL_INTERNER: RefCell<HashMap<String, StrRef>> = Default::default();
static LOCAL_INTERNER: RefCell<HashMap<String, StrRef>> = RefCell::default();
}
#[derive(Eq, PartialEq, Copy, Clone, Hash)]
@ -24,14 +24,14 @@ pub struct StrRef(SymbolU32);
impl fmt::Debug for StrRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s: String = (*self).into();
write!(f, "{:?}", s)
write!(f, "{s:?}")
}
}
impl fmt::Display for StrRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s: String = (*self).into();
write!(f, "{}", s)
write!(f, "{s}")
}
}
@ -69,6 +69,7 @@ pub fn get_str_ref(lock: &mut MutexGuard<Interner>, str: &str) -> StrRef {
StrRef(lock.get_or_intern(str))
}
#[must_use]
pub fn get_str_from_ref<'a>(lock: &'a MutexGuard<Interner>, id: StrRef) -> &'a str {
lock.resolve(id.0).unwrap()
}
@ -359,20 +360,20 @@ pub enum ExprKind<U = ()> {
}
pub type Expr<U = ()> = Located<ExprKind<U>, U>;
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ExprContext {
Load,
Store,
Del,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Boolop {
And,
Or,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Operator {
Add,
Sub,
@ -389,7 +390,7 @@ pub enum Operator {
FloorDiv,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Unaryop {
Invert,
Not,
@ -397,7 +398,7 @@ pub enum Unaryop {
USub,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Cmpop {
Eq,
NotEq,
@ -451,7 +452,7 @@ pub struct KeywordData<U = ()> {
}
pub type Keyword<U = ()> = Located<KeywordData<U>, U>;
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Alias {
pub name: Ident,
pub asname: Option<Ident>,

View File

@ -28,12 +28,12 @@ impl From<bool> for Constant {
}
impl From<i32> for Constant {
fn from(i: i32) -> Constant {
Self::Int(i as i128)
Self::Int(i128::from(i))
}
}
impl From<i64> for Constant {
fn from(i: i64) -> Constant {
Self::Int(i as i128)
Self::Int(i128::from(i))
}
}
@ -50,6 +50,7 @@ pub enum ConversionFlag {
}
impl ConversionFlag {
#[must_use]
pub fn try_from_byte(b: u8) -> Option<Self> {
match b {
b's' => Some(Self::Str),
@ -69,6 +70,7 @@ pub struct ConstantOptimizer {
#[cfg(feature = "constant-optimization")]
impl ConstantOptimizer {
#[inline]
#[must_use]
pub fn new() -> Self {
Self { _priv: () }
}

View File

@ -2,6 +2,7 @@ use crate::{Constant, ExprKind};
impl<U> ExprKind<U> {
/// Returns a short name for the node suitable for use in error messages.
#[must_use]
pub fn name(&self) -> &'static str {
match self {
ExprKind::BoolOp { .. } | ExprKind::BinOp { .. } | ExprKind::UnaryOp { .. } => {

View File

@ -1,15 +1,16 @@
#![deny(clippy::all)]
#![deny(
future_incompatible,
let_underscore,
nonstandard_style,
rust_2024_compatibility,
clippy::all
)]
#![warn(clippy::pedantic)]
#![allow(
clippy::cast_lossless,
clippy::default_trait_access,
clippy::missing_errors_doc,
clippy::missing_panics_doc,
clippy::module_name_repetitions,
clippy::must_use_candidate,
clippy::needless_pass_by_value,
clippy::too_many_lines,
clippy::uninlined_format_args,
clippy::wildcard_imports
)]

View File

@ -81,14 +81,17 @@ impl Location {
}
impl Location {
#[must_use]
pub fn new(row: usize, column: usize, file: FileName) -> Self {
Location { row, column, file }
}
#[must_use]
pub fn row(&self) -> usize {
self.row
}
#[must_use]
pub fn column(&self) -> usize {
self.column
}

View File

@ -164,7 +164,7 @@ pub trait UntypedArrayLikeAccessor<'ctx, Index = IntValue<'ctx>>:
idx: &Index,
name: Option<&str>,
) -> BasicValueEnum<'ctx> {
let ptr = self.ptr_offset_unchecked(ctx, generator, idx, name);
let ptr = unsafe { self.ptr_offset_unchecked(ctx, generator, idx, name) };
ctx.builder.build_load(ptr, name.unwrap_or_default()).unwrap()
}
@ -195,7 +195,7 @@ pub trait UntypedArrayLikeMutator<'ctx, Index = IntValue<'ctx>>:
idx: &Index,
value: BasicValueEnum<'ctx>,
) {
let ptr = self.ptr_offset_unchecked(ctx, generator, idx, None);
let ptr = unsafe { self.ptr_offset_unchecked(ctx, generator, idx, None) };
ctx.builder.build_store(ptr, value).unwrap();
}
@ -233,7 +233,7 @@ pub trait TypedArrayLikeAccessor<'ctx, T, Index = IntValue<'ctx>>:
idx: &Index,
name: Option<&str>,
) -> T {
let value = self.get_unchecked(ctx, generator, idx, name);
let value = unsafe { self.get_unchecked(ctx, generator, idx, name) };
self.downcast_to_type(ctx, value)
}
@ -272,7 +272,7 @@ pub trait TypedArrayLikeMutator<'ctx, T, Index = IntValue<'ctx>>:
value: T,
) {
let value = self.upcast_from_type(ctx, value);
self.set_unchecked(ctx, generator, idx, value);
unsafe { self.set_unchecked(ctx, generator, idx, value) }
}
/// Sets the data at the `idx`-th index.
@ -360,7 +360,7 @@ where
idx: &Index,
name: Option<&str>,
) -> PointerValue<'ctx> {
self.adapted.ptr_offset_unchecked(ctx, generator, idx, name)
unsafe { self.adapted.ptr_offset_unchecked(ctx, generator, idx, name) }
}
fn ptr_offset<G: CodeGenerator + ?Sized>(
@ -474,9 +474,11 @@ impl<'ctx> ArrayLikeIndexer<'ctx> for ArraySliceValue<'ctx> {
) -> PointerValue<'ctx> {
let var_name = name.map(|v| format!("{v}.addr")).unwrap_or_default();
ctx.builder
.build_in_bounds_gep(self.base_ptr(ctx, generator), &[*idx], var_name.as_str())
.unwrap()
unsafe {
ctx.builder
.build_in_bounds_gep(self.base_ptr(ctx, generator), &[*idx], var_name.as_str())
.unwrap()
}
}
fn ptr_offset<G: CodeGenerator + ?Sized>(
@ -830,9 +832,11 @@ impl<'ctx> ArrayLikeIndexer<'ctx> for ListDataProxy<'ctx, '_> {
) -> PointerValue<'ctx> {
let var_name = name.map(|v| format!("{v}.addr")).unwrap_or_default();
ctx.builder
.build_in_bounds_gep(self.base_ptr(ctx, generator), &[*idx], var_name.as_str())
.unwrap()
unsafe {
ctx.builder
.build_in_bounds_gep(self.base_ptr(ctx, generator), &[*idx], var_name.as_str())
.unwrap()
}
}
fn ptr_offset<G: CodeGenerator + ?Sized>(
@ -1501,9 +1505,11 @@ impl<'ctx> ArrayLikeIndexer<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_>
) -> PointerValue<'ctx> {
let var_name = name.map(|v| format!("{v}.addr")).unwrap_or_default();
ctx.builder
.build_in_bounds_gep(self.base_ptr(ctx, generator), &[*idx], var_name.as_str())
.unwrap()
unsafe {
ctx.builder
.build_in_bounds_gep(self.base_ptr(ctx, generator), &[*idx], var_name.as_str())
.unwrap()
}
}
fn ptr_offset<G: CodeGenerator + ?Sized>(
@ -1594,9 +1600,15 @@ impl<'ctx> ArrayLikeIndexer<'ctx> for NDArrayDataProxy<'ctx, '_> {
idx: &IntValue<'ctx>,
name: Option<&str>,
) -> PointerValue<'ctx> {
ctx.builder
.build_in_bounds_gep(self.base_ptr(ctx, generator), &[*idx], name.unwrap_or_default())
.unwrap()
unsafe {
ctx.builder
.build_in_bounds_gep(
self.base_ptr(ctx, generator),
&[*idx],
name.unwrap_or_default(),
)
.unwrap()
}
}
fn ptr_offset<G: CodeGenerator + ?Sized>(

View File

@ -110,9 +110,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
match val {
SymbolValue::I32(v) => self.ctx.i32_type().const_int(*v as u64, true).into(),
SymbolValue::I64(v) => self.ctx.i64_type().const_int(*v as u64, true).into(),
SymbolValue::U32(v) => self.ctx.i32_type().const_int(*v as u64, false).into(),
SymbolValue::U32(v) => self.ctx.i32_type().const_int(u64::from(*v), false).into(),
SymbolValue::U64(v) => self.ctx.i64_type().const_int(*v, false).into(),
SymbolValue::Bool(v) => self.ctx.i8_type().const_int(*v as u64, true).into(),
SymbolValue::Bool(v) => self.ctx.i8_type().const_int(u64::from(*v), true).into(),
SymbolValue::Double(v) => self.ctx.f64_type().const_float(*v).into(),
SymbolValue::Str(v) => {
let str_ptr = self
@ -299,7 +299,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
pub fn gen_int_ops<G: CodeGenerator + ?Sized>(
&mut self,
generator: &mut G,
op: &Operator,
op: Operator,
lhs: BasicValueEnum<'ctx>,
rhs: BasicValueEnum<'ctx>,
signed: bool,
@ -371,7 +371,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
self.current_loc,
);
match *op {
match op {
Operator::LShift => {
self.builder.build_left_shift(lhs, rhs, "lshift").map(Into::into).unwrap()
}
@ -399,7 +399,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
/// Generates a binary operation `op` between two floating-point operands `lhs` and `rhs`.
pub fn gen_float_ops(
&mut self,
op: &Operator,
op: Operator,
lhs: BasicValueEnum<'ctx>,
rhs: BasicValueEnum<'ctx>,
) -> BasicValueEnum<'ctx> {
@ -1148,7 +1148,7 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
left: (&Option<Type>, BasicValueEnum<'ctx>),
op: &Operator,
op: Operator,
right: (&Option<Type>, BasicValueEnum<'ctx>),
loc: Location,
is_aug_assign: bool,
@ -1166,14 +1166,14 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
Ok(Some(ctx.gen_int_ops(generator, op, left_val, right_val, true).into()))
} else if ty1 == ty2 && [ctx.primitives.uint32, ctx.primitives.uint64].contains(&ty1) {
Ok(Some(ctx.gen_int_ops(generator, op, left_val, right_val, false).into()))
} else if [Operator::LShift, Operator::RShift].contains(op) {
} else if [Operator::LShift, Operator::RShift].contains(&op) {
let signed = [ctx.primitives.int32, ctx.primitives.int64].contains(&ty1);
Ok(Some(ctx.gen_int_ops(generator, op, left_val, right_val, signed).into()))
} else if ty1 == ty2 && ctx.primitives.float == ty1 {
Ok(Some(ctx.gen_float_ops(op, left_val, right_val).into()))
} else if ty1 == ctx.primitives.float && ty2 == ctx.primitives.int32 {
// Pow is the only operator that would pass typecheck between float and int
assert_eq!(*op, Operator::Pow);
assert_eq!(op, Operator::Pow);
let res = call_float_powi(
ctx,
left_val.into_float_value(),
@ -1200,7 +1200,7 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
let right_val =
NDArrayValue::from_ptr_val(right_val.into_pointer_value(), llvm_usize, None);
let res = if *op == Operator::MatMult {
let res = if op == Operator::MatMult {
// MatMult is the only binop which is not an elementwise op
numpy::ndarray_matmul_2d(
generator,
@ -1330,7 +1330,7 @@ pub fn gen_binop_expr<'ctx, G: CodeGenerator>(
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
left: &Expr<Option<Type>>,
op: &Operator,
op: Operator,
right: &Expr<Option<Type>>,
loc: Location,
is_aug_assign: bool,
@ -1362,7 +1362,7 @@ pub fn gen_binop_expr<'ctx, G: CodeGenerator>(
pub fn gen_unaryop_expr_with_values<'ctx, G: CodeGenerator>(
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
op: &ast::Unaryop,
op: ast::Unaryop,
operand: (&Option<Type>, BasicValueEnum<'ctx>),
) -> Result<Option<ValueEnum<'ctx>>, String> {
let (ty, val) = operand;
@ -1370,7 +1370,7 @@ pub fn gen_unaryop_expr_with_values<'ctx, G: CodeGenerator>(
Ok(Some(if ty == ctx.primitives.bool {
let val = val.into_int_value();
if *op == ast::Unaryop::Not {
if op == ast::Unaryop::Not {
let not = ctx.builder.build_not(val, "not").unwrap();
let not_bool =
ctx.builder.build_and(not, not.get_type().const_int(1, false), "").unwrap();
@ -1434,8 +1434,8 @@ pub fn gen_unaryop_expr_with_values<'ctx, G: CodeGenerator>(
// ndarray uses `~` rather than `not` to perform elementwise inversion, convert it before
// passing it to the elementwise codegen function
let op = if ndarray_dtype.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::Bool.id()) {
if *op == ast::Unaryop::Invert {
&ast::Unaryop::Not
if op == ast::Unaryop::Invert {
ast::Unaryop::Not
} else {
unreachable!("ufunc {} not supported for ndarray[bool, N]", unaryop_name(op))
}
@ -1469,7 +1469,7 @@ pub fn gen_unaryop_expr_with_values<'ctx, G: CodeGenerator>(
pub fn gen_unaryop_expr<'ctx, G: CodeGenerator>(
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
op: &ast::Unaryop,
op: ast::Unaryop,
operand: &Expr<Option<Type>>,
) -> Result<Option<ValueEnum<'ctx>>, String> {
let val = if let Some(v) = generator.gen_expr(ctx, operand)? {
@ -1503,7 +1503,7 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
let (Some(left_ty), lhs) = left else { unreachable!() };
let (Some(right_ty), rhs) = comparators[0] else { unreachable!() };
let op = ops[0].clone();
let op = ops[0];
let is_ndarray1 =
left_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id());
@ -1530,7 +1530,7 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
generator,
ctx,
(Some(ndarray_dtype1), lhs),
&[op.clone()],
&[op],
&[(Some(ndarray_dtype2), rhs)],
)?
.unwrap()
@ -1562,7 +1562,7 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
generator,
ctx,
(Some(ndarray_dtype), lhs),
&[op.clone()],
&[op],
&[(Some(ndarray_dtype), rhs)],
)?
.unwrap()
@ -1743,7 +1743,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
.iter()
.map(|ndim| match *ndim {
SymbolValue::U64(v) => Ok(v),
SymbolValue::U32(v) => Ok(v as u64),
SymbolValue::U32(v) => Ok(u64::from(v)),
SymbolValue::I32(v) => u64::try_from(v)
.map_err(|_| format!("Expected non-negative literal for ndarray.ndims, got {v}")),
SymbolValue::I64(v) => u64::try_from(v)
@ -2202,9 +2202,9 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
}
}
ExprKind::BinOp { op, left, right } => {
return gen_binop_expr(generator, ctx, left, op, right, expr.location, false);
return gen_binop_expr(generator, ctx, left, *op, right, expr.location, false);
}
ExprKind::UnaryOp { op, operand } => return gen_unaryop_expr(generator, ctx, op, operand),
ExprKind::UnaryOp { op, operand } => return gen_unaryop_expr(generator, ctx, *op, operand),
ExprKind::Compare { left, ops, comparators } => {
return gen_cmpop_expr(generator, ctx, left, ops, comparators)
}

View File

@ -1629,7 +1629,7 @@ pub fn ndarray_matmul_2d<'ctx, G: CodeGenerator>(
generator,
ctx,
(&Some(elem_ty), a),
&Operator::Mult,
Operator::Mult,
(&Some(elem_ty), b),
ctx.current_loc,
false,
@ -1642,7 +1642,7 @@ pub fn ndarray_matmul_2d<'ctx, G: CodeGenerator>(
generator,
ctx,
(&Some(elem_ty), result),
&Operator::Add,
Operator::Add,
(&Some(elem_ty), a_mul_b),
ctx.current_loc,
false,

View File

@ -1574,7 +1574,7 @@ pub fn gen_stmt<G: CodeGenerator>(
StmtKind::For { .. } => generator.gen_for(ctx, stmt)?,
StmtKind::With { .. } => generator.gen_with(ctx, stmt)?,
StmtKind::AugAssign { target, op, value, .. } => {
let value = gen_binop_expr(generator, ctx, target, op, value, stmt.location, true)?;
let value = gen_binop_expr(generator, ctx, target, *op, value, stmt.location, true)?;
generator.gen_assign(ctx, target, value.unwrap())?;
}
StmtKind::Try { .. } => gen_try(generator, ctx, stmt)?,

View File

@ -1,12 +1,16 @@
#![deny(clippy::all)]
#![deny(
future_incompatible,
let_underscore,
nonstandard_style,
rust_2024_compatibility,
clippy::all
)]
#![warn(clippy::pedantic)]
#![allow(
dead_code,
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::enum_glob_use,
clippy::implicit_hasher,
clippy::missing_errors_doc,
clippy::missing_panics_doc,
clippy::module_name_repetitions,

View File

@ -241,7 +241,7 @@ impl TryFrom<SymbolValue> for u64 {
match value {
SymbolValue::I32(v) => u64::try_from(v).map_err(|_| ()),
SymbolValue::I64(v) => u64::try_from(v).map_err(|_| ()),
SymbolValue::U32(v) => Ok(v as u64),
SymbolValue::U32(v) => Ok(u64::from(v)),
SymbolValue::U64(v) => Ok(v),
_ => Err(()),
}
@ -255,10 +255,10 @@ impl TryFrom<SymbolValue> for i128 {
/// numeric.
fn try_from(value: SymbolValue) -> Result<Self, Self::Error> {
match value {
SymbolValue::I32(v) => Ok(v as i128),
SymbolValue::I64(v) => Ok(v as i128),
SymbolValue::U32(v) => Ok(v as i128),
SymbolValue::U64(v) => Ok(v as i128),
SymbolValue::I32(v) => Ok(i128::from(v)),
SymbolValue::I64(v) => Ok(i128::from(v)),
SymbolValue::U32(v) => Ok(i128::from(v)),
SymbolValue::U64(v) => Ok(i128::from(v)),
_ => Err(()),
}
}

View File

@ -561,7 +561,9 @@ impl TopLevelComposer {
unifier,
&primitive_types,
b,
vec![(*class_def_id, class_type_vars.clone())].into_iter().collect(),
vec![(*class_def_id, class_type_vars.clone())]
.into_iter()
.collect::<HashMap<_, _>>(),
)?;
if let TypeAnnotation::CustomClass { .. } = &base_ty {
@ -1154,7 +1156,7 @@ impl TopLevelComposer {
annotation_expr,
vec![(class_id, class_type_vars_def.clone())]
.into_iter()
.collect(),
.collect::<HashMap<_, _>>(),
)?
};
// find type vars within this method parameter type annotation
@ -1219,7 +1221,9 @@ impl TopLevelComposer {
unifier,
primitives,
result,
vec![(class_id, class_type_vars_def.clone())].into_iter().collect(),
vec![(class_id, class_type_vars_def.clone())]
.into_iter()
.collect::<HashMap<_, _>>(),
)?;
// find type vars within this return type annotation
let type_vars_within =
@ -1313,7 +1317,9 @@ impl TopLevelComposer {
unifier,
primitives,
annotation.as_ref(),
vec![(class_id, class_type_vars_def.clone())].into_iter().collect(),
vec![(class_id, class_type_vars_def.clone())]
.into_iter()
.collect::<HashMap<_, _>>(),
)?;
// find type vars within this return type annotation
let type_vars_within =

View File

@ -68,18 +68,18 @@ impl TypeAnnotation {
/// generic variables associated with the definition.
/// * `type_var` - The type variable associated with the type argument currently being parsed. Pass
/// [`None`] when this function is invoked externally.
pub fn parse_ast_to_type_annotation_kinds<T>(
pub fn parse_ast_to_type_annotation_kinds<T, S: std::hash::BuildHasher + Clone>(
resolver: &(dyn SymbolResolver + Send + Sync),
top_level_defs: &[Arc<RwLock<TopLevelDef>>],
unifier: &mut Unifier,
primitives: &PrimitiveStore,
expr: &ast::Expr<T>,
// the key stores the type_var of this topleveldef::class, we only need this field here
locked: HashMap<DefinitionId, Vec<Type>>,
locked: HashMap<DefinitionId, Vec<Type>, S>,
) -> Result<TypeAnnotation, HashSet<String>> {
let name_handle = |id: &StrRef,
unifier: &mut Unifier,
locked: HashMap<DefinitionId, Vec<Type>>| {
locked: HashMap<DefinitionId, Vec<Type>, S>| {
if id == &"int32".into() {
Ok(TypeAnnotation::Primitive(primitives.int32))
} else if id == &"int64".into() {
@ -144,7 +144,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|id: &StrRef,
slice: &ast::Expr<T>,
unifier: &mut Unifier,
mut locked: HashMap<DefinitionId, Vec<Type>>| {
mut locked: HashMap<DefinitionId, Vec<Type>, S>| {
if ["virtual".into(), "Generic".into(), "list".into(), "tuple".into(), "Option".into()]
.contains(id)
{

View File

@ -14,7 +14,7 @@ use std::rc::Rc;
use strum::IntoEnumIterator;
#[must_use]
pub fn binop_name(op: &Operator) -> &'static str {
pub fn binop_name(op: Operator) -> &'static str {
match op {
Operator::Add => "__add__",
Operator::Sub => "__sub__",
@ -33,7 +33,7 @@ pub fn binop_name(op: &Operator) -> &'static str {
}
#[must_use]
pub fn binop_assign_name(op: &Operator) -> &'static str {
pub fn binop_assign_name(op: Operator) -> &'static str {
match op {
Operator::Add => "__iadd__",
Operator::Sub => "__isub__",
@ -52,7 +52,7 @@ pub fn binop_assign_name(op: &Operator) -> &'static str {
}
#[must_use]
pub fn unaryop_name(op: &Unaryop) -> &'static str {
pub fn unaryop_name(op: Unaryop) -> &'static str {
match op {
Unaryop::UAdd => "__pos__",
Unaryop::USub => "__neg__",
@ -62,7 +62,7 @@ pub fn unaryop_name(op: &Unaryop) -> &'static str {
}
#[must_use]
pub fn comparison_name(op: &Cmpop) -> Option<&'static str> {
pub fn comparison_name(op: Cmpop) -> Option<&'static str> {
match op {
Cmpop::Lt => Some("__lt__"),
Cmpop::LtE => Some("__le__"),
@ -116,7 +116,7 @@ pub fn impl_binop(
let ret_ty = ret_ty.unwrap_or_else(|| unifier.get_fresh_var(None, None).0);
for op in ops {
fields.insert(binop_name(op).into(), {
fields.insert(binop_name(*op).into(), {
(
unifier.add_ty(TypeEnum::TFunc(FunSignature {
ret: ret_ty,
@ -131,7 +131,7 @@ pub fn impl_binop(
)
});
fields.insert(binop_assign_name(op).into(), {
fields.insert(binop_assign_name(*op).into(), {
(
unifier.add_ty(TypeEnum::TFunc(FunSignature {
ret: ret_ty,
@ -155,7 +155,7 @@ pub fn impl_unaryop(unifier: &mut Unifier, ty: Type, ret_ty: Option<Type>, ops:
for op in ops {
fields.insert(
unaryop_name(op).into(),
unaryop_name(*op).into(),
(
unifier.add_ty(TypeEnum::TFunc(FunSignature {
ret: ret_ty,
@ -195,7 +195,7 @@ pub fn impl_cmpop(
for op in ops {
fields.insert(
comparison_name(op).unwrap().into(),
comparison_name(*op).unwrap().into(),
(
unifier.add_ty(TypeEnum::TFunc(FunSignature {
ret: ret_ty,
@ -425,7 +425,7 @@ pub fn typeof_ndarray_broadcast(
pub fn typeof_binop(
unifier: &mut Unifier,
primitives: &PrimitiveStore,
op: &Operator,
op: Operator,
lhs: Type,
rhs: Type,
) -> Result<Option<Type>, String> {
@ -466,7 +466,7 @@ pub fn typeof_binop(
(lhs, rhs) if lhs == 0 || rhs == 0 => {
return Err(format!(
"Input operand {} does not have enough dimensions (has {lhs}, requires {rhs})",
(rhs == 0) as u8
u8::from(rhs == 0)
))
}
(lhs, rhs) => {
@ -520,12 +520,12 @@ pub fn typeof_binop(
pub fn typeof_unaryop(
unifier: &mut Unifier,
primitives: &PrimitiveStore,
op: &Unaryop,
op: Unaryop,
operand: Type,
) -> Result<Option<Type>, String> {
let operand_obj_id = operand.obj_id(unifier);
if *op == Unaryop::Not
if op == Unaryop::Not
&& operand_obj_id.is_some_and(|id| id == primitives.ndarray.obj_id(unifier).unwrap())
{
return Err(
@ -533,7 +533,7 @@ pub fn typeof_unaryop(
);
}
Ok(match *op {
Ok(match op {
Unaryop::Not => match operand_obj_id {
Some(v) if v == PrimDef::NDArray.id() => Some(operand),
Some(_) => Some(primitives.bool),
@ -554,7 +554,7 @@ pub fn typeof_unaryop(
if operand_obj_id.is_some_and(|id| id == PrimDef::NDArray.id()) {
let (dtype, _) = unpack_ndarray_var_tys(unifier, operand);
if dtype.obj_id(unifier).is_some_and(|id| id == PrimDef::Bool.id()) {
return Err(if *op == Unaryop::UAdd {
return Err(if op == Unaryop::UAdd {
"The ufunc 'positive' cannot be applied to ndarray[bool, N]".to_string()
} else {
"The numpy boolean negative, the `-` operator, is not supported, use the `~` operator function instead.".to_string()
@ -577,7 +577,7 @@ pub fn typeof_unaryop(
pub fn typeof_cmpop(
unifier: &mut Unifier,
primitives: &PrimitiveStore,
_op: &Cmpop,
_op: Cmpop,
lhs: Type,
rhs: Type,
) -> Result<Option<Type>, String> {

View File

@ -465,7 +465,7 @@ impl<'a> Fold<()> for Inferencer<'a> {
(None, None) => {}
},
ast::StmtKind::AugAssign { target, op, value, .. } => {
let res_ty = self.infer_bin_ops(stmt.location, target, op, value, true)?;
let res_ty = self.infer_bin_ops(stmt.location, target, *op, value, true)?;
self.unify(res_ty, target.custom.unwrap(), &stmt.location)?;
}
ast::StmtKind::Assert { test, msg, .. } => {
@ -543,20 +543,20 @@ impl<'a> Fold<()> for Inferencer<'a> {
ExprKind::List { elts, .. } => Some(self.infer_list(elts)?),
ExprKind::Tuple { elts, .. } => Some(self.infer_tuple(elts)?),
ExprKind::Attribute { value, attr, ctx } => {
Some(self.infer_attribute(value, *attr, ctx)?)
Some(self.infer_attribute(value, *attr, *ctx)?)
}
ExprKind::BoolOp { values, .. } => Some(self.infer_bool_ops(values)?),
ExprKind::BinOp { left, op, right } => {
Some(self.infer_bin_ops(expr.location, left, op, right, false)?)
Some(self.infer_bin_ops(expr.location, left, *op, right, false)?)
}
ExprKind::UnaryOp { op, operand } => {
Some(self.infer_unary_ops(expr.location, op, operand)?)
Some(self.infer_unary_ops(expr.location, *op, operand)?)
}
ExprKind::Compare { left, ops, comparators } => {
Some(self.infer_compare(expr.location, left, ops, comparators)?)
}
ExprKind::Subscript { value, slice, ctx, .. } => {
Some(self.infer_subscript(value.as_ref(), slice.as_ref(), ctx)?)
Some(self.infer_subscript(value.as_ref(), slice.as_ref(), *ctx)?)
}
ExprKind::IfExp { test, body, orelse } => {
Some(self.infer_if_expr(test, body.as_ref(), orelse.as_ref())?)
@ -860,7 +860,7 @@ impl<'a> Inferencer<'a> {
func: Box::new(Located {
custom: None,
location: func.location,
node: ExprKind::Name { id: *id, ctx: ctx.clone() },
node: ExprKind::Name { id: *id, ctx: *ctx },
}),
args: vec![arg0],
keywords: vec![],
@ -918,7 +918,7 @@ impl<'a> Inferencer<'a> {
func: Box::new(Located {
custom: Some(custom),
location: func.location,
node: ExprKind::Name { id: *id, ctx: ctx.clone() },
node: ExprKind::Name { id: *id, ctx: *ctx },
}),
args: vec![arg0],
keywords: vec![],
@ -956,7 +956,7 @@ impl<'a> Inferencer<'a> {
func: Box::new(Located {
custom: Some(custom),
location: func.location,
node: ExprKind::Name { id: *id, ctx: ctx.clone() },
node: ExprKind::Name { id: *id, ctx: *ctx },
}),
args: vec![arg0],
keywords: vec![],
@ -1058,7 +1058,7 @@ impl<'a> Inferencer<'a> {
func: Box::new(Located {
custom: Some(custom),
location: func.location,
node: ExprKind::Name { id: *id, ctx: ctx.clone() },
node: ExprKind::Name { id: *id, ctx: *ctx },
}),
args: vec![arg0, arg1],
keywords: vec![],
@ -1137,7 +1137,7 @@ impl<'a> Inferencer<'a> {
func: Box::new(Located {
custom: Some(custom),
location: func.location,
node: ExprKind::Name { id: *id, ctx: ctx.clone() },
node: ExprKind::Name { id: *id, ctx: *ctx },
}),
args: vec![arg0],
keywords: vec![],
@ -1188,7 +1188,7 @@ impl<'a> Inferencer<'a> {
func: Box::new(Located {
custom: Some(custom),
location: func.location,
node: ExprKind::Name { id: *id, ctx: ctx.clone() },
node: ExprKind::Name { id: *id, ctx: *ctx },
}),
args: vec![arg0],
keywords: vec![],
@ -1237,7 +1237,7 @@ impl<'a> Inferencer<'a> {
func: Box::new(Located {
custom: Some(custom),
location: func.location,
node: ExprKind::Name { id: *id, ctx: ctx.clone() },
node: ExprKind::Name { id: *id, ctx: *ctx },
}),
args: vec![arg0, arg1],
keywords: vec![],
@ -1301,7 +1301,7 @@ impl<'a> Inferencer<'a> {
func: Box::new(Located {
custom: Some(custom),
location: func.location,
node: ExprKind::Name { id: *id, ctx: ctx.clone() },
node: ExprKind::Name { id: *id, ctx: *ctx },
}),
args: vec![arg0],
keywords,
@ -1443,12 +1443,12 @@ impl<'a> Inferencer<'a> {
&mut self,
value: &ast::Expr<Option<Type>>,
attr: StrRef,
ctx: &ExprContext,
ctx: ExprContext,
) -> InferenceResult {
let ty = value.custom.unwrap();
if let TypeEnum::TObj { fields, .. } = &*self.unifier.get_ty(ty) {
// just a fast path
match (fields.get(&attr), ctx == &ExprContext::Store) {
match (fields.get(&attr), ctx == ExprContext::Store) {
(Some((ty, true)), _) | (Some((ty, false)), false) => Ok(*ty),
(Some((_, false)), true) => {
report_error(&format!("Field `{attr}` is immutable"), value.location)
@ -1465,7 +1465,7 @@ impl<'a> Inferencer<'a> {
let attr_ty = self.unifier.get_dummy_var().0;
let fields = once((
attr.into(),
RecordField::new(attr_ty, ctx == &ExprContext::Store, Some(value.location)),
RecordField::new(attr_ty, ctx == ExprContext::Store, Some(value.location)),
))
.collect();
let record = self.unifier.add_record(fields);
@ -1486,7 +1486,7 @@ impl<'a> Inferencer<'a> {
&mut self,
location: Location,
left: &ast::Expr<Option<Type>>,
op: &ast::Operator,
op: ast::Operator,
right: &ast::Expr<Option<Type>>,
is_aug_assign: bool,
) -> InferenceResult {
@ -1522,7 +1522,7 @@ impl<'a> Inferencer<'a> {
fn infer_unary_ops(
&mut self,
location: Location,
op: &ast::Unaryop,
op: ast::Unaryop,
operand: &ast::Expr<Option<Type>>,
) -> InferenceResult {
let method = unaryop_name(op).into();
@ -1555,14 +1555,14 @@ impl<'a> Inferencer<'a> {
let mut res = None;
for (a, b, c) in izip!(once(left).chain(comparators), comparators, ops) {
let method = comparison_name(c)
let method = comparison_name(*c)
.ok_or_else(|| HashSet::from(["unsupported comparator".to_string()]))?
.into();
let ret = typeof_cmpop(
self.unifier,
self.primitives,
c,
*c,
a.custom.unwrap(),
b.custom.unwrap(),
)
@ -1604,7 +1604,7 @@ impl<'a> Inferencer<'a> {
.iter()
.map(|ndim| match *ndim {
SymbolValue::U64(v) => Ok(v),
SymbolValue::U32(v) => Ok(v as u64),
SymbolValue::U32(v) => Ok(u64::from(v)),
SymbolValue::I32(v) => u64::try_from(v).map_err(|_| {
HashSet::from([format!(
"Expected non-negative literal for ndarray.ndims, got {v}"
@ -1653,7 +1653,7 @@ impl<'a> Inferencer<'a> {
&mut self,
value: &ast::Expr<Option<Type>>,
slice: &ast::Expr<Option<Type>>,
ctx: &ExprContext,
ctx: ExprContext,
) -> InferenceResult {
let ty = self.unifier.get_dummy_var().0;
match &slice.node {
@ -1689,7 +1689,7 @@ impl<'a> Inferencer<'a> {
ind.ok_or_else(|| HashSet::from(["Index must be int32".to_string()]))?;
let map = once((
ind.into(),
RecordField::new(ty, ctx == &ExprContext::Store