From b9ed20a76daff530bfc84fe2e3bb34a5a51ef61f Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Mon, 19 Jul 2021 11:17:10 -0700 Subject: [PATCH 1/2] use the array range access span for errors if the side is none --- asg/src/expression/array_range_access.rs | 46 +++++++++++++----------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/asg/src/expression/array_range_access.rs b/asg/src/expression/array_range_access.rs index 773e8c66a9..c0810a2586 100644 --- a/asg/src/expression/array_range_access.rs +++ b/asg/src/expression/array_range_access.rs @@ -148,24 +148,28 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx _ => None, }; let const_right = match right.map(|x| x.const_value()) { - Some(Some(ConstValue::Int(value))) => { - let value = value.to_usize(); - if let Some(value) = value { - if value > parent_size { - return Err(AsgConvertError::array_index_out_of_bounds( - value, - &right.unwrap().span().cloned().unwrap_or_default(), - )); + Some(Some(ConstValue::Int(inner_value))) => { + let usize_value = inner_value.to_usize(); + if let Some(inner_value) = usize_value { + if inner_value > parent_size { + let error_span = if let Some(right) = right { + right.span().cloned().unwrap_or_default() + } else { + value.span.clone() + }; + return Err(AsgConvertError::array_index_out_of_bounds(inner_value, &error_span)); } else if let Some(left) = const_left { - if left > value { - return Err(AsgConvertError::array_index_out_of_bounds( - value, - &right.unwrap().span().cloned().unwrap_or_default(), - )); + if left > inner_value { + let error_span = if let Some(right) = right { + right.span().cloned().unwrap_or_default() + } else { + value.span.clone() + }; + return Err(AsgConvertError::array_index_out_of_bounds(inner_value, &error_span)); } } } - value + usize_value } None => Some(parent_size), _ => None, @@ -188,12 +192,14 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx )); } } - if let Some(value) = const_left { - if value + expected_len > parent_size { - return Err(AsgConvertError::array_index_out_of_bounds( - value, - &left.unwrap().span().cloned().unwrap_or_default(), - )); + if let Some(left_value) = const_left { + if left_value + expected_len > parent_size { + let error_span = if let Some(left) = left { + left.span().cloned().unwrap_or_default() + } else { + value.span.clone() + }; + return Err(AsgConvertError::array_index_out_of_bounds(left_value, &error_span)); } } length = Some(expected_len); From a2577231fd2d41a8b8c0e9dce4dc98c8e95a021c Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Mon, 19 Jul 2021 11:33:18 -0700 Subject: [PATCH 2/2] add test --- tests/compiler/array/array_range_access_fail.leo | 12 ++++++++++++ .../compiler/array/input/array_range_access_fail.in | 2 ++ .../compiler/array/array_range_access_fail.leo.out | 5 +++++ 3 files changed, 19 insertions(+) create mode 100644 tests/compiler/array/array_range_access_fail.leo create mode 100644 tests/compiler/array/input/array_range_access_fail.in create mode 100644 tests/expectations/compiler/compiler/array/array_range_access_fail.leo.out diff --git a/tests/compiler/array/array_range_access_fail.leo b/tests/compiler/array/array_range_access_fail.leo new file mode 100644 index 0000000000..a73127b571 --- /dev/null +++ b/tests/compiler/array/array_range_access_fail.leo @@ -0,0 +1,12 @@ +/* +namespace: Compile +expectation: Fail +input_file: input/array_range_access_fail.in +*/ + +function main ( + const x: u32 +) { + const y = [1u8; 3]; + const z: [u8; 2] = y[..1u32][..x]; +} \ No newline at end of file diff --git a/tests/compiler/array/input/array_range_access_fail.in b/tests/compiler/array/input/array_range_access_fail.in new file mode 100644 index 0000000000..5bea7d78d8 --- /dev/null +++ b/tests/compiler/array/input/array_range_access_fail.in @@ -0,0 +1,2 @@ +[constants] +x: u32 = 1u32; \ No newline at end of file diff --git a/tests/expectations/compiler/compiler/array/array_range_access_fail.leo.out b/tests/expectations/compiler/compiler/array/array_range_access_fail.leo.out new file mode 100644 index 0000000000..4fa4f71894 --- /dev/null +++ b/tests/expectations/compiler/compiler/array/array_range_access_fail.leo.out @@ -0,0 +1,5 @@ +--- +namespace: Compile +expectation: Fail +outputs: + - " --> compiler-test:7:24\n |\n 7 | const z: [u8; 2] = y[..1u32][..x];\n | ^^^^^^^^^^^^^^\n |\n = array index out of bounds: '0'"