From 317fc08fc572268d9b6c98074633b72125247977 Mon Sep 17 00:00:00 2001 From: gluax Date: Wed, 17 Feb 2021 11:27:33 -0500 Subject: [PATCH] implicit value at start of array failing fix --- asg/src/expression/array_inline.rs | 22 ++++++++++++++++++++++ asg/src/expression/constant.rs | 4 +++- asg/tests/pass/array/implicit.leo | 6 ++++++ asg/tests/pass/array/mod.rs | 6 ++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 asg/tests/pass/array/implicit.leo diff --git a/asg/src/expression/array_inline.rs b/asg/src/expression/array_inline.rs index ad4de160fd..e716d6208a 100644 --- a/asg/src/expression/array_inline.rs +++ b/asg/src/expression/array_inline.rs @@ -105,6 +105,7 @@ impl FromAst for ArrayInlineExpression { value: &leo_ast::ArrayInlineExpression, expected_type: Option, ) -> Result { + // println!("v: {:?}", value); let (mut expected_item, expected_len) = match expected_type { Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims), None => (None, None), @@ -118,6 +119,27 @@ impl FromAst for ArrayInlineExpression { }; let mut len = 0; + + if len == 0 && expected_item.is_none() { + for expr in value.elements.iter() { + expected_item = match expr { + SpreadOrExpression::Expression(e) => { + match Arc::::from_ast(scope, e, expected_item.clone()) { + Ok(expr) => { + expr.get_type().map(Type::partial) + }, + Err(_) => continue, + } + }, + _ => None + }; + + if expected_item.is_some() { + break; + } + } + } + let output = ArrayInlineExpression { parent: RefCell::new(None), span: Some(value.span.clone()), diff --git a/asg/src/expression/constant.rs b/asg/src/expression/constant.rs index e2cfe82512..a2e5349140 100644 --- a/asg/src/expression/constant.rs +++ b/asg/src/expression/constant.rs @@ -161,7 +161,9 @@ impl FromAst for Constant { } } Implicit(value, span) => match expected_type { - None => return Err(AsgConvertError::unresolved_type("unknown", span)), + None => { + return Err(AsgConvertError::unresolved_type("unknown", span)) + }, Some(PartialType::Integer(Some(sub_type), _)) | Some(PartialType::Integer(None, Some(sub_type))) => { Constant { parent: RefCell::new(None), diff --git a/asg/tests/pass/array/implicit.leo b/asg/tests/pass/array/implicit.leo new file mode 100644 index 0000000000..a7077c8389 --- /dev/null +++ b/asg/tests/pass/array/implicit.leo @@ -0,0 +1,6 @@ +function main(){ + let a = [1u8, 2u8, 3u8, 4]; + let b = [1u8, 2u8, 3, 4u8]; + let c = [1u8, 2, 3u8, 4u8]; + let d = [1, 2u8, 3u8, 4u8]; +} \ No newline at end of file diff --git a/asg/tests/pass/array/mod.rs b/asg/tests/pass/array/mod.rs index bca1da1a97..60caab461d 100644 --- a/asg/tests/pass/array/mod.rs +++ b/asg/tests/pass/array/mod.rs @@ -104,6 +104,12 @@ fn test_slice_lower() { load_asg(program_string).unwrap(); } +#[test] +fn test_implicit() { + let program_string = include_str!("implicit.leo"); + load_asg(program_string).unwrap(); +} + #[test] fn test_type_nested_value_nested_3x2() { let program_string = include_str!("type_nested_value_nested_3x2.leo");