manually impl PartialEq for array types

This commit is contained in:
collin 2020-10-23 21:54:19 -07:00
parent d55bd0d2af
commit 8e75e36532
2 changed files with 75 additions and 28 deletions

View File

@ -17,6 +17,7 @@
use crate::{
assert_satisfied,
expect_compiler_error,
expect_dynamic_check_error,
get_output,
parse_program,
parse_program_with_input,
@ -150,17 +151,17 @@ fn test_input_tuple_3x2_fail() {
#[test]
fn test_multi_fail_initializer() {
let program_bytes = include_bytes!("multi_fail_initializer.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
#[test]
fn test_multi_inline_fail() {
let program_bytes = include_bytes!("multi_fail_inline.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
#[test]
@ -174,9 +175,9 @@ fn test_multi_initializer() {
#[test]
fn test_multi_initializer_fail() {
let program_bytes = include_bytes!("multi_initializer_fail.leo");
let program = parse_program(program_bytes).unwrap();
let err = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(err);
}
#[test]
@ -190,9 +191,9 @@ fn test_nested_3x2_value() {
#[test]
fn test_nested_3x2_value_fail() {
let program_bytes = include_bytes!("nested_3x2_value_fail.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
#[test]
@ -206,9 +207,9 @@ fn test_tuple_3x2_value() {
#[test]
fn test_tuple_3x2_value_fail() {
let program_bytes = include_bytes!("tuple_3x2_value_fail.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
#[test]
@ -250,9 +251,9 @@ fn test_type_nested_value_nested_3x2() {
#[test]
fn test_type_nested_value_nested_3x2_fail() {
let program_bytes = include_bytes!("type_nested_value_nested_3x2_fail.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
#[test]
@ -266,9 +267,9 @@ fn test_type_nested_value_nested_4x3x2() {
#[test]
fn test_type_nested_value_nested_4x3x2_fail() {
let program_bytes = include_bytes!("type_nested_value_nested_4x3x2_fail.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
#[test]
@ -282,9 +283,9 @@ fn test_type_nested_value_tuple_3x2() {
#[test]
fn test_type_nested_value_tuple_3x2_fail() {
let program_bytes = include_bytes!("type_nested_value_tuple_3x2_fail.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
#[test]
@ -298,9 +299,9 @@ fn test_type_nested_value_tuple_4x3x2() {
#[test]
fn test_type_nested_value_tuple_4x3x2_fail() {
let program_bytes = include_bytes!("type_nested_value_tuple_4x3x2_fail.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
#[test]
@ -314,9 +315,9 @@ fn test_type_tuple_value_nested_3x2() {
#[test]
fn test_type_tuple_value_nested_3x2_fail() {
let program_bytes = include_bytes!("type_tuple_value_nested_3x2_fail.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
#[test]
@ -330,9 +331,9 @@ fn test_type_tuple_value_nested_4x3x2() {
#[test]
fn test_type_tuple_value_nested_4x3x2_fail() {
let program_bytes = include_bytes!("type_tuple_value_nested_4x3x2_fail.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
#[test]
@ -346,9 +347,9 @@ fn test_type_tuple_value_tuple_3x2() {
#[test]
fn test_type_tuple_value_tuple_3x2_fail() {
let program_bytes = include_bytes!("type_tuple_value_tuple_3x2_fail.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
#[test]
@ -362,9 +363,9 @@ fn test_type_tuple_value_tuple_4x3x2() {
#[test]
fn test_type_tuple_value_tuple_4x3x2_fail() {
let program_bytes = include_bytes!("type_tuple_value_tuple_4x3x2_fail.leo");
let program = parse_program(program_bytes).unwrap();
let error = parse_program(program_bytes).err().unwrap();
expect_compiler_error(program);
expect_dynamic_check_error(error);
}
// Tests for nested multi-dimensional arrays as input to the program

View File

@ -17,10 +17,13 @@ use crate::{SymbolTable, TypeError, TypeVariable};
use leo_typed::{Identifier, IntegerType, Span, Type as UnresolvedType};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::{
cmp::{Eq, PartialEq},
fmt,
};
/// A type in a Leo program.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Clone, Debug, Hash, Serialize, Deserialize)]
pub enum Type {
// Data types
Address,
@ -273,3 +276,46 @@ impl fmt::Display for Type {
}
}
}
impl PartialEq for Type {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Type::Address, Type::Address) => true,
(Type::Boolean, Type::Boolean) => true,
(Type::Field, Type::Field) => true,
(Type::Group, Type::Group) => true,
(Type::IntegerType(integer_type1), Type::IntegerType(integer_type2)) => integer_type1.eq(integer_type2),
(Type::Array(type1, dimensions1), Type::Array(type2, dimensions2)) => {
// Flatten both array types before comparison.
let (type1_flat, dimensions1_flat) = flatten_array_type(type1, dimensions1.to_owned());
let (type2_flat, dimensions2_flat) = flatten_array_type(type2, dimensions2.to_owned());
// Element types and dimensions must match
type1_flat.eq(type2_flat) && dimensions1_flat.eq(&dimensions2_flat)
}
(Type::Tuple(types1), Type::Tuple(types2)) => types1.eq(types2),
(Type::Circuit(identifier1), Type::Circuit(identifier2)) => identifier1.eq(identifier2),
(Type::Function(identifier1), Type::Function(identifier2)) => identifier1.eq(identifier2),
(Type::TypeVariable(variable1), Type::TypeVariable(variable2)) => variable1.eq(variable2),
_ => false,
}
}
}
impl Eq for Type {}
///
/// Returns the data type of the array element and vector of dimensions.
///
/// Will flatten an array type `[[[u8; 1]; 2]; 3]` into `[u8; (3, 2, 1)]`.
///
fn flatten_array_type(type_: &Type, mut dimensions: Vec<usize>) -> (&Type, Vec<usize>) {
if let Type::Array(element_type, element_dimensions) = type_ {
dimensions.append(&mut element_dimensions.to_owned());
flatten_array_type(element_type, dimensions)
} else {
(type_, dimensions)
}
}