From c4259d14d183b6469a7db86a0b0f5decbc51906e Mon Sep 17 00:00:00 2001 From: pca006132 Date: Sat, 16 Oct 2021 18:08:13 +0800 Subject: [PATCH] fixed some clippy warnings --- nac3artiq/src/lib.rs | 8 ++--- nac3core/src/codegen/expr.rs | 2 +- nac3core/src/codegen/mod.rs | 16 ++++----- nac3core/src/codegen/stmt.rs | 8 ++--- nac3core/src/codegen/test.rs | 10 +++--- nac3core/src/toplevel/composer.rs | 16 ++++----- nac3core/src/toplevel/helper.rs | 4 +-- nac3core/src/toplevel/mod.rs | 4 +-- nac3core/src/toplevel/test.rs | 28 ++++++++------- nac3core/src/typecheck/type_inferencer/mod.rs | 4 +-- .../src/typecheck/type_inferencer/test.rs | 34 +++++++++++-------- nac3standalone/src/basic_symbol_resolver.rs | 24 ++++++++++--- nac3standalone/src/main.rs | 4 +-- 13 files changed, 94 insertions(+), 68 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index 7e364ee5..bbd298c9 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -94,7 +94,7 @@ impl Nac3 { })?; let parser_result = parser::parse_program(&source) .map_err(|e| exceptions::PySyntaxError::new_err(format!("parse error: {}", e)))?; - let resolver = Arc::new(Box::new(Resolver { + let resolver = Arc::new(Resolver { id_to_type: self.builtins_ty.clone().into(), id_to_def: self.builtins_def.clone().into(), pyid_to_def: self.pyid_to_def.clone(), @@ -104,7 +104,7 @@ impl Nac3 { class_names: Default::default(), name_to_pyid: name_to_pyid.clone(), module: obj, - }) as Box); + }) as Arc; let mut name_to_def = HashMap::new(); let mut name_to_type = HashMap::new(); @@ -334,7 +334,7 @@ impl Nac3 { ) }; let mut synthesized = parse_program(&synthesized).unwrap(); - let resolver = Arc::new(Box::new(Resolver { + let resolver = Arc::new(Resolver { id_to_type: self.builtins_ty.clone().into(), id_to_def: self.builtins_def.clone().into(), pyid_to_def: self.pyid_to_def.clone(), @@ -344,7 +344,7 @@ impl Nac3 { class_names: Default::default(), name_to_pyid, module: module.to_object(py), - }) as Box); + }) as Arc; let (_, def_id, _) = self .composer .register_top_level( diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index b3233c1b..69a4d50a 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -159,7 +159,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { let instance = instance_to_stmt.get(&key).unwrap(); let unifiers = self.top_level.unifiers.read(); let (unifier, primitives) = &unifiers[instance.unifier_id]; - let mut unifier = Unifier::from_shared_unifier(&unifier); + let mut unifier = Unifier::from_shared_unifier(unifier); let mut type_cache = [ (self.primitives.int32, primitives.int32), diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index e01c130b..81bf3e83 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -38,7 +38,7 @@ pub struct CodeGenContext<'ctx, 'a> { pub module: Module<'ctx>, pub top_level: &'a TopLevelContext, pub unifier: Unifier, - pub resolver: Arc>, + pub resolver: Arc, pub var_assignment: HashMap>, pub type_cache: HashMap>, pub primitives: PrimitiveStore, @@ -190,7 +190,7 @@ pub struct CodeGenTask { pub body: Arc>>>, pub calls: Arc>, pub unifier: (SharedUnifier, PrimitiveStore), - pub resolver: Arc>, + pub resolver: Arc, } fn get_llvm_type<'ctx>( @@ -287,7 +287,7 @@ pub fn gen_func<'ctx>( .args .iter() .map(|arg| { - get_llvm_type(&context, &mut unifier, top_level_ctx.as_ref(), &mut type_cache, arg.ty) + get_llvm_type(context, &mut unifier, top_level_ctx.as_ref(), &mut type_cache, arg.ty) }) .collect_vec(); @@ -295,7 +295,7 @@ pub fn gen_func<'ctx>( context.void_type().fn_type(¶ms, false) } else { get_llvm_type( - &context, + context, &mut unifier, top_level_ctx.as_ref(), &mut type_cache, @@ -309,9 +309,9 @@ pub fn gen_func<'ctx>( module.get_function(symbol).unwrap_or_else(|| module.add_function(symbol, fn_type, None)); if let Some(personality) = &top_level_ctx.personality_symbol { - let personality = module.get_function(&personality).unwrap_or_else(|| { + let personality = module.get_function(personality).unwrap_or_else(|| { let ty = context.i32_type().fn_type(&[], true); - module.add_function(&personality, ty, None) + module.add_function(personality, ty, None) }); fn_val.set_personality_function(personality); } @@ -324,7 +324,7 @@ pub fn gen_func<'ctx>( for (n, arg) in task.signature.args.iter().enumerate() { let param = fn_val.get_nth_param(n as u32).unwrap(); let alloca = builder.build_alloca( - get_llvm_type(&context, &mut unifier, top_level_ctx.as_ref(), &mut type_cache, arg.ty), + get_llvm_type(context, &mut unifier, top_level_ctx.as_ref(), &mut type_cache, arg.ty), &arg.name.to_string(), ); builder.build_store(alloca, param); @@ -334,7 +334,7 @@ pub fn gen_func<'ctx>( builder.position_at_end(body_bb); let mut code_gen_context = CodeGenContext { - ctx: &context, + ctx: context, resolver: task.resolver, top_level: top_level_ctx.as_ref(), calls: task.calls, diff --git a/nac3core/src/codegen/stmt.rs b/nac3core/src/codegen/stmt.rs index 69d97354..b40d8d1c 100644 --- a/nac3core/src/codegen/stmt.rs +++ b/nac3core/src/codegen/stmt.rs @@ -96,22 +96,22 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { match &stmt.node { StmtKind::Pass => {}, StmtKind::Expr { value } => { - self.gen_expr(&value); + self.gen_expr(value); } StmtKind::Return { value } => { - let value = value.as_ref().map(|v| self.gen_expr(&v).unwrap()); + let value = value.as_ref().map(|v| self.gen_expr(v).unwrap()); let value = value.as_ref().map(|v| v as &dyn BasicValue); self.builder.build_return(value); return true; } StmtKind::AnnAssign { target, value, .. } => { if let Some(value) = value { - let value = self.gen_expr(&value).unwrap(); + let value = self.gen_expr(value).unwrap(); self.gen_assignment(target, value); } } StmtKind::Assign { targets, value, .. } => { - let value = self.gen_expr(&value).unwrap(); + let value = self.gen_expr(value).unwrap(); for target in targets.iter() { self.gen_assignment(target, value); } diff --git a/nac3core/src/codegen/test.rs b/nac3core/src/codegen/test.rs index 2c7bad40..068d6790 100644 --- a/nac3core/src/codegen/test.rs +++ b/nac3core/src/codegen/test.rs @@ -66,11 +66,11 @@ fn test_primitives() { let top_level = Arc::new(composer.make_top_level_context()); unifier.top_level = Some(top_level.clone()); - let resolver = Arc::new(Box::new(Resolver { + let resolver = Arc::new(Resolver { id_to_type: HashMap::new(), id_to_def: RwLock::new(HashMap::new()), class_names: Default::default(), - }) as Box); + }) as Arc; let threads = ["test"]; let signature = FunSignature { @@ -229,13 +229,13 @@ fn test_simple_call() { codegen_callback: None, }))); - let resolver = Box::new(Resolver { + let resolver = Resolver { id_to_type: HashMap::new(), id_to_def: RwLock::new(HashMap::new()), class_names: Default::default(), - }); + }; resolver.add_id_def("foo".into(), DefinitionId(foo_id)); - let resolver = Arc::new(resolver as Box); + let resolver = Arc::new(resolver) as Arc; if let TopLevelDef::Function { resolver: r, .. } = &mut *top_level.definitions.read()[foo_id].write() diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index c0da44f8..d471e44b 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -387,7 +387,7 @@ impl TopLevelComposer { pub fn register_top_level( &mut self, ast: ast::Stmt<()>, - resolver: Option>>, + resolver: Option>, mod_path: String, ) -> Result<(StrRef, DefinitionId, Option), String> { let defined_names = &mut self.defined_names; @@ -720,7 +720,7 @@ impl TopLevelComposer { // the function parse_ast_to make sure that no type var occured in // bast_ty if it is a CustomClassKind let base_ty = parse_ast_to_type_annotation_kinds( - class_resolver.as_ref(), + class_resolver, &temp_def_list, unifier, &self.primitives_ty, @@ -926,7 +926,7 @@ impl TopLevelComposer { .as_ref(); let type_annotation = parse_ast_to_type_annotation_kinds( - resolver.as_ref(), + resolver, temp_def_list.as_slice(), unifier, primitives_store, @@ -975,7 +975,7 @@ impl TopLevelComposer { let return_ty_annotation = { let return_annotation = returns.as_ref(); parse_ast_to_type_annotation_kinds( - resolver.as_ref(), + resolver, &temp_def_list, unifier, primitives_store, @@ -1132,7 +1132,7 @@ impl TopLevelComposer { })? .as_ref(); parse_ast_to_type_annotation_kinds( - class_resolver.as_ref(), + class_resolver, temp_def_list, unifier, primitives, @@ -1178,7 +1178,7 @@ impl TopLevelComposer { if let Some(result) = returns { let result = result.as_ref(); let annotation = parse_ast_to_type_annotation_kinds( - class_resolver.as_ref(), + class_resolver, temp_def_list, unifier, primitives, @@ -1253,8 +1253,8 @@ impl TopLevelComposer { }; let annotation = parse_ast_to_type_annotation_kinds( - class_resolver.as_ref(), - &temp_def_list, + class_resolver, + temp_def_list, unifier, primitives, annotation.as_ref(), diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index c65f74d8..afd802cf 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -91,7 +91,7 @@ impl TopLevelComposer { /// when first regitering, the type_vars, fields, methods, ancestors are invalid pub fn make_top_level_class_def( index: usize, - resolver: Option>>, + resolver: Option>, name: StrRef, constructor: Option, ) -> TopLevelDef { @@ -112,7 +112,7 @@ impl TopLevelComposer { name: String, simple_name: StrRef, ty: Type, - resolver: Option>>, + resolver: Option>, ) -> TopLevelDef { TopLevelDef::Function { name, diff --git a/nac3core/src/toplevel/mod.rs b/nac3core/src/toplevel/mod.rs index 3f9ba3da..9d7c4d71 100644 --- a/nac3core/src/toplevel/mod.rs +++ b/nac3core/src/toplevel/mod.rs @@ -91,7 +91,7 @@ pub enum TopLevelDef { // ancestor classes, including itself. ancestors: Vec, // symbol resolver of the module defined the class, none if it is built-in type - resolver: Option>>, + resolver: Option>, // constructor type constructor: Option, }, @@ -116,7 +116,7 @@ pub enum TopLevelDef { /// rigid type variables that would be substituted when the function is instantiated. instance_to_stmt: HashMap, // symbol resolver of the module defined the class - resolver: Option>>, + resolver: Option>, // custom codegen callback codegen_callback: Option> }, diff --git a/nac3core/src/toplevel/test.rs b/nac3core/src/toplevel/test.rs index e652236b..343baded 100644 --- a/nac3core/src/toplevel/test.rs +++ b/nac3core/src/toplevel/test.rs @@ -35,7 +35,13 @@ impl ResolverInternal { struct Resolver(Arc); impl SymbolResolver for Resolver { - fn get_symbol_type(&self, _: &mut Unifier, _: &[Arc>], _: &PrimitiveStore, str: StrRef) -> Option { + fn get_symbol_type( + &self, + _: &mut Unifier, + _: &[Arc>], + _: &PrimitiveStore, + str: StrRef, + ) -> Option { let ret = self.0.id_to_type.lock().get(&str).cloned(); if ret.is_none() { // println!("unknown here resolver {}", str); @@ -138,9 +144,8 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s id_to_type: Default::default(), class_names: Default::default(), }); - let resolver = Arc::new( - Box::new(Resolver(internal_resolver.clone())) as Box - ); + let resolver = + Arc::new(Resolver(internal_resolver.clone())) as Arc; for s in source { let ast = parse_program(s).unwrap(); @@ -148,7 +153,7 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s let (id, def_id, ty) = composer.register_top_level(ast, Some(resolver.clone()), "".into()).unwrap(); - internal_resolver.add_id_def(id.clone(), def_id); + internal_resolver.add_id_def(id, def_id); if let Some(ty) = ty { internal_resolver.add_id_type(id, ty); } @@ -489,9 +494,8 @@ fn test_analyze(source: Vec<&str>, res: Vec<&str>) { &mut composer.unifier, print, ); - let resolver = Arc::new( - Box::new(Resolver(internal_resolver.clone())) as Box - ); + let resolver = + Arc::new(Resolver(internal_resolver.clone())) as Arc; for s in source { let ast = parse_program(s).unwrap(); @@ -685,8 +689,8 @@ fn test_inference(source: Vec<&str>, res: Vec<&str>) { print, ); let resolver = Arc::new( - Box::new(Resolver(internal_resolver.clone())) as Box - ); + Resolver(internal_resolver.clone()) + ) as Arc; for s in source { let ast = parse_program(s).unwrap(); @@ -705,7 +709,7 @@ fn test_inference(source: Vec<&str>, res: Vec<&str>) { } } }; - internal_resolver.add_id_def(id.clone(), def_id); + internal_resolver.add_id_def(id, def_id); if let Some(ty) = ty { internal_resolver.add_id_type(id, ty); } @@ -754,7 +758,7 @@ fn make_internal_resolver_with_tvar( id_to_type: tvars .into_iter() .map(|(name, range)| { - (name.clone(), { + (name, { let (ty, id) = unifier.get_fresh_var_with_range(range.as_slice()); if print { println!("{}: {:?}, tvar{}", name, ty, id); diff --git a/nac3core/src/typecheck/type_inferencer/mod.rs b/nac3core/src/typecheck/type_inferencer/mod.rs index ce02fe13..fd4a4e8e 100644 --- a/nac3core/src/typecheck/type_inferencer/mod.rs +++ b/nac3core/src/typecheck/type_inferencer/mod.rs @@ -38,7 +38,7 @@ pub struct PrimitiveStore { } pub struct FunctionData { - pub resolver: Arc>, + pub resolver: Arc, pub return_type: Option, pub bound_variables: Vec, } @@ -91,7 +91,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> { let annotation_type = self.function_data.resolver.parse_type_annotation( top_level_defs.as_slice(), self.unifier, - &self.primitives, + self.primitives, annotation.as_ref(), )?; self.unify(annotation_type, target.custom.unwrap(), &node.location)?; diff --git a/nac3core/src/typecheck/type_inferencer/test.rs b/nac3core/src/typecheck/type_inferencer/test.rs index 6c9f8dff..180313e5 100644 --- a/nac3core/src/typecheck/type_inferencer/test.rs +++ b/nac3core/src/typecheck/type_inferencer/test.rs @@ -67,11 +67,14 @@ impl TestEnvironment { params: HashMap::new().into(), }); if let TypeEnum::TObj { fields, .. } = &*unifier.get_ty(int32) { - let add_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature { - args: vec![FuncArg { name: "other".into(), ty: int32, default_value: None }], - ret: int32, - vars: HashMap::new() - }.into())); + let add_ty = unifier.add_ty(TypeEnum::TFunc( + FunSignature { + args: vec![FuncArg { name: "other".into(), ty: int32, default_value: None }], + ret: int32, + vars: HashMap::new(), + } + .into(), + )); fields.borrow_mut().insert("__add__".into(), add_ty); } let int64 = unifier.add_ty(TypeEnum::TObj { @@ -111,11 +114,11 @@ impl TestEnvironment { let mut identifier_mapping = HashMap::new(); identifier_mapping.insert("None".into(), none); - let resolver = Arc::new(Box::new(Resolver { + let resolver = Arc::new(Resolver { id_to_type: identifier_mapping.clone(), id_to_def: Default::default(), class_names: Default::default(), - }) as Box); + }) as Arc; TestEnvironment { top_level: TopLevelContext { @@ -147,11 +150,14 @@ impl TestEnvironment { params: HashMap::new().into(), }); if let TypeEnum::TObj { fields, .. } = &*unifier.get_ty(int32) { - let add_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature { - args: vec![FuncArg { name: "other".into(), ty: int32, default_value: None }], - ret: int32, - vars: HashMap::new() - }.into())); + let add_ty = unifier.add_ty(TypeEnum::TFunc( + FunSignature { + args: vec![FuncArg { name: "other".into(), ty: int32, default_value: None }], + ret: int32, + vars: HashMap::new(), + } + .into(), + )); fields.borrow_mut().insert("__add__".into(), add_ty); } let int64 = unifier.add_ty(TypeEnum::TObj { @@ -308,7 +314,7 @@ impl TestEnvironment { personality_symbol: None, }; - let resolver = Arc::new(Box::new(Resolver { + let resolver = Arc::new(Resolver { id_to_type: identifier_mapping.clone(), id_to_def: [ ("Foo".into(), DefinitionId(5)), @@ -319,7 +325,7 @@ impl TestEnvironment { .cloned() .collect(), class_names, - }) as Box); + }) as Arc; TestEnvironment { unifier, diff --git a/nac3standalone/src/basic_symbol_resolver.rs b/nac3standalone/src/basic_symbol_resolver.rs index 4d42cc37..28061c10 100644 --- a/nac3standalone/src/basic_symbol_resolver.rs +++ b/nac3standalone/src/basic_symbol_resolver.rs @@ -1,8 +1,14 @@ use inkwell::values::BasicValueEnum; -use nac3core::{codegen::CodeGenContext, location::Location, symbol_resolver::{SymbolResolver, SymbolValue}, toplevel::{DefinitionId, TopLevelDef}, typecheck::{ +use nac3core::{ + codegen::CodeGenContext, + location::Location, + symbol_resolver::SymbolResolver, + toplevel::{DefinitionId, TopLevelDef}, + typecheck::{ type_inferencer::PrimitiveStore, typedef::{Type, Unifier}, - }}; + }, +}; use parking_lot::{Mutex, RwLock}; use rustpython_parser::ast::StrRef; use std::{collections::HashMap, sync::Arc}; @@ -26,7 +32,13 @@ impl ResolverInternal { pub struct Resolver(pub Arc); impl SymbolResolver for Resolver { - fn get_symbol_type(&self, _: &mut Unifier, _: &[Arc>], _: &PrimitiveStore, str: StrRef) -> Option { + fn get_symbol_type( + &self, + _: &mut Unifier, + _: &[Arc>], + _: &PrimitiveStore, + str: StrRef, + ) -> Option { let ret = self.0.id_to_type.lock().get(&str).cloned(); if ret.is_none() { // println!("unknown here resolver {}", str); @@ -34,7 +46,11 @@ impl SymbolResolver for Resolver { ret } - fn get_symbol_value<'ctx, 'a>(&self, _: StrRef, _: &mut CodeGenContext<'ctx, 'a>) -> Option> { + fn get_symbol_value<'ctx, 'a>( + &self, + _: StrRef, + _: &mut CodeGenContext<'ctx, 'a>, + ) -> Option> { unimplemented!() } diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index fa51958d..65740ba0 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -40,8 +40,8 @@ fn main() { class_names: Default::default(), }.into(); let resolver = Arc::new( - Box::new(Resolver(internal_resolver.clone())) as Box - ); + Resolver(internal_resolver.clone()) + ) as Arc; let setup_time = SystemTime::now(); println!("setup time: {}ms", setup_time.duration_since(start).unwrap().as_millis());