diff --git a/ast/src/global_consts/global_consts.rs b/ast/src/global_consts/global_consts.rs index 6c31f8afa9..b5d23b70c2 100644 --- a/ast/src/global_consts/global_consts.rs +++ b/ast/src/global_consts/global_consts.rs @@ -52,12 +52,17 @@ impl GlobalConsts { } } + let values = match self.value.clone() { + Expression::TupleInit(expr) => expr.elements, + _ => vec![self.value.clone()], + }; + for (i, variable_name) in self.variable_names.iter().enumerate() { global_consts.push(GlobalConst { declaration_type: self.declaration_type.clone(), variable_name: variable_name.clone(), type_: types.get(i).unwrap_or(&None).clone(), - value: self.value.clone(), + value: values.get(i).unwrap_or(&self.value.clone()).clone(), span: self.span.clone(), }); } diff --git a/compiler/tests/global_consts/global_const_types.leo b/compiler/tests/global_consts/global_const_types.leo new file mode 100644 index 0000000000..8819e55662 --- /dev/null +++ b/compiler/tests/global_consts/global_const_types.leo @@ -0,0 +1,46 @@ +const basic: u32 = 8; +const array: [u8; (3, 2)] = [[0u8; 2]; 3]; +const tuple = (1u32, 2u32); +const (a, b) = (1u32, 2u32); +const simple_group: group = 1group; +const complex_group = (_, 1)group; +const field_test: field = 2; +const use_another_const = basic + 1; +const foo = Foo { width: 10, height: 20 }; +const uno = uno(); + +circuit Foo { + width: u32, + height: u32, +} + +function uno() -> u32 { + return 1u32 +} + +function main() { + // basic test + console.assert(basic == 8u32); + // array test + console.assert(array[0][0] == 0); + console.assert(array[2][1] == 0); + // tuple test + let (x, y) = (1u32, 2u32); + console.assert(x == 1u32); + console.assert(y == 2u32); + // tuple extraction test + console.assert(a == 1u32); + console.assert(b == 2u32); + // group test + console.assert(simple_group == 1group); + console.assert(complex_group == (_, 1)group); + // field test + console.assert(field_test == 2field); + // use another const test + console.assert(use_another_const == 9u32); + // circuit test + console.assert(foo.width == 10u32); + console.assert(foo.height == 20u32); + // function test + console.assert(uno == 1u32); +} \ No newline at end of file diff --git a/compiler/tests/global_consts/mod.rs b/compiler/tests/global_consts/mod.rs new file mode 100644 index 0000000000..dd143bdabe --- /dev/null +++ b/compiler/tests/global_consts/mod.rs @@ -0,0 +1,36 @@ +// Copyright (C) 2019-2021 Aleo Systems Inc. +// This file is part of the Leo library. + +// The Leo library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The Leo library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with the Leo library. If not, see . + +use crate::{assert_satisfied, expect_compiler_error, parse_program}; + + +#[test] +fn test_global_consts() { + let program_string = include_str!("global_consts.leo"); + + let program = parse_program(program_string).unwrap(); + + assert_satisfied(program); +} + +#[test] +fn test_modify_global_const() { + let program_string = include_str!("modify_global_const.leo"); + + let program = parse_program(program_string).unwrap(); + + assert!(parse_program(program_string).is_err()); +} \ No newline at end of file diff --git a/compiler/tests/global_consts/modify_global_const.leo b/compiler/tests/global_consts/modify_global_const.leo new file mode 100644 index 0000000000..762baad00a --- /dev/null +++ b/compiler/tests/global_consts/modify_global_const.leo @@ -0,0 +1,6 @@ +const basic: u32 = 8; + +function main() { + // Cannot re-assign! + basic = 2u32; +} \ No newline at end of file