fix ternary bug

This commit is contained in:
gluaxspeed 2021-08-18 01:09:45 -07:00
parent 8ac669f71d
commit 56996f989e
14 changed files with 71 additions and 27 deletions

View File

@ -86,7 +86,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

@ -68,7 +68,7 @@ pub(crate) fn parse_program(
theorem_options: Option<AstSnapshotOptions>,
cwd: Option<PathBuf>,
) -> Result<EdwardsTestCompiler, CompilerError> {
let mut compiler = new_compiler(cwd.unwrap_or("compiler-test".into()), theorem_options);
let mut compiler = new_compiler(cwd.unwrap_or_else(|| "compiler-test".into()), theorem_options);
compiler.parse_program_from_string(program_string)?;

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

@ -16,6 +16,6 @@ outputs:
r0:
type: bool
value: "true"
initial_ast: d6273a716c2546b210eeefb640b5c34830fb2b2a4195ae0b636843e855fcbc1e
canonicalized_ast: 41a3c4ffe7c243ffc76bc7d3c147f77f71119d64e632581a20eb1cd576752ac3
type_inferenced_ast: 196e5191c3d8c8cc552b419422b3e1411fa576336f63ae9d5c340bf2a7d62942
initial_ast: 8c00ae2b8a4f80635ec9277599592828009931229216fb74e49423175a46ebd4
canonicalized_ast: 99805c27122348834d2a550baccc61771a2234542e4fdd861fc1bf94403e5bc5
type_inferenced_ast: e5680b8864a5c8edb6c0ea314ca8cfe2e56eaaabfee94fb09702e3b4f538bc24

View File

@ -16,6 +16,6 @@ outputs:
r0:
type: bool
value: "true"
initial_ast: 1a78fdc95fad861e53d1b0463228701df726fa0bf5092bd94d1d35f57c8c2a94
canonicalized_ast: 1a78fdc95fad861e53d1b0463228701df726fa0bf5092bd94d1d35f57c8c2a94
type_inferenced_ast: be1c8166ce3ae7f805d8600441996328ce8f45ada3c16c4284f07d07af52ef75
initial_ast: f1a1c5fb3d3a5d23c30b3eb7f47aeb1211dbcf6f77a3b1ca262b7c231f4488ef
canonicalized_ast: f1a1c5fb3d3a5d23c30b3eb7f47aeb1211dbcf6f77a3b1ca262b7c231f4488ef
type_inferenced_ast: f5189d9b934533ed5225063ffe1adb59508c53e3951a74c03d6c27d41fc7621a

View File

@ -16,6 +16,6 @@ outputs:
r0:
type: bool
value: "true"
initial_ast: 47ef8cd0b57a42612bc9138c6da6e05fbca27c47464acf5af569c2fe0c91dd31
canonicalized_ast: 39c0b27ba63cc34eed735900912e154031ca1f291aade9d9964ce49e77eeb19e
type_inferenced_ast: 19bfc761f899b48a3e0c4142bac6cbdaceac8230e92422cf9f6a99d9f7eddc6f
initial_ast: 9b093fc03bf985f26e5e57c0b6508bb515535c79a951c9838a5320ba4a702ed8
canonicalized_ast: d34e1c3c2bbae1f2103e1b5d4058c607dc78af8f62a254947abb991ec6254eeb
type_inferenced_ast: a76a3eef4ef99bcb1ba050e9d3083106082608e838ea94e0ddbb943583c81483

View File

@ -16,6 +16,6 @@ outputs:
r0:
type: bool
value: "true"
initial_ast: 7e0ba7b09b3840a5d4fc9f0d413163ba2de8f207c3818f604f9a71297aac30f3
canonicalized_ast: 3f7efb61847fd75fed58b70526da8ceffb573a0806521fb30443f341c3d14a45
type_inferenced_ast: a1baf614c8ab13c1ff978faf8022f90eef4c482261928cb3256ab4d63e20096c
initial_ast: 2b6234a13e1b49f7ac587ea697e113b600bdf8e05437ce0352264234688b00ee
canonicalized_ast: ed79e2c766f81e1d60b6a9a25b59313d8dd3e93cf9dbd7b9cf2ffc7336ecb3ac
type_inferenced_ast: daf4bdd6a544c2f3da08cd7ab67c40688cb52f0eec2c13fa70fa827f42722674

View File

@ -16,6 +16,6 @@ outputs:
r0:
type: bool
value: "true"
initial_ast: 042e056c86e545ce256459a0de20d44c0bae24ff406becba4330e3208c48e180
canonicalized_ast: 6cab1ab863f40c96d5d6921e2ced0dd00ebe5e9e334d84508af708be19ae8f59
type_inferenced_ast: 17ff3e62f1c48edfbfb1c35ed4ac542260b59153d85afeca2e707dde217dc675
initial_ast: 17d120d595b3bd1276d4566a0bef48a90e0d545556d54b83da89d88b17342a1d
canonicalized_ast: 0f9e739ef57e1d870b2c5410f821d14194dd882e315720f207f83933f5c85fda
type_inferenced_ast: 1ecf52246630058f5b4294b015b787ca175c3189660c5f3135d6456fc011a2b5

View File

@ -16,6 +16,6 @@ outputs:
r0:
type: bool
value: "true"
initial_ast: ad1dd786c43b4d3de69b6e162bed3f01a0a53f0643e6fd2d95f12c98dfa16855
canonicalized_ast: ad1dd786c43b4d3de69b6e162bed3f01a0a53f0643e6fd2d95f12c98dfa16855
type_inferenced_ast: 1c983cf518cfafbe158ee3e42aed5cb190863b09aedfc4dd34e0268160ee79e2
initial_ast: b8decf097c42e00f24432d5d72b4c4c53f404089b913330e972f81494b4c4bfb
canonicalized_ast: b8decf097c42e00f24432d5d72b4c4c53f404089b913330e972f81494b4c4bfb
type_inferenced_ast: ebba4df8a81de92d3bf06d07ab611429653b570e1620405d246f3de5921055e0

View File

@ -16,6 +16,6 @@ outputs:
r0:
type: bool
value: "true"
initial_ast: 6a099d784f82bb8065c8efdd2b4018089d968a7f57cbab3406df3ec5d0612410
canonicalized_ast: 6a099d784f82bb8065c8efdd2b4018089d968a7f57cbab3406df3ec5d0612410
type_inferenced_ast: 58541200a815edfee41333b9b2b048add269d0924ca17457ecf9fbcbb5032ccf
initial_ast: 2e52456f9df93f41894195cca7658fc90b2fd91b08b356105a96394ca586c06c
canonicalized_ast: 2e52456f9df93f41894195cca7658fc90b2fd91b08b356105a96394ca586c06c
type_inferenced_ast: 86ef8ab226a58f140de1fcdadc31eb86e4a1930cabf36a8d9a4607233ea6ab80

View File

@ -16,6 +16,6 @@ outputs:
r0:
type: bool
value: "true"
initial_ast: 98debe360fb6fbede5a9db625cbc2e4e4dfddcb23f40e3914e909157334e1d70
canonicalized_ast: 98debe360fb6fbede5a9db625cbc2e4e4dfddcb23f40e3914e909157334e1d70
type_inferenced_ast: 50c06c3666a830c7f1fd533c412a4e8054fdb494845f4f5d85414b08f1c1a8dd
initial_ast: 9cd9cca5cd79f5db65ebcc4d90cd9c4aedf2310972eb3146b0057352f1164e48
canonicalized_ast: 9cd9cca5cd79f5db65ebcc4d90cd9c4aedf2310972eb3146b0057352f1164e48
type_inferenced_ast: dd2d745566c22b0a07d4340563cccf84492707955ac04f910c3a4edfedcbfcef

View File

@ -2,4 +2,4 @@
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"
- " --> compiler-test:4:23\n |\n 4 | let x = true ? x: true;\n | ^^^^\n |\n = unexpected type, expected: 'u32', received: 'bool'"

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: 720f64a9f5c18e2522d71e844010cc5d6d5dc390bd3fced327e792bf15488a3d
canonicalized_ast: 720f64a9f5c18e2522d71e844010cc5d6d5dc390bd3fced327e792bf15488a3d
type_inferenced_ast: 8347d144d24c35538a83ca0442204b08a35b2eee2dcb3fe92915421515ef60d1