mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-10-11 21:57:50 +03:00
Merge pull request #1169 from AleoHQ/bug/invalid-slice-length-rust-error
[Bugfix] Fixes Rust Error for Invalid Slice
This commit is contained in:
commit
5e00c7485f
@ -148,24 +148,28 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx
|
|||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
let const_right = match right.map(|x| x.const_value()) {
|
let const_right = match right.map(|x| x.const_value()) {
|
||||||
Some(Some(ConstValue::Int(value))) => {
|
Some(Some(ConstValue::Int(inner_value))) => {
|
||||||
let value = value.to_usize();
|
let usize_value = inner_value.to_usize();
|
||||||
if let Some(value) = value {
|
if let Some(inner_value) = usize_value {
|
||||||
if value > parent_size {
|
if inner_value > parent_size {
|
||||||
return Err(AsgConvertError::array_index_out_of_bounds(
|
let error_span = if let Some(right) = right {
|
||||||
value,
|
right.span().cloned().unwrap_or_default()
|
||||||
&right.unwrap().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 {
|
} else if let Some(left) = const_left {
|
||||||
if left > value {
|
if left > inner_value {
|
||||||
return Err(AsgConvertError::array_index_out_of_bounds(
|
let error_span = if let Some(right) = right {
|
||||||
value,
|
right.span().cloned().unwrap_or_default()
|
||||||
&right.unwrap().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 => Some(parent_size),
|
||||||
_ => None,
|
_ => None,
|
||||||
@ -188,12 +192,14 @@ impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessEx
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(value) = const_left {
|
if let Some(left_value) = const_left {
|
||||||
if value + expected_len > parent_size {
|
if left_value + expected_len > parent_size {
|
||||||
return Err(AsgConvertError::array_index_out_of_bounds(
|
let error_span = if let Some(left) = left {
|
||||||
value,
|
left.span().cloned().unwrap_or_default()
|
||||||
&left.unwrap().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);
|
length = Some(expected_len);
|
||||||
|
12
tests/compiler/array/array_range_access_fail.leo
Normal file
12
tests/compiler/array/array_range_access_fail.leo
Normal file
@ -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];
|
||||||
|
}
|
2
tests/compiler/array/input/array_range_access_fail.in
Normal file
2
tests/compiler/array/input/array_range_access_fail.in
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[constants]
|
||||||
|
x: u32 = 1u32;
|
@ -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'"
|
Loading…
Reference in New Issue
Block a user