From 4bb0e609819d657f8a6fc63091383258bdfe5661 Mon Sep 17 00:00:00 2001 From: David Mak Date: Mon, 1 Apr 2024 16:22:40 +0800 Subject: [PATCH] core: Apply clippy suggestions --- .clippy.toml | 1 + nac3artiq/src/symbol_resolver.rs | 15 +++++++++++-- nac3core/src/codegen/numpy.rs | 32 +++++++++++++-------------- nac3core/src/typecheck/typedef/mod.rs | 8 +++++++ 4 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 .clippy.toml diff --git a/.clippy.toml b/.clippy.toml new file mode 100644 index 000000000..cef920b0d --- /dev/null +++ b/.clippy.toml @@ -0,0 +1 @@ +doc-valid-idents = ["NumPy", ".."] \ No newline at end of file diff --git a/nac3artiq/src/symbol_resolver.rs b/nac3artiq/src/symbol_resolver.rs index ccba682a9..3fb9cbbfd 100644 --- a/nac3artiq/src/symbol_resolver.rs +++ b/nac3artiq/src/symbol_resolver.rs @@ -38,10 +38,15 @@ pub enum PrimitiveValue { Bool(bool), } +/// An entry in the [`DeferredEvaluationStore`], containing the deferred types, a [`PyObject`] +/// representing the `__constraints__` of the type variables, and the name of the type to be +/// instantiated. +type DeferredEvaluationEntry = (Vec, PyObject, String); + #[derive(Clone)] pub struct DeferredEvaluationStore { needs_defer: Arc, - store: Arc, PyObject, String)>>>, + store: Arc>>, } impl DeferredEvaluationStore { @@ -53,12 +58,18 @@ impl DeferredEvaluationStore { } } +/// A class field as stored in the [`InnerResolver`], represented by the ID and name of the +/// associated [`PythonValue`]. +type ResolverField = (u64, StrRef); +/// A class field as stored in Python, represented by the `id()` and [`PyObject`] of the field. +type PyFieldHandle = (u64, PyObject); + pub struct InnerResolver { pub id_to_type: RwLock>, pub id_to_def: RwLock>, pub id_to_pyval: RwLock>, pub id_to_primitive: RwLock>, - pub field_to_val: RwLock>>, + pub field_to_val: RwLock>>, pub global_value_ids: Arc>>, pub class_names: Mutex>, pub pyid_to_def: Arc>>, diff --git a/nac3core/src/codegen/numpy.rs b/nac3core/src/codegen/numpy.rs index 92598eb01..02d5d5b02 100644 --- a/nac3core/src/codegen/numpy.rs +++ b/nac3core/src/codegen/numpy.rs @@ -697,24 +697,24 @@ pub fn gen_ndarray_eye<'ctx>( .to_basic_value_enum(context, generator, nrows_ty)?; let ncols_ty = fun.0.args[1].ty; - let ncols_arg = args.iter() - .find(|arg| arg.0.is_some_and(|name| name == fun.0.args[1].name)) - .map(|arg| arg.1.clone().to_basic_value_enum(context, generator, ncols_ty)) - .unwrap_or_else(|| { - args[0].1.clone().to_basic_value_enum(context, generator, nrows_ty) - })?; + let ncols_arg = if let Some(arg) = + args.iter().find(|arg| arg.0.is_some_and(|name| name == fun.0.args[1].name)) { + arg.1.clone().to_basic_value_enum(context, generator, ncols_ty) + } else { + args[0].1.clone().to_basic_value_enum(context, generator, nrows_ty) + }?; let offset_ty = fun.0.args[2].ty; - let offset_arg = args.iter() - .find(|arg| arg.0.is_some_and(|name| name == fun.0.args[2].name)) - .map(|arg| arg.1.clone().to_basic_value_enum(context, generator, offset_ty)) - .unwrap_or_else(|| { - Ok(context.gen_symbol_val( - generator, - fun.0.args[2].default_value.as_ref().unwrap(), - offset_ty - )) - })?; + let offset_arg = if let Some(arg) = + args.iter().find(|arg| arg.0.is_some_and(|name| name == fun.0.args[2].name)) { + arg.1.clone().to_basic_value_enum(context, generator, offset_ty) + } else { + Ok(context.gen_symbol_val( + generator, + fun.0.args[2].default_value.as_ref().unwrap(), + offset_ty + )) + }?; call_ndarray_eye_impl( generator, diff --git a/nac3core/src/typecheck/typedef/mod.rs b/nac3core/src/typecheck/typedef/mod.rs index c23b3d840..4bfddce53 100644 --- a/nac3core/src/typecheck/typedef/mod.rs +++ b/nac3core/src/typecheck/typedef/mod.rs @@ -248,6 +248,14 @@ impl Unifier { self.primitive_store.replace(*primitives); } + /// Returns the [`UnificationTable`] associated with this `Unifier`. + /// + /// # Safety + /// + /// The use of this function is discouraged under most circumstances. Only use this function if + /// in-place manipulation of type variables and/or type fields is necessary, otherwise prefer to + /// [add a new type][`Unifier::add_ty`] and [unify the type][`Unifier::unify`] with an existing + /// type. pub unsafe fn get_unification_table(&mut self) -> &mut UnificationTable> { &mut self.unification_table }