Merge pull request #92 from AleoHQ/refactor/const

Refactor/const
This commit is contained in:
Collin Chin 2020-07-02 22:16:15 -07:00 committed by GitHub
commit 6b303551de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 3 deletions

View File

@ -42,7 +42,7 @@ Leo supports `let` and `const` keywords for variable definition.
**Allocated** variables define private variables in the constraint system. Their value is constrained in the circuit on initialization.
**Constant** variables do not define a variable in the constraint system. Their value is constrained in the circuit on computation with an **allocated** variable.
**Constant** variables can be mutable. They do not have the same functionality as `const` variables in other languages.
**Constant** variables cannot be mutable. They have the same functionality as `const` variables in other languages.
```rust
function addOne() -> {
let a = 0u8; // allocated, value enforced on this line

View File

@ -276,8 +276,13 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
expression,
)?;
if let Declare::Let = declare {
value.allocate_value(cs, span)?;
match declare {
Declare::Let => value.allocate_value(cs, span)?,
Declare::Const => {
if variable.mutable {
return Err(StatementError::immutable_assign(variable.to_string(), span));
}
}
}
self.store_definition(function_scope, variable, value)

View File

@ -0,0 +1,5 @@
// Constant variables are immutable by default.
function main() {
const a = 1u32;
a = 0;
}

View File

@ -0,0 +1,4 @@
// Adding the `mut` keyword to a constant variable is illegal
function main() {
const mut a = 1u32;
}

View File

@ -47,6 +47,22 @@ fn test_let_mut() {
mut_success(program);
}
#[test]
fn test_const_fail() {
let bytes = include_bytes!("const.leo");
let program = parse_program(bytes).unwrap();
mut_fail(program);
}
#[test]
fn test_const_mut_fail() {
let bytes = include_bytes!("const_mut.leo");
let program = parse_program(bytes).unwrap();
mut_fail(program);
}
#[test]
fn test_array() {
let bytes = include_bytes!("array.leo");