diff --git a/ast/src/input/input_value.rs b/ast/src/input/input_value.rs index acc7b3d12f..5d6fba2a9e 100644 --- a/ast/src/input/input_value.rs +++ b/ast/src/input/input_value.rs @@ -25,7 +25,7 @@ use pest::Span; use std::fmt; -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub enum InputValue { Address(String), Boolean(bool), diff --git a/compiler/src/errors/function.rs b/compiler/src/errors/function.rs index 3e020d33fd..5422761581 100644 --- a/compiler/src/errors/function.rs +++ b/compiler/src/errors/function.rs @@ -93,6 +93,15 @@ impl FunctionError { Self::new_from_span(message, span) } + pub fn invalid_input_array_dimensions(expected: usize, actual: usize, span: Span) -> Self { + let message = format!( + "Input array dimensions mismatch expected {}, found array dimensions {}", + expected, actual + ); + + Self::new_from_span(message, span) + } + pub fn invalid_tuple(actual: String, span: Span) -> Self { let message = format!("Expected function input tuple, found `{}`", actual); diff --git a/compiler/src/function/input/array.rs b/compiler/src/function/input/array.rs index 1732f87c21..f5e6ab2bf3 100644 --- a/compiler/src/function/input/array.rs +++ b/compiler/src/function/input/array.rs @@ -41,6 +41,14 @@ impl> ConstrainedProgram { match input_value { Some(InputValue::Array(arr)) => { + if array_len != arr.len() { + return Err(FunctionError::invalid_input_array_dimensions( + arr.len(), + array_len, + span.clone(), + )); + } + // Allocate each value in the current row for (i, value) in arr.into_iter().enumerate() { let value_name = format!("{}_{}", &name, &i.to_string()); diff --git a/compiler/src/function/input/input_section.rs b/compiler/src/function/input/input_section.rs index fea6f597a0..cbfee12200 100644 --- a/compiler/src/function/input/input_section.rs +++ b/compiler/src/function/input/input_section.rs @@ -37,7 +37,6 @@ impl> ConstrainedProgram { let mut members = Vec::with_capacity(section.len()); // Allocate each section definition as a circuit member value - for (parameter, option) in section.into_iter() { let section_members = expected_type.members.borrow(); let expected_type = match section_members.get(¶meter.variable.name) { diff --git a/compiler/tests/input_files/program_input/input/main_array_fail.in b/compiler/tests/input_files/program_input/input/main_array_fail.in new file mode 100644 index 0000000000..efdc161ee4 --- /dev/null +++ b/compiler/tests/input_files/program_input/input/main_array_fail.in @@ -0,0 +1,2 @@ +[main] +x: [i16; 1] = [0i16; 1]; \ No newline at end of file diff --git a/compiler/tests/input_files/program_input/main_array.leo b/compiler/tests/input_files/program_input/main_array.leo index 58acae24bf..dd7f265457 100644 --- a/compiler/tests/input_files/program_input/main_array.leo +++ b/compiler/tests/input_files/program_input/main_array.leo @@ -1,3 +1,4 @@ -function main (x: [i16; 2]) { +function main (x: [i16; 1]) { console.log("{}", x); + console.assert(x[0] == 0); } \ No newline at end of file diff --git a/compiler/tests/input_files/program_input/main_array_fail.leo b/compiler/tests/input_files/program_input/main_array_fail.leo new file mode 100644 index 0000000000..d8c6f301d8 --- /dev/null +++ b/compiler/tests/input_files/program_input/main_array_fail.leo @@ -0,0 +1,3 @@ +function main(x: [i16; 2]){ + console.log("x: {}", x); +} \ No newline at end of file diff --git a/compiler/tests/input_files/program_input/mod.rs b/compiler/tests/input_files/program_input/mod.rs index 4df45031e1..58efad93c1 100644 --- a/compiler/tests/input_files/program_input/mod.rs +++ b/compiler/tests/input_files/program_input/mod.rs @@ -83,3 +83,13 @@ fn test_input_multiple() { assert_satisfied(program); } + +#[test] +fn test_input_array_dimensions_mismatch() { + let program_string = include_str!("main_array_fail.leo"); + let input_string = include_str!("input/main_array_fail.in"); + + let program = parse_program_with_input(program_string, input_string).unwrap(); + + expect_fail(program); +}