core: code revision by clippy

This commit is contained in:
lyken 2024-06-12 12:15:55 +08:00
parent 0025d89eea
commit 1cedbbcd42
2 changed files with 44 additions and 45 deletions

View File

@ -450,7 +450,7 @@ impl<'a> BuiltinBuilder<'a> {
| PrimDef::Bool
| PrimDef::Str
| PrimDef::Range
| PrimDef::None => self.build_simple_primitive_class(prim),
| PrimDef::None => Self::build_simple_primitive_class(prim),
PrimDef::Exception => self.build_exception_class_related(prim),
@ -548,7 +548,7 @@ impl<'a> BuiltinBuilder<'a> {
}
/// Build "simple" primitive classes.
fn build_simple_primitive_class(&self, prim: PrimDef) -> TopLevelDef {
fn build_simple_primitive_class(prim: PrimDef) -> TopLevelDef {
debug_assert_prim_is_allowed(
prim,
&[
@ -624,9 +624,9 @@ impl<'a> BuiltinBuilder<'a> {
type_vars: vec![self.option_tvar.0],
fields: vec![],
methods: vec![
self.create_method(PrimDef::OptionIsSome, self.is_some_ty.0),
self.create_method(PrimDef::OptionIsNone, self.is_some_ty.0),
self.create_method(PrimDef::OptionUnwrap, self.unwrap_ty.0),
Self::create_method(PrimDef::OptionIsSome, self.is_some_ty.0),
Self::create_method(PrimDef::OptionIsNone, self.is_some_ty.0),
Self::create_method(PrimDef::OptionUnwrap, self.unwrap_ty.0),
],
ancestors: vec![TypeAnnotation::CustomClass {
id: prim.id(),
@ -673,10 +673,10 @@ impl<'a> BuiltinBuilder<'a> {
let returned_int = match prim {
PrimDef::OptionIsNone => {
ctx.builder.build_is_null(ptr, prim.simple_name().into())
ctx.builder.build_is_null(ptr, prim.simple_name())
}
PrimDef::OptionIsSome => {
ctx.builder.build_is_not_null(ptr, prim.simple_name().into())
ctx.builder.build_is_not_null(ptr, prim.simple_name())
}
_ => unreachable!(),
};
@ -737,8 +737,8 @@ impl<'a> BuiltinBuilder<'a> {
type_vars: vec![self.ndarray_dtype_tvar.0, self.ndarray_ndims_tvar.0],
fields: Vec::default(),
methods: vec![
self.create_method(PrimDef::NDArrayCopy, self.ndarray_copy_ty.0),
self.create_method(PrimDef::NDArrayFill, self.ndarray_fill_ty.0),
Self::create_method(PrimDef::NDArrayCopy, self.ndarray_copy_ty.0),
Self::create_method(PrimDef::NDArrayFill, self.ndarray_fill_ty.0),
],
ancestors: Vec::default(),
constructor: None,
@ -870,7 +870,7 @@ impl<'a> BuiltinBuilder<'a> {
);
create_fn_by_codegen(
&mut self.unifier,
self.unifier,
&[(common_ndim.1, common_ndim.0), (p0_ty.1, p0_ty.0), (ret_ty.1, ret_ty.0)]
.into_iter()
.collect(),
@ -889,17 +889,14 @@ impl<'a> BuiltinBuilder<'a> {
/// Build the functions `ceil()` and `floor()` and their 64 bit variants.
fn build_ceil_floor_function(&mut self, prim: PrimDef) -> TopLevelDef {
#[derive(Clone, Copy)]
enum Kind { Floor, Ceil }
debug_assert_prim_is_allowed(
prim,
&[PrimDef::FunFloor, PrimDef::FunFloor64, PrimDef::FunCeil, PrimDef::FunCeil64],
);
#[derive(Clone, Copy)]
enum Kind {
Floor,
Ceil,
}
let (size_variant, kind) = {
match prim {
PrimDef::FunFloor => (SizeVariant::Bits32, Kind::Floor),
@ -936,7 +933,7 @@ impl<'a> BuiltinBuilder<'a> {
);
create_fn_by_codegen(
&mut self.unifier,
self.unifier,
&[(common_ndim.1, common_ndim.0), (p0_ty.1, p0_ty.0), (ret_ty.1, ret_ty.0)]
.into_iter()
.collect(),
@ -965,7 +962,7 @@ impl<'a> BuiltinBuilder<'a> {
);
create_fn_by_codegen(
&mut self.unifier,
self.unifier,
&VarMap::new(),
prim.name(),
self.ndarray_float,
@ -1036,7 +1033,7 @@ impl<'a> BuiltinBuilder<'a> {
let tv = self.unifier.get_fresh_var(Some("T".into()), None);
create_fn_by_codegen(
&mut self.unifier,
self.unifier,
&[(tv.1, tv.0)].into_iter().collect(),
prim.name(),
self.primitives.ndarray,
@ -1085,7 +1082,7 @@ impl<'a> BuiltinBuilder<'a> {
}
}
PrimDef::FunNpIdentity => create_fn_by_codegen(
&mut self.unifier,
self.unifier,
&VarMap::new(),
prim.name(),
self.ndarray_float_2d,
@ -1257,7 +1254,7 @@ impl<'a> BuiltinBuilder<'a> {
debug_assert_prim_is_allowed(prim, &[PrimDef::FunNpCeil, PrimDef::FunNpFloor]);
create_fn_by_codegen(
&mut self.unifier,
self.unifier,
&self.float_or_ndarray_var_map,
prim.name(),
self.float_or_ndarray_ty.0,
@ -1281,7 +1278,7 @@ impl<'a> BuiltinBuilder<'a> {
let prim = PrimDef::FunNpRound;
create_fn_by_codegen(
&mut self.unifier,
self.unifier,
&self.float_or_ndarray_var_map,
prim.name(),
self.float_or_ndarray_ty.0,
@ -1461,7 +1458,7 @@ impl<'a> BuiltinBuilder<'a> {
.collect::<IndexMap<_, _>>();
create_fn_by_codegen(
&mut self.unifier,
self.unifier,
&var_map,
prim.name(),
ret_ty.0,
@ -1566,7 +1563,7 @@ impl<'a> BuiltinBuilder<'a> {
let PrimitiveStore { bool, float, .. } = *self.primitives;
create_fn_by_codegen(
&mut self.unifier,
self.unifier,
&VarMap::new(),
prim.name(),
bool,
@ -1630,7 +1627,7 @@ impl<'a> BuiltinBuilder<'a> {
};
create_fn_by_codegen(
&mut self.unifier,
self.unifier,
&self.float_or_ndarray_var_map,
prim.name(),
self.float_or_ndarray_ty.0,
@ -1705,13 +1702,13 @@ impl<'a> BuiltinBuilder<'a> {
// The argument types of the two input arguments are controlled here.
let (x1_ty, x2_ty) = match prim {
PrimDef::FunNpArctan2 => (float, float),
PrimDef::FunNpCopysign => (float, float),
PrimDef::FunNpFmax => (float, float),
PrimDef::FunNpFmin => (float, float),
PrimDef::FunNpArctan2
| PrimDef::FunNpCopysign
| PrimDef::FunNpFmax
| PrimDef::FunNpFmin
| PrimDef::FunNpHypot
| PrimDef::FunNpNextAfter => (float, float),
PrimDef::FunNpLdExp => (float, int32),
PrimDef::FunNpHypot => (float, float),
PrimDef::FunNpNextAfter => (float, float),
_ => unreachable!(),
};
@ -1763,7 +1760,7 @@ impl<'a> BuiltinBuilder<'a> {
}
}
fn create_method(&self, prim: PrimDef, method_ty: Type) -> (StrRef, Type, DefinitionId) {
fn create_method(prim: PrimDef, method_ty: Type) -> (StrRef, Type, DefinitionId) {
(prim.simple_name().into(), method_ty, prim.id())
}

View File

@ -118,24 +118,27 @@ impl PrimDef {
///
/// The assigned definition ID is defined by the position this [`PrimDef`] enum unit variant is defined at,
/// with the first `PrimDef`'s definition id being `0`.
#[must_use]
pub fn id(&self) -> DefinitionId {
return DefinitionId(*self as usize);
DefinitionId(*self as usize)
}
/// Check if a definition ID is that of a [`PrimDef`].
#[must_use]
pub fn contains_id(id: DefinitionId) -> bool {
Self::iter().any(|prim| prim.id() == id)
}
/// Get the definition "simple_name" of this [`PrimDef`].
/// Get the definition "simple name" of this [`PrimDef`].
///
/// If the [`PrimDef`] is a function, this corresponds to [`TopLevelDef::Function::simple_name`].
///
/// If the [`PrimDef`] is a class, this returns [`None`].
#[must_use]
pub fn simple_name(&self) -> &'static str {
match self.details() {
PrimDefDetails::PrimFunction { simple_name, .. } => simple_name,
PrimDefDetails::PrimClass {..} => panic!("PrimDef {:?} has no simple_name as it is not a function.", self),
PrimDefDetails::PrimClass {..} => panic!("PrimDef {self:?} has no simple_name as it is not a function."),
}
}
@ -144,15 +147,17 @@ impl PrimDef {
/// If the [`PrimDef`] is a function, this corresponds to [`TopLevelDef::Function::name`].
///
/// If the [`PrimDef`] is a class, this corresponds to [`TopLevelDef::Class::name`].
#[must_use]
pub fn name(&self) -> &'static str {
match self.details() {
PrimDefDetails::PrimFunction { name, .. } => name,
PrimDefDetails::PrimClass { name } => name,
PrimDefDetails::PrimFunction { name, .. }
| PrimDefDetails::PrimClass { name } => name,
}
}
/// Get the associated details of this [`PrimDef`]
fn details(&self) -> PrimDefDetails {
#[must_use]
fn details(self) -> PrimDefDetails {
fn class(name: &'static str) -> PrimDefDetails {
PrimDefDetails::PrimClass { name }
}
@ -261,12 +266,9 @@ impl PrimDef {
pub fn debug_assert_prim_is_allowed(prim: PrimDef, allowlist: &[PrimDef]) {
if cfg!(debug_assertions) {
let allowed = allowlist.iter().any(|p| *p == prim);
if !allowed {
panic!(
"Disallowed primitive definition. Got {:?}, but expects it to be in {:?}",
prim, allowlist
)
}
assert!(allowed,
"Disallowed primitive definition. Got {prim:?}, but expects it to be in {allowlist:?}"
);
}
}