mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-02 12:22:08 +03:00
commit
6b303551de
@ -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
|
||||
|
@ -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)
|
||||
|
5
compiler/tests/mutability/const.leo
Normal file
5
compiler/tests/mutability/const.leo
Normal file
@ -0,0 +1,5 @@
|
||||
// Constant variables are immutable by default.
|
||||
function main() {
|
||||
const a = 1u32;
|
||||
a = 0;
|
||||
}
|
4
compiler/tests/mutability/const_mut.leo
Normal file
4
compiler/tests/mutability/const_mut.leo
Normal file
@ -0,0 +1,4 @@
|
||||
// Adding the `mut` keyword to a constant variable is illegal
|
||||
function main() {
|
||||
const mut a = 1u32;
|
||||
}
|
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user