diff --git a/compiler/src/constraints/statement.rs b/compiler/src/constraints/statement.rs index be20365b03..17974bfda9 100644 --- a/compiler/src/constraints/statement.rs +++ b/compiler/src/constraints/statement.rs @@ -276,8 +276,13 @@ impl> ConstrainedProgram { 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) diff --git a/compiler/tests/mutability/const.leo b/compiler/tests/mutability/const.leo new file mode 100644 index 0000000000..a167625c02 --- /dev/null +++ b/compiler/tests/mutability/const.leo @@ -0,0 +1,5 @@ +// Constant variables are immutable by default. +function main() { + const a = 1u32; + a = 0; +} \ No newline at end of file diff --git a/compiler/tests/mutability/const_mut.leo b/compiler/tests/mutability/const_mut.leo new file mode 100644 index 0000000000..06dc51c222 --- /dev/null +++ b/compiler/tests/mutability/const_mut.leo @@ -0,0 +1,4 @@ +// Adding the `mut` keyword to a constant variable is illegal +function main() { + const mut a = 1u32; +} \ No newline at end of file diff --git a/compiler/tests/mutability/mod.rs b/compiler/tests/mutability/mod.rs index 41b03949a7..cc8dcdb2dd 100644 --- a/compiler/tests/mutability/mod.rs +++ b/compiler/tests/mutability/mod.rs @@ -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");