Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-schott committed Nov 4, 2023
1 parent 5237333 commit 5b0459a
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 132 deletions.
6 changes: 3 additions & 3 deletions compiler/ast/src/stub/function_stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl<N: Network, Instruction: InstructionTrait<N>> From<&ClosureCore<N, Instruct
let output_type = match output_vec.len() {
0 => Type::Unit,
1 => output_vec[0].clone(),
_ => Type::Tuple(Tuple(output_vec)),
_ => Type::Tuple(TupleType::new(output_vec)),
};
Self {
annotations: Vec::new(),
Expand Down Expand Up @@ -272,13 +272,13 @@ impl<N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>>
.iter()
.map(|output| match output {
Output::Internal(output) => output.type_.clone(),
Output::External(output) => Type::Identifier(output.record.clone()),
Output::External(output) => Type::Identifier(output.record),
})
.collect_vec();
let output_type = match output_vec.len() {
0 => Type::Unit,
1 => output_vec[0].clone(),
_ => Type::Tuple(Tuple(output_vec)),
_ => Type::Tuple(TupleType::new(output_vec)),
};

Self {
Expand Down
2 changes: 1 addition & 1 deletion compiler/ast/src/types/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::{ArrayType, common, Identifier, IntegerType, MappingType, TupleType};
use crate::{common, ArrayType, Identifier, IntegerType, MappingType, TupleType};

use itertools::Itertools;
use serde::{Deserialize, Serialize};
Expand Down
4 changes: 2 additions & 2 deletions compiler/parser/src/parser/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl ParserContext<'_> {
self.expect(&Token::Dot)?;

// Otherwise throw parser error
self.expect(&Token::Aleo).or_else(|_| Err(ParserError::invalid_network(self.token.span)))?;
self.expect(&Token::Aleo).map_err(|_| ParserError::invalid_network(self.token.span))?;

// Construct the program id.
let program_id =
Expand Down Expand Up @@ -221,7 +221,7 @@ impl ParserContext<'_> {
// Parse `program` keyword.
let start = self.expect(&Token::Program)?;
let (program_scope, imports) = self.parse_program_body(start)?;
if imports.len() != 0 {
if !imports.is_empty() {
self.emit_err(ParserError::cannot_import_inside_program_body(self.token.span));
}
Ok(program_scope)
Expand Down
2 changes: 1 addition & 1 deletion compiler/parser/src/parser/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl ParserContext<'_> {
if let Some(ident) = self.eat_identifier() {
// Check if using external type
let file_type = self.look_ahead(1, |t| &t.token);
if &self.token.token == &Token::Dot && (file_type == &Token::Leo || file_type == &Token::Aleo) {
if self.token.token == Token::Dot && (file_type == &Token::Leo || file_type == &Token::Aleo) {
return Err(ParserError::external_type_cannot_be_used_inside_function(
ident,
file_type,
Expand Down
6 changes: 3 additions & 3 deletions compiler/passes/src/code_generation/visit_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,9 @@ impl<'a> CodeGenerator<'a> {
{
Some(program) => program,
None => {
let stub_program = self.program.stubs.get(&program_name);
if stub_program.is_some() {
stub_scope = ProgramScope::from(stub_program.unwrap().clone());
let _stub_program = self.program.stubs.get(&program_name);
if let Some(stub) = self.program.stubs.get(&program_name) {
stub_scope = ProgramScope::from(stub.clone());
&stub_scope
} else {
unreachable!("Type checking guarantees that imported and stub programs are well defined.")
Expand Down
2 changes: 1 addition & 1 deletion compiler/passes/src/destructuring/destructure_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl StatementReconstructor for Destructurer<'_> {
let statements = lhs_tuple
.elements
.into_iter()
.zip_eq(rhs_tuple.elements.into_iter())
.zip_eq(rhs_tuple.elements)
.map(|(lhs, rhs)| {
// Get the type of the rhs.
let type_ = match self.type_table.get(&lhs.id()) {
Expand Down
64 changes: 2 additions & 62 deletions compiler/passes/src/type_checking/check_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> {

fn visit_stub(&mut self, input: &'a Stub) {
// Cannot have mappings in stubs.
if input.mappings.len() != 0 {
if !input.mappings.is_empty() {
self.emit_err(TypeCheckerError::stubs_can_only_have_records_and_transitions(
"mapping",
input.mappings.get(0).unwrap().1.span,
));
}

// Cannot have constant declarations in stubs.
if input.consts.len() != 0 {
if !input.consts.is_empty() {
self.emit_err(TypeCheckerError::stubs_can_only_have_records_and_transitions(
"constant declaration",
input.consts.get(0).unwrap().1.span,
Expand Down Expand Up @@ -111,66 +111,6 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> {
self.exit_scope(function_index);
}

fn visit_struct_stub(&mut self, input: &'a Struct) {
self.visit_struct(input);
}

fn visit_stub(&mut self, input: &'a Stub) {
// Cannot have mappings in stubs.
if input.mappings.len() != 0 {
self.emit_err(TypeCheckerError::stubs_can_only_have_records_and_transitions(
"mapping",
input.mappings.get(0).unwrap().1.span,
));
}

// Cannot have constant declarations in stubs.
if input.consts.len() != 0 {
self.emit_err(TypeCheckerError::stubs_can_only_have_records_and_transitions(
"constant declaration",
input.consts.get(0).unwrap().1.span,
));
}

// Typecheck the program's structs.
input.structs.iter().for_each(|(_, function)| self.visit_struct_stub(function));

// Typecheck the program's functions.
input.functions.iter().for_each(|(_, function)| self.visit_function_stub(function));
}

fn visit_function_stub(&mut self, input: &'a FunctionStub) {
// Cannot have finalize scopes
if input.finalize.is_some() {
self.emit_err(TypeCheckerError::stub_functions_must_have_no_finalize(
input.finalize.as_ref().unwrap().span,
));
}

// Must be transition functions
if input.variant == Variant::Inline {
self.emit_err(TypeCheckerError::stub_functions_must_not_be_inlines(input.span));
}

// Must be empty
if !input.block.statements.is_empty() {
self.emit_err(TypeCheckerError::stub_functions_must_be_empty(input.block.span));
}

// 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(input.identifier.name).unwrap().id;

// Enter the function's scope.
self.enter_scope(function_index);

// Query helper function to type check function parameters and outputs.
self.check_function_signature(&Function::from(input.clone()));

// Exit the function's scope.
self.exit_scope(function_index);
}

fn visit_struct_stub(&mut self, input: &'a Struct) {
// Allow records only.
if !input.is_record {
Expand Down
24 changes: 0 additions & 24 deletions errors/src/errors/parser/parser_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,30 +292,6 @@ create_messages!(
help: None,
}

/// Enforce that empty functions cannot have finalize functions attached to them
@formatted
empty_function_cannot_have_finalize {
args: (),
msg: format!("Empty functions cannot have finalize functions attached to them."),
help: None,
}

/// Enforce that cannot use an external type to do anything except input/output of function
@formatted
external_type_cannot_be_used_inside_function {
args: (program: impl Display, file_type: impl Display),
msg: format!("External types cannot be used inside function (only as input/output types) -- found exported type from '{program}.{file_type}'."),
help: None,
}

/// Enforce that cannot use import in program scope
@formatted
cannot_import_inside_program_body {
args: (),
msg: format!("Cannot use import inside program body."),
help: None,
}

@formatted
array_must_have_at_least_one_element {
args: (kind: impl Display),
Expand Down
28 changes: 1 addition & 27 deletions errors/src/errors/type_checker/type_checker_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,12 +699,7 @@ create_messages!(
help: None,
}

@formatted
stub_functions_must_be_empty {
args: (),
msg: format!("Functions stubs must be empty"),
help: None,
}


@formatted
stub_functions_must_have_no_finalize {
Expand All @@ -713,34 +708,13 @@ create_messages!(
help: None,
}

@formatted
stubs_can_only_have_records_and_transitions {
args: (found: impl Display),
msg: format!("Stubs can only have records, transitions, functions and imports -- found {found}"),
help: None,
}

@formatted
stub_functions_must_not_be_inlines {
args: (),
msg: format!("Function stubs must be transitions or function variants not inlines"),
help: None,
}

@formatted
stub_functions_must_be_empty {
args: (),
msg: format!("Functions stubs must be empty"),
help: None,
}

@formatted
stub_functions_must_have_no_finalize {
args: (),
msg: format!("Function stubs must not have finalize blocks"),
help: None,
}

@formatted
array_empty {
args: (),
Expand Down
2 changes: 1 addition & 1 deletion utils/disassembler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ workspace = true

[dependencies.leo-ast]
path = "../../compiler/ast"
version = "=1.9.4"
version = "=1.10.0"

[dependencies.leo-span]
path = "../../compiler/span"
Expand Down
14 changes: 7 additions & 7 deletions utils/disassembler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use snarkvm::{
console::network::Testnet3,
prelude::{Itertools, Network},
synthesizer::program::{CommandTrait, InstructionTrait, Program, ProgramCore},
synthesizer::program::{CommandTrait, InstructionTrait, ProgramCore},
};
use std::str::FromStr;

use leo_ast::{FunctionStub, Identifier, ProgramId, Struct, Stub};
use leo_span::symbol::create_session_if_not_set_then;

type CurrentNetwork = Testnet3;

fn main() {}

Expand Down Expand Up @@ -61,14 +56,19 @@ pub fn disassemble<N: Network, Instruction: InstructionTrait<N>, Command: Comman
#[cfg(test)]
mod tests {
use super::*;
use leo_span::symbol::create_session_if_not_set_then;
use snarkvm::{prelude::Testnet3, synthesizer::program::Program};
use std::str::FromStr;

type CurrentNetwork = Testnet3;

#[test]
fn credits_test() {
create_session_if_not_set_then(|_| {
let aleo_prog_1 =
std::fs::read_to_string("/Users/evanschott/work/leo/utils/disassembler/src/tests/credits.aleo")
.unwrap();
let program = Program::<CurrentNetwork>::from_str(&*aleo_prog_1);
let program = Program::<CurrentNetwork>::from_str(&aleo_prog_1);
match program {
Ok(p) => {
let disassembled = disassemble(p);
Expand Down

0 comments on commit 5b0459a

Please sign in to comment.