mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-24 07:48:04 +03:00
add const mut check
This commit is contained in:
parent
45a2664fd9
commit
6fbb848015
@ -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