mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-28 20:54:16 +03:00
fix nested mutable assignee bug, add swap test
This commit is contained in:
parent
5222fcfe93
commit
8a44eff4a5
@ -34,7 +34,13 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
// Check that assignee exists and is mutable
|
||||
Ok(match self.get_mut(name) {
|
||||
Some(value) => match value {
|
||||
ConstrainedValue::Mutable(mutable_value) => mutable_value,
|
||||
ConstrainedValue::Mutable(mutable_value) => {
|
||||
// Get the mutable value.
|
||||
mutable_value.get_inner_mut();
|
||||
|
||||
// Return the mutable value.
|
||||
mutable_value
|
||||
}
|
||||
_ => return Err(StatementError::immutable_assign(name.to_owned(), span.to_owned())),
|
||||
},
|
||||
None => return Err(StatementError::undefined_variable(name.to_owned(), span.to_owned())),
|
||||
|
@ -136,3 +136,11 @@ fn test_function_input_mut() {
|
||||
|
||||
assert_satisfied(program);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_swap() {
|
||||
let bytes = include_bytes!("swap.leo");
|
||||
let program = parse_program(bytes).unwrap();
|
||||
|
||||
assert_satisfied(program);
|
||||
}
|
||||
|
20
compiler/tests/mutability/swap.leo
Normal file
20
compiler/tests/mutability/swap.leo
Normal file
@ -0,0 +1,20 @@
|
||||
// Swap two elements of an array.
|
||||
function swap(mut a: [u32; 2], i: u32, j: u32) -> [u32; 2] {
|
||||
let t = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = t;
|
||||
return a
|
||||
}
|
||||
|
||||
function main() {
|
||||
let mut arr: [u32; 2] = [0, 1];
|
||||
let expected: [u32; 2] = [1, 0];
|
||||
|
||||
// Do swap.
|
||||
let actual = swap(arr, 0, 1);
|
||||
|
||||
// Check result.
|
||||
for i in 0..2 {
|
||||
console.assert(expected[i] == actual[i]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user