diff --git a/asg/src/expression/ternary.rs b/asg/src/expression/ternary.rs index 8f286d4559..4e26d254aa 100644 --- a/asg/src/expression/ternary.rs +++ b/asg/src/expression/ternary.rs @@ -87,7 +87,15 @@ impl<'a> FromAst<'a, leo_ast::TernaryExpression> for TernaryExpression<'a> { )?); 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 if_false = if expected_type.is_none() { + Cell::new(<&Expression<'a>>::from_ast( + scope, + &*value.if_false, + Some(left.clone()), + )?) + } else { + Cell::new(<&Expression<'a>>::from_ast(scope, &*value.if_false, expected_type)?) + }; let right = if_false.get().get_type().unwrap().into(); if left != right { diff --git a/tests/compiler/statements/inputs/ternary_explicit_and_implicit.in b/tests/compiler/statements/inputs/ternary_explicit_and_implicit.in new file mode 100644 index 0000000000..ef82e41af8 --- /dev/null +++ b/tests/compiler/statements/inputs/ternary_explicit_and_implicit.in @@ -0,0 +1,6 @@ +[main] +x: u8 = 3; +y: bool = true; + +[registers] +a: u8 = 0; \ No newline at end of file diff --git a/tests/compiler/statements/ternary_explicit_and_implicit.leo b/tests/compiler/statements/ternary_explicit_and_implicit.leo new file mode 100644 index 0000000000..d092bc2928 --- /dev/null +++ b/tests/compiler/statements/ternary_explicit_and_implicit.leo @@ -0,0 +1,9 @@ +/* +namespace: Compile +expectation: Pass +input_file: inputs/ternary_explicit_and_implicit.in +*/ + +function main(x: u8, y: bool) -> u8 { + return y ? x : 2; +} diff --git a/tests/expectations/compiler/compiler/statements/assign_ternary.leo.out b/tests/expectations/compiler/compiler/statements/assign_ternary.leo.out index 659cb7e8b4..9a641be8e0 100644 --- a/tests/expectations/compiler/compiler/statements/assign_ternary.leo.out +++ b/tests/expectations/compiler/compiler/statements/assign_ternary.leo.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - "Error [EASG0373021]: ternary sides had different types: left u32, right bool\n --> compiler-test:4:13\n |\n 4 | let x = true ? x: true;\n | ^^^^^^^^^^^^^^" + - "Error [EASG0373026]: unexpected type, expected: 'u32', received: 'bool'\n --> compiler-test:4:23\n |\n 4 | let x = true ? x: true;\n | ^^^^" diff --git a/tests/expectations/compiler/compiler/statements/ternary_explicit_and_implicit.leo.out b/tests/expectations/compiler/compiler/statements/ternary_explicit_and_implicit.leo.out new file mode 100644 index 0000000000..2486b2233d --- /dev/null +++ b/tests/expectations/compiler/compiler/statements/ternary_explicit_and_implicit.leo.out @@ -0,0 +1,21 @@ +--- +namespace: Compile +expectation: Pass +outputs: + - circuit: + num_public_variables: 0 + num_private_variables: 25 + num_constraints: 33 + at: 00318b0f80b01b9e2d5a8154a536deb1545f04abd493a84071ef13dce1a9d92d + bt: 799ee4dc07f6320b8c3dcbd5ebdf6de8a4cf5af8b1edc45dbe5621c8a34fa38d + ct: 7b6ec9eacd65cfa47dfe7e73eb7c8fa4be804a9fb878fddf59fe5d06e7c04489 + output: + - input_file: inputs/ternary_explicit_and_implicit.in + output: + registers: + a: + type: u8 + value: "3" + initial_ast: 29b5d6188c9fe56eaee3024a900b92e4bd9445364551b85e924fe9a392bac35d + canonicalized_ast: 29b5d6188c9fe56eaee3024a900b92e4bd9445364551b85e924fe9a392bac35d + type_inferenced_ast: 02edde30e615a1ac3e87818daa6fca7e4d7c0c0c0a1a9d9159a0a4190c11ec8a