From 1631dd6a60d2b7df57e24233127de01cc39400cc Mon Sep 17 00:00:00 2001 From: Pranav Gaddamadugu Date: Fri, 13 Oct 2023 16:21:18 -0400 Subject: [PATCH] Cleanup --- .../passes/src/code_generation/generator.rs | 1 - .../src/code_generation/visit_expressions.rs | 2 +- .../src/code_generation/visit_program.rs | 4 +-- .../passes/src/code_generation/visit_type.rs | 11 ++++--- compiler/passes/src/common/type_table/mod.rs | 5 +-- .../eliminate_expression.rs | 3 -- .../destructuring/destructure_expression.rs | 18 +---------- .../src/destructuring/destructure_program.rs | 2 +- .../destructuring/destructure_statement.rs | 10 ------ .../passes/src/destructuring/destructurer.rs | 32 ++----------------- compiler/passes/src/destructuring/mod.rs | 2 +- .../src/flattening/flatten_expression.rs | 7 ---- .../passes/src/flattening/flatten_program.rs | 2 +- .../src/flattening/flatten_statement.rs | 8 +---- .../static_single_assigner.rs | 2 +- compiler/passes/src/type_checking/checker.rs | 2 +- 16 files changed, 19 insertions(+), 92 deletions(-) diff --git a/compiler/passes/src/code_generation/generator.rs b/compiler/passes/src/code_generation/generator.rs index 6754d0998b..e9e4c8619f 100644 --- a/compiler/passes/src/code_generation/generator.rs +++ b/compiler/passes/src/code_generation/generator.rs @@ -20,7 +20,6 @@ use leo_ast::{Function, Program, ProgramId}; use leo_span::Symbol; use indexmap::IndexMap; -use snarkvm_console::program::Access; pub struct CodeGenerator<'a> { /// The symbol table for the program. diff --git a/compiler/passes/src/code_generation/visit_expressions.rs b/compiler/passes/src/code_generation/visit_expressions.rs index 7481a218e3..c2926606fc 100644 --- a/compiler/passes/src/code_generation/visit_expressions.rs +++ b/compiler/passes/src/code_generation/visit_expressions.rs @@ -170,7 +170,7 @@ impl<'a> CodeGenerator<'a> { Some(Type::Array(array_type)) => Type::Array(array_type), _ => unreachable!("All types should be known at this phase of compilation"), }; - let array_type: String = self.visit_type(&array_type); + let array_type: String = Self::visit_type(&array_type); let array_instruction = format!(" cast {expression_operands} into {destination_register} as {};\n", array_type); diff --git a/compiler/passes/src/code_generation/visit_program.rs b/compiler/passes/src/code_generation/visit_program.rs index 98dee76f76..ec6433ac1f 100644 --- a/compiler/passes/src/code_generation/visit_program.rs +++ b/compiler/passes/src/code_generation/visit_program.rs @@ -130,7 +130,7 @@ impl<'a> CodeGenerator<'a> { // Construct and append the record variables. for var in struct_.members.iter() { - writeln!(output_string, " {} as {};", var.identifier, self.visit_type(&var.type_),) + writeln!(output_string, " {} as {};", var.identifier, Self::visit_type(&var.type_),) .expect("failed to write to string"); } @@ -154,7 +154,7 @@ impl<'a> CodeGenerator<'a> { output_string, " {} as {}.{mode};", // todo: CAUTION private record variables only. var.identifier, - self.visit_type(&var.type_) + Self::visit_type(&var.type_) ) .expect("failed to write to string"); } diff --git a/compiler/passes/src/code_generation/visit_type.rs b/compiler/passes/src/code_generation/visit_type.rs index 674e065c53..b9364a7435 100644 --- a/compiler/passes/src/code_generation/visit_type.rs +++ b/compiler/passes/src/code_generation/visit_type.rs @@ -19,7 +19,7 @@ use crate::CodeGenerator; use leo_ast::{Mode, Type}; impl<'a> CodeGenerator<'a> { - pub(crate) fn visit_type(&mut self, input: &Type) -> String { + pub(crate) fn visit_type(input: &Type) -> String { match input { Type::Address | Type::Boolean @@ -31,7 +31,8 @@ impl<'a> CodeGenerator<'a> { | Type::Identifier(..) | Type::Integer(..) => format!("{input}"), Type::Array(array_type) => { - format!("[{}; {}u32]", self.visit_type(&array_type.element_type()), array_type.length()) + let element_type = Self::visit_type(array_type.element_type()); + format!("[{element_type}; {}u32]", array_type.length()) } Type::Mapping(_) => { unreachable!("Mapping types are not supported at this phase of compilation") @@ -44,7 +45,7 @@ impl<'a> CodeGenerator<'a> { } } - pub(crate) fn visit_type_with_visibility(&mut self, type_: &'a Type, visibility: Mode) -> String { + pub(crate) fn visit_type_with_visibility(&self, type_: &'a Type, visibility: Mode) -> String { match type_ { // When the type is a record. // Note that this unwrap is safe because all composite types have been added to the mapping. @@ -52,8 +53,8 @@ impl<'a> CodeGenerator<'a> { format!("{identifier}.record") } _ => match visibility { - Mode::None => self.visit_type(type_), - _ => format!("{}.{visibility}", self.visit_type(type_)), + Mode::None => Self::visit_type(type_), + _ => format!("{}.{visibility}", Self::visit_type(type_)), }, } } diff --git a/compiler/passes/src/common/type_table/mod.rs b/compiler/passes/src/common/type_table/mod.rs index fe855fc940..1e1fea894d 100644 --- a/compiler/passes/src/common/type_table/mod.rs +++ b/compiler/passes/src/common/type_table/mod.rs @@ -17,10 +17,7 @@ use leo_ast::{NodeID, Type}; use indexmap::IndexMap; -use std::{ - cell::{Ref, RefCell}, - fmt::Display, -}; +use std::cell::RefCell; /// A mapping between node IDs and their types. #[derive(Debug, Default, Clone)] diff --git a/compiler/passes/src/dead_code_elimination/eliminate_expression.rs b/compiler/passes/src/dead_code_elimination/eliminate_expression.rs index df429c0bc0..31ccaf3545 100644 --- a/compiler/passes/src/dead_code_elimination/eliminate_expression.rs +++ b/compiler/passes/src/dead_code_elimination/eliminate_expression.rs @@ -18,15 +18,12 @@ use crate::DeadCodeEliminator; use leo_ast::{ AccessExpression, - ArrayAccess, AssociatedFunction, Expression, ExpressionReconstructor, Identifier, - MemberAccess, StructExpression, StructVariableInitializer, - TupleAccess, Type, }; use leo_span::sym; diff --git a/compiler/passes/src/destructuring/destructure_expression.rs b/compiler/passes/src/destructuring/destructure_expression.rs index cb40386a1d..ae25af2b21 100644 --- a/compiler/passes/src/destructuring/destructure_expression.rs +++ b/compiler/passes/src/destructuring/destructure_expression.rs @@ -15,24 +15,8 @@ // along with the Leo library. If not, see . use crate::Destructurer; -use itertools::Itertools; -use leo_ast::{ - AccessExpression, - ArrayAccess, - AssociatedFunction, - Expression, - ExpressionReconstructor, - Member, - MemberAccess, - Node, - Statement, - StructExpression, - StructVariableInitializer, - TernaryExpression, - TupleAccess, - Type, -}; +use leo_ast::{Expression, ExpressionReconstructor, Statement, TupleAccess}; impl ExpressionReconstructor for Destructurer<'_> { type AdditionalOutput = Vec; diff --git a/compiler/passes/src/destructuring/destructure_program.rs b/compiler/passes/src/destructuring/destructure_program.rs index bcb5539299..0809fc68a6 100644 --- a/compiler/passes/src/destructuring/destructure_program.rs +++ b/compiler/passes/src/destructuring/destructure_program.rs @@ -16,6 +16,6 @@ use crate::Destructurer; -use leo_ast::{Finalize, Function, ProgramReconstructor, StatementReconstructor, Type}; +use leo_ast::ProgramReconstructor; impl ProgramReconstructor for Destructurer<'_> {} diff --git a/compiler/passes/src/destructuring/destructure_statement.rs b/compiler/passes/src/destructuring/destructure_statement.rs index 165f08fcd2..bbcf811784 100644 --- a/compiler/passes/src/destructuring/destructure_statement.rs +++ b/compiler/passes/src/destructuring/destructure_statement.rs @@ -16,16 +16,9 @@ use crate::Destructurer; use itertools::Itertools; -use std::borrow::Borrow; use leo_ast::{ - AccessExpression, - AssertStatement, - AssertVariant, AssignStatement, - AssociatedFunction, - BinaryExpression, - BinaryOperation, Block, ConditionalStatement, ConsoleStatement, @@ -40,10 +33,7 @@ use leo_ast::{ StatementReconstructor, TupleExpression, Type, - UnaryExpression, - UnaryOperation, }; -use leo_span::sym; impl StatementReconstructor for Destructurer<'_> { /// Flattens an assign statement, if necessary. diff --git a/compiler/passes/src/destructuring/destructurer.rs b/compiler/passes/src/destructuring/destructurer.rs index 6385a8b5ba..6d61f3ea30 100644 --- a/compiler/passes/src/destructuring/destructurer.rs +++ b/compiler/passes/src/destructuring/destructurer.rs @@ -14,37 +14,9 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Assigner, SymbolTable, TypeTable}; +use crate::{Assigner, TypeTable}; -use leo_ast::{ - AccessExpression, - ArrayAccess, - ArrayExpression, - ArrayType, - BinaryExpression, - BinaryOperation, - Block, - Expression, - ExpressionReconstructor, - Identifier, - IntegerType, - Literal, - Member, - MemberAccess, - Node, - NodeBuilder, - NonzeroNumber, - ReturnStatement, - Statement, - Struct, - StructExpression, - StructVariableInitializer, - TernaryExpression, - TupleAccess, - TupleExpression, - TupleType, - Type, -}; +use leo_ast::{Expression, Identifier, Node, NodeBuilder, Statement, TupleExpression}; use leo_span::Symbol; use indexmap::IndexMap; diff --git a/compiler/passes/src/destructuring/mod.rs b/compiler/passes/src/destructuring/mod.rs index 8c2ae20393..baf8ea5dd8 100644 --- a/compiler/passes/src/destructuring/mod.rs +++ b/compiler/passes/src/destructuring/mod.rs @@ -28,7 +28,7 @@ mod destructure_statement; pub mod destructurer; pub use destructurer::*; -use crate::{Assigner, Pass, SymbolTable, TypeTable}; +use crate::{Assigner, Pass, TypeTable}; use leo_ast::{Ast, NodeBuilder, ProgramReconstructor}; use leo_errors::Result; diff --git a/compiler/passes/src/flattening/flatten_expression.rs b/compiler/passes/src/flattening/flatten_expression.rs index e8d800a834..3a26a3cdbc 100644 --- a/compiler/passes/src/flattening/flatten_expression.rs +++ b/compiler/passes/src/flattening/flatten_expression.rs @@ -15,22 +15,15 @@ // along with the Leo library. If not, see . use crate::Flattener; -use itertools::Itertools; use leo_ast::{ - AccessExpression, - ArrayAccess, - AssociatedFunction, Expression, ExpressionReconstructor, - Member, - MemberAccess, Node, Statement, StructExpression, StructVariableInitializer, TernaryExpression, - TupleAccess, Type, }; diff --git a/compiler/passes/src/flattening/flatten_program.rs b/compiler/passes/src/flattening/flatten_program.rs index 940f5cbf66..3a15822c58 100644 --- a/compiler/passes/src/flattening/flatten_program.rs +++ b/compiler/passes/src/flattening/flatten_program.rs @@ -16,7 +16,7 @@ use crate::Flattener; -use leo_ast::{Finalize, Function, ProgramReconstructor, StatementReconstructor, Type}; +use leo_ast::{Finalize, Function, ProgramReconstructor, StatementReconstructor}; impl ProgramReconstructor for Flattener<'_> { /// Flattens a function's body and finalize block, if it exists. diff --git a/compiler/passes/src/flattening/flatten_statement.rs b/compiler/passes/src/flattening/flatten_statement.rs index c498690e97..7d79222ca5 100644 --- a/compiler/passes/src/flattening/flatten_statement.rs +++ b/compiler/passes/src/flattening/flatten_statement.rs @@ -16,14 +16,11 @@ use crate::Flattener; use itertools::Itertools; -use std::borrow::Borrow; use leo_ast::{ - AccessExpression, AssertStatement, AssertVariant, AssignStatement, - AssociatedFunction, BinaryExpression, BinaryOperation, Block, @@ -32,18 +29,15 @@ use leo_ast::{ DefinitionStatement, Expression, ExpressionReconstructor, - Identifier, IterationStatement, Node, ReturnStatement, Statement, StatementReconstructor, - TupleExpression, Type, UnaryExpression, UnaryOperation, }; -use leo_span::sym; impl StatementReconstructor for Flattener<'_> { /// Rewrites an assert statement into a flattened form. @@ -172,7 +166,7 @@ impl StatementReconstructor for Flattener<'_> { /// Otherwise, the statement is returned as is. fn reconstruct_assign(&mut self, assign: AssignStatement) -> (Statement, Self::AdditionalOutput) { // Flatten the rhs of the assignment. - let (value, mut statements) = self.reconstruct_expression(assign.value); + let (value, statements) = self.reconstruct_expression(assign.value); match (assign.place, &value) { (Expression::Identifier(identifier), _) => (self.simple_assign_statement(identifier, value), statements), (Expression::Tuple(tuple), expression) => { 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 df12a1c38a..ad4cf067d3 100644 --- a/compiler/passes/src/static_single_assignment/static_single_assigner.rs +++ b/compiler/passes/src/static_single_assignment/static_single_assigner.rs @@ -16,7 +16,7 @@ use crate::{Assigner, RenameTable, SymbolTable, TypeTable}; -use leo_ast::{AssignStatement, Expression, Identifier, Node, NodeBuilder, Statement}; +use leo_ast::{Expression, Identifier, Node, NodeBuilder, Statement}; pub struct StaticSingleAssigner<'a> { /// A counter used to generate unique node IDs. diff --git a/compiler/passes/src/type_checking/checker.rs b/compiler/passes/src/type_checking/checker.rs index ae4a04405b..f614649f59 100644 --- a/compiler/passes/src/type_checking/checker.rs +++ b/compiler/passes/src/type_checking/checker.rs @@ -16,7 +16,7 @@ use crate::{CallGraph, StructGraph, SymbolTable, TypeTable}; -use leo_ast::{ArrayType, CoreConstant, CoreFunction, Identifier, IntegerType, MappingType, Node, Type, Variant}; +use leo_ast::{CoreConstant, CoreFunction, Identifier, IntegerType, MappingType, Node, Type, Variant}; use leo_errors::{emitter::Handler, TypeCheckerError}; use leo_span::{Span, Symbol};