add const mut check

This commit is contained in:
collin 2020-06-29 20:29:53 -07:00
parent 45a2664fd9
commit 6fbb848015
4 changed files with 32 additions and 2 deletions

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");