Merge pull request #1267 from AleoHQ/bug/implicit-ternary-type-inference

[Bugfix] Ternary Implicit and Explicit Unkown Type
This commit is contained in:
Alessandro Coglio 2021-08-21 18:44:39 -07:00 committed by GitHub
commit 84e972c9ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 2 deletions

View File

@ -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 {

View File

@ -0,0 +1,6 @@
[main]
x: u8 = 3;
y: bool = true;
[registers]
a: u8 = 0;

View File

@ -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;
}

View File

@ -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 | ^^^^"

View File

@ -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