From 8adc6c76b8de9e1e2f25edf5f1ec40db7df0445f Mon Sep 17 00:00:00 2001 From: Pranav Gaddamadugu Date: Sun, 26 Mar 2023 23:18:03 -0700 Subject: [PATCH] Fmts and clippies --- build.rs | 13 +- compiler/ast/src/common/global_consts_json.rs | 10 +- compiler/ast/src/common/identifier.rs | 13 +- compiler/ast/src/common/imported_modules.rs | 5 +- compiler/ast/src/common/positive_number.rs | 3 +- compiler/ast/src/expressions/binary.rs | 68 +++++----- compiler/ast/src/expressions/struct_init.rs | 10 +- compiler/ast/src/expressions/tuple.rs | 10 +- compiler/ast/src/functions/external.rs | 6 +- compiler/ast/src/functions/finalize.rs | 20 +-- compiler/ast/src/functions/mod.rs | 13 +- compiler/ast/src/input/input_value.rs | 1 + compiler/ast/src/input/program_input.rs | 6 +- compiler/ast/src/lib.rs | 5 +- compiler/ast/src/mapping/mod.rs | 6 +- compiler/ast/src/passes/reconstructor.rs | 47 ++----- compiler/ast/src/passes/visitor.rs | 10 +- compiler/ast/src/program/mod.rs | 5 +- compiler/ast/src/program/program_id.rs | 3 +- compiler/ast/src/statement/block.rs | 4 +- compiler/ast/src/statement/iteration.rs | 9 +- compiler/ast/src/statement/mod.rs | 5 +- compiler/ast/src/types/tuple.rs | 6 +- compiler/ast/src/types/type_.rs | 7 +- compiler/ast/src/value/mod.rs | 40 +++--- compiler/compiler/src/compiler.rs | 14 +- compiler/compiler/tests/compile.rs | 3 +- compiler/compiler/tests/execute.rs | 32 +++-- compiler/compiler/tests/utilities/mod.rs | 17 +-- compiler/parser/examples/input_parser.rs | 16 +-- compiler/parser/examples/parser.rs | 6 +- compiler/parser/src/lib.rs | 3 +- compiler/parser/src/parser/context.rs | 22 +--- compiler/parser/src/parser/expression.rs | 53 ++------ compiler/parser/src/parser/file.rs | 95 +++----------- compiler/parser/src/parser/input.rs | 14 +- compiler/parser/src/parser/mod.rs | 3 +- compiler/parser/src/parser/statement.rs | 42 +----- compiler/parser/src/test.rs | 16 +-- compiler/parser/src/tokenizer/lexer.rs | 51 ++------ .../passes/src/code_generation/generator.rs | 3 +- compiler/passes/src/code_generation/mod.rs | 3 +- .../src/code_generation/visit_expressions.rs | 28 ++-- .../src/code_generation/visit_program.rs | 32 ++--- .../src/code_generation/visit_statements.rs | 19 ++- compiler/passes/src/common/assigner/mod.rs | 5 +- compiler/passes/src/common/graph/mod.rs | 8 +- .../passes/src/common/rename_table/mod.rs | 5 +- .../passes/src/common/symbol_table/mod.rs | 6 +- .../dead_code_eliminator.rs | 5 +- .../eliminate_statement.rs | 54 ++++---- .../src/flattening/flatten_expression.rs | 46 +++---- .../src/flattening/flatten_statement.rs | 81 ++++++------ compiler/passes/src/flattening/flattener.rs | 40 +++--- .../function_inlining/assignment_renamer.rs | 35 +++-- .../function_inlining/inline_expression.rs | 23 ++-- .../src/function_inlining/inline_program.rs | 8 +- .../src/function_inlining/inline_statement.rs | 52 +++----- .../src/loop_unrolling/range_iterator.rs | 9 +- .../src/loop_unrolling/unroll_program.rs | 7 +- .../src/loop_unrolling/unroll_statement.rs | 61 ++++----- .../passes/src/loop_unrolling/unroller.rs | 40 +++--- .../rename_expression.rs | 115 ++++++++--------- .../rename_program.rs | 49 +++---- .../rename_statement.rs | 69 +++++----- .../static_single_assigner.rs | 7 +- .../src/symbol_table_creation/creator.rs | 24 ++-- .../src/type_checking/check_expressions.rs | 51 +++----- .../passes/src/type_checking/check_program.rs | 94 ++++---------- .../src/type_checking/check_statements.rs | 122 ++++++------------ compiler/passes/src/type_checking/checker.rs | 75 ++--------- compiler/span/src/source_map.rs | 30 +---- compiler/span/src/span.rs | 5 +- compiler/span/src/span_json.rs | 9 +- compiler/span/src/symbol.rs | 24 ++-- docs/grammar/src/main.rs | 8 +- errors/src/common/backtraced.rs | 24 +--- errors/src/common/formatted.rs | 30 +---- errors/src/emitter/mod.rs | 12 +- leo/commands/build.rs | 29 ++--- leo/commands/clean.rs | 3 +- leo/commands/deploy.rs | 6 +- leo/commands/new.rs | 10 +- leo/commands/run.rs | 13 +- leo/context.rs | 4 +- leo/logger.rs | 10 +- leo/main.rs | 39 ++---- leo/package/src/build/directory.rs | 7 +- leo/package/src/imports/directory.rs | 7 +- leo/package/src/inputs/input.rs | 13 +- leo/package/src/lib.rs | 3 +- leo/package/src/outputs/ast_snapshot.rs | 24 ++-- leo/package/src/outputs/checksum.rs | 13 +- leo/package/src/outputs/circuit.rs | 13 +- leo/package/src/outputs/directory.rs | 7 +- leo/package/src/package.rs | 7 +- leo/package/src/root/gitignore.rs | 3 +- leo/package/src/source/main.rs | 8 +- tests/test-framework/benches/leo_compiler.rs | 120 +++++------------ tests/test-framework/src/error.rs | 97 +++----------- tests/test-framework/src/fetch.rs | 9 +- tests/test-framework/src/runner.rs | 49 ++----- 102 files changed, 839 insertions(+), 1685 deletions(-) diff --git a/build.rs b/build.rs index dd0ccb993a..1f93ce3426 100644 --- a/build.rs +++ b/build.rs @@ -26,17 +26,8 @@ use walkdir::WalkDir; const EXPECTED_LICENSE_TEXT: &str = include_str!(".resources/license_header"); // The following directories will be excluded from the license scan. -const DIRS_TO_SKIP: [&str; 9] = [ - ".cargo", - ".circleci", - ".git", - ".github", - ".resources", - "docs", - "examples", - "target", - "tests", -]; +const DIRS_TO_SKIP: [&str; 9] = + [".cargo", ".circleci", ".git", ".github", ".resources", "docs", "examples", "target", "tests"]; fn compare_license_text(path: &Path, expected_lines: &[&str]) { let file = File::open(path).unwrap(); diff --git a/compiler/ast/src/common/global_consts_json.rs b/compiler/ast/src/common/global_consts_json.rs index dfc61a24f8..91cf5246d2 100644 --- a/compiler/ast/src/common/global_consts_json.rs +++ b/compiler/ast/src/common/global_consts_json.rs @@ -29,10 +29,7 @@ pub fn serialize( let joined: IndexMap = global_consts .into_iter() .map(|(idents, program)| { - ( - idents.iter().map(|i| i.name.to_string()).collect::>().join(","), - program.clone(), - ) + (idents.iter().map(|i| i.name.to_string()).collect::>().join(","), program.clone()) }) .collect(); @@ -47,10 +44,7 @@ pub fn deserialize<'de, D: Deserializer<'de>>( .map(|(name, program)| { ( name.split(',') - .map(|ident_name| Identifier { - name: Symbol::intern(ident_name), - span: Default::default(), - }) + .map(|ident_name| Identifier { name: Symbol::intern(ident_name), span: Default::default() }) .collect::>(), program, ) diff --git a/compiler/ast/src/common/identifier.rs b/compiler/ast/src/common/identifier.rs index e255c9a0ed..03dd0ae659 100644 --- a/compiler/ast/src/common/identifier.rs +++ b/compiler/ast/src/common/identifier.rs @@ -20,9 +20,13 @@ use leo_span::{Span, Symbol}; use crate::{simple_node_impl, Node}; use serde::{ de::{ - Visitor, {self}, + Visitor, + {self}, }, - Deserialize, Deserializer, Serialize, Serializer, + Deserialize, + Deserializer, + Serialize, + Serializer, }; use std::{ collections::BTreeMap, @@ -48,10 +52,7 @@ simple_node_impl!(Identifier); impl Identifier { /// Constructs a new identifier with `name` and a default span. pub fn new(name: Symbol) -> Self { - Self { - name, - span: Span::default(), - } + Self { name, span: Span::default() } } /// Check if the Identifier name matches the other name. diff --git a/compiler/ast/src/common/imported_modules.rs b/compiler/ast/src/common/imported_modules.rs index 3872b86027..16fc81547e 100644 --- a/compiler/ast/src/common/imported_modules.rs +++ b/compiler/ast/src/common/imported_modules.rs @@ -30,10 +30,7 @@ pub fn serialize( imported_modules .into_iter() .map(|(package, program)| { - let package = package - .iter() - .map(|x| x.as_str(s, |s| s.to_owned())) - .collect::>(); + let package = package.iter().map(|x| x.as_str(s, |s| s.to_owned())).collect::>(); (package.join("."), program.clone()) }) .collect() diff --git a/compiler/ast/src/common/positive_number.rs b/compiler/ast/src/common/positive_number.rs index 4227cc6b5e..c7e817eb8d 100644 --- a/compiler/ast/src/common/positive_number.rs +++ b/compiler/ast/src/common/positive_number.rs @@ -15,8 +15,7 @@ // along with the Leo library. If not, see . use serde::{Deserialize, Serialize}; -use std::fmt; -use std::str::FromStr; +use std::{fmt, str::FromStr}; /// A number string guaranteed to be positive. #[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, Hash)] diff --git a/compiler/ast/src/expressions/binary.rs b/compiler/ast/src/expressions/binary.rs index d6a476ecd6..565039c78d 100644 --- a/compiler/ast/src/expressions/binary.rs +++ b/compiler/ast/src/expressions/binary.rs @@ -86,42 +86,38 @@ pub enum BinaryOperation { impl fmt::Display for BinaryOperation { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}", - match self { - Self::Add => "+", - Self::AddWrapped => "add_wrapped", - Self::And => "&&", - Self::BitwiseAnd => "&", - Self::Div => "/", - Self::DivWrapped => "div_wrapped", - Self::Eq => "==", - Self::Gte => ">=", - Self::Gt => ">", - Self::Lte => "<=", - Self::Lt => "<", - Self::Mod => "mod", - Self::Mul => "*", - Self::MulWrapped => "mul_wrapped", - Self::Nand => "NAND", - Self::Neq => "!=", - Self::Nor => "NOR", - Self::Or => "||", - Self::BitwiseOr => "|", - Self::Pow => "**", - Self::PowWrapped => "pow_wrapped", - Self::Rem => "%", - Self::RemWrapped => "rem_wrapped", - Self::Shl => "<<", - Self::ShlWrapped => "shl_wrapped", - Self::Shr => ">>", - Self::ShrWrapped => "shr_wrapped", - Self::Sub => "-", - Self::SubWrapped => "sub_wrapped", - Self::Xor => "^", - } - ) + write!(f, "{}", match self { + Self::Add => "+", + Self::AddWrapped => "add_wrapped", + Self::And => "&&", + Self::BitwiseAnd => "&", + Self::Div => "/", + Self::DivWrapped => "div_wrapped", + Self::Eq => "==", + Self::Gte => ">=", + Self::Gt => ">", + Self::Lte => "<=", + Self::Lt => "<", + Self::Mod => "mod", + Self::Mul => "*", + Self::MulWrapped => "mul_wrapped", + Self::Nand => "NAND", + Self::Neq => "!=", + Self::Nor => "NOR", + Self::Or => "||", + Self::BitwiseOr => "|", + Self::Pow => "**", + Self::PowWrapped => "pow_wrapped", + Self::Rem => "%", + Self::RemWrapped => "rem_wrapped", + Self::Shl => "<<", + Self::ShlWrapped => "shl_wrapped", + Self::Shr => ">>", + Self::ShrWrapped => "shr_wrapped", + Self::Sub => "-", + Self::SubWrapped => "sub_wrapped", + Self::Xor => "^", + }) } } diff --git a/compiler/ast/src/expressions/struct_init.rs b/compiler/ast/src/expressions/struct_init.rs index 23de07fac7..4c32f3f71f 100644 --- a/compiler/ast/src/expressions/struct_init.rs +++ b/compiler/ast/src/expressions/struct_init.rs @@ -82,15 +82,7 @@ impl StructExpression { impl fmt::Display for StructExpression { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{{{}}}", - self.members - .iter() - .map(|x| x.to_string()) - .collect::>() - .join(", ") - ) + write!(f, "{{{}}}", self.members.iter().map(|x| x.to_string()).collect::>().join(", ")) } } diff --git a/compiler/ast/src/expressions/tuple.rs b/compiler/ast/src/expressions/tuple.rs index 929f7ad032..a31970bdc7 100644 --- a/compiler/ast/src/expressions/tuple.rs +++ b/compiler/ast/src/expressions/tuple.rs @@ -30,15 +30,7 @@ pub struct TupleExpression { impl fmt::Display for TupleExpression { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "({})", - self.elements - .iter() - .map(|x| x.to_string()) - .collect::>() - .join(",") - ) + write!(f, "({})", self.elements.iter().map(|x| x.to_string()).collect::>().join(",")) } } diff --git a/compiler/ast/src/functions/external.rs b/compiler/ast/src/functions/external.rs index 7bad65c11a..faa9f08a80 100644 --- a/compiler/ast/src/functions/external.rs +++ b/compiler/ast/src/functions/external.rs @@ -41,11 +41,7 @@ impl External { impl fmt::Display for External { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}: {}.leo/{}.record", - self.identifier, self.program_name, self.record - ) + write!(f, "{}: {}.leo/{}.record", self.identifier, self.program_name, self.record) } } diff --git a/compiler/ast/src/functions/finalize.rs b/compiler/ast/src/functions/finalize.rs index d79acb1220..135929a0eb 100644 --- a/compiler/ast/src/functions/finalize.rs +++ b/compiler/ast/src/functions/finalize.rs @@ -47,14 +47,7 @@ impl Finalize { _ => Type::Tuple(Tuple(output.iter().map(|output| output.type_()).collect())), }; - Self { - identifier, - input, - output, - output_type, - block, - span, - } + Self { identifier, input, output, output_type, block, span } } } @@ -64,16 +57,9 @@ impl fmt::Display for Finalize { let returns = match self.output.len() { 0 => "()".to_string(), 1 => self.output[0].to_string(), - _ => format!( - "({})", - self.output.iter().map(|x| x.to_string()).collect::>().join(",") - ), + _ => format!("({})", self.output.iter().map(|x| x.to_string()).collect::>().join(",")), }; - write!( - f, - " finalize {}({parameters}) -> {returns} {}", - self.identifier, self.block - ) + write!(f, " finalize {}({parameters}) -> {returns} {}", self.identifier, self.block) } } diff --git a/compiler/ast/src/functions/mod.rs b/compiler/ast/src/functions/mod.rs index 17c73fe8c2..813788bc14 100644 --- a/compiler/ast/src/functions/mod.rs +++ b/compiler/ast/src/functions/mod.rs @@ -97,18 +97,9 @@ impl Function { _ => Type::Tuple(Tuple(output.iter().map(|output| get_output_type(output)).collect())), }; - Function { - annotations, - variant, - identifier, - input, - output, - output_type, - block, - finalize, - span, - } + Function { annotations, variant, identifier, input, output, output_type, block, finalize, span } } + /// Returns function name. pub fn name(&self) -> Symbol { self.identifier.name diff --git a/compiler/ast/src/input/input_value.rs b/compiler/ast/src/input/input_value.rs index 4eb0adc1ba..a2aa0452a9 100644 --- a/compiler/ast/src/input/input_value.rs +++ b/compiler/ast/src/input/input_value.rs @@ -31,6 +31,7 @@ pub enum InputValue { impl TryFrom<(Type, Expression)> for InputValue { type Error = LeoError; + fn try_from(value: (Type, Expression)) -> Result { Ok(match value { (type_, Expression::Literal(lit)) => match (type_, lit) { diff --git a/compiler/ast/src/input/program_input.rs b/compiler/ast/src/input/program_input.rs index e58ac623cd..0490bfe6f7 100644 --- a/compiler/ast/src/input/program_input.rs +++ b/compiler/ast/src/input/program_input.rs @@ -24,6 +24,7 @@ pub struct ProgramInput { impl TryFrom for ProgramInput { type Error = LeoError; + fn try_from(input: InputAst) -> Result { let mut main = IndexMap::new(); @@ -34,10 +35,7 @@ impl TryFrom for ProgramInput { }; for definition in section.definitions { - target.insert( - definition.name.name, - InputValue::try_from((definition.type_, definition.value))?, - ); + target.insert(definition.name.name, InputValue::try_from((definition.type_, definition.value))?); } } diff --git a/compiler/ast/src/lib.rs b/compiler/ast/src/lib.rs index 0e5e71a7b2..fa2151664c 100644 --- a/compiler/ast/src/lib.rs +++ b/compiler/ast/src/lib.rs @@ -157,10 +157,7 @@ impl AsRef for Ast { pub(crate) fn remove_key_from_json(value: serde_json::Value, key: &str) -> serde_json::Value { match value { serde_json::Value::Object(map) => serde_json::Value::Object( - map.into_iter() - .filter(|(k, _)| k != key) - .map(|(k, v)| (k, remove_key_from_json(v, key))) - .collect(), + map.into_iter().filter(|(k, _)| k != key).map(|(k, v)| (k, remove_key_from_json(v, key))).collect(), ), serde_json::Value::Array(values) => { serde_json::Value::Array(values.into_iter().map(|v| remove_key_from_json(v, key)).collect()) diff --git a/compiler/ast/src/mapping/mod.rs b/compiler/ast/src/mapping/mod.rs index 62e1bbd8d5..9212a8f55b 100644 --- a/compiler/ast/src/mapping/mod.rs +++ b/compiler/ast/src/mapping/mod.rs @@ -36,11 +36,7 @@ pub struct Mapping { impl fmt::Display for Mapping { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "mapping {}: {} => {}", - self.identifier, self.key_type, self.value_type - ) + write!(f, "mapping {}: {} => {}", self.identifier, self.key_type, self.value_type) } } diff --git a/compiler/ast/src/passes/reconstructor.rs b/compiler/ast/src/passes/reconstructor.rs index 6ba2ffe5d5..dc7ab8e8c8 100644 --- a/compiler/ast/src/passes/reconstructor.rs +++ b/compiler/ast/src/passes/reconstructor.rs @@ -47,11 +47,7 @@ pub trait ExpressionReconstructor { AccessExpression::AssociatedFunction(AssociatedFunction { ty: function.ty, name: function.name, - args: function - .args - .into_iter() - .map(|arg| self.reconstruct_expression(arg).0) - .collect(), + args: function.args.into_iter().map(|arg| self.reconstruct_expression(arg).0).collect(), span: function.span, }) } @@ -87,11 +83,7 @@ pub trait ExpressionReconstructor { ( Expression::Call(CallExpression { function: Box::new(self.reconstruct_expression(*input.function).0), - arguments: input - .arguments - .into_iter() - .map(|arg| self.reconstruct_expression(arg).0) - .collect(), + arguments: input.arguments.into_iter().map(|arg| self.reconstruct_expression(arg).0).collect(), external: input.external, span: input.span, }), @@ -130,11 +122,7 @@ pub trait ExpressionReconstructor { fn reconstruct_tuple(&mut self, input: TupleExpression) -> (Expression, Self::AdditionalOutput) { ( Expression::Tuple(TupleExpression { - elements: input - .elements - .into_iter() - .map(|element| self.reconstruct_expression(element).0) - .collect(), + elements: input.elements.into_iter().map(|element| self.reconstruct_expression(element).0).collect(), span: input.span, }), Default::default(), @@ -212,11 +200,7 @@ pub trait StatementReconstructor: ExpressionReconstructor { fn reconstruct_block(&mut self, input: Block) -> (Block, Self::AdditionalOutput) { ( Block { - statements: input - .statements - .into_iter() - .map(|s| self.reconstruct_statement(s).0) - .collect(), + statements: input.statements.into_iter().map(|s| self.reconstruct_statement(s).0).collect(), span: input.span, }, Default::default(), @@ -326,10 +310,7 @@ pub trait StatementReconstructor: ExpressionReconstructor { Statement::Return(ReturnStatement { expression: self.reconstruct_expression(input.expression).0, finalize_arguments: input.finalize_arguments.map(|arguments| { - arguments - .into_iter() - .map(|argument| self.reconstruct_expression(argument).0) - .collect() + arguments.into_iter().map(|argument| self.reconstruct_expression(argument).0).collect() }), span: input.span, }), @@ -358,21 +339,9 @@ pub trait ProgramReconstructor: StatementReconstructor { fn reconstruct_program_scope(&mut self, input: ProgramScope) -> ProgramScope { ProgramScope { program_id: input.program_id, - structs: input - .structs - .into_iter() - .map(|(i, c)| (i, self.reconstruct_struct(c))) - .collect(), - mappings: input - .mappings - .into_iter() - .map(|(id, mapping)| (id, self.reconstruct_mapping(mapping))) - .collect(), - functions: input - .functions - .into_iter() - .map(|(i, f)| (i, self.reconstruct_function(f))) - .collect(), + structs: input.structs.into_iter().map(|(i, c)| (i, self.reconstruct_struct(c))).collect(), + mappings: input.mappings.into_iter().map(|(id, mapping)| (id, self.reconstruct_mapping(mapping))).collect(), + functions: input.functions.into_iter().map(|(i, f)| (i, self.reconstruct_function(f))).collect(), span: input.span, } } diff --git a/compiler/ast/src/passes/visitor.rs b/compiler/ast/src/passes/visitor.rs index 06cc2c0c47..aa8a2d0abf 100644 --- a/compiler/ast/src/passes/visitor.rs +++ b/compiler/ast/src/passes/visitor.rs @@ -214,10 +214,7 @@ pub trait ProgramVisitor<'a>: StatementVisitor<'a> { fn visit_program(&mut self, input: &'a Program) { input.imports.values().for_each(|import| self.visit_import(&import.0)); - input - .program_scopes - .values() - .for_each(|scope| self.visit_program_scope(scope)); + input.program_scopes.values().for_each(|scope| self.visit_program_scope(scope)); } fn visit_program_scope(&mut self, input: &'a ProgramScope) { @@ -225,10 +222,7 @@ pub trait ProgramVisitor<'a>: StatementVisitor<'a> { input.mappings.values().for_each(|mapping| self.visit_mapping(mapping)); - input - .functions - .values() - .for_each(|function| self.visit_function(function)); + input.functions.values().for_each(|function| self.visit_function(function)); } fn visit_import(&mut self, input: &'a Program) { diff --git a/compiler/ast/src/program/mod.rs b/compiler/ast/src/program/mod.rs index a821ff0e81..f5b0d85c4c 100644 --- a/compiler/ast/src/program/mod.rs +++ b/compiler/ast/src/program/mod.rs @@ -53,9 +53,6 @@ impl fmt::Display for Program { impl Default for Program { /// Constructs an empty program node. fn default() -> Self { - Self { - imports: IndexMap::new(), - program_scopes: IndexMap::new(), - } + Self { imports: IndexMap::new(), program_scopes: IndexMap::new() } } } diff --git a/compiler/ast/src/program/program_id.rs b/compiler/ast/src/program/program_id.rs index 100d510e46..77f36c9010 100644 --- a/compiler/ast/src/program/program_id.rs +++ b/compiler/ast/src/program/program_id.rs @@ -17,8 +17,7 @@ use crate::Identifier; use core::fmt; -use serde::de::Visitor; -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +use serde::{de, de::Visitor, Deserialize, Deserializer, Serialize, Serializer}; use std::collections::BTreeMap; /// An identifier for a program that is eventually deployed to the network. diff --git a/compiler/ast/src/statement/block.rs b/compiler/ast/src/statement/block.rs index dde981dcc3..15b959e3db 100644 --- a/compiler/ast/src/statement/block.rs +++ b/compiler/ast/src/statement/block.rs @@ -35,9 +35,7 @@ impl fmt::Display for Block { if self.statements.is_empty() { writeln!(f, "\t")?; } else { - self.statements - .iter() - .try_for_each(|statement| writeln!(f, "\t{statement}"))?; + self.statements.iter().try_for_each(|statement| writeln!(f, "\t{statement}"))?; } write!(f, "}}") } diff --git a/compiler/ast/src/statement/iteration.rs b/compiler/ast/src/statement/iteration.rs index 0236152db4..b79cae2a19 100644 --- a/compiler/ast/src/statement/iteration.rs +++ b/compiler/ast/src/statement/iteration.rs @@ -19,8 +19,7 @@ use crate::{Block, Expression, Identifier, Node, Type, Value}; use leo_span::Span; use serde::{Deserialize, Serialize}; -use std::cell::RefCell; -use std::fmt; +use std::{cell::RefCell, fmt}; /// A bounded `for` loop statement `for variable in start .. =? stop block`. #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] @@ -51,11 +50,7 @@ pub struct IterationStatement { impl fmt::Display for IterationStatement { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let eq = if self.inclusive { "=" } else { "" }; - write!( - f, - "for {} in {}..{eq}{} {}", - self.variable, self.start, self.stop, self.block - ) + write!(f, "for {} in {}..{eq}{} {}", self.variable, self.start, self.stop, self.block) } } diff --git a/compiler/ast/src/statement/mod.rs b/compiler/ast/src/statement/mod.rs index 40c005448e..599e0b247b 100644 --- a/compiler/ast/src/statement/mod.rs +++ b/compiler/ast/src/statement/mod.rs @@ -84,10 +84,7 @@ pub enum Statement { impl Statement { /// Returns a dummy statement made from an empty block `{}`. pub fn dummy(span: Span) -> Self { - Self::Block(Block { - statements: Vec::new(), - span, - }) + Self::Block(Block { statements: Vec::new(), span }) } } diff --git a/compiler/ast/src/types/tuple.rs b/compiler/ast/src/types/tuple.rs index de8fdbb4fa..d5e7bc4c35 100644 --- a/compiler/ast/src/types/tuple.rs +++ b/compiler/ast/src/types/tuple.rs @@ -35,10 +35,6 @@ impl Deref for Tuple { impl fmt::Display for Tuple { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "({})", - self.0.iter().map(|x| x.to_string()).collect::>().join(",") - ) + write!(f, "({})", self.0.iter().map(|x| x.to_string()).collect::>().join(",")) } } diff --git a/compiler/ast/src/types/type_.rs b/compiler/ast/src/types/type_.rs index 0dcd10be10..f938e8fe7b 100644 --- a/compiler/ast/src/types/type_.rs +++ b/compiler/ast/src/types/type_.rs @@ -70,10 +70,9 @@ impl Type { (Type::Mapping(left), Type::Mapping(right)) => { left.key.eq_flat(&right.key) && left.value.eq_flat(&right.value) } - (Type::Tuple(left), Type::Tuple(right)) if left.len() == right.len() => left - .iter() - .zip_eq(right.iter()) - .all(|(left_type, right_type)| left_type.eq_flat(right_type)), + (Type::Tuple(left), Type::Tuple(right)) if left.len() == right.len() => { + left.iter().zip_eq(right.iter()).all(|(left_type, right_type)| left_type.eq_flat(right_type)) + } (Type::Identifier(left), Type::Identifier(right)) => left.matches(right), _ => false, } diff --git a/compiler/ast/src/value/mod.rs b/compiler/ast/src/value/mod.rs index dedfc894cd..860005d230 100644 --- a/compiler/ast/src/value/mod.rs +++ b/compiler/ast/src/value/mod.rs @@ -201,26 +201,6 @@ pub enum Value { } impl Value { - // TODO: This is temporary since the currently unused code is used in constant folding. - #[allow(dead_code)] - pub(crate) fn is_supported_const_fold_type(&self) -> bool { - use Value::*; - matches!( - self, - Boolean(_, _) - | I8(_, _) - | I16(_, _) - | I32(_, _) - | I64(_, _) - | I128(_, _) - | U8(_, _) - | U16(_, _) - | U32(_, _) - | U64(_, _) - | U128(_, _) - ) - } - implement_const_unary!( @overflowing name: abs, @@ -720,6 +700,26 @@ impl Value { [U128, [U128], U128, u128, u128] ] ); + + // TODO: This is temporary since the currently unused code is used in constant folding. + #[allow(dead_code)] + pub(crate) fn is_supported_const_fold_type(&self) -> bool { + use Value::*; + matches!( + self, + Boolean(_, _) + | I8(_, _) + | I16(_, _) + | I32(_, _) + | I64(_, _) + | I128(_, _) + | U8(_, _) + | U16(_, _) + | U32(_, _) + | U64(_, _) + | U128(_, _) + ) + } } impl Display for Value { diff --git a/compiler/compiler/src/compiler.rs b/compiler/compiler/src/compiler.rs index a79610b09f..f44c82b24a 100644 --- a/compiler/compiler/src/compiler.rs +++ b/compiler/compiler/src/compiler.rs @@ -19,16 +19,13 @@ //! The [`Compiler`] type compiles Leo programs into R1CS circuits. use leo_ast::Program; pub use leo_ast::{Ast, InputAst}; -use leo_errors::emitter::Handler; -use leo_errors::{CompilerError, Result}; +use leo_errors::{emitter::Handler, CompilerError, Result}; pub use leo_passes::SymbolTable; use leo_passes::*; -use leo_span::source_map::FileName; -use leo_span::symbol::with_session_globals; +use leo_span::{source_map::FileName, symbol::with_session_globals}; use sha2::{Digest, Sha256}; -use std::fs; -use std::path::PathBuf; +use std::{fs, path::PathBuf}; use crate::CompilerOptions; @@ -273,10 +270,7 @@ impl<'a> Compiler<'a> { fn write_ast_to_json(&self, file_suffix: &str) -> Result<()> { // Remove `Span`s if they are not enabled. if self.compiler_options.spans_enabled { - self.ast.to_json_file( - self.output_directory.clone(), - &format!("{}.{file_suffix}", self.program_name), - )?; + self.ast.to_json_file(self.output_directory.clone(), &format!("{}.{file_suffix}", self.program_name))?; } else { self.ast.to_json_file_without_keys( self.output_directory.clone(), diff --git a/compiler/compiler/tests/compile.rs b/compiler/compiler/tests/compile.rs index 5972858dde..285d0cd5ce 100644 --- a/compiler/compiler/tests/compile.rs +++ b/compiler/compiler/tests/compile.rs @@ -29,8 +29,7 @@ use snarkvm::prelude::*; use crate::utilities::{get_cwd_option, hash_asts, hash_content, setup_build_directory}; use serde::{Deserialize, Serialize}; use serde_yaml::Value; -use std::rc::Rc; -use std::{fs, path::Path}; +use std::{fs, path::Path, rc::Rc}; struct CompileNamespace; diff --git a/compiler/compiler/tests/execute.rs b/compiler/compiler/tests/execute.rs index 8565447fa8..732ad97591 100644 --- a/compiler/compiler/tests/execute.rs +++ b/compiler/compiler/tests/execute.rs @@ -15,8 +15,16 @@ // along with the Leo library. If not, see . mod utilities; -use utilities::{buffer_if_err, compile_and_process, parse_program, BufferEmitter}; -use utilities::{get_cwd_option, setup_build_directory, Aleo, Network}; +use utilities::{ + buffer_if_err, + compile_and_process, + get_cwd_option, + parse_program, + setup_build_directory, + Aleo, + BufferEmitter, + Network, +}; use crate::utilities::{hash_asts, hash_content}; @@ -27,15 +35,13 @@ use leo_test_framework::{ Test, }; -use snarkvm::console; -use snarkvm::prelude::*; +use snarkvm::{console, prelude::*}; use leo_test_framework::test::TestExpectationMode; use regex::Regex; use serde::{Deserialize, Serialize}; use serde_yaml::Value; -use std::collections::BTreeMap; -use std::{fs, path::Path, rc::Rc}; +use std::{collections::BTreeMap, fs, path::Path, rc::Rc}; // TODO: Evaluate namespace. struct ExecuteNamespace; @@ -69,10 +75,7 @@ struct ExecuteOutput { fn run_test(test: Test, handler: &Handler, err_buf: &BufferEmitter) -> Result { // Check that config expectation is always pass. if test.config.expectation != TestExpectationMode::Pass { - buffer_if_err( - err_buf, - Err("Test expectation must be `Pass` for `Execute` tests.".to_string()), - )?; + buffer_if_err(err_buf, Err("Test expectation must be `Pass` for `Execute` tests.".to_string()))?; } // Check for CWD option: @@ -86,13 +89,8 @@ fn run_test(test: Test, handler: &Handler, err_buf: &BufferEmitter) -> Result>>, - pub Rc>>, -); +pub struct BufferEmitter(pub Rc>>, pub Rc>>); impl Emitter for BufferEmitter { fn emit_err(&mut self, err: LeoError) { @@ -181,9 +178,7 @@ pub fn buffer_if_err(buf: &BufferEmitter, res: Result) -> Result PathBuf { - tempfile::tempdir() - .expect("Failed to open temporary directory") - .into_path() + tempfile::tempdir().expect("Failed to open temporary directory").into_path() } pub fn compile_and_process<'a>(parsed: &'a mut Compiler<'a>) -> Result { diff --git a/compiler/parser/examples/input_parser.rs b/compiler/parser/examples/input_parser.rs index 075550b974..09c9469e21 100644 --- a/compiler/parser/examples/input_parser.rs +++ b/compiler/parser/examples/input_parser.rs @@ -26,10 +26,7 @@ use std::{ }; #[derive(Debug, StructOpt)] -#[structopt( - name = "input parser", - about = "Parse an Input file and save its JSON representation" -)] +#[structopt(name = "input parser", about = "Parse an Input file and save its JSON representation")] struct Opt { /// Path to the input file. #[structopt(parse(from_os_str))] @@ -47,10 +44,7 @@ struct Opt { fn main() -> Result<(), String> { let opt = Opt::parse(); let input_tree = create_session_if_not_set_then(|s| { - let input_string = s - .source_map - .load_file(&opt.input_path) - .expect("failed to open an input file"); + let input_string = s.source_map.load_file(&opt.input_path).expect("failed to open an input file"); Handler::with(|handler| { let input = leo_parser::parse_program_inputs(handler, &input_string.src, input_string.start_pos)?; @@ -64,11 +58,7 @@ fn main() -> Result<(), String> { } let out_path = if let Some(out_dir) = opt.out_dir_path { - format!( - "{}/{}.json", - out_dir.as_path().display(), - opt.input_path.file_stem().unwrap().to_str().unwrap() - ) + format!("{}/{}.json", out_dir.as_path().display(), opt.input_path.file_stem().unwrap().to_str().unwrap()) } else { format!("./{}.json", opt.input_path.file_stem().unwrap().to_str().unwrap()) }; diff --git a/compiler/parser/examples/parser.rs b/compiler/parser/examples/parser.rs index 1cb208e505..246ddef0ff 100644 --- a/compiler/parser/examples/parser.rs +++ b/compiler/parser/examples/parser.rs @@ -62,11 +62,7 @@ fn main() -> Result<(), String> { } let out_path = if let Some(out_dir) = opt.out_dir_path { - format!( - "{}/{}.json", - out_dir.as_path().display(), - opt.input_path.file_stem().unwrap().to_str().unwrap() - ) + format!("{}/{}.json", out_dir.as_path().display(), opt.input_path.file_stem().unwrap().to_str().unwrap()) } else { format!("./{}.json", opt.input_path.file_stem().unwrap().to_str().unwrap()) }; diff --git a/compiler/parser/src/lib.rs b/compiler/parser/src/lib.rs index 21ad6b98b8..fb14cbc0ba 100644 --- a/compiler/parser/src/lib.rs +++ b/compiler/parser/src/lib.rs @@ -32,8 +32,7 @@ pub mod parser; pub use parser::*; use leo_ast::{input::InputData, Ast, ProgramInput}; -use leo_errors::emitter::Handler; -use leo_errors::Result; +use leo_errors::{emitter::Handler, Result}; #[cfg(test)] mod test; diff --git a/compiler/parser/src/parser/context.rs b/compiler/parser/src/parser/context.rs index 9c63a5dc2f..d283c5fe3b 100644 --- a/compiler/parser/src/parser/context.rs +++ b/compiler/parser/src/parser/context.rs @@ -17,12 +17,10 @@ use crate::{tokenizer::*, Token}; use leo_ast::*; -use leo_errors::emitter::Handler; -use leo_errors::{ParserError, ParserWarning, Result}; +use leo_errors::{emitter::Handler, ParserError, ParserWarning, Result}; use leo_span::{Span, Symbol}; -use std::fmt::Display; -use std::mem; +use std::{fmt::Display, mem}; /// Stores a program in tokenized format plus additional context. /// May be converted into a [`Program`] AST by parsing all tokens. @@ -44,10 +42,7 @@ pub(crate) struct ParserContext<'a> { } /// Dummy span used to appease borrow checker. -const DUMMY_EOF: SpannedToken = SpannedToken { - token: Token::Eof, - span: Span::dummy(), -}; +const DUMMY_EOF: SpannedToken = SpannedToken { token: Token::Eof, span: Span::dummy() }; impl<'a> ParserContext<'a> { /// Returns a new [`ParserContext`] type given a vector of tokens. @@ -81,10 +76,7 @@ impl<'a> ParserContext<'a> { } // Extract next token, or `Eof` if there was none. - let next_token = self.tokens.pop().unwrap_or(SpannedToken { - token: Token::Eof, - span: self.token.span, - }); + let next_token = self.tokens.pop().unwrap_or(SpannedToken { token: Token::Eof, span: self.token.span }); // Set the new token. self.prev_token = mem::replace(&mut self.token, next_token); @@ -183,11 +175,7 @@ impl<'a> ParserContext<'a> { /// Eats the expected `token`, or errors. pub(super) fn expect(&mut self, token: &Token) -> Result { - if self.eat(token) { - Ok(self.prev_token.span) - } else { - self.unexpected(token) - } + if self.eat(token) { Ok(self.prev_token.span) } else { self.unexpected(token) } } /// Eats one of the expected `tokens`, or errors. diff --git a/compiler/parser/src/parser/expression.rs b/compiler/parser/src/parser/expression.rs index b8737aa90a..a6890db78f 100644 --- a/compiler/parser/src/parser/expression.rs +++ b/compiler/parser/src/parser/expression.rs @@ -215,10 +215,7 @@ impl ParserContext<'_> { /// /// Otherwise, tries to parse the next token using [`parse_exponential_expression`]. fn parse_multiplicative_expression(&mut self) -> Result { - self.parse_bin_expr( - &[Token::Mul, Token::Div, Token::Rem], - Self::parse_exponential_expression, - ) + self.parse_bin_expr(&[Token::Mul, Token::Div, Token::Rem], Self::parse_exponential_expression) } /// Returns an [`Expression`] AST node if the next tokens represent a @@ -266,11 +263,7 @@ impl ParserContext<'_> { Expression::Literal(Literal::Integer(integer_type, format!("-{string}"), op_span + span)) } // Otherwise, produce a unary expression. - _ => Expression::Unary(UnaryExpression { - span: op_span + inner.span(), - op, - receiver: Box::new(inner), - }), + _ => Expression::Unary(UnaryExpression { span: op_span + inner.span(), op, receiver: Box::new(inner) }), }; } Ok(inner) @@ -285,11 +278,7 @@ impl ParserContext<'_> { if let (true, Some(op)) = (args.is_empty(), UnaryOperation::from_symbol(method.name)) { // Found an unary operator and the argument list is empty. - Ok(Expression::Unary(UnaryExpression { - span, - op, - receiver: Box::new(receiver), - })) + Ok(Expression::Unary(UnaryExpression { span, op, receiver: Box::new(receiver) })) } else if let (1, Some(op)) = (args.len(), BinaryOperation::from_symbol(method.name)) { // Found a binary operator and the argument list contains a single argument. Ok(Expression::Binary(BinaryExpression { @@ -359,11 +348,8 @@ impl ParserContext<'_> { if self.check_int() { // Eat a tuple member access. let (index, span) = self.eat_integer()?; - expr = Expression::Access(AccessExpression::Tuple(TupleAccess { - tuple: Box::new(expr), - index, - span, - })) + expr = + Expression::Access(AccessExpression::Tuple(TupleAccess { tuple: Box::new(expr), index, span })) } else if self.eat(&Token::Leo) { // Eat an external function call. self.eat(&Token::Div); // todo: Make `/` a more general token. @@ -484,11 +470,7 @@ impl ParserContext<'_> { let end_span = check_ahead(dist, &Token::Group)?; dist += 1; // Standing at `)` so advance one for 'group'. - let gt = GroupTuple { - span: start_span + &end_span, - x: first_gc, - y: second_gc, - }; + let gt = GroupTuple { span: start_span + &end_span, x: first_gc, y: second_gc }; // Eat everything so that this isn't just peeking. for _ in 0..dist { @@ -525,15 +507,10 @@ impl ParserContext<'_> { /// struct initialization expression. /// let foo = Foo { x: 1u8 }; pub fn parse_struct_init_expression(&mut self, identifier: Identifier) -> Result { - let (members, _, end) = self.parse_list(Delimiter::Brace, Some(Token::Comma), |p| { - p.parse_struct_member().map(Some) - })?; + let (members, _, end) = + self.parse_list(Delimiter::Brace, Some(Token::Comma), |p| p.parse_struct_member().map(Some))?; - Ok(Expression::Struct(StructExpression { - span: identifier.span + end, - name: identifier, - members, - })) + Ok(Expression::Struct(StructExpression { span: identifier.span + end, name: identifier, members })) } /// Returns an [`Expression`] AST node if the next token is a primary expression: @@ -600,14 +577,10 @@ impl ParserContext<'_> { Expression::Identifier(ident) } } - Token::SelfLower => Expression::Identifier(Identifier { - name: sym::SelfLower, - span, - }), - t if crate::type_::TYPE_TOKENS.contains(&t) => Expression::Identifier(Identifier { - name: t.keyword_to_symbol().unwrap(), - span, - }), + Token::SelfLower => Expression::Identifier(Identifier { name: sym::SelfLower, span }), + t if crate::type_::TYPE_TOKENS.contains(&t) => { + Expression::Identifier(Identifier { name: t.keyword_to_symbol().unwrap(), span }) + } token => { return Err(ParserError::unexpected_str(token, "expression", span).into()); } diff --git a/compiler/parser/src/parser/file.rs b/compiler/parser/src/parser/file.rs index 5d96423c8c..c95bc358f9 100644 --- a/compiler/parser/src/parser/file.rs +++ b/compiler/parser/src/parser/file.rs @@ -17,8 +17,7 @@ use super::*; use crate::parse_ast; use leo_errors::{CompilerError, ParserError, Result}; -use leo_span::source_map::FileName; -use leo_span::symbol::with_session_globals; +use leo_span::{source_map::FileName, symbol::with_session_globals}; use std::fs; @@ -57,10 +56,7 @@ impl ParserContext<'_> { return Err(ParserError::missing_program_scope(self.token.span).into()); } - Ok(Program { - imports, - program_scopes, - }) + Ok(Program { imports, program_scopes }) } fn unexpected_item(token: &SpannedToken, expected: &[Token]) -> ParserError { @@ -162,19 +158,16 @@ impl ParserContext<'_> { } Token::RightCurly => break, _ => { - return Err(Self::unexpected_item( - &self.token, - &[ - Token::Struct, - Token::Record, - Token::Mapping, - Token::At, - Token::Function, - Token::Transition, - Token::Inline, - ], - ) - .into()) + return Err(Self::unexpected_item(&self.token, &[ + Token::Struct, + Token::Record, + Token::Mapping, + Token::At, + Token::Function, + Token::Transition, + Token::Inline, + ]) + .into()); } } } @@ -182,13 +175,7 @@ impl ParserContext<'_> { // Parse `}`. let end = self.expect(&Token::RightCurly)?; - Ok(ProgramScope { - program_id, - functions, - structs, - mappings, - span: start + end, - }) + Ok(ProgramScope { program_id, functions, structs, mappings, span: start + end }) } /// Returns a [`Vec`] AST node if the next tokens represent a struct member. @@ -236,12 +223,7 @@ impl ParserContext<'_> { let (identifier, type_, span) = self.parse_typed_ident()?; - Ok(Member { - mode, - identifier, - type_, - span, - }) + Ok(Member { mode, identifier, type_, span }) } /// Parses a struct or record definition, e.g., `struct Foo { ... }` or `record Foo { ... }`. @@ -253,15 +235,7 @@ impl ParserContext<'_> { self.expect(&Token::LeftCurly)?; let (members, end) = self.parse_struct_members()?; - Ok(( - struct_name.name, - Struct { - identifier: struct_name, - members, - is_record, - span: start + end, - }, - )) + Ok((struct_name.name, Struct { identifier: struct_name, members, is_record, span: start + end })) } /// Parses a mapping declaration, e.g. `mapping balances: address => u128`. @@ -273,15 +247,7 @@ impl ParserContext<'_> { self.expect(&Token::BigArrow)?; let (value_type, _) = self.parse_type()?; let end = self.expect(&Token::Semicolon)?; - Ok(( - identifier.name, - Mapping { - identifier, - key_type, - value_type, - span: start + end, - }, - )) + Ok((identifier.name, Mapping { identifier, key_type, value_type, span: start + end })) } // TODO: Return a span associated with the mode. @@ -332,21 +298,11 @@ impl ParserContext<'_> { self.eat(&Token::Record); span = span + self.prev_token.span; - Ok(functions::Input::External(External { - identifier: name, - program_name: external, - record, - span, - })) + Ok(functions::Input::External(External { identifier: name, program_name: external, record, span })) } else { let type_ = self.parse_type()?.0; - Ok(functions::Input::Internal(FunctionInput { - identifier: name, - mode, - type_, - span: name.span, - })) + Ok(functions::Input::Internal(FunctionInput { identifier: name, mode, type_, span: name.span })) } } @@ -389,10 +345,7 @@ impl ParserContext<'_> { } fn peek_is_external(&self) -> bool { - matches!( - (&self.token.token, self.look_ahead(1, |t| &t.token)), - (Token::Identifier(_), Token::Dot) - ) + matches!((&self.token.token, self.look_ahead(1, |t| &t.token)), (Token::Identifier(_), Token::Dot)) } /// Returns an [`Annotation`] AST node if the next tokens represent an annotation. @@ -400,10 +353,7 @@ impl ParserContext<'_> { // Parse the `@` symbol and identifier. let start = self.expect(&Token::At)?; let identifier = match self.token.token { - Token::Program => Identifier { - name: sym::program, - span: self.expect(&Token::Program)?, - }, + Token::Program => Identifier { name: sym::program, span: self.expect(&Token::Program)? }, _ => self.expect_identifier()?, }; let span = start + identifier.span; @@ -490,10 +440,7 @@ impl ParserContext<'_> { }; let span = start + block.span; - Ok(( - name.name, - Function::new(annotations, variant, name, inputs, output, block, finalize, span), - )) + Ok((name.name, Function::new(annotations, variant, name, inputs, output, block, finalize, span))) } } diff --git a/compiler/parser/src/parser/input.rs b/compiler/parser/src/parser/input.rs index 58bcaf50bc..4a02c9c720 100644 --- a/compiler/parser/src/parser/input.rs +++ b/compiler/parser/src/parser/input.rs @@ -55,11 +55,7 @@ impl ParserContext<'_> { definitions.push(self.parse_input_definition()?); } - Ok(Section { - name: section.name, - span: section.span, - definitions, - }) + Ok(Section { name: section.name, span: section.span, definitions }) } /// Parses a single parameter definition: @@ -75,12 +71,6 @@ impl ParserContext<'_> { let value = self.parse_unary_expression()?; self.expect(&Token::Semicolon)?; - Ok(Definition { - mode, - name, - type_, - value, - span, - }) + Ok(Definition { mode, name, type_, value, span }) } } diff --git a/compiler/parser/src/parser/mod.rs b/compiler/parser/src/parser/mod.rs index 51554a902c..023952a75b 100644 --- a/compiler/parser/src/parser/mod.rs +++ b/compiler/parser/src/parser/mod.rs @@ -22,8 +22,7 @@ use crate::{tokenizer::*, Token}; use leo_ast::*; -use leo_errors::emitter::Handler; -use leo_errors::Result; +use leo_errors::{emitter::Handler, Result}; use leo_span::Span; use indexmap::IndexMap; diff --git a/compiler/parser/src/parser/statement.rs b/compiler/parser/src/parser/statement.rs index 92406a1e02..f15e5a5965 100644 --- a/compiler/parser/src/parser/statement.rs +++ b/compiler/parser/src/parser/statement.rs @@ -133,10 +133,7 @@ impl ParserContext<'_> { } else { // Parse the expression as a statement. let end = self.expect(&Token::Semicolon)?; - Ok(Statement::Expression(ExpressionStatement { - span: place.span() + end, - expression: place, - })) + Ok(Statement::Expression(ExpressionStatement { span: place.span() + end, expression: place })) } } @@ -174,11 +171,7 @@ impl ParserContext<'_> { }; let end = self.expect(&Token::Semicolon)?; let span = start + end; - Ok(ReturnStatement { - span, - expression, - finalize_arguments: finalize_args, - }) + Ok(ReturnStatement { span, expression, finalize_arguments: finalize_args }) } /// Returns a [`DecrementStatement`] AST node if the next tokens represent a decrement statement. @@ -194,12 +187,7 @@ impl ParserContext<'_> { let end = self.expect(&Token::RightParen)?; self.expect(&Token::Semicolon)?; let span = start + end; - Ok(DecrementStatement { - mapping, - index, - amount, - span, - }) + Ok(DecrementStatement { mapping, index, amount, span }) } /// Returns an [`IncrementStatement`] AST node if the next tokens represent an increment statement. @@ -215,12 +203,7 @@ impl ParserContext<'_> { let end = self.expect(&Token::RightParen)?; self.expect(&Token::Semicolon)?; let span = start + end; - Ok(IncrementStatement { - mapping, - index, - amount, - span, - }) + Ok(IncrementStatement { mapping, index, amount, span }) } /// Returns a [`ConditionalStatement`] AST node if the next tokens represent a conditional statement. @@ -316,18 +299,13 @@ impl ParserContext<'_> { )); ( Default::default(), - ConsoleFunction::Assert(Expression::Err(ErrExpression { - span: Default::default(), - })), + ConsoleFunction::Assert(Expression::Err(ErrExpression { span: Default::default() })), ) } }; self.expect(&Token::Semicolon)?; - Ok(ConsoleStatement { - span: keyword + span, - function, - }) + Ok(ConsoleStatement { span: keyword + span, function }) } /// Returns a [`DefinitionStatement`] AST node if the next tokens represent a definition statement. @@ -349,12 +327,6 @@ impl ParserContext<'_> { let value = self.parse_expression()?; self.expect(&Token::Semicolon)?; - Ok(DefinitionStatement { - span: decl_span + value.span(), - declaration_type: decl_type, - place, - type_, - value, - }) + Ok(DefinitionStatement { span: decl_span + value.span(), declaration_type: decl_type, place, type_, value }) } } diff --git a/compiler/parser/src/test.rs b/compiler/parser/src/test.rs index 08caf27fad..7c77f40308 100644 --- a/compiler/parser/src/test.rs +++ b/compiler/parser/src/test.rs @@ -40,13 +40,7 @@ impl Namespace for TokenNamespace { fn run_test(&self, test: Test) -> Result { create_session_if_not_set_then(|s| { tokenize(test, s).map(|tokens| { - Value::String( - tokens - .into_iter() - .map(|x| x.to_string()) - .collect::>() - .join(","), - ) + Value::String(tokens.into_iter().map(|x| x.to_string()).collect::>().join(",")) }) }) } @@ -71,9 +65,7 @@ fn with_handler( ) -> Result { let (handler, buf) = Handler::new_with_buf(); let mut tokens = ParserContext::new(&handler, tokens); - let parsed = handler - .extend_if_error(logic(&mut tokens)) - .map_err(|_| buf.extract_errs().to_string())?; + let parsed = handler.extend_if_error(logic(&mut tokens)).map_err(|_| buf.extract_errs().to_string())?; not_fully_consumed(&mut tokens)?; Ok(parsed) } @@ -84,9 +76,7 @@ fn tokenize(test: Test, s: &SessionGlobals) -> Result, String> } fn all_are_comments(tokens: &[SpannedToken]) -> bool { - tokens - .iter() - .all(|x| matches!(x.token, Token::CommentLine(_) | Token::CommentBlock(_))) + tokens.iter().all(|x| matches!(x.token, Token::CommentLine(_) | Token::CommentBlock(_))) } fn yaml_or_fail(value: T) -> Value { diff --git a/compiler/parser/src/tokenizer/lexer.rs b/compiler/parser/src/tokenizer/lexer.rs index 1d92eef35f..9407fdfc06 100644 --- a/compiler/parser/src/tokenizer/lexer.rs +++ b/compiler/parser/src/tokenizer/lexer.rs @@ -196,11 +196,7 @@ impl Token { // Otherwise, return the `first_token` that matches the one character. let match_two = |input: &mut Peekable<_>, first_token, second_char, second_token| { input.next(); - Ok(if input.next_if_eq(&second_char).is_some() { - (2, second_token) - } else { - (1, first_token) - }) + Ok(if input.next_if_eq(&second_char).is_some() { (2, second_token) } else { (1, first_token) }) }; // Returns one token matching one or two characters. @@ -281,7 +277,7 @@ impl Token { Token::And, '=', Token::AndAssign, - ) + ); } '(' => return match_one(&mut input, Token::LeftParen), ')' => return match_one(&mut input, Token::RightParen), @@ -296,7 +292,7 @@ impl Token { Token::Pow, '=', Token::PowAssign, - ) + ); } '+' => return match_two(&mut input, Token::Add, '=', Token::AddAssign), ',' => return match_one(&mut input, Token::Comma), @@ -350,30 +346,8 @@ impl Token { '%' => return match_two(&mut input, Token::Rem, '=', Token::RemAssign), ':' => return match_two(&mut input, Token::Colon, ':', Token::DoubleColon), ';' => return match_one(&mut input, Token::Semicolon), - '<' => { - return match_four( - &mut input, - Token::Lt, - '=', - Token::LtEq, - '<', - Token::Shl, - '=', - Token::ShlAssign, - ) - } - '>' => { - return match_four( - &mut input, - Token::Gt, - '=', - Token::GtEq, - '>', - Token::Shr, - '=', - Token::ShrAssign, - ) - } + '<' => return match_four(&mut input, Token::Lt, '=', Token::LtEq, '<', Token::Shl, '=', Token::ShlAssign), + '>' => return match_four(&mut input, Token::Gt, '=', Token::GtEq, '>', Token::Shr, '=', Token::ShrAssign), '=' => return match_three(&mut input, Token::Assign, '=', Token::Eq, '>', Token::BigArrow), '[' => return match_one(&mut input, Token::LeftSquare), ']' => return match_one(&mut input, Token::RightSquare), @@ -389,7 +363,7 @@ impl Token { Token::Or, '=', Token::OrAssign, - ) + ); } '^' => return match_two(&mut input, Token::BitXor, '=', Token::BitXorAssign), '@' => return Ok((1, Token::At)), @@ -451,12 +425,8 @@ impl Token { )); } - Err(ParserError::could_not_lex( - input - .take_while(|c| *c != ';' && !c.is_whitespace()) - .collect::(), - ) - .into()) + Err(ParserError::could_not_lex(input.take_while(|c| *c != ';' && !c.is_whitespace()).collect::()) + .into()) } } @@ -469,10 +439,7 @@ pub struct SpannedToken { impl SpannedToken { /// Returns a dummy token at a dummy span. pub const fn dummy() -> Self { - Self { - token: Token::Question, - span: Span::dummy(), - } + Self { token: Token::Question, span: Span::dummy() } } } diff --git a/compiler/passes/src/code_generation/generator.rs b/compiler/passes/src/code_generation/generator.rs index 4734945b6c..981c919a38 100644 --- a/compiler/passes/src/code_generation/generator.rs +++ b/compiler/passes/src/code_generation/generator.rs @@ -14,8 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::SymbolTable; -use crate::{CallGraph, StructGraph}; +use crate::{CallGraph, StructGraph, SymbolTable}; use leo_ast::Function; use leo_span::Symbol; diff --git a/compiler/passes/src/code_generation/mod.rs b/compiler/passes/src/code_generation/mod.rs index 1a441b0abe..551e23539c 100644 --- a/compiler/passes/src/code_generation/mod.rs +++ b/compiler/passes/src/code_generation/mod.rs @@ -25,8 +25,7 @@ mod visit_statements; mod visit_type; -use crate::SymbolTable; -use crate::{CallGraph, Pass, StructGraph}; +use crate::{CallGraph, Pass, StructGraph, SymbolTable}; use leo_ast::Ast; use leo_errors::Result; diff --git a/compiler/passes/src/code_generation/visit_expressions.rs b/compiler/passes/src/code_generation/visit_expressions.rs index 0855ce3c72..482e0d1e24 100644 --- a/compiler/passes/src/code_generation/visit_expressions.rs +++ b/compiler/passes/src/code_generation/visit_expressions.rs @@ -16,9 +16,23 @@ use crate::CodeGenerator; use leo_ast::{ - AccessExpression, AssociatedFunction, BinaryExpression, BinaryOperation, CallExpression, ErrExpression, Expression, - Identifier, Literal, MemberAccess, StructExpression, TernaryExpression, TupleExpression, Type, UnaryExpression, - UnaryOperation, UnitExpression, + AccessExpression, + AssociatedFunction, + BinaryExpression, + BinaryOperation, + CallExpression, + ErrExpression, + Expression, + Identifier, + Literal, + MemberAccess, + StructExpression, + TernaryExpression, + TupleExpression, + Type, + UnaryExpression, + UnaryOperation, + UnitExpression, }; use leo_span::sym; use std::borrow::Borrow; @@ -287,13 +301,7 @@ impl<'a> CodeGenerator<'a> { Expression::Identifier(identifier) => identifier.name, _ => unreachable!("Parsing guarantees that all `input.function` is always an identifier."), }; - let return_type = &self - .symbol_table - .borrow() - .functions - .get(&function_name) - .unwrap() - .output_type; + let return_type = &self.symbol_table.borrow().functions.get(&function_name).unwrap().output_type; match return_type { Type::Unit => { call_instruction.push(';'); diff --git a/compiler/passes/src/code_generation/visit_program.rs b/compiler/passes/src/code_generation/visit_program.rs index 25559ee26b..cb233f4aa4 100644 --- a/compiler/passes/src/code_generation/visit_program.rs +++ b/compiler/passes/src/code_generation/visit_program.rs @@ -76,13 +76,7 @@ impl<'a> CodeGenerator<'a> { program_string.push('\n'); // Visit each mapping in the Leo AST and produce an Aleo mapping declaration. - program_string.push_str( - &program_scope - .mappings - .values() - .map(|mapping| self.visit_mapping(mapping)) - .join("\n"), - ); + program_string.push_str(&program_scope.mappings.values().map(|mapping| self.visit_mapping(mapping)).join("\n")); // Visit each function in the program scope and produce an Aleo function. // Note that in the function inlining pass, we reorder the functions such that they are in post-order. @@ -118,17 +112,12 @@ impl<'a> CodeGenerator<'a> { } fn visit_struct_or_record(&mut self, struct_: &'a Struct) -> String { - if struct_.is_record { - self.visit_record(struct_) - } else { - self.visit_struct(struct_) - } + if struct_.is_record { self.visit_record(struct_) } else { self.visit_struct(struct_) } } fn visit_struct(&mut self, struct_: &'a Struct) -> String { // Add private symbol to composite types. - self.composite_mapping - .insert(&struct_.identifier.name, (false, String::from("private"))); // todo: private by default here. + self.composite_mapping.insert(&struct_.identifier.name, (false, String::from("private"))); // todo: private by default here. let mut output_string = format!("struct {}:\n", struct_.identifier); // todo: check if this is safe from name conflicts. @@ -143,8 +132,7 @@ impl<'a> CodeGenerator<'a> { fn visit_record(&mut self, record: &'a Struct) -> String { // Add record symbol to composite types. let mut output_string = String::from("record"); - self.composite_mapping - .insert(&record.identifier.name, (true, output_string.clone())); + self.composite_mapping.insert(&record.identifier.name, (true, output_string.clone())); writeln!(output_string, " {}:", record.identifier).expect("failed to write to string"); // todo: check if this is safe from name conflicts. // Construct and append the record variables. @@ -190,8 +178,7 @@ impl<'a> CodeGenerator<'a> { let type_string = match input { functions::Input::Internal(input) => { - self.variable_mapping - .insert(&input.identifier.name, register_string.clone()); + self.variable_mapping.insert(&input.identifier.name, register_string.clone()); let visibility = match (self.is_transition_function, input.mode) { (true, Mode::None) => Mode::Private, _ => input.mode, @@ -199,8 +186,7 @@ impl<'a> CodeGenerator<'a> { self.visit_type_with_visibility(&input.type_, visibility) } functions::Input::External(input) => { - self.variable_mapping - .insert(&input.identifier.name, register_string.clone()); + self.variable_mapping.insert(&input.identifier.name, register_string.clone()); format!("{}.aleo/{}.record", input.program_name, input.record) } }; @@ -234,8 +220,7 @@ impl<'a> CodeGenerator<'a> { // TODO: Dedup code. let type_string = match input { functions::Input::Internal(input) => { - self.variable_mapping - .insert(&input.identifier.name, register_string.clone()); + self.variable_mapping.insert(&input.identifier.name, register_string.clone()); let visibility = match (self.is_transition_function, input.mode) { (true, Mode::None) => Mode::Public, @@ -244,8 +229,7 @@ impl<'a> CodeGenerator<'a> { self.visit_type_with_visibility(&input.type_, visibility) } functions::Input::External(input) => { - self.variable_mapping - .insert(&input.program_name.name, register_string.clone()); + self.variable_mapping.insert(&input.program_name.name, register_string.clone()); format!("{}.aleo/{}.record", input.program_name, input.record) } }; diff --git a/compiler/passes/src/code_generation/visit_statements.rs b/compiler/passes/src/code_generation/visit_statements.rs index 04ccb90b1b..39e49a9a45 100644 --- a/compiler/passes/src/code_generation/visit_statements.rs +++ b/compiler/passes/src/code_generation/visit_statements.rs @@ -17,9 +17,22 @@ use crate::CodeGenerator; use leo_ast::{ - AssertStatement, AssertVariant, AssignStatement, Block, ConditionalStatement, ConsoleStatement, DecrementStatement, - DefinitionStatement, Expression, ExpressionStatement, IncrementStatement, IterationStatement, Mode, Output, - ReturnStatement, Statement, + AssertStatement, + AssertVariant, + AssignStatement, + Block, + ConditionalStatement, + ConsoleStatement, + DecrementStatement, + DefinitionStatement, + Expression, + ExpressionStatement, + IncrementStatement, + IterationStatement, + Mode, + Output, + ReturnStatement, + Statement, }; use itertools::Itertools; diff --git a/compiler/passes/src/common/assigner/mod.rs b/compiler/passes/src/common/assigner/mod.rs index f6d1845fc4..f4ea07ab83 100644 --- a/compiler/passes/src/common/assigner/mod.rs +++ b/compiler/passes/src/common/assigner/mod.rs @@ -48,10 +48,7 @@ impl Assigner { // Create a new variable for the expression. let name = self.unique_symbol("$var", "$"); - let place = Identifier { - name, - span: Default::default(), - }; + let place = Identifier { name, span: Default::default() }; (place, self.simple_assign_statement(place, expr)) } diff --git a/compiler/passes/src/common/graph/mod.rs b/compiler/passes/src/common/graph/mod.rs index 52ead80806..4a9445463e 100644 --- a/compiler/passes/src/common/graph/mod.rs +++ b/compiler/passes/src/common/graph/mod.rs @@ -17,8 +17,7 @@ use leo_span::Symbol; use indexmap::{IndexMap, IndexSet}; -use std::fmt::Debug; -use std::hash::Hash; +use std::{fmt::Debug, hash::Hash}; /// A struct dependency graph. pub type StructGraph = DiGraph; @@ -54,10 +53,7 @@ pub struct DiGraph { impl DiGraph { /// Initializes a new `DiGraph` from a vector of source nodes. pub fn new(nodes: IndexSet) -> Self { - Self { - nodes, - edges: IndexMap::new(), - } + Self { nodes, edges: IndexMap::new() } } /// Adds an edge to the graph. diff --git a/compiler/passes/src/common/rename_table/mod.rs b/compiler/passes/src/common/rename_table/mod.rs index 665721d77c..f5a267ade6 100644 --- a/compiler/passes/src/common/rename_table/mod.rs +++ b/compiler/passes/src/common/rename_table/mod.rs @@ -30,10 +30,7 @@ pub struct RenameTable { impl RenameTable { /// Create a new `RenameTable` with the given parent. pub(crate) fn new(parent: Option>) -> Self { - Self { - parent, - mapping: IndexMap::new(), - } + Self { parent, mapping: IndexMap::new() } } /// Returns the symbols that were renamed in the current scope. diff --git a/compiler/passes/src/common/symbol_table/mod.rs b/compiler/passes/src/common/symbol_table/mod.rs index c9e812fb7b..8b507a127c 100644 --- a/compiler/passes/src/common/symbol_table/mod.rs +++ b/compiler/passes/src/common/symbol_table/mod.rs @@ -146,11 +146,7 @@ impl SymbolTable { /// Returns true if the variable exists in any parent scope pub fn variable_in_parent_scope(&self, symbol: Symbol) -> bool { if let Some(parent) = self.parent.as_ref() { - if parent.variables.contains_key(&symbol) { - true - } else { - parent.variable_in_parent_scope(symbol) - } + if parent.variables.contains_key(&symbol) { true } else { parent.variable_in_parent_scope(symbol) } } else { false } diff --git a/compiler/passes/src/dead_code_elimination/dead_code_eliminator.rs b/compiler/passes/src/dead_code_elimination/dead_code_eliminator.rs index 6d7c18c5eb..83919755bf 100644 --- a/compiler/passes/src/dead_code_elimination/dead_code_eliminator.rs +++ b/compiler/passes/src/dead_code_elimination/dead_code_eliminator.rs @@ -29,9 +29,6 @@ pub struct DeadCodeEliminator { impl DeadCodeEliminator { /// Initializes a new `DeadCodeEliminator`. pub fn new() -> Self { - Self { - used_variables: Default::default(), - is_necessary: false, - } + Self { used_variables: Default::default(), is_necessary: false } } } diff --git a/compiler/passes/src/dead_code_elimination/eliminate_statement.rs b/compiler/passes/src/dead_code_elimination/eliminate_statement.rs index a25872d345..030680b9f9 100644 --- a/compiler/passes/src/dead_code_elimination/eliminate_statement.rs +++ b/compiler/passes/src/dead_code_elimination/eliminate_statement.rs @@ -17,9 +17,22 @@ use crate::DeadCodeEliminator; use leo_ast::{ - AssertStatement, AssertVariant, AssignStatement, Block, ConditionalStatement, ConsoleStatement, DecrementStatement, - DefinitionStatement, Expression, ExpressionReconstructor, ExpressionStatement, IncrementStatement, - IterationStatement, ReturnStatement, Statement, StatementReconstructor, + AssertStatement, + AssertVariant, + AssignStatement, + Block, + ConditionalStatement, + ConsoleStatement, + DecrementStatement, + DefinitionStatement, + Expression, + ExpressionReconstructor, + ExpressionStatement, + IncrementStatement, + IterationStatement, + ReturnStatement, + Statement, + StatementReconstructor, }; impl StatementReconstructor for DeadCodeEliminator { @@ -31,14 +44,12 @@ impl StatementReconstructor for DeadCodeEliminator { let statement = Statement::Assert(AssertStatement { variant: match input.variant { AssertVariant::Assert(expr) => AssertVariant::Assert(self.reconstruct_expression(expr).0), - AssertVariant::AssertEq(left, right) => AssertVariant::AssertEq( - self.reconstruct_expression(left).0, - self.reconstruct_expression(right).0, - ), - AssertVariant::AssertNeq(left, right) => AssertVariant::AssertNeq( - self.reconstruct_expression(left).0, - self.reconstruct_expression(right).0, - ), + AssertVariant::AssertEq(left, right) => { + AssertVariant::AssertEq(self.reconstruct_expression(left).0, self.reconstruct_expression(right).0) + } + AssertVariant::AssertNeq(left, right) => { + AssertVariant::AssertNeq(self.reconstruct_expression(left).0, self.reconstruct_expression(right).0) + } }, span: input.span, }); @@ -95,23 +106,13 @@ impl StatementReconstructor for DeadCodeEliminator { /// Reconstructs the statements inside a basic block, eliminating any dead code. fn reconstruct_block(&mut self, block: Block) -> (Block, Self::AdditionalOutput) { // Reconstruct each of the statements in reverse. - let mut statements: Vec = block - .statements - .into_iter() - .rev() - .map(|statement| self.reconstruct_statement(statement).0) - .collect(); + let mut statements: Vec = + block.statements.into_iter().rev().map(|statement| self.reconstruct_statement(statement).0).collect(); // Reverse the direction of `statements`. statements.reverse(); - ( - Block { - statements, - span: block.span, - }, - Default::default(), - ) + (Block { statements, span: block.span }, Default::default()) } /// Flattening removes conditional statements from the program. @@ -200,10 +201,7 @@ impl StatementReconstructor for DeadCodeEliminator { let statement = Statement::Return(ReturnStatement { expression: self.reconstruct_expression(input.expression).0, finalize_arguments: input.finalize_arguments.map(|arguments| { - arguments - .into_iter() - .map(|argument| self.reconstruct_expression(argument).0) - .collect() + arguments.into_iter().map(|argument| self.reconstruct_expression(argument).0).collect() }), span: input.span, }); diff --git a/compiler/passes/src/flattening/flatten_expression.rs b/compiler/passes/src/flattening/flatten_expression.rs index 9743df6c30..f99e405cdd 100644 --- a/compiler/passes/src/flattening/flatten_expression.rs +++ b/compiler/passes/src/flattening/flatten_expression.rs @@ -18,8 +18,17 @@ use crate::Flattener; use itertools::Itertools; use leo_ast::{ - AccessExpression, AssociatedFunction, Expression, ExpressionReconstructor, Member, MemberAccess, Statement, - StructExpression, StructVariableInitializer, TernaryExpression, TupleExpression, + AccessExpression, + AssociatedFunction, + Expression, + ExpressionReconstructor, + Member, + MemberAccess, + Statement, + StructExpression, + StructVariableInitializer, + TernaryExpression, + TupleExpression, }; // TODO: Clean up logic. To be done in a follow-up PR (feat/tuples) @@ -36,11 +45,7 @@ impl ExpressionReconstructor for Flattener<'_> { Expression::Access(AccessExpression::AssociatedFunction(AssociatedFunction { ty: function.ty, name: function.name, - args: function - .args - .into_iter() - .map(|arg| self.reconstruct_expression(arg).0) - .collect(), + args: function.args.into_iter().map(|arg| self.reconstruct_expression(arg).0).collect(), span: function.span, })) } @@ -83,20 +88,10 @@ impl ExpressionReconstructor for Flattener<'_> { // Accumulate any statements produced. statements.extend(stmts); // Accumulate the struct members. - members.push(StructVariableInitializer { - identifier: member.identifier, - expression: Some(expr), - }); + members.push(StructVariableInitializer { identifier: member.identifier, expression: Some(expr) }); } - ( - Expression::Struct(StructExpression { - name: input.name, - members, - span: input.span, - }), - statements, - ) + (Expression::Struct(StructExpression { name: input.name, members, span: input.span }), statements) } /// Reconstructs ternary expressions over tuples and structs, accumulating any statements that are generated. @@ -224,8 +219,7 @@ impl ExpressionReconstructor for Flattener<'_> { let (identifier, statement) = self.unique_simple_assign_statement(expr); // Mark the lhs of the assignment as a struct. - self.structs - .insert(identifier.name, first_member_struct.identifier.name); + self.structs.insert(identifier.name, first_member_struct.identifier.name); statements.push(statement); @@ -261,14 +255,8 @@ impl ExpressionReconstructor for Flattener<'_> { (Expression::Identifier(first), Expression::Identifier(second)) if self.structs.contains_key(&first.name) && self.structs.contains_key(&second.name) => { - let first_struct = self - .symbol_table - .lookup_struct(*self.structs.get(&first.name).unwrap()) - .unwrap(); - let second_struct = self - .symbol_table - .lookup_struct(*self.structs.get(&second.name).unwrap()) - .unwrap(); + let first_struct = self.symbol_table.lookup_struct(*self.structs.get(&first.name).unwrap()).unwrap(); + let second_struct = self.symbol_table.lookup_struct(*self.structs.get(&second.name).unwrap()).unwrap(); // Note that type checking guarantees that both expressions have the same same type. This is a sanity check. assert_eq!(first_struct, second_struct); diff --git a/compiler/passes/src/flattening/flatten_statement.rs b/compiler/passes/src/flattening/flatten_statement.rs index 0bb3451ca9..e44bd4ed6d 100644 --- a/compiler/passes/src/flattening/flatten_statement.rs +++ b/compiler/passes/src/flattening/flatten_statement.rs @@ -19,9 +19,27 @@ use itertools::Itertools; use std::borrow::Borrow; use leo_ast::{ - AssertStatement, AssertVariant, AssignStatement, BinaryExpression, BinaryOperation, Block, ConditionalStatement, - ConsoleStatement, DefinitionStatement, Expression, ExpressionReconstructor, Identifier, IterationStatement, Node, - ReturnStatement, Statement, StatementReconstructor, TupleExpression, Type, UnaryExpression, UnaryOperation, + AssertStatement, + AssertVariant, + AssignStatement, + BinaryExpression, + BinaryOperation, + Block, + ConditionalStatement, + ConsoleStatement, + DefinitionStatement, + Expression, + ExpressionReconstructor, + Identifier, + IterationStatement, + Node, + ReturnStatement, + Statement, + StatementReconstructor, + TupleExpression, + Type, + UnaryExpression, + UnaryOperation, }; impl StatementReconstructor for Flattener<'_> { @@ -135,10 +153,7 @@ impl StatementReconstructor for Flattener<'_> { { // Lookup the entry in `self.tuples` and add it for the lhs of the assignment. // Note that the `unwrap` is safe since the match arm checks that the entry exists. - self.tuples.insert( - lhs_identifier.name, - self.tuples.get(&rhs_identifier.name).unwrap().clone(), - ); + self.tuples.insert(lhs_identifier.name, self.tuples.get(&rhs_identifier.name).unwrap().clone()); // Note that tuple assignments are removed from the AST. (Statement::dummy(Default::default()), statements) } @@ -205,10 +220,7 @@ impl StatementReconstructor for Flattener<'_> { } (Expression::Identifier(identifier), expression) => { self.update_structs(&identifier, &expression); - ( - self.assigner.simple_assign_statement(identifier, expression), - statements, - ) + (self.assigner.simple_assign_statement(identifier, expression), statements) } // If the lhs is a tuple and the rhs is a function call, then return the reconstructed statement. (Expression::Tuple(tuple), Expression::Call(call)) => { @@ -226,22 +238,16 @@ impl StatementReconstructor for Flattener<'_> { _ => unreachable!("Type checking guarantees that the output type is a tuple."), }; - tuple - .elements - .iter() - .zip_eq(output_type.0.iter()) - .for_each(|(identifier, type_)| { - let identifier = match identifier { - Expression::Identifier(identifier) => identifier, - _ => unreachable!( - "Type checking guarantees that a tuple element on the lhs is an identifier." - ), - }; - // If the output type is a struct, add it to `self.structs`. - if let Type::Identifier(struct_name) = type_ { - self.structs.insert(identifier.name, struct_name.name); - } - }); + tuple.elements.iter().zip_eq(output_type.0.iter()).for_each(|(identifier, type_)| { + let identifier = match identifier { + Expression::Identifier(identifier) => identifier, + _ => unreachable!("Type checking guarantees that a tuple element on the lhs is an identifier."), + }; + // If the output type is a struct, add it to `self.structs`. + if let Type::Identifier(struct_name) = type_ { + self.structs.insert(identifier.name, struct_name.name); + } + }); ( Statement::Assign(Box::new(AssignStatement { @@ -318,13 +324,7 @@ impl StatementReconstructor for Flattener<'_> { statements.push(reconstructed_statement); } - ( - Block { - span: block.span, - statements, - }, - Default::default(), - ) + (Block { span: block.span, statements }, Default::default()) } /// Flatten a conditional statement into a list of statements. @@ -390,14 +390,11 @@ impl StatementReconstructor for Flattener<'_> { Expression::Identifier(identifier) if self.tuples.contains_key(&identifier.name) => { // Note that the `unwrap` is safe since the match arm checks that the entry exists in `self.tuples`. let tuple = self.tuples.get(&identifier.name).unwrap().clone(); - self.returns.push(( - guard, - ReturnStatement { - span: input.span, - expression: Expression::Tuple(tuple), - finalize_arguments: input.finalize_arguments, - }, - )); + self.returns.push((guard, ReturnStatement { + span: input.span, + expression: Expression::Tuple(tuple), + finalize_arguments: input.finalize_arguments, + })); } // Otherwise, add the expression directly. _ => self.returns.push((guard, input)), diff --git a/compiler/passes/src/flattening/flattener.rs b/compiler/passes/src/flattening/flattener.rs index 9c153abe0b..b35ce7b4dc 100644 --- a/compiler/passes/src/flattening/flattener.rs +++ b/compiler/passes/src/flattening/flattener.rs @@ -17,8 +17,19 @@ use crate::{Assigner, SymbolTable}; use leo_ast::{ - AccessExpression, BinaryExpression, BinaryOperation, Block, Expression, ExpressionReconstructor, Identifier, - Member, ReturnStatement, Statement, TernaryExpression, TupleExpression, Type, + AccessExpression, + BinaryExpression, + BinaryOperation, + Block, + Expression, + ExpressionReconstructor, + Identifier, + Member, + ReturnStatement, + Statement, + TernaryExpression, + TupleExpression, + Type, }; use leo_span::Symbol; @@ -92,10 +103,7 @@ impl<'a> Flattener<'a> { // Helper to construct and store ternary assignments. e.g `$ret$0 = $var$0 ? $var$1 : $var$2` let mut construct_ternary_assignment = |guard: Expression, if_true: Expression, if_false: Expression| { - let place = Identifier { - name: self.assigner.unique_symbol(prefix, "$"), - span: Default::default(), - }; + let place = Identifier { name: self.assigner.unique_symbol(prefix, "$"), span: Default::default() }; let (value, stmts) = self.reconstruct_ternary(TernaryExpression { condition: Box::new(guard), if_true: Box::new(if_true), @@ -116,14 +124,11 @@ impl<'a> Flattener<'a> { } }; - let expression = guards - .into_iter() - .rev() - .fold(last_expression, |acc, (guard, expr)| match guard { - None => unreachable!("All expressions except for the last one must have a guard."), - // Note that type checking guarantees that all expressions have the same type. - Some(guard) => construct_ternary_assignment(guard, expr, acc), - }); + let expression = guards.into_iter().rev().fold(last_expression, |acc, (guard, expr)| match guard { + None => unreachable!("All expressions except for the last one must have a guard."), + // Note that type checking guarantees that all expressions have the same type. + Some(guard) => construct_ternary_assignment(guard, expr, acc), + }); (expression, statements) } @@ -136,11 +141,8 @@ impl<'a> Flattener<'a> { // The inner expression of an access expression is either an identifier or another access expression. let name = self.lookup_struct_symbol(&access.inner).unwrap(); let struct_ = self.symbol_table.lookup_struct(name).unwrap(); - let Member { type_, .. } = struct_ - .members - .iter() - .find(|member| member.name() == access.name.name) - .unwrap(); + let Member { type_, .. } = + struct_.members.iter().find(|member| member.name() == access.name.name).unwrap(); match type_ { Type::Identifier(identifier) => Some(identifier.name), _ => None, diff --git a/compiler/passes/src/function_inlining/assignment_renamer.rs b/compiler/passes/src/function_inlining/assignment_renamer.rs index 2ce3bcd92f..2da7f0dbd8 100644 --- a/compiler/passes/src/function_inlining/assignment_renamer.rs +++ b/compiler/passes/src/function_inlining/assignment_renamer.rs @@ -16,8 +16,18 @@ use crate::{Assigner, RenameTable}; use leo_ast::{ - AssignStatement, ConditionalStatement, ConsoleStatement, DefinitionStatement, Expression, ExpressionReconstructor, - Identifier, IterationStatement, ProgramReconstructor, Statement, StatementReconstructor, StructExpression, + AssignStatement, + ConditionalStatement, + ConsoleStatement, + DefinitionStatement, + Expression, + ExpressionReconstructor, + Identifier, + IterationStatement, + ProgramReconstructor, + Statement, + StatementReconstructor, + StructExpression, StructVariableInitializer, }; use leo_span::Symbol; @@ -34,11 +44,7 @@ pub struct AssignmentRenamer { impl AssignmentRenamer { /// Initialize a new `AssignmentRenamer`. pub fn new(assigner: Assigner) -> Self { - Self { - assigner, - rename_table: RenameTable::new(None), - is_lhs: false, - } + Self { assigner, rename_table: RenameTable::new(None), is_lhs: false } } /// Load the internal rename table with a set of entries. @@ -56,6 +62,7 @@ impl AssignmentRenamer { impl ExpressionReconstructor for AssignmentRenamer { type AdditionalOutput = (); + /// Rename the identifier if it is the left-hand side of an assignment, otherwise look up for a new name in the internal rename table. fn reconstruct_identifier(&mut self, input: Identifier) -> (Expression, Self::AdditionalOutput) { let name = match self.is_lhs { @@ -72,10 +79,7 @@ impl ExpressionReconstructor for AssignmentRenamer { false => *self.rename_table.lookup(input.name).unwrap_or(&input.name), }; - ( - Expression::Identifier(Identifier { name, span: input.span }), - Default::default(), - ) + (Expression::Identifier(Identifier { name, span: input.span }), Default::default()) } /// Rename the variable initializers in the struct expression. @@ -115,14 +119,7 @@ impl StatementReconstructor for AssignmentRenamer { let place = self.reconstruct_expression(input.place).0; self.is_lhs = false; - ( - Statement::Assign(Box::new(AssignStatement { - place, - value, - span: input.span, - })), - Default::default(), - ) + (Statement::Assign(Box::new(AssignStatement { place, value, span: input.span })), Default::default()) } /// Flattening removes conditional statements from the program. diff --git a/compiler/passes/src/function_inlining/inline_expression.rs b/compiler/passes/src/function_inlining/inline_expression.rs index a957f245d5..60e0673365 100644 --- a/compiler/passes/src/function_inlining/inline_expression.rs +++ b/compiler/passes/src/function_inlining/inline_expression.rs @@ -17,8 +17,15 @@ use crate::{FunctionInliner, Replacer}; use leo_ast::{ - CallExpression, Expression, ExpressionReconstructor, Identifier, ReturnStatement, Statement, - StatementReconstructor, UnitExpression, Variant, + CallExpression, + Expression, + ExpressionReconstructor, + Identifier, + ReturnStatement, + Statement, + StatementReconstructor, + UnitExpression, + Variant, }; use indexmap::IndexMap; @@ -56,12 +63,8 @@ impl ExpressionReconstructor for FunctionInliner<'_> { .collect::>(); // Initializer `self.assignment_renamer` with the function parameters. - self.assignment_renamer.load( - callee - .input - .iter() - .map(|input| (input.identifier().name, input.identifier().name)), - ); + self.assignment_renamer + .load(callee.input.iter().map(|input| (input.identifier().name, input.identifier().name))); // Duplicate the body of the callee and create a unique assignment statement for each assignment in the body. // This is necessary to ensure the inlined variables do not conflict with variables in the caller. @@ -86,9 +89,7 @@ impl ExpressionReconstructor for FunctionInliner<'_> { _ => unreachable!("This branch checks that the last statement is a return statement."), } } - _ => Expression::Unit(UnitExpression { - span: Default::default(), - }), + _ => Expression::Unit(UnitExpression { span: Default::default() }), }; (result, inlined_statements) diff --git a/compiler/passes/src/function_inlining/inline_program.rs b/compiler/passes/src/function_inlining/inline_program.rs index 8bf6815161..4b20cb05e4 100644 --- a/compiler/passes/src/function_inlining/inline_program.rs +++ b/compiler/passes/src/function_inlining/inline_program.rs @@ -33,16 +33,12 @@ impl ProgramReconstructor for FunctionInliner<'_> { // Reconstruct the function. let reconstructed_function = self.reconstruct_function(function); // Add the reconstructed function to the mapping. - self.reconstructed_functions - .insert(function_name, reconstructed_function); + self.reconstructed_functions.insert(function_name, reconstructed_function); } } // Check that `input.functions` is empty. // This is a sanity check to ensure that functions in the program scope have been processed. - assert!( - input.functions.is_empty(), - "All functions in the program scope should have been processed." - ); + assert!(input.functions.is_empty(), "All functions in the program scope should have been processed."); // Note that this intentionally clears `self.reconstructed_functions` for the next program scope. let functions = core::mem::take(&mut self.reconstructed_functions); diff --git a/compiler/passes/src/function_inlining/inline_statement.rs b/compiler/passes/src/function_inlining/inline_statement.rs index cffd7560f4..ae86dd7e5a 100644 --- a/compiler/passes/src/function_inlining/inline_statement.rs +++ b/compiler/passes/src/function_inlining/inline_statement.rs @@ -17,8 +17,17 @@ use crate::FunctionInliner; use leo_ast::{ - AssignStatement, Block, ConditionalStatement, ConsoleStatement, DefinitionStatement, Expression, - ExpressionReconstructor, ExpressionStatement, IterationStatement, Statement, StatementReconstructor, + AssignStatement, + Block, + ConditionalStatement, + ConsoleStatement, + DefinitionStatement, + Expression, + ExpressionReconstructor, + ExpressionStatement, + IterationStatement, + Statement, + StatementReconstructor, }; impl StatementReconstructor for FunctionInliner<'_> { @@ -29,29 +38,15 @@ impl StatementReconstructor for FunctionInliner<'_> { match (input.place, value) { // If the function call produces a tuple, we need to segment the tuple into multiple assignment statements. (Expression::Tuple(left), Expression::Tuple(right)) if left.elements.len() == right.elements.len() => { - statements.extend( - left.elements - .into_iter() - .zip(right.elements.into_iter()) - .map(|(lhs, rhs)| { - Statement::Assign(Box::new(AssignStatement { - place: lhs, - value: rhs, - span: Default::default(), - })) - }), - ); + statements.extend(left.elements.into_iter().zip(right.elements.into_iter()).map(|(lhs, rhs)| { + Statement::Assign(Box::new(AssignStatement { place: lhs, value: rhs, span: Default::default() })) + })); (Statement::dummy(Default::default()), statements) } - (place, value) => ( - Statement::Assign(Box::new(AssignStatement { - place, - value, - span: input.span, - })), - statements, - ), + (place, value) => { + (Statement::Assign(Box::new(AssignStatement { place, value, span: input.span })), statements) + } } } @@ -65,13 +60,7 @@ impl StatementReconstructor for FunctionInliner<'_> { statements.push(reconstructed_statement); } - ( - Block { - span: block.span, - statements, - }, - Default::default(), - ) + (Block { span: block.span, statements }, Default::default()) } /// Flattening removes conditional statements from the program. @@ -98,10 +87,7 @@ impl StatementReconstructor for FunctionInliner<'_> { // If the resulting expression is a unit expression, return a dummy statement. let statement = match expression { Expression::Unit(_) => Statement::dummy(Default::default()), - _ => Statement::Expression(ExpressionStatement { - expression, - span: input.span, - }), + _ => Statement::Expression(ExpressionStatement { expression, span: input.span }), }; (statement, additional_statements) diff --git a/compiler/passes/src/loop_unrolling/range_iterator.rs b/compiler/passes/src/loop_unrolling/range_iterator.rs index dbe555d246..78d2aeae10 100644 --- a/compiler/passes/src/loop_unrolling/range_iterator.rs +++ b/compiler/passes/src/loop_unrolling/range_iterator.rs @@ -15,8 +15,7 @@ // along with the Leo library. If not, see . use num_traits::One; -use std::fmt::Display; -use std::ops::Add; +use std::{fmt::Display, ops::Add}; use leo_ast::Value; use leo_errors::LeoError; @@ -47,11 +46,7 @@ pub(crate) struct RangeIterator { impl RangeIterator { pub(crate) fn new(start: I, end: I, clusivity: Clusivity) -> Self { - Self { - end, - current: Some(start), - clusivity, - } + Self { end, current: Some(start), clusivity } } } diff --git a/compiler/passes/src/loop_unrolling/unroll_program.rs b/compiler/passes/src/loop_unrolling/unroll_program.rs index 57905a91b2..42749f025d 100644 --- a/compiler/passes/src/loop_unrolling/unroll_program.rs +++ b/compiler/passes/src/loop_unrolling/unroll_program.rs @@ -22,12 +22,7 @@ impl ProgramReconstructor for Unroller<'_> { fn reconstruct_function(&mut self, function: Function) -> Function { // Lookup function metadata in the symbol table. // Note that this unwrap is safe since function metadata is stored in a prior pass. - let function_index = self - .symbol_table - .borrow() - .lookup_fn_symbol(function.identifier.name) - .unwrap() - .id; + let function_index = self.symbol_table.borrow().lookup_fn_symbol(function.identifier.name).unwrap().id; // Enter the function's scope. let previous_function_index = self.enter_scope(function_index); diff --git a/compiler/passes/src/loop_unrolling/unroll_statement.rs b/compiler/passes/src/loop_unrolling/unroll_statement.rs index 904676683e..687fd1fdbd 100644 --- a/compiler/passes/src/loop_unrolling/unroll_statement.rs +++ b/compiler/passes/src/loop_unrolling/unroll_statement.rs @@ -18,8 +18,7 @@ use itertools::Itertools; use leo_ast::*; use leo_span::{Span, Symbol}; -use crate::unroller::Unroller; -use crate::{VariableSymbol, VariableType}; +use crate::{unroller::Unroller, VariableSymbol, VariableType}; impl StatementReconstructor for Unroller<'_> { fn reconstruct_block(&mut self, input: Block) -> (Block, Self::AdditionalOutput) { @@ -29,11 +28,7 @@ impl StatementReconstructor for Unroller<'_> { let previous_scope_index = self.enter_scope(scope_index); let block = Block { - statements: input - .statements - .into_iter() - .map(|s| self.reconstruct_statement(s).0) - .collect(), + statements: input.statements.into_iter().map(|s| self.reconstruct_statement(s).0).collect(), span: input.span, }; @@ -46,32 +41,28 @@ impl StatementReconstructor for Unroller<'_> { fn reconstruct_definition(&mut self, input: DefinitionStatement) -> (Statement, Self::AdditionalOutput) { // If we are unrolling a loop, then we need to repopulate the symbol table. if self.is_unrolling { - let declaration = if input.declaration_type == DeclarationType::Const { - VariableType::Const - } else { - VariableType::Mut - }; + let declaration = + if input.declaration_type == DeclarationType::Const { VariableType::Const } else { VariableType::Mut }; let insert_variable = |symbol: Symbol, type_: Type, span: Span, declaration: VariableType| { - if let Err(err) = self.symbol_table.borrow_mut().insert_variable( - symbol, - VariableSymbol { - type_, - span, - declaration, - }, - ) { + if let Err(err) = + self.symbol_table.borrow_mut().insert_variable(symbol, VariableSymbol { type_, span, declaration }) + { self.handler.emit_err(err); } }; // Insert the variables in the into the symbol table. match &input.place { - Expression::Identifier(identifier) => insert_variable(identifier.name, input.type_.clone(), identifier.span, declaration), + Expression::Identifier(identifier) => { + insert_variable(identifier.name, input.type_.clone(), identifier.span, declaration) + } Expression::Tuple(tuple_expression) => { let tuple_type = match input.type_ { Type::Tuple(ref tuple_type) => tuple_type, - _ => unreachable!("Type checking guarantees that if the lhs is a tuple, its associated type is also a tuple.") + _ => unreachable!( + "Type checking guarantees that if the lhs is a tuple, its associated type is also a tuple." + ), }; tuple_expression.elements.iter().zip_eq(tuple_type.0.iter()).for_each(|(expression, type_)| { let identifier = match expression { @@ -80,9 +71,10 @@ impl StatementReconstructor for Unroller<'_> { }; insert_variable(identifier.name, type_.clone(), identifier.span, declaration) }); - }, - _ => unreachable!("Type checking guarantees that the lhs of a `DefinitionStatement` is either an identifier or tuple.") - + } + _ => unreachable!( + "Type checking guarantees that the lhs of a `DefinitionStatement` is either an identifier or tuple." + ), } } (Statement::Definition(input), Default::default()) @@ -91,27 +83,22 @@ impl StatementReconstructor for Unroller<'_> { fn reconstruct_iteration(&mut self, input: IterationStatement) -> (Statement, Self::AdditionalOutput) { // We match on start and stop cause loops require // bounds to be constants. - match ( - input.start_value.clone().into_inner(), - input.stop_value.clone().into_inner(), - ) { + match (input.start_value.clone().into_inner(), input.stop_value.clone().into_inner()) { (Some(start), Some(stop)) => match (Type::from(&start), Type::from(&stop)) { (Type::Integer(IntegerType::I8), Type::Integer(IntegerType::I8)) | (Type::Integer(IntegerType::I16), Type::Integer(IntegerType::I16)) | (Type::Integer(IntegerType::I32), Type::Integer(IntegerType::I32)) | (Type::Integer(IntegerType::I64), Type::Integer(IntegerType::I64)) - | (Type::Integer(IntegerType::I128), Type::Integer(IntegerType::I128)) => ( - self.unroll_iteration_statement::(input, start, stop), - Default::default(), - ), + | (Type::Integer(IntegerType::I128), Type::Integer(IntegerType::I128)) => { + (self.unroll_iteration_statement::(input, start, stop), Default::default()) + } (Type::Integer(IntegerType::U8), Type::Integer(IntegerType::U8)) | (Type::Integer(IntegerType::U16), Type::Integer(IntegerType::U16)) | (Type::Integer(IntegerType::U32), Type::Integer(IntegerType::U32)) | (Type::Integer(IntegerType::U64), Type::Integer(IntegerType::U64)) - | (Type::Integer(IntegerType::U128), Type::Integer(IntegerType::U128)) => ( - self.unroll_iteration_statement::(input, start, stop), - Default::default(), - ), + | (Type::Integer(IntegerType::U128), Type::Integer(IntegerType::U128)) => { + (self.unroll_iteration_statement::(input, start, stop), Default::default()) + } _ => unreachable!("Type checking ensures that `start` and `stop` have the same type."), }, // If both loop bounds are not constant, then the loop is not unrolled. diff --git a/compiler/passes/src/loop_unrolling/unroller.rs b/compiler/passes/src/loop_unrolling/unroller.rs index 334088a666..2273ccb7d5 100644 --- a/compiler/passes/src/loop_unrolling/unroller.rs +++ b/compiler/passes/src/loop_unrolling/unroller.rs @@ -15,8 +15,17 @@ // along with the Leo library. If not, see . use leo_ast::{ - Block, DeclarationType, DefinitionStatement, Expression, IntegerType, IterationStatement, Literal, Statement, - StatementReconstructor, Type, Value, + Block, + DeclarationType, + DefinitionStatement, + Expression, + IntegerType, + IterationStatement, + Literal, + Statement, + StatementReconstructor, + Type, + Value, }; use std::cell::RefCell; @@ -37,29 +46,19 @@ pub struct Unroller<'a> { impl<'a> Unroller<'a> { pub(crate) fn new(symbol_table: SymbolTable, handler: &'a Handler) -> Self { - Self { - symbol_table: RefCell::new(symbol_table), - scope_index: 0, - handler, - is_unrolling: false, - } + Self { symbol_table: RefCell::new(symbol_table), scope_index: 0, handler, is_unrolling: false } } /// Returns the index of the current scope. /// Note that if we are in the midst of unrolling an IterationStatement, a new scope is created. pub(crate) fn current_scope_index(&mut self) -> usize { - if self.is_unrolling { - self.symbol_table.borrow_mut().insert_block() - } else { - self.scope_index - } + if self.is_unrolling { self.symbol_table.borrow_mut().insert_block() } else { self.scope_index } } /// Enters a child scope. pub(crate) fn enter_scope(&mut self, index: usize) -> usize { let previous_symbol_table = std::mem::take(&mut self.symbol_table); - self.symbol_table - .swap(previous_symbol_table.borrow().lookup_scope_by_index(index).unwrap()); + self.symbol_table.swap(previous_symbol_table.borrow().lookup_scope_by_index(index).unwrap()); self.symbol_table.borrow_mut().parent = Some(Box::new(previous_symbol_table.into_inner())); core::mem::replace(&mut self.scope_index, 0) } @@ -121,13 +120,11 @@ impl<'a> Unroller<'a> { statements: match input.inclusive { true => { let iter = RangeIterator::new(start, stop, Clusivity::Inclusive); - iter.map(|iteration_count| self.unroll_single_iteration(&input, iteration_count)) - .collect() + iter.map(|iteration_count| self.unroll_single_iteration(&input, iteration_count)).collect() } false => { let iter = RangeIterator::new(start, stop, Clusivity::Exclusive); - iter.map(|iteration_count| self.unroll_single_iteration(&input, iteration_count)) - .collect() + iter.map(|iteration_count| self.unroll_single_iteration(&input, iteration_count)).collect() } }, }); @@ -201,10 +198,7 @@ impl<'a> Unroller<'a> { statements.push(self.reconstruct_statement(s).0); }); - let block = Statement::Block(Block { - statements, - span: input.block.span, - }); + let block = Statement::Block(Block { statements, span: input.block.span }); self.is_unrolling = prior_is_unrolling; diff --git a/compiler/passes/src/static_single_assignment/rename_expression.rs b/compiler/passes/src/static_single_assignment/rename_expression.rs index bd7bb54881..b71fc7922d 100644 --- a/compiler/passes/src/static_single_assignment/rename_expression.rs +++ b/compiler/passes/src/static_single_assignment/rename_expression.rs @@ -17,9 +17,24 @@ use crate::StaticSingleAssigner; use leo_ast::{ - AccessExpression, AssociatedFunction, BinaryExpression, CallExpression, Expression, ExpressionConsumer, Identifier, - Literal, MemberAccess, Statement, Struct, StructExpression, StructVariableInitializer, TernaryExpression, - TupleAccess, TupleExpression, UnaryExpression, UnitExpression, + AccessExpression, + AssociatedFunction, + BinaryExpression, + CallExpression, + Expression, + ExpressionConsumer, + Identifier, + Literal, + MemberAccess, + Statement, + Struct, + StructExpression, + StructVariableInitializer, + TernaryExpression, + TupleAccess, + TupleExpression, + UnaryExpression, + UnitExpression, }; use leo_span::{sym, Symbol}; @@ -100,14 +115,12 @@ impl ExpressionConsumer for StaticSingleAssigner<'_> { statements.append(&mut right_statements); // Construct and accumulate a unique assignment statement storing the result of the binary expression. - let (place, statement) = self - .assigner - .unique_simple_assign_statement(Expression::Binary(BinaryExpression { - left: Box::new(left_expression), - right: Box::new(right_expression), - op: input.op, - span: input.span, - })); + let (place, statement) = self.assigner.unique_simple_assign_statement(Expression::Binary(BinaryExpression { + left: Box::new(left_expression), + right: Box::new(right_expression), + op: input.op, + span: input.span, + })); statements.push(statement); (Expression::Identifier(place), statements) @@ -129,16 +142,14 @@ impl ExpressionConsumer for StaticSingleAssigner<'_> { .collect(); // Construct and accumulate a new assignment statement for the call expression. - let (place, statement) = self - .assigner - .unique_simple_assign_statement(Expression::Call(CallExpression { - // Note that we do not rename the function name. - function: input.function, - // Consume the arguments. - arguments, - external: input.external, - span: input.span, - })); + let (place, statement) = self.assigner.unique_simple_assign_statement(Expression::Call(CallExpression { + // Note that we do not rename the function name. + function: input.function, + // Consume the arguments. + arguments, + external: input.external, + span: input.span, + })); statements.push(statement); (Expression::Identifier(place), statements) @@ -165,10 +176,7 @@ impl ExpressionConsumer for StaticSingleAssigner<'_> { statements.append(&mut stmts); // Return the new member. - StructVariableInitializer { - identifier: arg.identifier, - expression: Some(expression), - } + StructVariableInitializer { identifier: arg.identifier, expression: Some(expression) } }) .collect(); @@ -182,10 +190,8 @@ impl ExpressionConsumer for StaticSingleAssigner<'_> { let mut reordered_members = Vec::with_capacity(members.len()); // Collect the members of the init expression into a map. - let mut member_map: IndexMap = members - .into_iter() - .map(|member| (member.identifier.name, member)) - .collect(); + let mut member_map: IndexMap = + members.into_iter().map(|member| (member.identifier.name, member)).collect(); // If we are initializing a record, add the `owner` and `gates` fields, first and second respectively. // Note that type checking guarantees that the above fields exist. @@ -209,13 +215,11 @@ impl ExpressionConsumer for StaticSingleAssigner<'_> { } // Construct and accumulate a new assignment statement for the struct expression. - let (place, statement) = self - .assigner - .unique_simple_assign_statement(Expression::Struct(StructExpression { - name: input.name, - span: input.span, - members: reordered_members, - })); + let (place, statement) = self.assigner.unique_simple_assign_statement(Expression::Struct(StructExpression { + name: input.name, + span: input.span, + members: reordered_members, + })); statements.push(statement); (Expression::Identifier(place), statements) @@ -237,13 +241,7 @@ impl ExpressionConsumer for StaticSingleAssigner<'_> { false => *self.rename_table.lookup(identifier.name).unwrap_or(&identifier.name), }; - ( - Expression::Identifier(Identifier { - name, - span: identifier.span, - }), - Default::default(), - ) + (Expression::Identifier(Identifier { name, span: identifier.span }), Default::default()) } /// Consumes and returns the literal without making any modifications. @@ -265,14 +263,12 @@ impl ExpressionConsumer for StaticSingleAssigner<'_> { statements.append(&mut if_false_statements); // Construct and accumulate a unique assignment statement storing the result of the ternary expression. - let (place, statement) = self - .assigner - .unique_simple_assign_statement(Expression::Ternary(TernaryExpression { - condition: Box::new(cond_expr), - if_true: Box::new(if_true_expr), - if_false: Box::new(if_false_expr), - span: input.span, - })); + let (place, statement) = self.assigner.unique_simple_assign_statement(Expression::Ternary(TernaryExpression { + condition: Box::new(cond_expr), + if_true: Box::new(if_true_expr), + if_false: Box::new(if_false_expr), + span: input.span, + })); statements.push(statement); (Expression::Identifier(place), statements) @@ -296,10 +292,7 @@ impl ExpressionConsumer for StaticSingleAssigner<'_> { // Construct and accumulate a new assignment statement for the tuple expression. let (place, statement) = self .assigner - .unique_simple_assign_statement(Expression::Tuple(TupleExpression { - elements, - span: input.span, - })); + .unique_simple_assign_statement(Expression::Tuple(TupleExpression { elements, span: input.span })); statements.push(statement); (Expression::Identifier(place), statements) @@ -311,13 +304,11 @@ impl ExpressionConsumer for StaticSingleAssigner<'_> { let (receiver, mut statements) = self.consume_expression(*input.receiver); // Construct and accumulate a new assignment statement for the unary expression. - let (place, statement) = self - .assigner - .unique_simple_assign_statement(Expression::Unary(UnaryExpression { - op: input.op, - receiver: Box::new(receiver), - span: input.span, - })); + let (place, statement) = self.assigner.unique_simple_assign_statement(Expression::Unary(UnaryExpression { + op: input.op, + receiver: Box::new(receiver), + span: input.span, + })); statements.push(statement); (Expression::Identifier(place), statements) diff --git a/compiler/passes/src/static_single_assignment/rename_program.rs b/compiler/passes/src/static_single_assignment/rename_program.rs index 2ae451381b..8ea86d587d 100644 --- a/compiler/passes/src/static_single_assignment/rename_program.rs +++ b/compiler/passes/src/static_single_assignment/rename_program.rs @@ -17,8 +17,18 @@ use crate::StaticSingleAssigner; use leo_ast::{ - Block, Finalize, Function, FunctionConsumer, Member, Program, ProgramConsumer, ProgramScope, ProgramScopeConsumer, - StatementConsumer, Struct, StructConsumer, + Block, + Finalize, + Function, + FunctionConsumer, + Member, + Program, + ProgramConsumer, + ProgramScope, + ProgramScopeConsumer, + StatementConsumer, + Struct, + StructConsumer, }; use leo_span::{sym, Symbol}; @@ -33,11 +43,8 @@ impl StructConsumer for StaticSingleAssigner<'_> { false => struct_, true => { let mut members = Vec::with_capacity(struct_.members.len()); - let mut member_map: IndexMap = struct_ - .members - .into_iter() - .map(|member| (member.identifier.name, member)) - .collect(); + let mut member_map: IndexMap = + struct_.members.into_iter().map(|member| (member.identifier.name, member)).collect(); // Add the owner field to the beginning of the members list. // Note that type checking ensures that the owner field exists. @@ -67,14 +74,10 @@ impl FunctionConsumer for StaticSingleAssigner<'_> { // There is no need to reconstruct `function.inputs`. // However, for each input, we must add each symbol to the rename table. for input_variable in function.input.iter() { - self.rename_table - .update(input_variable.identifier().name, input_variable.identifier().name); + self.rename_table.update(input_variable.identifier().name, input_variable.identifier().name); } - let block = Block { - span: function.block.span, - statements: self.consume_block(function.block), - }; + let block = Block { span: function.block.span, statements: self.consume_block(function.block) }; // Remove the `RenameTable` for the function. self.pop(); @@ -86,14 +89,10 @@ impl FunctionConsumer for StaticSingleAssigner<'_> { // There is no need to reconstruct `finalize.inputs`. // However, for each input, we must add each symbol to the rename table. for input_variable in finalize.input.iter() { - self.rename_table - .update(input_variable.identifier().name, input_variable.identifier().name); + self.rename_table.update(input_variable.identifier().name, input_variable.identifier().name); } - let block = Block { - span: finalize.block.span, - statements: self.consume_block(finalize.block), - }; + let block = Block { span: finalize.block.span, statements: self.consume_block(finalize.block) }; // Remove the `RenameTable` for the finalize block. self.pop(); @@ -128,17 +127,9 @@ impl ProgramScopeConsumer for StaticSingleAssigner<'_> { fn consume_program_scope(&mut self, input: ProgramScope) -> Self::Output { ProgramScope { program_id: input.program_id, - structs: input - .structs - .into_iter() - .map(|(i, s)| (i, self.consume_struct(s))) - .collect(), + structs: input.structs.into_iter().map(|(i, s)| (i, self.consume_struct(s))).collect(), mappings: input.mappings, - functions: input - .functions - .into_iter() - .map(|(i, f)| (i, self.consume_function(f))) - .collect(), + functions: input.functions.into_iter().map(|(i, f)| (i, self.consume_function(f))).collect(), span: input.span, } } diff --git a/compiler/passes/src/static_single_assignment/rename_statement.rs b/compiler/passes/src/static_single_assignment/rename_statement.rs index 37c2eb0977..8742118bcd 100644 --- a/compiler/passes/src/static_single_assignment/rename_statement.rs +++ b/compiler/passes/src/static_single_assignment/rename_statement.rs @@ -17,9 +17,25 @@ use crate::{RenameTable, StaticSingleAssigner}; use leo_ast::{ - AssertStatement, AssertVariant, AssignStatement, Block, CallExpression, ConditionalStatement, ConsoleStatement, - DecrementStatement, DefinitionStatement, Expression, ExpressionConsumer, ExpressionStatement, Identifier, - IncrementStatement, IterationStatement, ReturnStatement, Statement, StatementConsumer, TernaryExpression, + AssertStatement, + AssertVariant, + AssignStatement, + Block, + CallExpression, + ConditionalStatement, + ConsoleStatement, + DecrementStatement, + DefinitionStatement, + Expression, + ExpressionConsumer, + ExpressionStatement, + Identifier, + IncrementStatement, + IterationStatement, + ReturnStatement, + Statement, + StatementConsumer, + TernaryExpression, TupleExpression, }; use leo_span::Symbol; @@ -59,10 +75,7 @@ impl StatementConsumer for StaticSingleAssigner<'_> { }; // Add the assert statement to the list of produced statements. - statements.push(Statement::Assert(AssertStatement { - variant, - span: input.span, - })); + statements.push(Statement::Assert(AssertStatement { variant, span: input.span })); statements } @@ -88,11 +101,7 @@ impl StatementConsumer for StaticSingleAssigner<'_> { /// Consumes a `Block`, flattening its constituent `ConditionalStatement`s. fn consume_block(&mut self, block: Block) -> Self::Output { - block - .statements - .into_iter() - .flat_map(|statement| self.consume_statement(statement)) - .collect() + block.statements.into_iter().flat_map(|statement| self.consume_statement(statement)).collect() } /// Consumes a `ConditionalStatement`, producing phi functions (assign statements) for variables written in the then-block and otherwise-block. @@ -110,10 +119,7 @@ impl StatementConsumer for StaticSingleAssigner<'_> { self.push(); // Consume the then-block. - let then = Block { - span: conditional.then.span, - statements: self.consume_block(conditional.then), - }; + let then = Block { span: conditional.then.span, statements: self.consume_block(conditional.then) }; // Remove the `RenameTable` for the then-block. let if_table = self.pop(); @@ -156,13 +162,9 @@ impl StatementConsumer for StaticSingleAssigner<'_> { if self.rename_table.lookup(**symbol).is_some() { // Helper to lookup a symbol and create an argument for the phi function. let create_phi_argument = |table: &RenameTable, symbol: Symbol| { - let name = *table - .lookup(symbol) - .unwrap_or_else(|| panic!("Symbol {symbol} should exist in the program.")); - Box::new(Expression::Identifier(Identifier { - name, - span: Default::default(), - })) + let name = + *table.lookup(symbol).unwrap_or_else(|| panic!("Symbol {symbol} should exist in the program.")); + Box::new(Expression::Identifier(Identifier { name, span: Default::default() })) }; // Create a new name for the variable written to in the `ConditionalStatement`. @@ -178,13 +180,9 @@ impl StatementConsumer for StaticSingleAssigner<'_> { statements.extend(stmts); // Create a new `AssignStatement` for the phi function. - let assignment = self.assigner.simple_assign_statement( - Identifier { - name: new_name, - span: Default::default(), - }, - value, - ); + let assignment = self + .assigner + .simple_assign_statement(Identifier { name: new_name, span: Default::default() }, value); // Update the `RenameTable` with the new name of the variable. self.rename_table.update(*(*symbol), new_name); @@ -251,15 +249,14 @@ impl StatementConsumer for StaticSingleAssigner<'_> { } }).collect(); statements.push(Statement::Assign(Box::new(AssignStatement { - place: Expression::Tuple(TupleExpression { - elements, - span: Default::default() - }), + place: Expression::Tuple(TupleExpression { elements, span: Default::default() }), value, - span: Default::default() + span: Default::default(), }))); } - _ => unreachable!("Type checking guarantees that the left-hand-side of a `DefinitionStatement` is an identifier or tuple."), + _ => unreachable!( + "Type checking guarantees that the left-hand-side of a `DefinitionStatement` is an identifier or tuple." + ), } self.is_lhs = false; diff --git a/compiler/passes/src/static_single_assignment/static_single_assigner.rs b/compiler/passes/src/static_single_assignment/static_single_assigner.rs index b019c07236..40cdea3ea0 100644 --- a/compiler/passes/src/static_single_assignment/static_single_assigner.rs +++ b/compiler/passes/src/static_single_assignment/static_single_assigner.rs @@ -30,12 +30,7 @@ pub struct StaticSingleAssigner<'a> { impl<'a> StaticSingleAssigner<'a> { /// Initializes a new `StaticSingleAssigner` with an empty `RenameTable`. pub(crate) fn new(symbol_table: &'a SymbolTable) -> Self { - Self { - symbol_table, - rename_table: RenameTable::new(None), - is_lhs: false, - assigner: Assigner::default(), - } + Self { symbol_table, rename_table: RenameTable::new(None), is_lhs: false, assigner: Assigner::default() } } /// Pushes a new scope, setting the current scope as the new scope's parent. diff --git a/compiler/passes/src/symbol_table_creation/creator.rs b/compiler/passes/src/symbol_table_creation/creator.rs index d837d7e395..243a4b62e3 100644 --- a/compiler/passes/src/symbol_table_creation/creator.rs +++ b/compiler/passes/src/symbol_table_creation/creator.rs @@ -31,10 +31,7 @@ pub struct SymbolTableCreator<'a> { impl<'a> SymbolTableCreator<'a> { pub fn new(handler: &'a Handler) -> Self { - Self { - symbol_table: Default::default(), - handler, - } + Self { symbol_table: Default::default(), handler } } } @@ -58,17 +55,14 @@ impl<'a> ProgramVisitor<'a> for SymbolTableCreator<'a> { fn visit_mapping(&mut self, input: &'a Mapping) { // Add the variable associated with the mapping to the symbol table. - if let Err(err) = self.symbol_table.insert_variable( - input.identifier.name, - VariableSymbol { - type_: Type::Mapping(MappingType { - key: Box::new(input.key_type.clone()), - value: Box::new(input.value_type.clone()), - }), - span: input.span, - declaration: VariableType::Mut, - }, - ) { + if let Err(err) = self.symbol_table.insert_variable(input.identifier.name, VariableSymbol { + type_: Type::Mapping(MappingType { + key: Box::new(input.key_type.clone()), + value: Box::new(input.value_type.clone()), + }), + span: input.span, + declaration: VariableType::Mut, + }) { self.handler.emit_err(err); } } diff --git a/compiler/passes/src/type_checking/check_expressions.rs b/compiler/passes/src/type_checking/check_expressions.rs index 2c1c274eee..97c46ff2a8 100644 --- a/compiler/passes/src/type_checking/check_expressions.rs +++ b/compiler/passes/src/type_checking/check_expressions.rs @@ -17,8 +17,7 @@ use crate::TypeChecker; use leo_ast::*; -use leo_errors::emitter::Handler; -use leo_errors::TypeCheckerError; +use leo_errors::{emitter::Handler, TypeCheckerError}; use leo_span::{sym, Span}; use std::str::FromStr; @@ -28,11 +27,7 @@ fn return_incorrect_type(t1: Option, t2: Option, expected: &Option Some(t1), (Some(t1), Some(t2)) => { if let Some(expected) = expected { - if &t1 != expected { - Some(t1) - } else { - Some(t2) - } + if &t1 != expected { Some(t1) } else { Some(t2) } } else { Some(t1) } @@ -482,12 +477,9 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> { } // Check function argument types. - func.input - .iter() - .zip(input.arguments.iter()) - .for_each(|(expected, argument)| { - self.visit_expression(argument, &Some(expected.type_())); - }); + func.input.iter().zip(input.arguments.iter()).for_each(|(expected, argument)| { + self.visit_expression(argument, &Some(expected.type_())); + }); // Add the call to the call graph. let caller_name = match self.function { @@ -524,11 +516,7 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> { // Check struct member types. struct_.members.iter().for_each(|Member { identifier, type_, .. }| { // Lookup struct variable name. - if let Some(actual) = input - .members - .iter() - .find(|member| member.identifier.name == identifier.name) - { + if let Some(actual) = input.members.iter().find(|member| member.identifier.name == identifier.name) { match &actual.expression { // If `expression` is None, then the member uses the identifier shorthand, e.g. `Foo { a }` None => self.visit_identifier(&actual.identifier, &Some(type_.clone())), @@ -546,11 +534,7 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> { Some(ret) } else { - self.emit_err(TypeCheckerError::unknown_sym( - "struct", - input.name.name, - input.name.span(), - )); + self.emit_err(TypeCheckerError::unknown_sym("struct", input.name.name, input.name.span())); None } } @@ -655,16 +639,13 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> { )); } - expected_types - .iter() - .zip(input.elements.iter()) - .for_each(|(expected, expr)| { - // Check that the component expression is not a tuple. - if matches!(expr, Expression::Tuple(_)) { - self.emit_err(TypeCheckerError::nested_tuple_expression(expr.span())) - } - self.visit_expression(expr, &Some(expected.clone())); - }); + expected_types.iter().zip(input.elements.iter()).for_each(|(expected, expr)| { + // Check that the component expression is not a tuple. + if matches!(expr, Expression::Tuple(_)) { + self.emit_err(TypeCheckerError::nested_tuple_expression(expr.span())) + } + self.visit_expression(expr, &Some(expected.clone())); + }); Some(Type::Tuple(expected_types.clone())) } else { @@ -727,9 +708,7 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> { fn visit_unit(&mut self, input: &'a UnitExpression, _additional: &Self::AdditionalInput) -> Self::Output { // Unit expression are only allowed inside a return statement. if !self.is_return { - self.emit_err(TypeCheckerError::unit_expression_only_in_return_statements( - input.span(), - )); + self.emit_err(TypeCheckerError::unit_expression_only_in_return_statements(input.span())); } Some(Type::Unit) } diff --git a/compiler/passes/src/type_checking/check_program.rs b/compiler/passes/src/type_checking/check_program.rs index c8af36d342..3d22178c00 100644 --- a/compiler/passes/src/type_checking/check_program.rs +++ b/compiler/passes/src/type_checking/check_program.rs @@ -48,10 +48,7 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> { } // Typecheck the program scopes. - input - .program_scopes - .values() - .for_each(|scope| self.visit_program_scope(scope)); + input.program_scopes.values().for_each(|scope| self.visit_program_scope(scope)); } fn visit_program_scope(&mut self, input: &'a ProgramScope) { @@ -94,18 +91,11 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> { // Check for conflicting struct/record member names. let mut used = HashSet::new(); // TODO: Better span to target duplicate member. - if !input.members.iter().all( - |Member { - identifier, - type_, - span, - .. - }| { - // Check that the member types are defined. - self.assert_type_is_defined(type_, *span); - used.insert(identifier.name) - }, - ) { + if !input.members.iter().all(|Member { identifier, type_, span, .. }| { + // Check that the member types are defined. + self.assert_type_is_defined(type_, *span); + used.insert(identifier.name) + }) { self.emit_err(if input.is_record { TypeCheckerError::duplicate_record_variable(input.name(), input.span()) } else { @@ -121,31 +111,17 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> { }) { Some((_, actual_ty)) if expected_ty.eq_flat(actual_ty) => {} // All good, found + right type! Some((field, _)) => { - self.emit_err(TypeCheckerError::record_var_wrong_type( - field, - expected_ty, - input.span(), - )); + self.emit_err(TypeCheckerError::record_var_wrong_type(field, expected_ty, input.span())); } None => { - self.emit_err(TypeCheckerError::required_record_variable( - need, - expected_ty, - input.span(), - )); + self.emit_err(TypeCheckerError::required_record_variable(need, expected_ty, input.span())); } }; check_has_field(sym::owner, Type::Address); check_has_field(sym::gates, Type::Integer(IntegerType::U64)); } - for Member { - mode, - identifier, - type_, - span, - } in input.members.iter() - { + for Member { mode, identifier, type_, span } in input.members.iter() { // Check that the member type is not a tuple. if matches!(type_, Type::Tuple(_)) { self.emit_err(TypeCheckerError::composite_data_type_cannot_contain_tuple( @@ -201,12 +177,7 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> { // Lookup function metadata in the symbol table. // Note that this unwrap is safe since function metadata is stored in a prior pass. - let function_index = self - .symbol_table - .borrow() - .lookup_fn_symbol(function.identifier.name) - .unwrap() - .id; + let function_index = self.symbol_table.borrow().lookup_fn_symbol(function.identifier.name).unwrap().id; // Enter the function's scope. self.enter_scope(function_index); @@ -235,25 +206,24 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> { // Note that this unwrap is safe since we assign to `self.variant` above. match self.variant.unwrap() { // If the function is a transition function, then check that the parameter mode is not a constant. - Variant::Transition if input_var.mode() == Mode::Constant => self.emit_err( - TypeCheckerError::transition_function_inputs_cannot_be_const(input_var.span()), - ), + Variant::Transition if input_var.mode() == Mode::Constant => { + self.emit_err(TypeCheckerError::transition_function_inputs_cannot_be_const(input_var.span())) + } // If the function is not a transition function, then check that the parameters do not have an associated mode. - Variant::Standard | Variant::Inline if input_var.mode() != Mode::None => self.emit_err( - TypeCheckerError::regular_function_inputs_cannot_have_modes(input_var.span()), - ), + Variant::Standard | Variant::Inline if input_var.mode() != Mode::None => { + self.emit_err(TypeCheckerError::regular_function_inputs_cannot_have_modes(input_var.span())) + } _ => {} // Do nothing. } // Check for conflicting variable names. - if let Err(err) = self.symbol_table.borrow_mut().insert_variable( - input_var.identifier().name, - VariableSymbol { + if let Err(err) = + self.symbol_table.borrow_mut().insert_variable(input_var.identifier().name, VariableSymbol { type_: input_var.type_(), span: input_var.identifier().span(), declaration: VariableType::Input(input_var.mode()), - }, - ) { + }) + { self.handler.emit_err(err); } }); @@ -277,12 +247,7 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> { // If the function is not a transition function, then it cannot output a record. if let Type::Identifier(identifier) = function_output.type_ { if !matches!(function.variant, Variant::Transition) - && self - .symbol_table - .borrow() - .lookup_struct(identifier.name) - .unwrap() - .is_record + && self.symbol_table.borrow().lookup_struct(identifier.name).unwrap().is_record { self.emit_err(TypeCheckerError::function_cannot_output_record(function_output.span)); } @@ -325,9 +290,7 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> { // Check that the function is a transition function. if !matches!(function.variant, Variant::Transition) { - self.emit_err(TypeCheckerError::only_transition_functions_can_have_finalize( - finalize.span, - )); + self.emit_err(TypeCheckerError::only_transition_functions_can_have_finalize(finalize.span)); } // Check that the name of the finalize block matches the function name. @@ -354,14 +317,13 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> { self.emit_err(TypeCheckerError::finalize_input_mode_must_be_public(input_var.span())); } // Check for conflicting variable names. - if let Err(err) = self.symbol_table.borrow_mut().insert_variable( - input_var.identifier().name, - VariableSymbol { + if let Err(err) = + self.symbol_table.borrow_mut().insert_variable(input_var.identifier().name, VariableSymbol { type_: input_var.type_(), span: input_var.identifier().span(), declaration: VariableType::Input(input_var.mode()), - }, - ) { + }) + { self.handler.emit_err(err); } }); @@ -378,9 +340,7 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> { // Check that the mode of the output is valid. // Note that a finalize block can have only public outputs. if matches!(output_type.mode(), Mode::Constant | Mode::Private) { - self.emit_err(TypeCheckerError::finalize_output_mode_must_be_public( - output_type.span(), - )); + self.emit_err(TypeCheckerError::finalize_output_mode_must_be_public(output_type.span())); } }); diff --git a/compiler/passes/src/type_checking/check_statements.rs b/compiler/passes/src/type_checking/check_statements.rs index 5c6f6a475f..135d45b9d5 100644 --- a/compiler/passes/src/type_checking/check_statements.rs +++ b/compiler/passes/src/type_checking/check_statements.rs @@ -162,10 +162,7 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { self.assert_mapping_type(&mapping_type, input.span()); match mapping_type { - None => self.emit_err(TypeCheckerError::could_not_determine_type( - input.mapping, - input.mapping.span, - )), + None => self.emit_err(TypeCheckerError::could_not_determine_type(input.mapping, input.mapping.span)), Some(Type::Mapping(mapping_type)) => { // Check that the index matches the key type of the mapping. let index_type = self.visit_expression(&input.index, &None); @@ -178,20 +175,15 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { // Check that the amount type is incrementable. self.assert_field_group_scalar_int_type(&amount_type, input.amount.span()); } - Some(mapping_type) => self.emit_err(TypeCheckerError::expected_one_type_of( - "mapping", - mapping_type, - input.mapping.span, - )), + Some(mapping_type) => { + self.emit_err(TypeCheckerError::expected_one_type_of("mapping", mapping_type, input.mapping.span)) + } } } fn visit_definition(&mut self, input: &'a DefinitionStatement) { - let declaration = if input.declaration_type == DeclarationType::Const { - VariableType::Const - } else { - VariableType::Mut - }; + let declaration = + if input.declaration_type == DeclarationType::Const { VariableType::Const } else { VariableType::Mut }; // Check that the type of the definition is defined. self.assert_type_is_defined(&input.type_, input.span); @@ -220,14 +212,9 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { // TODO: Dedup with unrolling pass. // Helper to insert the variables into the symbol table. let insert_variable = |symbol: Symbol, type_: Type, span: Span, declaration: VariableType| { - if let Err(err) = self.symbol_table.borrow_mut().insert_variable( - symbol, - VariableSymbol { - type_, - span, - declaration, - }, - ) { + if let Err(err) = + self.symbol_table.borrow_mut().insert_variable(symbol, VariableSymbol { type_, span, declaration }) + { self.handler.emit_err(err); } }; @@ -244,21 +231,17 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { "Type checking guarantees that if the lhs is a tuple, its associated type is also a tuple." ), }; - tuple_expression - .elements - .iter() - .zip_eq(tuple_type.0.iter()) - .for_each(|(expression, type_)| { - let identifier = match expression { - Expression::Identifier(identifier) => identifier, - _ => { - return self.emit_err(TypeCheckerError::lhs_tuple_element_must_be_an_identifier( - expression.span(), - )) - } - }; - insert_variable(identifier.name, type_.clone(), identifier.span, declaration) - }); + tuple_expression.elements.iter().zip_eq(tuple_type.0.iter()).for_each(|(expression, type_)| { + let identifier = match expression { + Expression::Identifier(identifier) => identifier, + _ => { + return self.emit_err(TypeCheckerError::lhs_tuple_element_must_be_an_identifier( + expression.span(), + )); + } + }; + insert_variable(identifier.name, type_.clone(), identifier.span, declaration) + }); } _ => self.emit_err(TypeCheckerError::lhs_must_be_identifier_or_tuple(input.place.span())), } @@ -267,9 +250,7 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { fn visit_expression_statement(&mut self, input: &'a ExpressionStatement) { // Expression statements can only be function calls. if !matches!(input.expression, Expression::Call(_)) { - self.emit_err(TypeCheckerError::expression_statement_must_be_function_call( - input.span(), - )); + self.emit_err(TypeCheckerError::expression_statement_must_be_function_call(input.span())); } else { // Check the expression. // TODO: Should the output type be restricted to unit types? @@ -287,10 +268,7 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { self.assert_mapping_type(&mapping_type, input.span()); match mapping_type { - None => self.emit_err(TypeCheckerError::could_not_determine_type( - input.mapping, - input.mapping.span, - )), + None => self.emit_err(TypeCheckerError::could_not_determine_type(input.mapping, input.mapping.span)), Some(Type::Mapping(mapping_type)) => { // Check that the index matches the key type of the mapping. let index_type = self.visit_expression(&input.index, &None); @@ -303,11 +281,9 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { // Check that the amount type is incrementable. self.assert_field_group_scalar_int_type(&amount_type, input.amount.span()); } - Some(mapping_type) => self.emit_err(TypeCheckerError::expected_one_type_of( - "mapping", - mapping_type, - input.mapping.span, - )), + Some(mapping_type) => { + self.emit_err(TypeCheckerError::expected_one_type_of("mapping", mapping_type, input.mapping.span)) + } } } @@ -319,14 +295,11 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { let scope_index = self.create_child_scope(); // Add the loop variable to the scope of the loop body. - if let Err(err) = self.symbol_table.borrow_mut().insert_variable( - input.variable.name, - VariableSymbol { - type_: input.type_.clone(), - span: input.span(), - declaration: VariableType::Const, - }, - ) { + if let Err(err) = self.symbol_table.borrow_mut().insert_variable(input.variable.name, VariableSymbol { + type_: input.type_.clone(), + span: input.span(), + declaration: VariableType::Const, + }) { self.handler.emit_err(err); } @@ -372,16 +345,12 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { // We can safely unwrap all self.parent instances because // statements should always have some parent block let parent = self.function.unwrap(); - let return_type = &self - .symbol_table - .borrow() - .lookup_fn_symbol(parent) - .map(|f| match self.is_finalize { - // TODO: Check this. - // Note that this `unwrap()` is safe since we checked that the function has a finalize block. - true => f.finalize.as_ref().unwrap().output_type.clone(), - false => f.output_type.clone(), - }); + let return_type = &self.symbol_table.borrow().lookup_fn_symbol(parent).map(|f| match self.is_finalize { + // TODO: Check this. + // Note that this `unwrap()` is safe since we checked that the function has a finalize block. + true => f.finalize.as_ref().unwrap().output_type.clone(), + false => f.output_type.clone(), + }); // Set the `has_return` flag. self.has_return = true; @@ -413,13 +382,8 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { // Check that the function has a finalize block. // Note that `self.function.unwrap()` is safe since every `self.function` is set for every function. // Note that `(self.function.unwrap()).unwrap()` is safe since all functions have been checked to exist. - let finalize = self - .symbol_table - .borrow() - .lookup_fn_symbol(self.function.unwrap()) - .unwrap() - .finalize - .clone(); + let finalize = + self.symbol_table.borrow().lookup_fn_symbol(self.function.unwrap()).unwrap().finalize.clone(); match finalize { None => self.emit_err(TypeCheckerError::finalize_without_finalize_block(input.span())), Some(finalize) => { @@ -433,13 +397,9 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { } // Check function argument types. - finalize - .input - .iter() - .zip(arguments.iter()) - .for_each(|(expected, argument)| { - self.visit_expression(argument, &Some(expected.type_())); - }); + finalize.input.iter().zip(arguments.iter()).for_each(|(expected, argument)| { + self.visit_expression(argument, &Some(expected.type_())); + }); } } } diff --git a/compiler/passes/src/type_checking/checker.rs b/compiler/passes/src/type_checking/checker.rs index 0ba10fbf35..290a75f920 100644 --- a/compiler/passes/src/type_checking/checker.rs +++ b/compiler/passes/src/type_checking/checker.rs @@ -87,11 +87,8 @@ const UNSIGNED_INT_TYPES: [Type; 5] = [ Type::Integer(IntegerType::U128), ]; -const MAGNITUDE_TYPES: [Type; 3] = [ - Type::Integer(IntegerType::U8), - Type::Integer(IntegerType::U16), - Type::Integer(IntegerType::U32), -]; +const MAGNITUDE_TYPES: [Type; 3] = + [Type::Integer(IntegerType::U8), Type::Integer(IntegerType::U16), Type::Integer(IntegerType::U32)]; impl<'a> TypeChecker<'a> { /// Returns a new type checker given a symbol table and error handler. @@ -119,8 +116,7 @@ impl<'a> TypeChecker<'a> { /// Enters a child scope. pub(crate) fn enter_scope(&mut self, index: usize) { let previous_symbol_table = std::mem::take(&mut self.symbol_table); - self.symbol_table - .swap(previous_symbol_table.borrow().lookup_scope_by_index(index).unwrap()); + self.symbol_table.swap(previous_symbol_table.borrow().lookup_scope_by_index(index).unwrap()); self.symbol_table.borrow_mut().parent = Some(Box::new(previous_symbol_table.into_inner())); } @@ -137,8 +133,7 @@ impl<'a> TypeChecker<'a> { /// Exits the current scope. pub(crate) fn exit_scope(&mut self, index: usize) { let previous_symbol_table = *self.symbol_table.borrow_mut().parent.take().unwrap(); - self.symbol_table - .swap(previous_symbol_table.lookup_scope_by_index(index).unwrap()); + self.symbol_table.swap(previous_symbol_table.lookup_scope_by_index(index).unwrap()); self.symbol_table = RefCell::new(previous_symbol_table); } @@ -183,22 +178,12 @@ impl<'a> TypeChecker<'a> { /// Emits an error to the error handler if the `actual` type is not equal to the `expected` type. pub(crate) fn assert_type(&self, actual: &Option, expected: &Type, span: Span) { - self.check_type( - |actual: &Type| actual.eq_flat(expected), - expected.to_string(), - actual, - span, - ) + self.check_type(|actual: &Type| actual.eq_flat(expected), expected.to_string(), actual, span) } /// Emits an error to the handler if the given type is not a boolean. pub(crate) fn assert_bool_type(&self, type_: &Option, span: Span) { - self.check_type( - |type_: &Type| BOOLEAN_TYPE.eq(type_), - BOOLEAN_TYPE.to_string(), - type_, - span, - ) + self.check_type(|type_: &Type| BOOLEAN_TYPE.eq(type_), BOOLEAN_TYPE.to_string(), type_, span) } /// Emits an error to the handler if the given type is not a field. @@ -213,22 +198,12 @@ impl<'a> TypeChecker<'a> { /// Emits an error to the handler if the given type is not a scalar. pub(crate) fn assert_scalar_type(&self, type_: &Option, span: Span) { - self.check_type( - |type_: &Type| SCALAR_TYPE.eq(type_), - SCALAR_TYPE.to_string(), - type_, - span, - ) + self.check_type(|type_: &Type| SCALAR_TYPE.eq(type_), SCALAR_TYPE.to_string(), type_, span) } /// Emits an error to the handler if the given type is not an integer. pub(crate) fn assert_int_type(&self, type_: &Option, span: Span) { - self.check_type( - |type_: &Type| INT_TYPES.contains(type_), - types_to_string(&INT_TYPES), - type_, - span, - ) + self.check_type(|type_: &Type| INT_TYPES.contains(type_), types_to_string(&INT_TYPES), type_, span) } /// Emits an error to the handler if the given type is not a signed integer. @@ -253,12 +228,7 @@ impl<'a> TypeChecker<'a> { /// Emits an error to the handler if the given type is not a magnitude (u8, u16, u32). pub(crate) fn assert_magnitude_type(&self, type_: &Option, span: Span) { - self.check_type( - |type_: &Type| MAGNITUDE_TYPES.contains(type_), - types_to_string(&MAGNITUDE_TYPES), - type_, - span, - ) + self.check_type(|type_: &Type| MAGNITUDE_TYPES.contains(type_), types_to_string(&MAGNITUDE_TYPES), type_, span) } /// Emits an error to the handler if the given type is not a boolean or an integer. @@ -327,13 +297,7 @@ impl<'a> TypeChecker<'a> { |type_: &Type| { FIELD_TYPE.eq(type_) | GROUP_TYPE.eq(type_) | SCALAR_TYPE.eq(type_) | INT_TYPES.contains(type_) }, - format!( - "{}, {}, {}, {}", - FIELD_TYPE, - GROUP_TYPE, - SCALAR_TYPE, - types_to_string(&INT_TYPES), - ), + format!("{}, {}, {}, {}", FIELD_TYPE, GROUP_TYPE, SCALAR_TYPE, types_to_string(&INT_TYPES),), type_, span, ) @@ -347,11 +311,7 @@ impl<'a> TypeChecker<'a> { match CoreInstruction::from_symbols(ident.name, function.name) { None => { // Not a core library struct. - self.emit_err(TypeCheckerError::invalid_core_function( - ident.name, - function.name, - ident.span(), - )); + self.emit_err(TypeCheckerError::invalid_core_function(ident.name, function.name, ident.span())); } Some(core_instruction) => return Some(core_instruction), } @@ -380,11 +340,7 @@ impl<'a> TypeChecker<'a> { .lookup_struct(identifier.name) .map_or(false, |struct_| struct_.is_record) => { - self.emit_err(TypeCheckerError::struct_or_record_cannot_contain_record( - parent, - identifier.name, - span, - )) + self.emit_err(TypeCheckerError::struct_or_record_cannot_contain_record(parent, identifier.name, span)) } Type::Tuple(tuple_type) => { for type_ in tuple_type.iter() { @@ -423,12 +379,7 @@ impl<'a> TypeChecker<'a> { /// Emits an error if the type is not a mapping. pub(crate) fn assert_mapping_type(&self, type_: &Option, span: Span) { - self.check_type( - |type_| matches!(type_, Type::Mapping(_)), - "mapping".to_string(), - type_, - span, - ) + self.check_type(|type_| matches!(type_, Type::Mapping(_)), "mapping".to_string(), type_, span) } } diff --git a/compiler/span/src/source_map.rs b/compiler/span/src/source_map.rs index 052369b54f..3a66e4aa05 100644 --- a/compiler/span/src/source_map.rs +++ b/compiler/span/src/source_map.rs @@ -17,7 +17,9 @@ use crate::span::{BytePos, CharPos, Pos, Span}; use std::{ cell::RefCell, - fmt, fs, io, + fmt, + fs, + io, path::{Path, PathBuf}, rc::Rc, }; @@ -105,10 +107,7 @@ impl SourceMap { if loc.line_start == loc.line_stop { format!("{}:{}-{}", loc.line_start, loc.col_start, loc.col_stop) } else { - format!( - "{}:{}-{}:{}", - loc.line_start, loc.col_start, loc.line_stop, loc.col_stop - ) + format!("{}:{}-{}:{}", loc.line_start, loc.col_start, loc.line_stop, loc.col_stop) } } @@ -140,11 +139,7 @@ impl SourceMap { let idx_lo = begin.lookup_line(span.lo).unwrap_or(0); let idx_hi = begin.lookup_line(span.hi).unwrap_or(0) + 1; let lo_line_pos = begin.lines[idx_lo]; - let hi_line_pos = if idx_hi < begin.lines.len() { - begin.lines[idx_hi] - } else { - begin.end_pos - }; + let hi_line_pos = if idx_hi < begin.lines.len() { begin.lines[idx_hi] } else { begin.end_pos }; Some(begin.contents_of_span(Span::new(lo_line_pos, hi_line_pos))) } } @@ -183,11 +178,7 @@ impl fmt::Display for FileName { /// Is the env var `LEO_TESTFRAMEWORK` not enabled? pub fn is_not_test_framework() -> bool { - std::env::var("LEO_TESTFRAMEWORK") - .unwrap_or_default() - .trim() - .to_owned() - .is_empty() + std::env::var("LEO_TESTFRAMEWORK").unwrap_or_default().trim().to_owned().is_empty() } /// A single source in the [`SourceMap`]. @@ -215,14 +206,7 @@ impl SourceFile { normalize_src(&mut src); let end_pos = start_pos + BytePos::from_usize(src.len()); let (lines, multibyte_chars) = analyze_source_file(&src, start_pos); - Self { - name, - src, - start_pos, - end_pos, - lines, - multibyte_chars, - } + Self { name, src, start_pos, end_pos, lines, multibyte_chars } } /// Converts an absolute `BytePos` to a `CharPos` relative to the `SourceFile`. diff --git a/compiler/span/src/span.rs b/compiler/span/src/span.rs index 1132d9476b..c724fa301a 100644 --- a/compiler/span/src/span.rs +++ b/compiler/span/src/span.rs @@ -42,10 +42,7 @@ impl Span { /// Generates a dummy span with all defaults. /// Should only be used in temporary situations. pub const fn dummy() -> Self { - Self { - lo: BytePos(0), - hi: BytePos(0), - } + Self { lo: BytePos(0), hi: BytePos(0) } } /// Is the span a dummy? diff --git a/compiler/span/src/span_json.rs b/compiler/span/src/span_json.rs index 51b2513f88..3194f01488 100644 --- a/compiler/span/src/span_json.rs +++ b/compiler/span/src/span_json.rs @@ -18,9 +18,12 @@ use crate::Span; -use serde::de::{MapAccess, Visitor}; -use serde::ser::SerializeMap; -use serde::{Deserializer, Serializer}; +use serde::{ + de::{MapAccess, Visitor}, + ser::SerializeMap, + Deserializer, + Serializer, +}; use std::fmt; /// The AST contains a few tuple-like enum variants that contain spans. diff --git a/compiler/span/src/symbol.rs b/compiler/span/src/symbol.rs index 6b3c1aac3d..a63c172f5a 100644 --- a/compiler/span/src/symbol.rs +++ b/compiler/span/src/symbol.rs @@ -16,12 +16,15 @@ use crate::source_map::SourceMap; -use core::borrow::Borrow; -use core::cmp::PartialEq; -use core::hash::{Hash, Hasher}; -use core::num::NonZeroU32; -use core::ops::Deref; -use core::{fmt, str}; +use core::{ + borrow::Borrow, + cmp::PartialEq, + fmt, + hash::{Hash, Hasher}, + num::NonZeroU32, + ops::Deref, + str, +}; use fxhash::FxBuildHasher; use indexmap::IndexSet; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -289,10 +292,7 @@ pub struct SessionGlobals { impl Default for SessionGlobals { fn default() -> Self { - Self { - symbol_interner: Interner::prefilled(), - source_map: SourceMap::default(), - } + Self { symbol_interner: Interner::prefilled(), source_map: SourceMap::default() } } } @@ -381,9 +381,7 @@ impl Interner { // arena: <_>::default(), set: init.iter().copied().map(InternedStr::Static).collect(), }; - Self { - inner: RefCell::new(inner), - } + Self { inner: RefCell::new(inner) } } /// Interns `string`, returning a `Symbol` corresponding to it. diff --git a/docs/grammar/src/main.rs b/docs/grammar/src/main.rs index e23e16020e..8eff45acbe 100644 --- a/docs/grammar/src/main.rs +++ b/docs/grammar/src/main.rs @@ -68,13 +68,7 @@ impl<'a> Processor<'a> { // we need a hashmap to pull rules easily let rules: HashMap = abnf.into_iter().map(|rule| (rule.name().to_string(), rule)).collect(); - Processor { - grammar, - line: 0, - out: String::new(), - rules, - scope: Scope::Free, - } + Processor { grammar, line: 0, out: String::new(), rules, scope: Scope::Free } } /// Main function for this struct. diff --git a/errors/src/common/backtraced.rs b/errors/src/common/backtraced.rs index abe5a1eaea..7e11823c7c 100644 --- a/errors/src/common/backtraced.rs +++ b/errors/src/common/backtraced.rs @@ -64,15 +64,7 @@ impl Backtraced { where S: ToString, { - Self { - message: message.to_string(), - help, - code, - code_identifier, - type_, - error, - backtrace, - } + Self { message: message.to_string(), help, code, code_identifier, type_, error, backtrace } } /// Gets the backtraced error exit code. @@ -113,11 +105,7 @@ impl Backtraced { impl fmt::Display for Backtraced { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let (kind, code) = if self.error { - ("Error", self.error_code()) - } else { - ("Warning", self.warning_code()) - }; + let (kind, code) = if self.error { ("Error", self.error_code()) } else { ("Warning", self.warning_code()) }; let message = format!("{kind} [{code}]: {message}", message = self.message,); // To avoid the color enabling characters for comparison with test expectations. @@ -144,17 +132,13 @@ impl fmt::Display for Backtraced { "1" => { let mut printer = BacktracePrinter::default(); printer = printer.lib_verbosity(Verbosity::Medium); - let trace = printer - .format_trace_to_string(&self.backtrace) - .map_err(|_| fmt::Error)?; + let trace = printer.format_trace_to_string(&self.backtrace).map_err(|_| fmt::Error)?; write!(f, "{trace}")?; } "full" => { let mut printer = BacktracePrinter::default(); printer = printer.lib_verbosity(Verbosity::Full); - let trace = printer - .format_trace_to_string(&self.backtrace) - .map_err(|_| fmt::Error)?; + let trace = printer.format_trace_to_string(&self.backtrace).map_err(|_| fmt::Error)?; write!(f, "{trace}")?; } _ => {} diff --git a/errors/src/common/formatted.rs b/errors/src/common/formatted.rs index 3f36df0771..6781cd28a7 100644 --- a/errors/src/common/formatted.rs +++ b/errors/src/common/formatted.rs @@ -109,32 +109,20 @@ impl fmt::Display for Formatted { let (loc, contents) = with_session_globals(|s| { ( - s.source_map - .span_to_location(self.span) - .unwrap_or_else(SpanLocation::dummy), - s.source_map - .line_contents_of_span(self.span) - .unwrap_or_else(|| "".to_owned()), + s.source_map.span_to_location(self.span).unwrap_or_else(SpanLocation::dummy), + s.source_map.line_contents_of_span(self.span).unwrap_or_else(|| "".to_owned()), ) }); let underlined = underline(loc.col_start, loc.col_stop); - let (kind, code) = if self.backtrace.error { - ("Error", self.error_code()) - } else { - ("Warning", self.warning_code()) - }; + let (kind, code) = + if self.backtrace.error { ("Error", self.error_code()) } else { ("Warning", self.warning_code()) }; let message = format!("{kind} [{code}]: {message}", message = self.backtrace.message,); // To avoid the color enabling characters for comparison with test expectations. - if std::env::var("LEO_TESTFRAMEWORK") - .unwrap_or_default() - .trim() - .to_owned() - .is_empty() - { + if std::env::var("LEO_TESTFRAMEWORK").unwrap_or_default().trim().to_owned().is_empty() { if self.backtrace.error { write!(f, "{}", message.bold().red())?; } else { @@ -180,18 +168,14 @@ impl fmt::Display for Formatted { let mut printer = BacktracePrinter::default(); printer = printer.verbosity(Verbosity::Medium); printer = printer.lib_verbosity(Verbosity::Medium); - let trace = printer - .format_trace_to_string(&self.backtrace.backtrace) - .map_err(|_| fmt::Error)?; + let trace = printer.format_trace_to_string(&self.backtrace.backtrace).map_err(|_| fmt::Error)?; write!(f, "\n{trace}")?; } "full" => { let mut printer = BacktracePrinter::default(); printer = printer.verbosity(Verbosity::Full); printer = printer.lib_verbosity(Verbosity::Full); - let trace = printer - .format_trace_to_string(&self.backtrace.backtrace) - .map_err(|_| fmt::Error)?; + let trace = printer.format_trace_to_string(&self.backtrace.backtrace).map_err(|_| fmt::Error)?; write!(f, "\n{trace}")?; } _ => {} diff --git a/errors/src/emitter/mod.rs b/errors/src/emitter/mod.rs index fb9520b75b..0769b5e06c 100644 --- a/errors/src/emitter/mod.rs +++ b/errors/src/emitter/mod.rs @@ -17,10 +17,8 @@ use crate::LeoWarning; use super::LeoError; -use core::default::Default; -use core::fmt; -use std::cell::RefCell; -use std::rc::Rc; +use core::{default::Default, fmt}; +use std::{cell::RefCell, rc::Rc}; /// Types that are sinks for compiler errors. pub trait Emitter { @@ -182,11 +180,7 @@ impl Default for Handler { impl Handler { /// Construct a `Handler` using the given `emitter`. pub fn new(emitter: Box) -> Self { - let inner = RefCell::new(HandlerInner { - err_count: 0, - warn_count: 0, - emitter, - }); + let inner = RefCell::new(HandlerInner { err_count: 0, warn_count: 0, emitter }); Self { inner } } diff --git a/leo/commands/build.rs b/leo/commands/build.rs index 969405a479..3b92e4ce42 100644 --- a/leo/commands/build.rs +++ b/leo/commands/build.rs @@ -14,14 +14,15 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::commands::ALEO_CLI_COMMAND; -use crate::{commands::Command, context::Context}; +use crate::{ + commands::{Command, ALEO_CLI_COMMAND}, + context::Context, +}; use leo_ast::Struct; use leo_compiler::{Compiler, CompilerOptions, InputAst}; use leo_errors::{CliError, CompilerError, PackageError, Result}; -use leo_package::source::SourceDirectory; -use leo_package::{inputs::InputFile, outputs::OutputsDirectory}; +use leo_package::{inputs::InputFile, outputs::OutputsDirectory, source::SourceDirectory}; use leo_span::symbol::with_session_globals; use aleo::commands::Build as AleoBuild; @@ -29,12 +30,13 @@ use aleo::commands::Build as AleoBuild; use clap::StructOpt; use indexmap::IndexMap; use snarkvm::prelude::{ProgramID, Testnet3}; -use std::io::Write; -use std::path::{Path, PathBuf}; +use std::{ + io::Write, + path::{Path, PathBuf}, +}; use leo_errors::emitter::Handler; -use leo_package::build::BuildDirectory; -use leo_package::imports::ImportsDirectory; +use leo_package::{build::BuildDirectory, imports::ImportsDirectory}; use leo_span::Symbol; use tracing::span::Span; @@ -226,19 +228,14 @@ fn compile_leo_file( is_import: bool, ) -> Result> { // Construct the Leo file name with extension `foo.leo`. - let file_name = file_path - .file_name() - .and_then(|name| name.to_str()) - .ok_or_else(PackageError::failed_to_get_file_name)?; + let file_name = + file_path.file_name().and_then(|name| name.to_str()).ok_or_else(PackageError::failed_to_get_file_name)?; // If the program is an import, construct program name from file_path // Otherwise, use the program_id found in `package.json`. let program_name = match is_import { false => program_id.name().to_string(), - true => file_name - .strip_suffix(".leo") - .ok_or_else(PackageError::failed_to_get_file_name)? - .to_string(), + true => file_name.strip_suffix(".leo").ok_or_else(PackageError::failed_to_get_file_name)?.to_string(), }; // Create the path to the Aleo file. diff --git a/leo/commands/clean.rs b/leo/commands/clean.rs index 2a1477fb36..3a9ffce15b 100644 --- a/leo/commands/clean.rs +++ b/leo/commands/clean.rs @@ -16,8 +16,7 @@ use crate::{commands::Command, context::Context}; use leo_errors::Result; -use leo_package::build::BuildDirectory; -use leo_package::outputs::OutputsDirectory; +use leo_package::{build::BuildDirectory, outputs::OutputsDirectory}; use clap::StructOpt; use colored::Colorize; diff --git a/leo/commands/deploy.rs b/leo/commands/deploy.rs index 4513dbb441..87ab46dcf7 100644 --- a/leo/commands/deploy.rs +++ b/leo/commands/deploy.rs @@ -14,8 +14,10 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::commands::ALEO_CLI_COMMAND; -use crate::{commands::Command, context::Context}; +use crate::{ + commands::{Command, ALEO_CLI_COMMAND}, + context::Context, +}; use leo_errors::{CliError, PackageError, Result}; use leo_package::build::BuildDirectory; diff --git a/leo/commands/new.rs b/leo/commands/new.rs index e8593bfc4e..b0f0d3b9ee 100644 --- a/leo/commands/new.rs +++ b/leo/commands/new.rs @@ -14,14 +14,12 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::commands::Network; use crate::{ - commands::{Command, ALEO_CLI_COMMAND}, + commands::{Command, Network, ALEO_CLI_COMMAND}, context::Context, }; use leo_errors::{CliError, PackageError, Result}; -use leo_package::build::BUILD_DIRECTORY_NAME; -use leo_package::package::Package; +use leo_package::{build::BUILD_DIRECTORY_NAME, package::Package}; use snarkvm::file::AleoFile; use aleo::commands::New as AleoNew; @@ -97,9 +95,7 @@ impl Command for New { aleo_file_path.push(AleoFile::::main_file_name()); // Remove the Aleo file from the package directory. - aleo_file - .remove(&aleo_file_path) - .map_err(PackageError::failed_to_remove_aleo_file)?; + aleo_file.remove(&aleo_file_path).map_err(PackageError::failed_to_remove_aleo_file)?; Ok(()) } diff --git a/leo/commands/run.rs b/leo/commands/run.rs index b099e83cef..f9bc54b2e5 100644 --- a/leo/commands/run.rs +++ b/leo/commands/run.rs @@ -15,9 +15,8 @@ // along with the Leo library. If not, see . use super::build::BuildOptions; -use crate::commands::ALEO_CLI_COMMAND; use crate::{ - commands::{Build, Command}, + commands::{Build, Command, ALEO_CLI_COMMAND}, context::Context, }; use leo_errors::{CliError, PackageError, Result}; @@ -34,10 +33,7 @@ pub struct Run { #[structopt(name = "NAME", help = "The name of the program to run.", default_value = "main")] name: String, - #[structopt( - name = "INPUTS", - help = "The inputs to the program. If none are provided, the input file is used." - )] + #[structopt(name = "INPUTS", help = "The inputs to the program. If none are provided, the input file is used.")] inputs: Vec, #[structopt(flatten)] @@ -53,10 +49,7 @@ impl Command for Run { } fn prelude(&self, context: Context) -> Result { - (Build { - options: self.compiler_options.clone(), - }) - .execute(context) + (Build { options: self.compiler_options.clone() }).execute(context) } fn apply(self, context: Context, input: Self::Input) -> Result { diff --git a/leo/context.rs b/leo/context.rs index 3b0fbc4620..96d68a94a8 100644 --- a/leo/context.rs +++ b/leo/context.rs @@ -19,10 +19,10 @@ use leo_errors::{CliError, PackageError, Result}; use snarkvm::file::Manifest; use leo_package::build::{BuildDirectory, BUILD_DIRECTORY_NAME}; -use std::fs::File; -use std::io::Write; use std::{ env::current_dir, + fs::File, + io::Write, path::{Path, PathBuf}, }; diff --git a/leo/logger.rs b/leo/logger.rs index f719ff0483..510fe401d2 100644 --- a/leo/logger.rs +++ b/leo/logger.rs @@ -125,10 +125,7 @@ impl Format { /// /// [thread ID]: https://doc.rust-lang.org/stable/std/thread/struct.ThreadId.html pub fn with_thread_ids(self, display_thread_id: bool) -> Format { - Format { - display_thread_id, - ..self - } + Format { display_thread_id, ..self } } /// Sets whether or not the [name] of the current thread is displayed @@ -136,10 +133,7 @@ impl Format { /// /// [name]: https://doc.rust-lang.org/stable/std/thread/index.html#naming-threads pub fn with_thread_names(self, display_thread_name: bool) -> Format { - Format { - display_thread_name, - ..self - } + Format { display_thread_name, ..self } } } diff --git a/leo/main.rs b/leo/main.rs index a74f36a968..46b92409db 100644 --- a/leo/main.rs +++ b/leo/main.rs @@ -19,14 +19,12 @@ pub mod context; pub mod logger; pub mod updater; -use crate::commands::*; -use crate::context::*; +use crate::{commands::*, context::*}; use leo_errors::Result; use leo_span::symbol::create_session_if_not_set_then; use clap::StructOpt; -use std::path::PathBuf; -use std::process::exit; +use std::{path::PathBuf, process::exit}; /// CLI Arguments entry point - includes global parameters and subcommands #[derive(StructOpt, Debug)] @@ -44,12 +42,7 @@ pub struct CLI { #[structopt(help = "Custom Aleo PM backend URL", env = "APM_URL")] api: Option, - #[structopt( - long, - global = true, - help = "Optional path to Leo program root folder", - parse(from_os_str) - )] + #[structopt(long, global = true, help = "Optional path to Leo program root folder", parse(from_os_str))] path: Option, } @@ -95,15 +88,13 @@ fn set_panic_hook() { #[cfg(not(debug_assertions))] std::panic::set_hook({ Box::new(move |e| { - eprintln!( - "thread `{}` {}", - std::thread::current().name().unwrap_or(""), - e - ); + eprintln!("thread `{}` {}", std::thread::current().name().unwrap_or(""), e); eprintln!("stack backtrace: \n{:?}", backtrace::Backtrace::new()); eprintln!("error: internal compiler error: unexpected panic\n"); eprintln!("note: the compiler unexpectedly panicked. this is a bug.\n"); - eprintln!("note: we would appreciate a bug report: https://github.com/AleoHQ/leo/issues/new?labels=bug,panic&template=bug.md&title=[Bug]\n"); + eprintln!( + "note: we would appreciate a bug report: https://github.com/AleoHQ/leo/issues/new?labels=bug,panic&template=bug.md&title=[Bug]\n" + ); eprintln!( "note: {} {} running on {} {}\n", env!("CARGO_PKG_NAME"), @@ -111,10 +102,7 @@ fn set_panic_hook() { sys_info::os_type().unwrap_or_else(|e| e.to_string()), sys_info::os_release().unwrap_or_else(|e| e.to_string()), ); - eprintln!( - "note: compiler args: {}\n", - std::env::args().collect::>().join(" ") - ); + eprintln!("note: compiler args: {}\n", std::env::args().collect::>().join(" ")); eprintln!("note: compiler flags: {:?}\n", CLI::parse()); }) }); @@ -134,13 +122,10 @@ pub fn handle_error(res: Result) -> T { pub fn run_with_args(cli: CLI) -> Result<()> { if !cli.quiet { // Init logger with optional debug flag. - logger::init_logger( - "leo", - match cli.debug { - false => 1, - true => 2, - }, - )?; + logger::init_logger("leo", match cli.debug { + false => 1, + true => 2, + })?; } // Get custom root folder and create context for it. diff --git a/leo/package/src/build/directory.rs b/leo/package/src/build/directory.rs index 8e277b9170..536c032266 100644 --- a/leo/package/src/build/directory.rs +++ b/leo/package/src/build/directory.rs @@ -16,8 +16,11 @@ use leo_errors::{PackageError, Result}; -use std::path::PathBuf; -use std::{borrow::Cow, fs, path::Path}; +use std::{ + borrow::Cow, + fs, + path::{Path, PathBuf}, +}; pub static BUILD_DIRECTORY_NAME: &str = "build/"; diff --git a/leo/package/src/imports/directory.rs b/leo/package/src/imports/directory.rs index 886ee73beb..89e2744ee4 100644 --- a/leo/package/src/imports/directory.rs +++ b/leo/package/src/imports/directory.rs @@ -17,8 +17,11 @@ use crate::parse_file_paths; use leo_errors::{PackageError, Result}; -use std::path::PathBuf; -use std::{borrow::Cow, fs, path::Path}; +use std::{ + borrow::Cow, + fs, + path::{Path, PathBuf}, +}; pub static IMPORTS_DIRECTORY_NAME: &str = "imports/"; diff --git a/leo/package/src/inputs/input.rs b/leo/package/src/inputs/input.rs index 052d1a2e03..e7e47e0098 100644 --- a/leo/package/src/inputs/input.rs +++ b/leo/package/src/inputs/input.rs @@ -24,7 +24,8 @@ use serde::Deserialize; use std::{ borrow::Cow, fs::{ - File, {self}, + File, + {self}, }, io::Write, path::Path, @@ -39,9 +40,7 @@ pub struct InputFile { impl InputFile { pub fn new(package_name: &str) -> Self { - Self { - package_name: package_name.to_string(), - } + Self { package_name: package_name.to_string() } } pub fn filename(&self) -> String { @@ -67,8 +66,7 @@ impl InputFile { let path = self.setup_file_path(path); let mut file = File::create(path).map_err(PackageError::io_error_input_file)?; - file.write_all(self.template().as_bytes()) - .map_err(PackageError::io_error_input_file)?; + file.write_all(self.template().as_bytes()).map_err(PackageError::io_error_input_file)?; Ok(()) } @@ -89,8 +87,7 @@ b: u32 = 2u32; if !path.ends_with(INPUTS_DIRECTORY_NAME) { path.to_mut().push(INPUTS_DIRECTORY_NAME); } - path.to_mut() - .push(format!("{}{INPUT_FILE_EXTENSION}", self.package_name)); + path.to_mut().push(format!("{}{INPUT_FILE_EXTENSION}", self.package_name)); } path } diff --git a/leo/package/src/lib.rs b/leo/package/src/lib.rs index e4f753eb09..c8a1a56885 100644 --- a/leo/package/src/lib.rs +++ b/leo/package/src/lib.rs @@ -27,8 +27,7 @@ pub mod source; use leo_errors::{PackageError, Result}; -use std::fs::ReadDir; -use std::{fs, path::PathBuf}; +use std::{fs, fs::ReadDir, path::PathBuf}; pub static LEO_FILE_EXTENSION: &str = ".leo"; diff --git a/leo/package/src/outputs/ast_snapshot.rs b/leo/package/src/outputs/ast_snapshot.rs index fb9ceb0a68..66ffc80cc3 100644 --- a/leo/package/src/outputs/ast_snapshot.rs +++ b/leo/package/src/outputs/ast_snapshot.rs @@ -33,16 +33,12 @@ pub enum Snapshot { impl fmt::Display for Snapshot { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}", - match self { - Self::Initial => "initial_ast", - Self::ImportsResolved => "imports_resolved_ast", - Self::TypeInference => "type_inferenced_ast", - Self::Canonicalization => "canonicalization_ast", - } - ) + write!(f, "{}", match self { + Self::Initial => "initial_ast", + Self::ImportsResolved => "imports_resolved_ast", + Self::TypeInference => "type_inferenced_ast", + Self::Canonicalization => "canonicalization_ast", + }) } } @@ -58,10 +54,7 @@ pub struct SnapshotFile { impl SnapshotFile { pub fn new(package_name: &str, snapshot: Snapshot) -> Self { - Self { - package_name: package_name.to_string(), - snapshot, - } + Self { package_name: package_name.to_string(), snapshot } } pub fn exists_at(&self, path: &Path) -> bool { @@ -97,8 +90,7 @@ impl SnapshotFile { if !path.ends_with(OUTPUTS_DIRECTORY_NAME) { path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } - path.to_mut() - .push(format!("{}{AST_SNAPSHOT_FILE_EXTENSION}", self.snapshot)); + path.to_mut().push(format!("{}{AST_SNAPSHOT_FILE_EXTENSION}", self.snapshot)); } path } diff --git a/leo/package/src/outputs/checksum.rs b/leo/package/src/outputs/checksum.rs index 4f9e966c9b..e751461cc3 100644 --- a/leo/package/src/outputs/checksum.rs +++ b/leo/package/src/outputs/checksum.rs @@ -23,7 +23,8 @@ use serde::Deserialize; use std::{ borrow::Cow, fs::{ - File, {self}, + File, + {self}, }, io::Write, path::Path, @@ -38,9 +39,7 @@ pub struct ChecksumFile { impl ChecksumFile { pub fn new(package_name: &str) -> Self { - Self { - package_name: package_name.to_string(), - } + Self { package_name: package_name.to_string() } } pub fn exists_at(&self, path: &Path) -> bool { @@ -62,8 +61,7 @@ impl ChecksumFile { let path = self.setup_file_path(path); let mut file = File::create(path).map_err(PackageError::io_error_checksum_file)?; - file.write_all(checksum.as_bytes()) - .map_err(PackageError::io_error_checksum_file)?; + file.write_all(checksum.as_bytes()).map_err(PackageError::io_error_checksum_file)?; Ok(()) } @@ -85,8 +83,7 @@ impl ChecksumFile { if !path.ends_with(OUTPUTS_DIRECTORY_NAME) { path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } - path.to_mut() - .push(format!("{}{CHECKSUM_FILE_EXTENSION}", self.package_name)); + path.to_mut().push(format!("{}{CHECKSUM_FILE_EXTENSION}", self.package_name)); } path } diff --git a/leo/package/src/outputs/circuit.rs b/leo/package/src/outputs/circuit.rs index 19ac7d7561..e50a443624 100644 --- a/leo/package/src/outputs/circuit.rs +++ b/leo/package/src/outputs/circuit.rs @@ -23,7 +23,8 @@ use serde::Deserialize; use std::{ borrow::Cow, fs::{ - File, {self}, + File, + {self}, }, io::Write, path::Path, @@ -38,9 +39,7 @@ pub struct CircuitFile { impl CircuitFile { pub fn new(package_name: &str) -> Self { - Self { - package_name: package_name.to_string(), - } + Self { package_name: package_name.to_string() } } pub fn exists_at(&self, path: &Path) -> bool { @@ -62,8 +61,7 @@ impl CircuitFile { let path = self.setup_file_path(path); let mut file = File::create(path).map_err(PackageError::io_error_circuit_file)?; - file.write_all(circuit.as_bytes()) - .map_err(PackageError::io_error_circuit_file)?; + file.write_all(circuit.as_bytes()).map_err(PackageError::io_error_circuit_file)?; Ok(()) } @@ -85,8 +83,7 @@ impl CircuitFile { if !path.ends_with(OUTPUTS_DIRECTORY_NAME) { path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } - path.to_mut() - .push(format!("{}{CIRCUIT_FILE_EXTENSION}", self.package_name)); + path.to_mut().push(format!("{}{CIRCUIT_FILE_EXTENSION}", self.package_name)); } path } diff --git a/leo/package/src/outputs/directory.rs b/leo/package/src/outputs/directory.rs index 96edf622e6..911186828c 100644 --- a/leo/package/src/outputs/directory.rs +++ b/leo/package/src/outputs/directory.rs @@ -16,8 +16,11 @@ use leo_errors::{PackageError, Result}; -use std::path::PathBuf; -use std::{borrow::Cow, fs, path::Path}; +use std::{ + borrow::Cow, + fs, + path::{Path, PathBuf}, +}; pub static OUTPUTS_DIRECTORY_NAME: &str = "outputs/"; diff --git a/leo/package/src/package.rs b/leo/package/src/package.rs index 265d923153..b867dacea9 100644 --- a/leo/package/src/package.rs +++ b/leo/package/src/package.rs @@ -41,12 +41,7 @@ impl Package { return Err(PackageError::invalid_package_name(package_name).into()); } - Ok(Self { - name: package_name.to_owned(), - version: "0.1.0".to_owned(), - description: None, - license: None, - }) + Ok(Self { name: package_name.to_owned(), version: "0.1.0".to_owned(), description: None, license: None }) } /// Returns `true` if the package name is valid. diff --git a/leo/package/src/root/gitignore.rs b/leo/package/src/root/gitignore.rs index edb1b33d9f..616e6245ea 100644 --- a/leo/package/src/root/gitignore.rs +++ b/leo/package/src/root/gitignore.rs @@ -46,8 +46,7 @@ impl Gitignore { } let mut file = File::create(&path).map_err(PackageError::io_error_gitignore_file)?; - file.write_all(self.template().as_bytes()) - .map_err(PackageError::io_error_gitignore_file)?; + file.write_all(self.template().as_bytes()).map_err(PackageError::io_error_gitignore_file)?; Ok(()) } diff --git a/leo/package/src/source/main.rs b/leo/package/src/source/main.rs index c0d13221ce..eb9fdc8c31 100644 --- a/leo/package/src/source/main.rs +++ b/leo/package/src/source/main.rs @@ -31,9 +31,7 @@ pub struct MainFile { impl MainFile { pub fn new(package_name: &str) -> Self { - Self { - package_name: package_name.to_string(), - } + Self { package_name: package_name.to_string() } } pub fn filename() -> String { @@ -61,9 +59,7 @@ impl MainFile { } let mut file = File::create(&path).map_err(PackageError::io_error_main_file)?; - Ok(file - .write_all(self.template().as_bytes()) - .map_err(PackageError::io_error_main_file)?) + Ok(file.write_all(self.template().as_bytes()).map_err(PackageError::io_error_main_file)?) } // TODO: Generalize to other networks. diff --git a/tests/test-framework/benches/leo_compiler.rs b/tests/test-framework/benches/leo_compiler.rs index af52563e68..d06226ceb3 100644 --- a/tests/test-framework/benches/leo_compiler.rs +++ b/tests/test-framework/benches/leo_compiler.rs @@ -104,10 +104,7 @@ impl Sample { /// Leverages the test-framework to grab all tests /// that are passing compiler tests or marked as benchmark tests. fn load_samples() -> Vec { - get_benches() - .into_iter() - .map(|(name, input)| Self { name, input }) - .collect() + get_benches().into_iter().map(|(name, input)| Self { name, input }).collect() } fn data(&self) -> (&str, FileName) { @@ -147,9 +144,7 @@ impl Sample { fn bencher_after_parse(&self, c: &mut Criterion, mode: &str, mut logic: impl FnMut(Compiler) -> Duration) { self.bencher(c, mode, |mut compiler| { let (input, name) = self.data(); - compiler - .parse_program_from_string(input, name) - .expect("Failed to parse program"); + compiler.parse_program_from_string(input, name).expect("Failed to parse program"); logic(compiler) }); } @@ -189,9 +184,8 @@ impl Sample { fn bench_loop_unroller(&self, c: &mut Criterion) { self.bencher_after_parse(c, "loop unrolling pass", |mut compiler| { let symbol_table = compiler.symbol_table_pass().expect("failed to generate symbol table"); - let (symbol_table, _struct_graph, _call_graph) = compiler - .type_checker_pass(symbol_table) - .expect("failed to run type check pass"); + let (symbol_table, _struct_graph, _call_graph) = + compiler.type_checker_pass(symbol_table).expect("failed to run type check pass"); let start = Instant::now(); let out = compiler.loop_unrolling_pass(symbol_table); let time = start.elapsed(); @@ -203,12 +197,9 @@ impl Sample { fn bench_ssa(&self, c: &mut Criterion) { self.bencher_after_parse(c, "full", |mut compiler| { let symbol_table = compiler.symbol_table_pass().expect("failed to generate symbol table"); - let (symbol_table, _struct_graph, _call_graph) = compiler - .type_checker_pass(symbol_table) - .expect("failed to run type check pass"); - let symbol_table = compiler - .loop_unrolling_pass(symbol_table) - .expect("failed to run loop unrolling pass"); + let (symbol_table, _struct_graph, _call_graph) = + compiler.type_checker_pass(symbol_table).expect("failed to run type check pass"); + let symbol_table = compiler.loop_unrolling_pass(symbol_table).expect("failed to run loop unrolling pass"); let start = Instant::now(); let out = compiler.static_single_assignment_pass(&symbol_table); let time = start.elapsed(); @@ -220,15 +211,10 @@ impl Sample { fn bench_flattener(&self, c: &mut Criterion) { self.bencher_after_parse(c, "flattener pass", |mut compiler| { let symbol_table = compiler.symbol_table_pass().expect("failed to generate symbol table"); - let (symbol_table, _struct_graph, _call_graph) = compiler - .type_checker_pass(symbol_table) - .expect("failed to run type check pass"); - let symbol_table = compiler - .loop_unrolling_pass(symbol_table) - .expect("failed to run loop unrolling pass"); - let assigner = compiler - .static_single_assignment_pass(&symbol_table) - .expect("failed to run ssa pass"); + let (symbol_table, _struct_graph, _call_graph) = + compiler.type_checker_pass(symbol_table).expect("failed to run type check pass"); + let symbol_table = compiler.loop_unrolling_pass(symbol_table).expect("failed to run loop unrolling pass"); + let assigner = compiler.static_single_assignment_pass(&symbol_table).expect("failed to run ssa pass"); let start = Instant::now(); let out = compiler.flattening_pass(&symbol_table, assigner); let time = start.elapsed(); @@ -240,18 +226,11 @@ impl Sample { fn bench_inline(&self, c: &mut Criterion) { self.bencher_after_parse(c, "inliner pass", |mut compiler| { let symbol_table = compiler.symbol_table_pass().expect("failed to generate symbol table"); - let (symbol_table, _struct_graph, call_graph) = compiler - .type_checker_pass(symbol_table) - .expect("failed to run type check pass"); - let symbol_table = compiler - .loop_unrolling_pass(symbol_table) - .expect("failed to run loop unrolling pass"); - let assigner = compiler - .static_single_assignment_pass(&symbol_table) - .expect("failed to run ssa pass"); - let assigner = compiler - .flattening_pass(&symbol_table, assigner) - .expect("failed to run flattener pass"); + let (symbol_table, _struct_graph, call_graph) = + compiler.type_checker_pass(symbol_table).expect("failed to run type check pass"); + let symbol_table = compiler.loop_unrolling_pass(symbol_table).expect("failed to run loop unrolling pass"); + let assigner = compiler.static_single_assignment_pass(&symbol_table).expect("failed to run ssa pass"); + let assigner = compiler.flattening_pass(&symbol_table, assigner).expect("failed to run flattener pass"); let start = Instant::now(); let out = compiler.function_inlining_pass(&call_graph, assigner); let time = start.elapsed(); @@ -263,21 +242,12 @@ impl Sample { fn bench_dce(&self, c: &mut Criterion) { self.bencher_after_parse(c, "inliner pass", |mut compiler| { let symbol_table = compiler.symbol_table_pass().expect("failed to generate symbol table"); - let (symbol_table, _struct_graph, call_graph) = compiler - .type_checker_pass(symbol_table) - .expect("failed to run type check pass"); - let symbol_table = compiler - .loop_unrolling_pass(symbol_table) - .expect("failed to run loop unrolling pass"); - let assigner = compiler - .static_single_assignment_pass(&symbol_table) - .expect("failed to run ssa pass"); - let assigner = compiler - .flattening_pass(&symbol_table, assigner) - .expect("failed to run flattener pass"); - let _ = compiler - .function_inlining_pass(&call_graph, assigner) - .expect("failed to run inliner pass"); + let (symbol_table, _struct_graph, call_graph) = + compiler.type_checker_pass(symbol_table).expect("failed to run type check pass"); + let symbol_table = compiler.loop_unrolling_pass(symbol_table).expect("failed to run loop unrolling pass"); + let assigner = compiler.static_single_assignment_pass(&symbol_table).expect("failed to run ssa pass"); + let assigner = compiler.flattening_pass(&symbol_table, assigner).expect("failed to run flattener pass"); + let _ = compiler.function_inlining_pass(&call_graph, assigner).expect("failed to run inliner pass"); let start = Instant::now(); let out = compiler.dead_code_elimination_pass(); let time = start.elapsed(); @@ -289,21 +259,12 @@ impl Sample { fn bench_codegen(&self, c: &mut Criterion) { self.bencher_after_parse(c, "inliner pass", |mut compiler| { let symbol_table = compiler.symbol_table_pass().expect("failed to generate symbol table"); - let (symbol_table, struct_graph, call_graph) = compiler - .type_checker_pass(symbol_table) - .expect("failed to run type check pass"); - let symbol_table = compiler - .loop_unrolling_pass(symbol_table) - .expect("failed to run loop unrolling pass"); - let assigner = compiler - .static_single_assignment_pass(&symbol_table) - .expect("failed to run ssa pass"); - let assigner = compiler - .flattening_pass(&symbol_table, assigner) - .expect("failed to run flattener pass"); - let _ = compiler - .function_inlining_pass(&call_graph, assigner) - .expect("failed to run inliner pass"); + let (symbol_table, struct_graph, call_graph) = + compiler.type_checker_pass(symbol_table).expect("failed to run type check pass"); + let symbol_table = compiler.loop_unrolling_pass(symbol_table).expect("failed to run loop unrolling pass"); + let assigner = compiler.static_single_assignment_pass(&symbol_table).expect("failed to run ssa pass"); + let assigner = compiler.flattening_pass(&symbol_table, assigner).expect("failed to run flattener pass"); + let _ = compiler.function_inlining_pass(&call_graph, assigner).expect("failed to run inliner pass"); compiler.dead_code_elimination_pass().expect("failed to run dce pass"); let start = Instant::now(); let out = compiler.code_generation_pass(&symbol_table, &struct_graph, &call_graph); @@ -317,25 +278,14 @@ impl Sample { self.bencher(c, "full", |mut compiler| { let (input, name) = self.data(); let start = Instant::now(); - compiler - .parse_program_from_string(input, name) - .expect("Failed to parse program"); + compiler.parse_program_from_string(input, name).expect("Failed to parse program"); let symbol_table = compiler.symbol_table_pass().expect("failed to generate symbol table"); - let (symbol_table, struct_graph, call_graph) = compiler - .type_checker_pass(symbol_table) - .expect("failed to run type check pass"); - let symbol_table = compiler - .loop_unrolling_pass(symbol_table) - .expect("failed to run loop unrolling pass"); - let assigner = compiler - .static_single_assignment_pass(&symbol_table) - .expect("failed to run ssa pass"); - let assigner = compiler - .flattening_pass(&symbol_table, assigner) - .expect("failed to run flattening pass"); - compiler - .function_inlining_pass(&call_graph, assigner) - .expect("failed to run function inlining pass"); + let (symbol_table, struct_graph, call_graph) = + compiler.type_checker_pass(symbol_table).expect("failed to run type check pass"); + let symbol_table = compiler.loop_unrolling_pass(symbol_table).expect("failed to run loop unrolling pass"); + let assigner = compiler.static_single_assignment_pass(&symbol_table).expect("failed to run ssa pass"); + let assigner = compiler.flattening_pass(&symbol_table, assigner).expect("failed to run flattening pass"); + compiler.function_inlining_pass(&call_graph, assigner).expect("failed to run function inlining pass"); compiler.dead_code_elimination_pass().expect("failed to run dce pass"); compiler .code_generation_pass(&symbol_table, &struct_graph, &call_graph) diff --git a/tests/test-framework/src/error.rs b/tests/test-framework/src/error.rs index 81880e631c..743ab229b6 100644 --- a/tests/test-framework/src/error.rs +++ b/tests/test-framework/src/error.rs @@ -27,32 +27,11 @@ pub struct TestFailure { #[derive(Debug)] pub enum TestError { - Panicked { - test: String, - index: usize, - error: String, - }, - UnexpectedOutput { - test: String, - index: usize, - expected: Value, - output: Value, - }, - PassedAndShouldntHave { - test: String, - index: usize, - }, - FailedAndShouldntHave { - test: String, - index: usize, - error: String, - }, - UnexpectedError { - test: String, - index: usize, - expected: String, - output: String, - }, + Panicked { test: String, index: usize, error: String }, + UnexpectedOutput { test: String, index: usize, expected: Value, output: Value }, + PassedAndShouldntHave { test: String, index: usize }, + FailedAndShouldntHave { test: String, index: usize, error: String }, + UnexpectedError { test: String, index: usize, expected: String, output: String }, MismatchedTestExpectationLength, MissingTestConfig, } @@ -60,28 +39,13 @@ pub enum TestError { impl fmt::Display for TestError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let format_test = |test: &str| -> String { - if test.len() > 50 { - String::new() - } else { - format!("\n\n{test}\n\n") - } + if test.len() > 50 { String::new() } else { format!("\n\n{test}\n\n") } }; match self { TestError::Panicked { test, index, error } => { - write!( - f, - "test #{}: {}encountered a rust panic:\n{}", - index + 1, - format_test(test), - error - ) + write!(f, "test #{}: {}encountered a rust panic:\n{}", index + 1, format_test(test), error) } - TestError::UnexpectedOutput { - test, - index, - expected, - output, - } => { + TestError::UnexpectedOutput { test, index, expected, output } => { write!( f, "test #{}: {}expected\n{}\ngot\n{}", @@ -95,28 +59,10 @@ impl fmt::Display for TestError { write!(f, "test #{}: {}passed and shouldn't have", index + 1, format_test(test)) } TestError::FailedAndShouldntHave { test, index, error } => { - write!( - f, - "test #{}: {}failed and shouldn't have:\n{}", - index + 1, - format_test(test), - error - ) + write!(f, "test #{}: {}failed and shouldn't have:\n{}", index + 1, format_test(test), error) } - TestError::UnexpectedError { - test, - expected, - output, - index, - } => { - write!( - f, - "test #{}: {}expected error\n{}\ngot\n{}", - index + 1, - format_test(test), - expected, - output - ) + TestError::UnexpectedError { test, expected, output, index } => { + write!(f, "test #{}: {}expected error\n{}\ngot\n{}", index + 1, format_test(test), expected, output) } TestError::MismatchedTestExpectationLength => write!(f, "invalid number of test expectations"), TestError::MissingTestConfig => write!(f, "missing test config"), @@ -132,11 +78,7 @@ pub fn emit_errors( test_index: usize, ) -> Option { match (output, mode) { - (Err(e), _) => Some(TestError::Panicked { - test: test.to_string(), - index: test_index, - error: e.to_string(), - }), + (Err(e), _) => Some(TestError::Panicked { test: test.to_string(), index: test_index, error: e.to_string() }), (Ok(Ok(output)), TestExpectationMode::Pass) => { // passed and should have if let Some(expected_output) = expected_output.as_ref() { @@ -152,15 +94,12 @@ pub fn emit_errors( } None } - (Ok(Ok(_tokens)), TestExpectationMode::Fail) => Some(TestError::PassedAndShouldntHave { - test: test.to_string(), - index: test_index, - }), - (Ok(Err(err)), TestExpectationMode::Pass) => Some(TestError::FailedAndShouldntHave { - test: test.to_string(), - error: err.to_string(), - index: test_index, - }), + (Ok(Ok(_tokens)), TestExpectationMode::Fail) => { + Some(TestError::PassedAndShouldntHave { test: test.to_string(), index: test_index }) + } + (Ok(Err(err)), TestExpectationMode::Pass) => { + Some(TestError::FailedAndShouldntHave { test: test.to_string(), error: err.to_string(), index: test_index }) + } (Ok(Err(err)), TestExpectationMode::Fail) => { let expected_output: Option = expected_output.map(|x| serde_yaml::from_value(x).expect("test expectation deserialize failed")); diff --git a/tests/test-framework/src/fetch.rs b/tests/test-framework/src/fetch.rs index f057f8084e..4e21d2afc4 100644 --- a/tests/test-framework/src/fetch.rs +++ b/tests/test-framework/src/fetch.rs @@ -24,12 +24,9 @@ use walkdir::WalkDir; pub fn find_tests(path: &Path) -> impl Iterator { WalkDir::new(path).into_iter().flatten().filter_map(move |f| { let path = f.path(); - path.extension().filter(|s| *s == "leo").map(|_| { - ( - path.to_path_buf(), - fs::read_to_string(path).expect("failed to read test"), - ) - }) + path.extension() + .filter(|s| *s == "leo") + .map(|_| (path.to_path_buf(), fs::read_to_string(path).expect("failed to read test"))) }) } diff --git a/tests/test-framework/src/runner.rs b/tests/test-framework/src/runner.rs index b215e13014..1fb9e0f78d 100644 --- a/tests/test-framework/src/runner.rs +++ b/tests/test-framework/src/runner.rs @@ -142,12 +142,7 @@ impl TestCases { let mut output = Vec::new(); for ((path, content), config) in self.tests.clone().iter().zip(configs.into_iter()) { - let test_name = path - .file_stem() - .expect("no file name for test") - .to_str() - .unwrap() - .to_string(); + let test_name = path.file_stem().expect("no file name for test").to_str().unwrap().to_string(); let end_of_header = content.find("*/").expect("failed to find header block in test"); let content = &content[end_of_header + 2..]; @@ -176,10 +171,7 @@ impl TestCases { (expectation_path, None) } else { let raw = std::fs::read_to_string(&expectation_path).expect("failed to read expectations file"); - ( - expectation_path, - Some(serde_yaml::from_str(&raw).expect("invalid yaml in expectations file")), - ) + (expectation_path, Some(serde_yaml::from_str(&raw).expect("invalid yaml in expectations file"))) } } else { (expectation_path, None) @@ -204,10 +196,7 @@ pub fn run_tests(runner: &T, expectation_category: &str) { let (expectation_path, expectations) = cases.load_expectations(path); let tests = match namespace.parse_type() { - ParseType::Line => crate::fetch::split_tests_one_line(content) - .into_iter() - .map(|x| x.to_string()) - .collect(), + ParseType::Line => crate::fetch::split_tests_one_line(content).into_iter().map(|x| x.to_string()).collect(), ParseType::ContinuousLines => crate::fetch::split_tests_two_line(content), ParseType::Whole => vec![content.to_string()], }; @@ -251,30 +240,21 @@ pub fn run_tests(runner: &T, expectation_category: &str) { if errors.is_empty() { if expectations.is_none() { - outputs.push(( - expectation_path, - TestExpectation { - namespace: config.namespace, - expectation: config.expectation, - outputs: new_outputs, - }, - )); + outputs.push((expectation_path, TestExpectation { + namespace: config.namespace, + expectation: config.expectation, + outputs: new_outputs, + })); } pass_categories += 1; } else { - cases.fail_categories.push(TestFailure { - path: path.to_str().unwrap().to_string(), - errors, - }) + cases.fail_categories.push(TestFailure { path: path.to_str().unwrap().to_string(), errors }) } }); if !cases.fail_categories.is_empty() { for (i, fail) in cases.fail_categories.iter().enumerate() { - println!( - "\n\n-----------------TEST #{} FAILED (and shouldn't have)-----------------", - i + 1 - ); + println!("\n\n-----------------TEST #{} FAILED (and shouldn't have)-----------------", i + 1); println!("File: {}", fail.path); for error in &fail.errors { println!("{error}"); @@ -313,13 +293,8 @@ pub fn get_benches() -> Vec<(String, String)> { let (mut cases, configs) = TestCases::new("compiler", |config| { (&config.namespace == "Bench" && config.expectation == TestExpectationMode::Pass) || ((&config.namespace == "Compile" || &config.namespace == "Execute") - && !matches!( - config.expectation, - TestExpectationMode::Fail | TestExpectationMode::Skip - )) + && !matches!(config.expectation, TestExpectationMode::Fail | TestExpectationMode::Skip)) }); - cases.process_tests(configs, |_, (_, content, test_name, _)| { - (test_name.to_string(), content.to_string()) - }) + cases.process_tests(configs, |_, (_, content, test_name, _)| (test_name.to_string(), content.to_string())) }