Merge pull request #1369 from AleoHQ/fix-array-eq

[Fix] Throw an error when comparing arrays of different lengths
This commit is contained in:
Alessandro Coglio 2021-09-28 09:10:05 -07:00 committed by GitHub
commit 884061ffd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 1 deletions

View File

@ -57,6 +57,11 @@ pub fn evaluate_eq<'a, F: PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
}
(ConstrainedValue::Array(arr_1), ConstrainedValue::Array(arr_2)) => {
let mut current = ConstrainedValue::Boolean(Boolean::constant(true));
if arr_1.len() != arr_2.len() {
return Err(CompilerError::array_sizes_must_match_in_eq(arr_1.len(), arr_2.len(), span).into());
}
for (i, (left, right)) in arr_1.into_iter().zip(arr_2.into_iter()).enumerate() {
let next = evaluate_eq(&mut cs.ns(|| format!("array[{}]", i)), left, right, span)?;

View File

@ -163,7 +163,7 @@ create_errors!(
help: None,
}
/// For when the operation has no implmentation on the type of variable received.
/// For when the operation has no implementation on the type of variable received.
@formatted
incompatible_types {
args: (operation: impl Display),
@ -841,4 +841,11 @@ create_errors!(
msg: "len() can only be called on an array value".to_string(),
help: None,
}
@formatted
array_sizes_must_match_in_eq {
args: (lhs: impl Display, rhs: impl Display),
msg: format!("array sizes must match for comparison; left: {}, right: {}", lhs, rhs),
help: None,
}
);

View File

@ -0,0 +1,9 @@
/*
namespace: Compile
expectation: Fail
*/
function main() {
let x: [u8; _] = [1u8,2];
let z: bool = x == [1u8,2,3]; // array size mismatch
}

View File

@ -0,0 +1,5 @@
---
namespace: Compile
expectation: Fail
outputs:
- "Error [ECMP0376093]: array sizes must match for comparison; left: 2, right: 3\n --> compiler-test:5:19\n |\n 5 | let z: bool = x == [1u8,2,3]; // array size mismatch\n | ^^^^^^^^^^^^^^"