Skip to content

Commit

Permalink
Merge #672 #673
Browse files Browse the repository at this point in the history
672: Feature/515 eliminate const vars r=collinc97 a=gluax

Resolves #515. Removes const keyword from statements. Also resolves #514. Still allows const y in function declaration of args.

673: feature/671-primefield-refactor r=collinc97 a=gluax

Resolves #671. Refactor "Field + PrimeField" -> "PrimeField" and all tests still pass.

Co-authored-by: gluaxspeed <jonathan.t.pavlik@gmail.com>
Co-authored-by: gluax <jonathan.t.pavlik@gmail.com>
Co-authored-by: Collin Chin <collin.chin@berkeley.edu>
  • Loading branch information
3 people committed Feb 18, 2021
3 parents 13af616 + 34b8e76 + a7b28fb commit c62af03
Show file tree
Hide file tree
Showing 137 changed files with 309 additions and 414 deletions.
7 changes: 5 additions & 2 deletions asg/src/statement/definition.rs
Expand Up @@ -28,6 +28,7 @@ use crate::{
Type,
Variable,
};
use leo_ast::{AstError, DeprecatedError};

use std::cell::{Cell, RefCell};

Expand Down Expand Up @@ -89,8 +90,10 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
}

for (variable, type_) in statement.variable_names.iter().zip(output_types.into_iter()) {
if statement.declaration_type == leo_ast::Declare::Const && variable.mutable {
return Err(AsgConvertError::illegal_ast_structure("cannot have const mut"));
if statement.declaration_type == leo_ast::Declare::Const {
return Err(AsgConvertError::AstError(AstError::DeprecatedError(
DeprecatedError::const_statement(&statement.span),
)));
}
variables.push(&*scope.alloc_variable(RefCell::new(InnerVariable {
id: uuid::Uuid::new_v4(),
Expand Down
2 changes: 1 addition & 1 deletion asg/tests/fail/array/nested_3x2_value_fail.leo
@@ -1,4 +1,4 @@
// Multidimensional array syntax in leo
function main() {
const a: [u32; (3, 2)] = [[0; 3]; 2]; // initializer (incorrectly reversed ordering)
let a: [u32; (3, 2)] = [[0; 3]; 2]; // initializer (incorrectly reversed ordering)
}
2 changes: 1 addition & 1 deletion asg/tests/fail/array/tuple_3x2_value_fail.leo
@@ -1,4 +1,4 @@
// Multidimensional array syntax in leo
function main() {
const a: [u32; (3, 2)] = [0; (2, 3)]; // initializer (incorrectly reversed ordering)
let a: [u32; (3, 2)] = [0; (2, 3)]; // initializer (incorrectly reversed ordering)
}
2 changes: 1 addition & 1 deletion asg/tests/fail/array/type_nested_value_nested_3x2_fail.leo
@@ -1,3 +1,3 @@
function main() {
const b: [[u8; 2]; 3] = [[0; 3]; 2]; // initializer (incorrectly reversed ordering)
let b: [[u8; 2]; 3] = [[0; 3]; 2]; // initializer (incorrectly reversed ordering)
}
@@ -1,3 +1,3 @@
function main() {
const b: [[[u8; 2]; 3]; 4] = [[[0; 4]; 3]; 2]; // initializer (incorrectly reversed ordering)
let b: [[[u8; 2]; 3]; 4] = [[[0; 4]; 3]; 2]; // initializer (incorrectly reversed ordering)
}
2 changes: 1 addition & 1 deletion asg/tests/fail/array/type_nested_value_tuple_3x2_fail.leo
@@ -1,3 +1,3 @@
function main() {
const b: [[u8; 2]; 3] = [0; (2, 3)]; // initializer (incorrectly reversed ordering)
let b: [[u8; 2]; 3] = [0; (2, 3)]; // initializer (incorrectly reversed ordering)
}
@@ -1,3 +1,3 @@
function main() {
const b: [[[u8; 2]; 3]; 4] = [0; (2, 3, 4)]; // initializer (incorrectly reversed ordering)
let b: [[[u8; 2]; 3]; 4] = [0; (2, 3, 4)]; // initializer (incorrectly reversed ordering)
}
2 changes: 1 addition & 1 deletion asg/tests/fail/array/type_tuple_value_nested_3x2_fail.leo
@@ -1,3 +1,3 @@
function main() {
const b: [u8; (2, 3)] = [[0; 2]; 3]; // initializer (incorrectly reversed ordering)
let b: [u8; (2, 3)] = [[0; 2]; 3]; // initializer (incorrectly reversed ordering)
}
@@ -1,7 +1,7 @@
function main() {
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
let a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline

const b: [u8; (2, 3)] = [[0; 3]; 2]; // initializer
let b: [u8; (2, 3)] = [[0; 3]; 2]; // initializer

console.assert(a == b);
}
@@ -1,3 +1,3 @@
function main() {
const b: [u8; (4, 3, 2)] = [[[0; 4]; 3]; 2]; // initializer (incorrectly reversed ordering)
let b: [u8; (4, 3, 2)] = [[[0; 4]; 3]; 2]; // initializer (incorrectly reversed ordering)
}
2 changes: 1 addition & 1 deletion asg/tests/fail/array/type_tuple_value_tuple_3x2_fail.leo
@@ -1,3 +1,3 @@
function main() {
const b: [u8; (2, 3)] = [0; (3, 2)]; // initializer (incorrectly reversed ordering)
let b: [u8; (2, 3)] = [0; (3, 2)]; // initializer (incorrectly reversed ordering)
}
4 changes: 2 additions & 2 deletions asg/tests/fail/array/type_tuple_value_tuple_3x2_swap_fail.leo
@@ -1,7 +1,7 @@
function main() {
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
let a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline

const b: [u8; (2, 3)] = [0; (2, 3)]; // initializer
let b: [u8; (2, 3)] = [0; (2, 3)]; // initializer

console.assert(a == b);
}
2 changes: 1 addition & 1 deletion asg/tests/fail/array/type_tuple_value_tuple_4x3x2_fail.leo
@@ -1,3 +1,3 @@
function main() {
const b: [u8; (4, 3, 2)] = [0; (2, 3, 4)]; // initializer (incorrectly reversed order)
let b: [u8; (4, 3, 2)] = [0; (2, 3, 4)]; // initializer (incorrectly reversed order)
}
2 changes: 1 addition & 1 deletion asg/tests/fail/function/scope_fail.leo
Expand Up @@ -3,6 +3,6 @@ function foo() -> field {
}

function main() {
const myGlobal = 42field;
let myGlobal = 42field;
let err = foo();
}
4 changes: 2 additions & 2 deletions asg/tests/fail/mutability/const.leo
@@ -1,5 +1,5 @@
// Constant variables are immutable by default.
// Let variables are immutable by default.
function main() {
const a = 1u32;
let a = 1u32;
a = 0;
}
4 changes: 0 additions & 4 deletions asg/tests/fail/mutability/const_mut.leo

This file was deleted.

6 changes: 0 additions & 6 deletions asg/tests/fail/mutability/mod.rs
Expand Up @@ -30,12 +30,6 @@ fn test_const_fail() {
load_asg(&new_context(), program_string).err().unwrap();
}

#[test]
fn test_const_mut_fail() {
let program_string = include_str!("const_mut.leo");
load_asg(&new_context(), program_string).err().unwrap();
}

#[test]
fn test_array() {
let program_string = include_str!("array.leo");
Expand Down
3 changes: 3 additions & 0 deletions asg/tests/fail/statements/const_declaration_fail.leo
@@ -0,0 +1,3 @@
function main() {
const x = 1u32;
}
6 changes: 6 additions & 0 deletions asg/tests/fail/statements/mod.rs
Expand Up @@ -23,3 +23,9 @@ fn test_num_returns_fail() {
let program_string = include_str!("num_returns_fail.leo");
load_asg(&new_context(), program_string).err().unwrap();
}

#[test]
fn test_const_declaration_fail() {
let program_string = include_str!("const_declaration_fail.leo");
load_asg(&new_context(), program_string).err().unwrap();
}
4 changes: 2 additions & 2 deletions asg/tests/pass/array/multi_initializer.leo
@@ -1,7 +1,7 @@
function main() {
const a: [u8; (2, 2, 2)] = [1u8; (2, 2, 2)];
let a: [u8; (2, 2, 2)] = [1u8; (2, 2, 2)];

const b: [u8; (2, 2, 2)] = [[[1u8; 2]; 2]; 2];
let b: [u8; (2, 2, 2)] = [[[1u8; 2]; 2]; 2];

console.assert(a == b);
}
4 changes: 2 additions & 2 deletions asg/tests/pass/array/nested_3x2_value.leo
@@ -1,8 +1,8 @@
// Multidimensional array syntax in leo
function main() {
const a = [[0u32, 0u32], [0u32, 0u32], [0u32, 0u32]]; // inline
let a = [[0u32, 0u32], [0u32, 0u32], [0u32, 0u32]]; // inline

const b: [u32; (3, 2)] = [[0; 2]; 3]; // initializer
let b: [u32; (3, 2)] = [[0; 2]; 3]; // initializer

console.assert(a == b);
}
4 changes: 2 additions & 2 deletions asg/tests/pass/array/tuple_3x2_value.leo
@@ -1,8 +1,8 @@
// Multidimensional array syntax in leo
function main() {
const a = [[0u32, 0u32], [0u32, 0u32], [0u32, 0u32]]; // inline
let a = [[0u32, 0u32], [0u32, 0u32], [0u32, 0u32]]; // inline

const b: [u32; (3, 2)] = [0; (3, 2)]; // initializer
let b: [u32; (3, 2)] = [0; (3, 2)]; // initializer

console.assert(a == b);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/array/type_input_3x2.leo
@@ -1,5 +1,5 @@
function main(a: [[u8; 2]; 3]) {
const b = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
let b = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline

console.assert(a == b);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/array/type_input_4x3x2.leo
@@ -1,5 +1,5 @@
function main(a: [[[u8; 2]; 3]; 4]) {
const b = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
let b = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]]; // inline
Expand Down
4 changes: 2 additions & 2 deletions asg/tests/pass/array/type_nested_value_nested_3x2.leo
@@ -1,7 +1,7 @@
function main() {
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
let a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline

const b: [[u8; 2]; 3] = [[0; 2]; 3]; // initializer
let b: [[u8; 2]; 3] = [[0; 2]; 3]; // initializer

console.assert(a == b);
}
4 changes: 2 additions & 2 deletions asg/tests/pass/array/type_nested_value_nested_4x3x2.leo
@@ -1,10 +1,10 @@
function main() {
const a = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
let a = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]]; // inline

const b: [[[u8; 2]; 3]; 4] = [[[0; 2]; 3]; 4]; // initializer
let b: [[[u8; 2]; 3]; 4] = [[[0; 2]; 3]; 4]; // initializer

console.assert(a == b);
}
4 changes: 2 additions & 2 deletions asg/tests/pass/array/type_nested_value_tuple_3x2.leo
@@ -1,7 +1,7 @@
function main() {
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
let a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline

const b: [[u8; 2]; 3] = [0; (3, 2)]; // initializer
let b: [[u8; 2]; 3] = [0; (3, 2)]; // initializer

console.assert(a == b);
}
4 changes: 2 additions & 2 deletions asg/tests/pass/array/type_nested_value_tuple_4x3x2.leo
@@ -1,10 +1,10 @@
function main() {
const a = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
let a = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]]; // inline

const b: [[[u8; 2]; 3]; 4] = [0; (4, 3, 2)]; // initializer
let b: [[[u8; 2]; 3]; 4] = [0; (4, 3, 2)]; // initializer

console.assert(a == b);
}
4 changes: 2 additions & 2 deletions asg/tests/pass/array/type_tuple_value_nested_3x2.leo
@@ -1,7 +1,7 @@
function main() {
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
let a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline

const b: [u8; (3, 2)] = [[0; 2]; 3]; // initializer
let b: [u8; (3, 2)] = [[0; 2]; 3]; // initializer

console.assert(a == b);
}
4 changes: 2 additions & 2 deletions asg/tests/pass/array/type_tuple_value_nested_4x3x2.leo
@@ -1,10 +1,10 @@
function main() {
const a = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
let a = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]]; // inline

const b: [u8; (4, 3, 2)] = [[[0; 2]; 3]; 4]; // initializer
let b: [u8; (4, 3, 2)] = [[[0; 2]; 3]; 4]; // initializer

console.assert(a == b);
}
4 changes: 2 additions & 2 deletions asg/tests/pass/array/type_tuple_value_tuple_3x2.leo
@@ -1,7 +1,7 @@
function main() {
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
let a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline

const b: [u8; (3, 2)] = [0; (3, 2)]; // initializer
let b: [u8; (3, 2)] = [0; (3, 2)]; // initializer

console.assert(a == b);
}
4 changes: 2 additions & 2 deletions asg/tests/pass/array/type_tuple_value_tuple_4x3x2.leo
@@ -1,10 +1,10 @@
function main() {
const a = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
let a = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]]; // inline

const b: [u8; (4, 3, 2)] = [0; (4, 3, 2)]; // initializer
let b: [u8; (4, 3, 2)] = [0; (4, 3, 2)]; // initializer

console.assert(a == b);
}
6 changes: 3 additions & 3 deletions asg/tests/pass/boolean/all.leo
@@ -1,8 +1,8 @@
// !(true && (false || true))
function main() {
const a = true;
const b = false || a;
const c = !(true && b);
let a = true;
let b = false || a;
let c = !(true && b);

console.assert(c == false);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/group/point.leo
@@ -1,3 +1,3 @@
function main() {
const point = (7374112779530666882856915975292384652154477718021969292781165691637980424078, 3435195339177955418892975564890903138308061187980579490487898366607011481796)group;
let point = (7374112779530666882856915975292384652154477718021969292781165691637980424078, 3435195339177955418892975564890903138308061187980579490487898366607011481796)group;
}
14 changes: 7 additions & 7 deletions asg/tests/pass/import/many_import.leo
Expand Up @@ -12,15 +12,15 @@ import bar.( // imports directory import
import car.Car; // imports directory import

function main() {
const point = Point { x: 1u32, y: 1u32 };
const foo = foo();
let point = Point { x: 1u32, y: 1u32 };
let foo = foo();

const bar = Bar { r: 1u32 };
const baz = Baz { z: 1u32 };
const bazzar = Bazzar { a: 1u32 };
const bat = Bat { t: 1u32 };
let bar = Bar { r: 1u32 };
let baz = Baz { z: 1u32 };
let bazzar = Bazzar { a: 1u32 };
let bat = Bat { t: 1u32 };

const car = Car { c: 1u32 };
let car = Car { c: 1u32 };

console.assert(car.c == 1u32);
}
12 changes: 6 additions & 6 deletions asg/tests/pass/import/many_import_star.leo
Expand Up @@ -6,14 +6,14 @@ import bar.bat.bat.*; // imports directory import
import car.*; // imports directory import

function main() {
const point = Point { x: 1u32, y: 1u32 };
const foo = foo();
let point = Point { x: 1u32, y: 1u32 };
let foo = foo();

const bar = Bar { r: 1u32 };
const bat = Bat { t: 1u32 };
const baz = Baz { z: 1u32 };
let bar = Bar { r: 1u32 };
let bat = Bat { t: 1u32 };
let baz = Baz { z: 1u32 };

const car = Car { c: 1u32 };
let car = Car { c: 1u32 };

console.assert(car.c == 1u32);
}
2 changes: 1 addition & 1 deletion asg/tests/pass/statements/ternary_basic.leo
@@ -1,5 +1,5 @@
function main(a: bool, b: bool) {
let c = if a ? true : false;

const d = c == b;
let d = c == b;
}
7 changes: 7 additions & 0 deletions ast/src/errors/deprecated.rs
Expand Up @@ -61,3 +61,10 @@ impl<'ast> TryFrom<AnnotationName<'ast>> for DeprecatedError {
}
}
}

impl DeprecatedError {
pub fn const_statement(span: &Span) -> Self {
let message = "const _ = ... is deprecated. Did you mean let?".to_string();
Self::new_from_span(message, span.clone())
}
}

0 comments on commit c62af03

Please sign in to comment.