From c15ca6ca53c7d144c3add0cff937c5c96f804926 Mon Sep 17 00:00:00 2001 From: evan-schott <53463459+evan-schott@users.noreply.github.com> Date: Mon, 25 Sep 2023 16:41:20 -0700 Subject: [PATCH 1/2] create error for tuple length mismatch --- compiler/passes/src/type_checking/check_statements.rs | 9 ++++++++- errors/src/errors/type_checker/type_checker_error.rs | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/compiler/passes/src/type_checking/check_statements.rs b/compiler/passes/src/type_checking/check_statements.rs index 436de46bfe..40aa65e79d 100644 --- a/compiler/passes/src/type_checking/check_statements.rs +++ b/compiler/passes/src/type_checking/check_statements.rs @@ -210,7 +210,7 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { } }; - // Insert the variables in the into the symbol table. + // Insert the variables into the symbol table. match &input.place { Expression::Identifier(identifier) => { insert_variable(identifier.name, input.type_.clone(), identifier.span, declaration) @@ -222,6 +222,13 @@ impl<'a> StatementVisitor<'a> for TypeChecker<'a> { "Type checking guarantees that if the lhs is a tuple, its associated type is also a tuple." ), }; + if tuple_expression.elements.len() != tuple_type.len() { + return self.emit_err(TypeCheckerError::incorrect_num_tuple_elements( + tuple_expression.elements.len(), + tuple_type.len(), + input.span(), + )); + } tuple_expression.elements.iter().zip_eq(tuple_type.0.iter()).for_each(|(expression, type_)| { let identifier = match expression { Expression::Identifier(identifier) => identifier, diff --git a/errors/src/errors/type_checker/type_checker_error.rs b/errors/src/errors/type_checker/type_checker_error.rs index 98ccf0b186..bebdbe4c86 100644 --- a/errors/src/errors/type_checker/type_checker_error.rs +++ b/errors/src/errors/type_checker/type_checker_error.rs @@ -671,4 +671,11 @@ create_messages!( help: None, } + @formatted + incorrect_num_tuple_elements { + args: (identifiers: impl Display, types: impl Display), + msg: format!("Tuple length mismatch:`length {identifiers}` tuple of identifiers declared, but length `{types}` tuple of types given`"), + help: None, + } + ); From f6ea2b35fe4a0d0769388496ccff8602c622af5d Mon Sep 17 00:00:00 2001 From: evan-schott <53463459+evan-schott@users.noreply.github.com> Date: Mon, 25 Sep 2023 16:42:16 -0700 Subject: [PATCH 2/2] create test --- .../definition/define_multiple_variables_fail.out | 5 +++++ .../definition/define_multiple_variables_fail.leo | 13 +++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 tests/expectations/compiler/definition/define_multiple_variables_fail.out create mode 100644 tests/tests/compiler/definition/define_multiple_variables_fail.leo diff --git a/tests/expectations/compiler/definition/define_multiple_variables_fail.out b/tests/expectations/compiler/definition/define_multiple_variables_fail.out new file mode 100644 index 0000000000..601e964e3c --- /dev/null +++ b/tests/expectations/compiler/definition/define_multiple_variables_fail.out @@ -0,0 +1,5 @@ +--- +namespace: Compile +expectation: Fail +outputs: + - "Error [ETYC0372082]: Tuple length mismatch:`length 3` tuple of identifiers declared, but length `2` tuple of types given`\n --> compiler-test:5:9\n |\n 5 | let (a,b,c): (u8,u8) = (2u8,3u8);\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nError [ETYC0372082]: Tuple length mismatch:`length 2` tuple of identifiers declared, but length `3` tuple of types given`\n --> compiler-test:6:9\n |\n 6 | let (d,e): (u8,u8,u8) = (1u8,2u8,3u8);\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" diff --git a/tests/tests/compiler/definition/define_multiple_variables_fail.leo b/tests/tests/compiler/definition/define_multiple_variables_fail.leo new file mode 100644 index 0000000000..adcc7b4880 --- /dev/null +++ b/tests/tests/compiler/definition/define_multiple_variables_fail.leo @@ -0,0 +1,13 @@ +/* +namespace: Compile +expectation: Fail +*/ + +program test.aleo { + function main(y: bool) -> bool { + let (a,b,c): (u8,u8) = (2u8,3u8); + let (d,e): (u8,u8,u8) = (1u8,2u8,3u8); + return y; + } +} +