From 0bd7ba9534b69051dd157237907a19a43059d136 Mon Sep 17 00:00:00 2001 From: Aria Date: Fri, 6 Oct 2023 11:50:54 +0100 Subject: more clippy fixes --- primrose/src/analysis.rs | 49 ++++++++---------- primrose/src/description.rs | 15 ++---- primrose/src/generator.rs | 8 +-- primrose/src/inference.rs | 8 ++- primrose/src/lib.rs | 2 +- primrose/src/lib_spec_processor.rs | 102 +++++++++++++++++++++---------------- primrose/src/parser.rs | 30 +++-------- primrose/src/run_matching.rs | 2 +- primrose/src/types.rs | 16 ++---- 9 files changed, 104 insertions(+), 128 deletions(-) (limited to 'primrose') diff --git a/primrose/src/analysis.rs b/primrose/src/analysis.rs index a424e85..c76ec29 100644 --- a/primrose/src/analysis.rs +++ b/primrose/src/analysis.rs @@ -43,7 +43,7 @@ fn gen_symbolic(n: &str) -> String { ) } -fn gen_symbolics(symbolics: &Vec) -> String { +fn gen_symbolics(symbolics: &[String]) -> String { let provide = symbolics.join(" "); let mut code = String::new(); for s in symbolics.iter() { @@ -139,13 +139,13 @@ impl Analyser { let code = "(define ".to_string() + id + " " - + &self.analyse_term(&mut mterm, true, false, &mut cdr_added, &mut symbolics) + + &Self::analyse_term(&mut mterm, true, false, &mut cdr_added, &mut symbolics) + ")\n" + "(provide " + id + ")"; let filename = id.to_string() + ".rkt"; - let mut symbolics_provided = gen_symbolics(&vec!["n".to_string()]); + let mut symbolics_provided = gen_symbolics(&["n".to_string()]); if (!symbolics.is_empty()) { symbolics_provided = gen_symbolics(&symbolics); } @@ -232,7 +232,7 @@ impl Analyser { Box::new(ins.clone().into_iter().collect::>()), ); tags.push(i_tag); - match self.analyse_ref(r.deref(), vid) { + match self.analyse_ref(r.deref()) { Ok(prop_tags) => { let mut prop_tags_mut = prop_tags.clone(); tags.append(&mut prop_tags_mut); @@ -247,11 +247,7 @@ impl Analyser { } } - fn analyse_ref<'a>( - &self, - r: &'a Refinement, - vid: &'a Box, - ) -> Result, AnalyserError> { + fn analyse_ref(&self, r: &Refinement) -> Result, AnalyserError> { match r { Refinement::Prop(term) => match term.deref() { Term::AppTerm(term1, term2) => match self.retrive_ref_term(term1) { @@ -263,8 +259,8 @@ impl Analyser { }, _ => Err("Not a valid term for refining the type Con".to_string()), }, - Refinement::AndProps(r1, r2) => match self.analyse_ref(r1, vid) { - Ok(tags1) => match self.analyse_ref(r2, vid) { + Refinement::AndProps(r1, r2) => match self.analyse_ref(r1) { + Ok(tags1) => match self.analyse_ref(r2) { Ok(tags2) => Ok([tags1, tags2].concat()), Err(e) => Err(e), }, @@ -287,7 +283,6 @@ impl Analyser { } pub fn analyse_term( - &self, term: &mut Term, is_outter_app: bool, is_quantifier: bool, @@ -309,12 +304,12 @@ impl Analyser { "(list ".to_string() + id + ") " - + &self.analyse_term(t, true, false, cdr_added, symbolics) + + &Self::analyse_term(t, true, false, cdr_added, symbolics) } else { "(lambda (".to_string() + id + ") " - + &self.analyse_term(t, true, false, cdr_added, symbolics) + + &Self::analyse_term(t, true, false, cdr_added, symbolics) + ")" } } @@ -326,47 +321,47 @@ impl Analyser { Box::new(Term::VarTerm(Box::new("cdr".to_string()))), Box::new(term.clone()), ); - self.analyse_term(term, is_outter_app, is_quantifier, cdr_added, symbolics) + Self::analyse_term(term, is_outter_app, is_quantifier, cdr_added, symbolics) } else { let mut result = String::new(); match ((*t1.clone()).is_quantifier(), *t2.clone()) { (_, Term::AppTerm(_, _)) => { if (is_outter_app) { "(".to_string() - + &self.analyse_term(t1, false, false, cdr_added, symbolics) + + &Self::analyse_term(t1, false, false, cdr_added, symbolics) + " " - + &self.analyse_term(t2, true, false, cdr_added, symbolics) + + &Self::analyse_term(t2, true, false, cdr_added, symbolics) + ")" } else { - self.analyse_term(t1, false, false, cdr_added, symbolics) + Self::analyse_term(t1, false, false, cdr_added, symbolics) + " " - + &self.analyse_term(t2, true, false, cdr_added, symbolics) + + &Self::analyse_term(t2, true, false, cdr_added, symbolics) } } (false, _) => { if (is_outter_app) { "(".to_string() - + &self.analyse_term(t1, false, false, cdr_added, symbolics) + + &Self::analyse_term(t1, false, false, cdr_added, symbolics) + " " - + &self.analyse_term(t2, false, false, cdr_added, symbolics) + + &Self::analyse_term(t2, false, false, cdr_added, symbolics) + ")" } else { - self.analyse_term(t1, false, false, cdr_added, symbolics) + Self::analyse_term(t1, false, false, cdr_added, symbolics) + " " - + &self.analyse_term(t2, false, false, cdr_added, symbolics) + + &Self::analyse_term(t2, false, false, cdr_added, symbolics) } } (true, _) => { if (is_outter_app) { "(".to_string() - + &self.analyse_term(t1, false, false, cdr_added, symbolics) + + &Self::analyse_term(t1, false, false, cdr_added, symbolics) + " " - + &self.analyse_term(t2, false, true, cdr_added, symbolics) + + &Self::analyse_term(t2, false, true, cdr_added, symbolics) + ")" } else { - self.analyse_term(t1, false, false, cdr_added, symbolics) + Self::analyse_term(t1, false, false, cdr_added, symbolics) + " " - + &self.analyse_term(t2, false, true, cdr_added, symbolics) + + &Self::analyse_term(t2, false, true, cdr_added, symbolics) } } } diff --git a/primrose/src/description.rs b/primrose/src/description.rs index 70f7e66..2a6c74c 100644 --- a/primrose/src/description.rs +++ b/primrose/src/description.rs @@ -17,24 +17,15 @@ pub enum Tag { impl Tag { pub fn is_prop_tag(&self) -> bool { - match self { - Tag::Prop(_) => true, - _ => false, - } + matches!(self, Tag::Prop(..)) } pub fn is_bound_tag(&self) -> bool { - match self { - Tag::Bound(_, _) => true, - _ => false, - } + matches!(self, Tag::Bound(..)) } pub fn is_con_tag(&self) -> bool { - match self { - Tag::Con(..) => true, - _ => false, - } + matches!(self, Tag::Con(..)) } pub fn extract_prop_desc(&self) -> Description { diff --git a/primrose/src/generator.rs b/primrose/src/generator.rs index 9add18e..465fca2 100644 --- a/primrose/src/generator.rs +++ b/primrose/src/generator.rs @@ -200,8 +200,8 @@ fn write_provided_ops(provided_ops: &ProvidedOps) -> Result<(), Error> { let (code, ops) = provided_ops; let mut output = fs::File::create(ops_path)?; write!(output, "{}", LANGDECL)?; - for i in 0..code.len() { - write!(output, "{}", code[i])?; + for item in code.iter() { + write!(output, "{}", item)?; } let ops_string = ops.join(" "); let provide = "\n(provide ".to_string() + &ops_string + ")"; @@ -394,11 +394,11 @@ fn mark_src_blocks(src: String) -> String { Some(m) => { if (n > 0) { let code = &trimed_src[..n]; - result = result + CODE + &code + CODEEND; + result = result + CODE + code + CODEEND; } let spec = &trimed_src[n..(m + SPECEND.len())]; trimed_src = &trimed_src[(m + SPECEND.len())..].trim(); - result = result + &spec; + result += spec; } None => { result = result + CODE + trimed_src + CODEEND; diff --git a/primrose/src/inference.rs b/primrose/src/inference.rs index 6235aa2..0164755 100644 --- a/primrose/src/inference.rs +++ b/primrose/src/inference.rs @@ -26,9 +26,7 @@ impl DerefMut for TypeEnv { impl Types for TypeEnv { fn ftv(&self) -> HashSet { - self.values().cloned() - .collect::>() - .ftv() + self.values().cloned().collect::>().ftv() } fn apply(&self, s: &Subst) -> TypeEnv { @@ -62,7 +60,7 @@ impl TypeEnv { }, // Infer abstraction Term::LambdaTerm((n, bounds), ref e) => { - let mut tv = Type::Var(tvg.next()); + let mut tv = Type::Var(tvg.gen()); let mut env = self.clone(); if (!bounds.is_empty()) { tv = Type::Con(Box::new("Con".to_string()), Box::new(tv), bounds.clone()); @@ -98,7 +96,7 @@ impl TypeEnv { Term::AppTerm(ref e1, ref e2) => { let (s1, t1) = self.ti(e1, tvg)?; let (s2, t2) = self.apply(&s1).ti(e2, tvg)?; - let tv = Type::Var(tvg.next()); + let tv = Type::Var(tvg.gen()); let s3 = t1 .apply(&s2) .mgu(&Type::Fun(Box::new(t2), Box::new(tv.clone())))?; diff --git a/primrose/src/lib.rs b/primrose/src/lib.rs index dd9aa13..6f8c320 100644 --- a/primrose/src/lib.rs +++ b/primrose/src/lib.rs @@ -1,4 +1,4 @@ -#![allow(unused)] +#![allow(unused, clippy::new_without_default)] #![feature(linked_list_cursors)] pub mod analysis; diff --git a/primrose/src/lib_spec_processor.rs b/primrose/src/lib_spec_processor.rs index cfacbe8..8f288db 100644 --- a/primrose/src/lib_spec_processor.rs +++ b/primrose/src/lib_spec_processor.rs @@ -1,10 +1,9 @@ use std::collections::btree_map::Iter; use std::collections::BTreeMap; +use std::collections::HashMap; use std::env; use std::fs; use std::io::{BufRead, BufReader, Error, ErrorKind, Write}; -//use std::collections::hash_map::Iter; -use std::collections::HashMap; use crate::spec_map::{Bounds, LibSpecs, ProvidedOps}; @@ -21,7 +20,7 @@ const IMPLEND: &str = "*ENDIMPL*/"; type ErrorMessage = String; -fn is_next_pragma_impl(src: &String) -> bool { +fn is_next_pragma_impl(src: &str) -> bool { match src.find(IMPL) { Some(impl_pos) => match src.find(LIBSPEC) { Some(spec_pos) => impl_pos < spec_pos, @@ -31,17 +30,24 @@ fn is_next_pragma_impl(src: &String) -> bool { } } -fn has_pragma_impl(src: &String) -> bool { +fn has_pragma_impl(src: &str) -> bool { src.contains(IMPL) } -fn has_pragma_spec(src: &String) -> bool { +fn has_pragma_spec(src: &str) -> bool { src.contains(LIBSPEC) } -pub fn read_lib_file( - filename: String, -) -> Result<(String, String, Vec, String, Bounds, ProvidedOps), ErrorMessage> { +pub struct LibraryFileSpec { + spec_name: String, + struct_name: String, + specs: Vec, + provide: String, + interface_provide_map: Bounds, + provided_ops: ProvidedOps, +} + +pub fn read_lib_file(filename: String) -> Result { let contents = fs::read_to_string(filename).expect("Something went wrong reading the file"); let trimed_contents = contents.trim().to_string(); @@ -86,12 +92,17 @@ pub fn read_lib_file( trimed_contents = v5.get(1).unwrap().trim().to_string(); let lib_specs = extract_lib_specs(trimed_contents); match lib_specs { - Ok((rest, mut v, infos, mut ops)) => { - code.append(&mut v); - interface_info.insert(interface_name.clone(), infos); + Ok(LibSpec { + contents, + mut result, + op_infos, + provided_ops: mut ops, + }) => { + code.append(&mut result); + interface_info.insert(interface_name.clone(), op_infos); interfaces.push(interface_name.clone()); provided_ops.append(&mut ops); - trimed_contents = rest; + trimed_contents = contents; } Err(e) => { return Err(e); @@ -99,14 +110,14 @@ pub fn read_lib_file( } } let (provide, interface_provide_map) = generate_provide(interface_info); - Ok(( + Ok(LibraryFileSpec { spec_name, struct_name, - code.clone(), + specs: code.clone(), provide, interface_provide_map, - (code, provided_ops), - )) + provided_ops: (code, provided_ops), + }) } } } @@ -147,22 +158,19 @@ pub fn generate_provide( (provide, interface_provide_map) } -pub fn extract_lib_specs( - src: String, -) -> Result< - ( - String, - Vec, - BTreeMap, - Vec, - ), - ErrorMessage, -> { +pub struct LibSpec { + contents: String, + result: Vec, + op_infos: BTreeMap, + provided_ops: Vec, +} + +pub fn extract_lib_specs(src: String) -> Result { let mut result = Vec::::new(); let mut contents = src.trim(); let mut op_infos = BTreeMap::::new(); let mut provided_ops = Vec::::new(); - while (!contents.is_empty() && !is_next_pragma_impl(&contents.to_string())) { + while (!contents.is_empty() && !is_next_pragma_impl(contents)) { if (contents.contains(LIBSPEC) && contents.contains(LIBSPECEND)) { let v1: Vec<&str> = contents.splitn(2, LIBSPEC).collect(); let s = v1.get(1).expect("Error, invalid specification."); @@ -183,22 +191,25 @@ pub fn extract_lib_specs( break; } } - Ok((contents.to_string(), result, op_infos, provided_ops)) + Ok(LibSpec { + contents: contents.to_string(), + result, + op_infos, + provided_ops, + }) } pub fn extract_op_info(spec: String) -> Result<(String, String, String, String), ErrorMessage> { let op_name_pragmas: Vec<&str> = spec.matches(OPNAME).collect(); let op_name_end_pragmas: Vec<&str> = spec.matches(OPNAMEEND).collect(); - if (spec.chars().next().unwrap() == '/' - && op_name_pragmas.len() == 1 - && op_name_end_pragmas.len() == 1) - { + if (spec.starts_with('/') && op_name_pragmas.len() == 1 && op_name_end_pragmas.len() == 1) { let v1: Vec<&str> = spec.split(OPNAME).collect(); let s = v1 .get(1) .expect("Error, invaild operation information declaration."); let v2: Vec<&str> = s.split(OPNAMEEND).collect(); - let info_string = v2.first() + let info_string = v2 + .first() .expect("Error, invaild operation information declaration."); let mut infos: Vec<&str> = info_string.trim().split(' ').collect(); if (infos.len() == 4) { @@ -220,17 +231,13 @@ pub fn extract_op_info(spec: String) -> Result<(String, String, String, String), } } -pub fn write_lib_file( - filename: String, - contents: Vec, - provide: String, -) -> Result<(), Error> { +pub fn write_lib_file(filename: String, contents: &[String], provide: String) -> Result<(), Error> { let path = GENPATH; let mut output = fs::File::create(path.to_owned() + &filename)?; write!(output, "{}", LANGDECL)?; - for i in 0..contents.len() { - write!(output, "{}", contents[i])?; + for item in contents.iter() { + write!(output, "{}", item)?; } write!(output, "{}", provide)?; Ok(()) @@ -241,9 +248,16 @@ pub fn process_lib_spec( ) -> Result<(String, String, Bounds, ProvidedOps), ErrorMessage> { let result = read_lib_file(filename); match result { - Ok((name, struct_name, specs, provide, interface_provide_map, provided_ops)) => { - let spec_name = name + ".rkt"; - let state = write_lib_file(spec_name.clone(), specs, provide); + Ok(LibraryFileSpec { + spec_name, + struct_name, + specs, + provide, + interface_provide_map, + provided_ops, + }) => { + let spec_name = spec_name + ".rkt"; + let state = write_lib_file(spec_name.clone(), &specs, provide); if state.is_err() { return Err("Unable to create lib specification file".to_string()); } diff --git a/primrose/src/parser.rs b/primrose/src/parser.rs index 6ec2386..580c6b5 100644 --- a/primrose/src/parser.rs +++ b/primrose/src/parser.rs @@ -27,18 +27,14 @@ pub enum Term { impl Term { pub fn is_quantifier(&self) -> bool { match self { - Term::VarTerm(id) => { - id.to_string().eq("forall") - } + Term::VarTerm(id) => id.to_string().eq("forall"), _ => false, } } pub fn require_cdr(&self) -> bool { match self { - Term::VarTerm(id) => { - id.to_string().eq("pop") - } + Term::VarTerm(id) => id.to_string().eq("pop"), _ => false, } } @@ -63,17 +59,11 @@ pub enum Decl { impl Decl { pub fn is_prop_decl(&self) -> bool { - match self { - Decl::PropertyDecl(_, _) => true, - _ => false, - } + matches!(self, Decl::PropertyDecl(_, _)) } pub fn is_contype_decl(&self) -> bool { - match self { - Decl::ConTypeDecl(_, _) => true, - _ => false, - } + matches!(self, Decl::ConTypeDecl(_, _)) } pub fn get_name(&self) -> String { @@ -98,17 +88,11 @@ pub enum Block { impl Block { pub fn is_spec_block(&self) -> bool { - match self { - Block::SpecBlock(_, _) => true, - _ => false, - } + matches!(self, Block::SpecBlock(_, _)) } pub fn is_code_block(&self) -> bool { - match self { - Block::CodeBlock(_, _) => true, - _ => false, - } + matches!(self, Block::CodeBlock(_, _)) } pub fn extract_spec(&self) -> Spec { @@ -162,7 +146,7 @@ pub grammar spec() for str { -- v:id() { Term::VarTerm(Box::new(v)) } -- - "\\" v:id() _ "->" _ t:term() { Term::LambdaTerm((Box::new(v), Box::new(Bounds::new())), Box::new(t)) } + "\\" v:id() _ "->" _ t:term() { Term::LambdaTerm((Box::new(v), Box::default()), Box::new(t)) } -- "\\" v:id() _ "<:" _ "(" _ b:bounds() _ ")" _ "->" _ t:term() { Term::LambdaTerm((Box::new(v), Box::new(b)), Box::new(t)) } -- diff --git a/primrose/src/run_matching.rs b/primrose/src/run_matching.rs index d9957e9..fb912a2 100644 --- a/primrose/src/run_matching.rs +++ b/primrose/src/run_matching.rs @@ -36,7 +36,7 @@ pub fn gen_match_script( prop_spec_file: String, lib_spec_file: String, interface_spec: String, - symbolics: &Vec, + symbolics: &[String], ) -> Result { let mut output = fs::File::create(GENNAME)?; write!(output, "{}", LANGDECL)?; diff --git a/primrose/src/types.rs b/primrose/src/types.rs index 359edb5..608912b 100644 --- a/primrose/src/types.rs +++ b/primrose/src/types.rs @@ -21,17 +21,11 @@ pub enum Type { impl Type { pub fn is_var(&self) -> bool { - match self { - Type::Var(_) => true, - _ => false, - } + matches!(self, Type::Var(_)) } pub fn is_bool(&self) -> bool { - match self { - Type::Bool() => true, - _ => false, - } + matches!(self, Type::Bool()) } pub fn get_con_elem(&self) -> Option<(String, String)> { @@ -166,7 +160,7 @@ impl TypeVarGen { pub fn new() -> TypeVarGen { TypeVarGen { supply: 0 } } - pub fn next(&mut self) -> TypeVar { + pub fn gen(&mut self) -> TypeVar { let name = "#T".to_owned() + &self.supply.to_string(); let v = TypeVar::new(name); self.supply += 1; @@ -215,7 +209,7 @@ pub trait Types { fn apply(&self, s: &Subst) -> Self; } -impl<'a, T> Types for Vec +impl Types for Vec where T: Types, { @@ -290,7 +284,7 @@ impl Types for TypeScheme { impl TypeScheme { /// Instantiates a typescheme into a type. pub fn instantiate(&self, tvg: &mut TypeVarGen) -> Type { - let newvars = self.vars.iter().map(|_| Type::Var(tvg.next())); + let newvars = self.vars.iter().map(|_| Type::Var(tvg.gen())); self.ty .apply(&Subst(self.vars.iter().cloned().zip(newvars).collect())) } -- cgit v1.2.3