From 1c3362d1913a9428636ec72af4572a412bbf6fb4 Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Fri, 23 Jul 2021 17:12:30 -0700 Subject: [PATCH] ternary different types errors out --- asg/src/error/mod.rs | 6 +++++ asg/src/expression/ternary.rs | 26 ++++++++++++++----- tests/compiler/statements/assign_ternary.leo | 9 +++++++ .../statements/assign_ternary.leo.out | 5 ++++ 4 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 tests/compiler/statements/assign_ternary.leo create mode 100644 tests/expectations/compiler/compiler/statements/assign_ternary.leo.out diff --git a/asg/src/error/mod.rs b/asg/src/error/mod.rs index 3205e733ae..f3d9533ffa 100644 --- a/asg/src/error/mod.rs +++ b/asg/src/error/mod.rs @@ -204,6 +204,12 @@ impl AsgConvertError { Self::new_from_span(format!("array index out of bounds: '{}'", index), span) } + pub fn ternary_different_types(left: &str, right: &str, span: &Span) -> Self { + let message = format!("ternary sides had different types: left {}, right {}", left, right); + + Self::new_from_span(message, span) + } + pub fn unknown_array_size(span: &Span) -> Self { Self::new_from_span("array size cannot be inferred, add explicit types".to_string(), span) } diff --git a/asg/src/expression/ternary.rs b/asg/src/expression/ternary.rs index 56564a5a81..ee071f05e8 100644 --- a/asg/src/expression/ternary.rs +++ b/asg/src/expression/ternary.rs @@ -79,6 +79,24 @@ impl<'a> FromAst<'a, leo_ast::TernaryExpression> for TernaryExpression<'a> { value: &leo_ast::TernaryExpression, expected_type: Option>, ) -> Result, AsgConvertError> { + let if_true = Cell::new(<&Expression<'a>>::from_ast( + scope, + &*value.if_true, + expected_type.clone(), + )?); + let left: PartialType = if_true.get().get_type().unwrap().into(); + + let if_false = Cell::new(<&Expression<'a>>::from_ast(scope, &*value.if_false, expected_type)?); + let right = if_false.get().get_type().unwrap().into(); + + if left != right { + return Err(AsgConvertError::ternary_different_types( + &left.to_string(), + &right.to_string(), + &value.span, + )); + } + Ok(TernaryExpression { parent: Cell::new(None), span: Some(value.span.clone()), @@ -87,12 +105,8 @@ impl<'a> FromAst<'a, leo_ast::TernaryExpression> for TernaryExpression<'a> { &*value.condition, Some(Type::Boolean.partial()), )?), - if_true: Cell::new(<&Expression<'a>>::from_ast( - scope, - &*value.if_true, - expected_type.clone(), - )?), - if_false: Cell::new(<&Expression<'a>>::from_ast(scope, &*value.if_false, expected_type)?), + if_true, + if_false, }) } } diff --git a/tests/compiler/statements/assign_ternary.leo b/tests/compiler/statements/assign_ternary.leo new file mode 100644 index 0000000000..ed49709cb4 --- /dev/null +++ b/tests/compiler/statements/assign_ternary.leo @@ -0,0 +1,9 @@ +/* +namespace: Compile +expectation: Fail +input_file: inputs/u32_3.in +*/ + +function main(x: u32) { + let x = true ? x: true; +} \ No newline at end of file diff --git a/tests/expectations/compiler/compiler/statements/assign_ternary.leo.out b/tests/expectations/compiler/compiler/statements/assign_ternary.leo.out new file mode 100644 index 0000000000..6518a49fff --- /dev/null +++ b/tests/expectations/compiler/compiler/statements/assign_ternary.leo.out @@ -0,0 +1,5 @@ +--- +namespace: Compile +expectation: Fail +outputs: + - " --> compiler-test:4:13\n |\n 4 | let x = true ? x: true;\n | ^^^^^^^^^^^^^^\n |\n = ternary sides had different types: left u32, right bool"