From 970f8082909407cc8028b1bb4aa9d25268ac6c47 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Thu, 15 Oct 2020 10:18:31 +0200 Subject: [PATCH 1/2] perf: refactor a lot of the compiler so that Spans and Strings are rarely cloned Signed-off-by: ljedrz --- compiler/src/compiler.rs | 4 +- compiler/src/console/assert.rs | 17 ++- compiler/src/console/console.rs | 6 +- compiler/src/console/format.rs | 13 +- compiler/src/constraints/constraints.rs | 10 +- compiler/src/definition/definition.rs | 4 +- compiler/src/definition/definitions.rs | 6 +- compiler/src/expression/arithmetic/add.rs | 8 +- compiler/src/expression/arithmetic/div.rs | 8 +- compiler/src/expression/arithmetic/mul.rs | 8 +- compiler/src/expression/arithmetic/negate.rs | 7 +- compiler/src/expression/arithmetic/pow.rs | 8 +- compiler/src/expression/arithmetic/sub.rs | 8 +- compiler/src/expression/array/access.rs | 21 +-- compiler/src/expression/array/array.rs | 10 +- compiler/src/expression/array/index.rs | 10 +- compiler/src/expression/binary/binary.rs | 26 +--- compiler/src/expression/binary/operand.rs | 6 +- compiler/src/expression/circuit/access.rs | 20 +-- compiler/src/expression/circuit/circuit.rs | 14 +- .../src/expression/circuit/static_access.rs | 8 +- .../src/expression/conditional/conditional.rs | 34 ++--- compiler/src/expression/expression.rs | 74 +++++------ .../src/expression/function/core_circuit.rs | 9 +- compiler/src/expression/function/function.rs | 22 ++-- .../src/expression/identifier/identifier.rs | 12 +- compiler/src/expression/logical/and.rs | 6 +- compiler/src/expression/logical/or.rs | 6 +- compiler/src/expression/relational/eq.rs | 28 ++-- compiler/src/expression/relational/ge.rs | 10 +- compiler/src/expression/relational/gt.rs | 10 +- compiler/src/expression/relational/le.rs | 10 +- compiler/src/expression/relational/lt.rs | 10 +- compiler/src/expression/tuple/access.rs | 12 +- compiler/src/expression/tuple/tuple.rs | 6 +- compiler/src/function/function.rs | 44 +++---- compiler/src/function/input/array.rs | 27 ++-- compiler/src/function/input/function_input.rs | 6 +- compiler/src/function/input/input_section.rs | 4 +- .../src/function/input/main_function_input.rs | 6 +- compiler/src/function/input/tuple.rs | 25 ++-- compiler/src/function/main_function.rs | 17 +-- compiler/src/function/result/result.rs | 4 +- compiler/src/import/store/core_package.rs | 6 +- compiler/src/import/store/import.rs | 4 +- compiler/src/import/store/symbol.rs | 20 +-- compiler/src/program/program.rs | 3 +- compiler/src/statement/assign/array.rs | 32 ++--- compiler/src/statement/assign/assign.rs | 31 ++--- compiler/src/statement/assign/assignee.rs | 14 +- .../src/statement/assign/circuit_variable.rs | 33 +++-- compiler/src/statement/assign/tuple.rs | 12 +- compiler/src/statement/branch/branch.rs | 10 +- .../src/statement/conditional/conditional.rs | 20 +-- .../src/statement/definition/definition.rs | 66 +++++----- compiler/src/statement/iteration/iteration.rs | 16 +-- compiler/src/statement/return_/return_.rs | 21 +-- compiler/src/statement/statement.rs | 16 +-- compiler/src/value/address/address.rs | 13 +- compiler/src/value/boolean/input.rs | 16 +-- compiler/src/value/field/field_type.rs | 34 ++--- compiler/src/value/field/input.rs | 12 +- compiler/src/value/group/group_type.rs | 8 +- compiler/src/value/group/input.rs | 12 +- .../src/value/group/targets/edwards_bls12.rs | 20 +-- compiler/src/value/implicit/implicit.rs | 2 +- compiler/src/value/integer/integer.rs | 120 +++++++++--------- compiler/src/value/integer/macros.rs | 64 ++++++---- compiler/src/value/value.rs | 61 +++++---- core/src/types/core_circuit_struct_list.rs | 4 +- typed/src/functions/function.rs | 4 +- 71 files changed, 618 insertions(+), 660 deletions(-) diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index 946cfbed19..ea067475a0 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -155,10 +155,10 @@ impl> Compiler { })?; // Derive the package name. - let package_name = self.package_name.clone(); + let package_name = &self.package_name; // Use the typed parser to construct the typed syntax tree. - let typed_tree = LeoTypedAst::new(&package_name, &ast); + let typed_tree = LeoTypedAst::new(package_name, &ast); self.program = typed_tree.into_repr(); self.imported_programs = ImportParser::parse(&self.program)?; diff --git a/compiler/src/console/assert.rs b/compiler/src/console/assert.rs index 3822212dfe..6e407c215b 100644 --- a/compiler/src/console/assert.rs +++ b/compiler/src/console/assert.rs @@ -28,11 +28,11 @@ impl> ConstrainedProgram { pub fn evaluate_console_assert>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, indicator: Option, expression: Expression, - span: Span, + span: &Span, ) -> Result<(), ConsoleError> { let expected_type = Some(Type::Boolean); let expression_string = expression.to_string(); @@ -53,12 +53,17 @@ impl> ConstrainedProgram { // Unwrap assertion value and handle errors let result_option = match assert_expression { ConstrainedValue::Boolean(boolean) => boolean.get_value(), - _ => return Err(ConsoleError::assertion_must_be_boolean(expression_string, span)), + _ => { + return Err(ConsoleError::assertion_must_be_boolean( + expression_string, + span.to_owned(), + )); + } }; - let result_bool = result_option.ok_or_else(|| ConsoleError::assertion_depends_on_input(span.clone()))?; + let result_bool = result_option.ok_or_else(|| ConsoleError::assertion_depends_on_input(span.to_owned()))?; if !result_bool { - return Err(ConsoleError::assertion_failed(expression_string, span)); + return Err(ConsoleError::assertion_failed(expression_string, span.to_owned())); } Ok(()) diff --git a/compiler/src/console/console.rs b/compiler/src/console/console.rs index 3b96a101b5..af8da4ac25 100644 --- a/compiler/src/console/console.rs +++ b/compiler/src/console/console.rs @@ -28,14 +28,14 @@ impl> ConstrainedProgram { pub fn evaluate_console_function_call>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, indicator: Option, console: ConsoleFunctionCall, ) -> Result<(), ConsoleError> { match console.function { ConsoleFunction::Assert(expression) => { - self.evaluate_console_assert(cs, file_scope, function_scope, indicator, expression, console.span)?; + self.evaluate_console_assert(cs, file_scope, function_scope, indicator, expression, &console.span)?; } ConsoleFunction::Debug(string) => { let string = self.format(cs, file_scope, function_scope, string)?; diff --git a/compiler/src/console/format.rs b/compiler/src/console/format.rs index 576dad5bcd..a3b82f6c2c 100644 --- a/compiler/src/console/format.rs +++ b/compiler/src/console/format.rs @@ -28,8 +28,8 @@ impl> ConstrainedProgram { pub fn format>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, formatted: FormattedString, ) -> Result { // Check that containers and parameters match @@ -53,13 +53,8 @@ impl> ConstrainedProgram { let mut result = string.to_string(); for parameter in formatted.parameters.into_iter() { - let parameter_value = self.enforce_expression( - cs, - file_scope.clone(), - function_scope.clone(), - None, - parameter.expression, - )?; + let parameter_value = + self.enforce_expression(cs, file_scope, function_scope, None, parameter.expression)?; result = result.replacen("{}", ¶meter_value.to_string(), 1); } diff --git a/compiler/src/constraints/constraints.rs b/compiler/src/constraints/constraints.rs index 42583f0ed2..5327622245 100644 --- a/compiler/src/constraints/constraints.rs +++ b/compiler/src/constraints/constraints.rs @@ -44,7 +44,7 @@ pub fn generate_constraints, CS: Constrai ) -> Result { let mut resolved_program = ConstrainedProgram::::new(); let program_name = program.get_name(); - let main_function_name = new_scope(program_name.clone(), "main".into()); + let main_function_name = new_scope(&program_name, "main"); resolved_program.store_definitions(program, imported_programs)?; @@ -54,7 +54,7 @@ pub fn generate_constraints, CS: Constrai match main.clone() { ConstrainedValue::Function(_circuit_identifier, function) => { - let result = resolved_program.enforce_main_function(cs, program_name, function, input)?; + let result = resolved_program.enforce_main_function(cs, &program_name, function, input)?; Ok(result) } _ => Err(CompilerError::NoMainFunction), @@ -87,7 +87,7 @@ pub fn generate_test_constraints>( for (test_name, test) in tests.into_iter() { let cs = &mut TestConstraintSystem::::new(); - let full_test_name = format!("{}::{}", program_name.clone(), test_name.to_string()); + let full_test_name = format!("{}::{}", program_name, test_name); let mut output_file_name = program_name.clone(); // get input file name from annotation or use test_name @@ -118,9 +118,9 @@ pub fn generate_test_constraints>( input.parse_state(state_ast)?; // run test function on new program with input - let result = resolved_program.clone().enforce_main_function( + let result = resolved_program.enforce_main_function( cs, - program_name.clone(), + &program_name, test.function, input, // pass program input into every test ); diff --git a/compiler/src/definition/definition.rs b/compiler/src/definition/definition.rs index 7ac5918f30..d78aa90aa0 100644 --- a/compiler/src/definition/definition.rs +++ b/compiler/src/definition/definition.rs @@ -28,7 +28,7 @@ use snarkos_models::curves::{Field, PrimeField}; impl> ConstrainedProgram { pub fn store_definition( &mut self, - function_scope: String, + function_scope: &str, mutable: bool, identifier: Identifier, mut value: ConstrainedValue, @@ -38,7 +38,7 @@ impl> ConstrainedProgram { value = ConstrainedValue::Mutable(Box::new(value)); } - let variable_program_identifier = new_scope(function_scope, identifier.name); + let variable_program_identifier = new_scope(function_scope, &identifier.name); self.store(variable_program_identifier, value); } diff --git a/compiler/src/definition/definitions.rs b/compiler/src/definition/definitions.rs index 6ff9778977..9eaa597007 100644 --- a/compiler/src/definition/definitions.rs +++ b/compiler/src/definition/definitions.rs @@ -35,18 +35,18 @@ impl> ConstrainedProgram { program .imports .iter() - .map(|import| self.store_import(program_name.to_owned(), import, imported_programs)) + .map(|import| self.store_import(&program_name, import, imported_programs)) .collect::, ImportError>>()?; // evaluate and store all circuit definitions program.circuits.into_iter().for_each(|(identifier, circuit)| { - let resolved_circuit_name = new_scope(program_name.to_owned(), identifier.to_string()); + let resolved_circuit_name = new_scope(program_name, &identifier.name); self.store(resolved_circuit_name, ConstrainedValue::CircuitDefinition(circuit)); }); // evaluate and store all function definitions program.functions.into_iter().for_each(|(function_name, function)| { - let resolved_function_name = new_scope(program_name.to_owned(), function_name.to_string()); + let resolved_function_name = new_scope(program_name, &function_name.name); self.store(resolved_function_name, ConstrainedValue::Function(None, function)); }); diff --git a/compiler/src/expression/arithmetic/add.rs b/compiler/src/expression/arithmetic/add.rs index 662a235f11..ba4f09a33a 100644 --- a/compiler/src/expression/arithmetic/add.rs +++ b/compiler/src/expression/arithmetic/add.rs @@ -28,7 +28,7 @@ pub fn enforce_add, CS: ConstraintSystem< cs: &mut CS, left: ConstrainedValue, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, ExpressionError> { match (left, right) { (ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => { @@ -41,16 +41,16 @@ pub fn enforce_add, CS: ConstraintSystem< Ok(ConstrainedValue::Group(point_1.add(cs, &point_2, span)?)) } (ConstrainedValue::Unresolved(string), val_2) => { - let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?; + let val_1 = ConstrainedValue::from_other(string, &val_2, span)?; enforce_add(cs, val_1, val_2, span) } (val_1, ConstrainedValue::Unresolved(string)) => { - let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?; + let val_2 = ConstrainedValue::from_other(string, &val_1, span)?; enforce_add(cs, val_1, val_2, span) } (val_1, val_2) => Err(ExpressionError::incompatible_types( format!("{} + {}", val_1, val_2), - span, + span.to_owned(), )), } } diff --git a/compiler/src/expression/arithmetic/div.rs b/compiler/src/expression/arithmetic/div.rs index 829846f22e..daf79b142c 100644 --- a/compiler/src/expression/arithmetic/div.rs +++ b/compiler/src/expression/arithmetic/div.rs @@ -28,7 +28,7 @@ pub fn enforce_div, CS: ConstraintSystem< cs: &mut CS, left: ConstrainedValue, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, ExpressionError> { match (left, right) { (ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => { @@ -38,16 +38,16 @@ pub fn enforce_div, CS: ConstraintSystem< Ok(ConstrainedValue::Field(field_1.div(cs, &field_2, span)?)) } (ConstrainedValue::Unresolved(string), val_2) => { - let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?; + let val_1 = ConstrainedValue::from_other(string, &val_2, span)?; enforce_div(cs, val_1, val_2, span) } (val_1, ConstrainedValue::Unresolved(string)) => { - let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?; + let val_2 = ConstrainedValue::from_other(string, &val_1, span)?; enforce_div(cs, val_1, val_2, span) } (val_1, val_2) => Err(ExpressionError::incompatible_types( format!("{} / {}", val_1, val_2,), - span, + span.to_owned(), )), } } diff --git a/compiler/src/expression/arithmetic/mul.rs b/compiler/src/expression/arithmetic/mul.rs index 5ac8981d1d..0849d7ccd4 100644 --- a/compiler/src/expression/arithmetic/mul.rs +++ b/compiler/src/expression/arithmetic/mul.rs @@ -28,7 +28,7 @@ pub fn enforce_mul, CS: ConstraintSystem< cs: &mut CS, left: ConstrainedValue, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, ExpressionError> { match (left, right) { (ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => { @@ -38,16 +38,16 @@ pub fn enforce_mul, CS: ConstraintSystem< Ok(ConstrainedValue::Field(field_1.mul(cs, &field_2, span)?)) } (ConstrainedValue::Unresolved(string), val_2) => { - let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?; + let val_1 = ConstrainedValue::from_other(string, &val_2, span)?; enforce_mul(cs, val_1, val_2, span) } (val_1, ConstrainedValue::Unresolved(string)) => { - let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?; + let val_2 = ConstrainedValue::from_other(string, &val_1, span)?; enforce_mul(cs, val_1, val_2, span) } (val_1, val_2) => Err(ExpressionError::incompatible_types( format!("{} * {}", val_1, val_2), - span, + span.to_owned(), )), } } diff --git a/compiler/src/expression/arithmetic/negate.rs b/compiler/src/expression/arithmetic/negate.rs index 1fea24d87a..2996283ae0 100644 --- a/compiler/src/expression/arithmetic/negate.rs +++ b/compiler/src/expression/arithmetic/negate.rs @@ -27,12 +27,15 @@ use snarkos_models::{ pub fn enforce_negate, CS: ConstraintSystem>( cs: &mut CS, value: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, ExpressionError> { match value { ConstrainedValue::Integer(integer) => Ok(ConstrainedValue::Integer(integer.negate(cs, span)?)), ConstrainedValue::Field(field) => Ok(ConstrainedValue::Field(field.negate(cs, span)?)), ConstrainedValue::Group(group) => Ok(ConstrainedValue::Group(group.negate(cs, span)?)), - value => Err(ExpressionError::incompatible_types(format!("-{}", value), span)), + value => Err(ExpressionError::incompatible_types( + format!("-{}", value), + span.to_owned(), + )), } } diff --git a/compiler/src/expression/arithmetic/pow.rs b/compiler/src/expression/arithmetic/pow.rs index b5f7771f05..a416e58ce6 100644 --- a/compiler/src/expression/arithmetic/pow.rs +++ b/compiler/src/expression/arithmetic/pow.rs @@ -28,23 +28,23 @@ pub fn enforce_pow, CS: ConstraintSystem< cs: &mut CS, left: ConstrainedValue, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, ExpressionError> { match (left, right) { (ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => { Ok(ConstrainedValue::Integer(num_1.pow(cs, num_2, span)?)) } (ConstrainedValue::Unresolved(string), val_2) => { - let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?; + let val_1 = ConstrainedValue::from_other(string, &val_2, span)?; enforce_pow(cs, val_1, val_2, span) } (val_1, ConstrainedValue::Unresolved(string)) => { - let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?; + let val_2 = ConstrainedValue::from_other(string, &val_1, span)?; enforce_pow(cs, val_1, val_2, span) } (val_1, val_2) => Err(ExpressionError::incompatible_types( format!("{} ** {}", val_1, val_2,), - span, + span.to_owned(), )), } } diff --git a/compiler/src/expression/arithmetic/sub.rs b/compiler/src/expression/arithmetic/sub.rs index 7f68772a83..bbad8a4262 100644 --- a/compiler/src/expression/arithmetic/sub.rs +++ b/compiler/src/expression/arithmetic/sub.rs @@ -28,7 +28,7 @@ pub fn enforce_sub, CS: ConstraintSystem< cs: &mut CS, left: ConstrainedValue, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, ExpressionError> { match (left, right) { (ConstrainedValue::Integer(num_1), ConstrainedValue::Integer(num_2)) => { @@ -41,16 +41,16 @@ pub fn enforce_sub, CS: ConstraintSystem< Ok(ConstrainedValue::Group(point_1.sub(cs, &point_2, span)?)) } (ConstrainedValue::Unresolved(string), val_2) => { - let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?; + let val_1 = ConstrainedValue::from_other(string, &val_2, &span)?; enforce_sub(cs, val_1, val_2, span) } (val_1, ConstrainedValue::Unresolved(string)) => { - let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?; + let val_2 = ConstrainedValue::from_other(string, &val_1, &span)?; enforce_sub(cs, val_1, val_2, span) } (val_1, val_2) => Err(ExpressionError::incompatible_types( format!("{} - {}", val_1, val_2), - span, + span.to_owned(), )), } } diff --git a/compiler/src/expression/array/access.rs b/compiler/src/expression/array/access.rs index 15ea723f20..7ab4cb2456 100644 --- a/compiler/src/expression/array/access.rs +++ b/compiler/src/expression/array/access.rs @@ -29,31 +29,22 @@ impl> ConstrainedProgram { pub fn enforce_array_access>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, array: Box, index: RangeOrExpression, - span: Span, + span: &Span, ) -> Result, ExpressionError> { - let array = match self.enforce_operand( - cs, - file_scope.clone(), - function_scope.clone(), - expected_type, - *array, - span.clone(), - )? { + let array = match self.enforce_operand(cs, file_scope, function_scope, expected_type, *array, &span)? { ConstrainedValue::Array(array) => array, - value => return Err(ExpressionError::undefined_array(value.to_string(), span)), + value => return Err(ExpressionError::undefined_array(value.to_string(), span.to_owned())), }; match index { RangeOrExpression::Range(from, to) => { let from_resolved = match from { - Some(from_index) => { - self.enforce_index(cs, file_scope.clone(), function_scope.clone(), from_index, span.clone())? - } + Some(from_index) => self.enforce_index(cs, file_scope, function_scope, from_index, span)?, None => 0usize, // Array slice starts at index 0 }; let to_resolved = match to { diff --git a/compiler/src/expression/array/array.rs b/compiler/src/expression/array/array.rs index 578c005bf4..9ff67a1ea7 100644 --- a/compiler/src/expression/array/array.rs +++ b/compiler/src/expression/array/array.rs @@ -34,8 +34,8 @@ impl> ConstrainedProgram { pub fn enforce_array>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, mut expected_type: Option, array: Vec, span: Span, @@ -65,7 +65,7 @@ impl> ConstrainedProgram { match element { SpreadOrExpression::Spread(spread) => match spread { Expression::Identifier(identifier) => { - let array_name = new_scope(function_scope.clone(), identifier.to_string()); + let array_name = new_scope(&function_scope, &identifier.name); match self.get(&array_name) { Some(value) => match value { ConstrainedValue::Array(array) => result.extend(array.clone()), @@ -79,8 +79,8 @@ impl> ConstrainedProgram { SpreadOrExpression::Expression(expression) => { result.push(self.enforce_expression( cs, - file_scope.clone(), - function_scope.clone(), + file_scope, + function_scope, expected_type.clone(), expression, )?); diff --git a/compiler/src/expression/array/index.rs b/compiler/src/expression/array/index.rs index 3f0a9ee436..575d4f5103 100644 --- a/compiler/src/expression/array/index.rs +++ b/compiler/src/expression/array/index.rs @@ -28,15 +28,15 @@ impl> ConstrainedProgram { pub(crate) fn enforce_index>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, index: Expression, - span: Span, + span: &Span, ) -> Result { let expected_type = Some(Type::IntegerType(IntegerType::U32)); - match self.enforce_operand(cs, file_scope, function_scope, expected_type, index, span.clone())? { + match self.enforce_operand(cs, file_scope, function_scope, expected_type, index, &span)? { ConstrainedValue::Integer(number) => Ok(number.to_usize(span)?), - value => Err(ExpressionError::invalid_index(value.to_string(), span)), + value => Err(ExpressionError::invalid_index(value.to_string(), span.to_owned())), } } } diff --git a/compiler/src/expression/binary/binary.rs b/compiler/src/expression/binary/binary.rs index 02b54f53d3..91d9f98cbd 100644 --- a/compiler/src/expression/binary/binary.rs +++ b/compiler/src/expression/binary/binary.rs @@ -31,29 +31,17 @@ impl> ConstrainedProgram { pub fn enforce_binary_expression>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, left: Expression, right: Expression, - span: Span, + span: &Span, ) -> Result, ExpressionError> { - let mut resolved_left = self.enforce_operand( - cs, - file_scope.clone(), - function_scope.clone(), - expected_type.clone(), - left, - span.clone(), - )?; - let mut resolved_right = self.enforce_operand( - cs, - file_scope, - function_scope, - expected_type.clone(), - right, - span.clone(), - )?; + let mut resolved_left = + self.enforce_operand(cs, file_scope, function_scope, expected_type.clone(), left, span)?; + let mut resolved_right = + self.enforce_operand(cs, file_scope, function_scope, expected_type.clone(), right, span)?; resolved_left.resolve_types(&mut resolved_right, expected_type, span)?; diff --git a/compiler/src/expression/binary/operand.rs b/compiler/src/expression/binary/operand.rs index bc0f815ac7..e6ff9530b6 100644 --- a/compiler/src/expression/binary/operand.rs +++ b/compiler/src/expression/binary/operand.rs @@ -31,11 +31,11 @@ impl> ConstrainedProgram { pub fn enforce_operand>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, expression: Expression, - span: Span, + span: &Span, ) -> Result, ExpressionError> { let mut branch = self.enforce_expression(cs, file_scope, function_scope, expected_type.clone(), expression)?; diff --git a/compiler/src/expression/circuit/access.rs b/compiler/src/expression/circuit/access.rs index 785053bfdb..6668bc6eeb 100644 --- a/compiler/src/expression/circuit/access.rs +++ b/compiler/src/expression/circuit/access.rs @@ -36,8 +36,8 @@ impl> ConstrainedProgram { pub fn enforce_circuit_access>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, circuit_identifier: Box, circuit_member: Identifier, @@ -46,11 +46,11 @@ impl> ConstrainedProgram { // access a circuit member using the `self` keyword if let Expression::Identifier(ref identifier) = *circuit_identifier { if identifier.is_self() { - let self_file_scope = new_scope(file_scope, identifier.name.to_string()); - let self_function_scope = new_scope(self_file_scope.clone(), identifier.name.to_string()); + let self_file_scope = new_scope(&file_scope, &identifier.name); + let self_function_scope = new_scope(&self_file_scope, &identifier.name); let member_value = - self.evaluate_identifier(self_file_scope, self_function_scope, None, circuit_member)?; + self.evaluate_identifier(&self_file_scope, &self_function_scope, None, circuit_member)?; return Ok(member_value); } @@ -58,11 +58,11 @@ impl> ConstrainedProgram { let (circuit_name, members) = match self.enforce_operand( cs, - file_scope.clone(), + file_scope, function_scope, expected_type, *circuit_identifier, - span.clone(), + &span, )? { ConstrainedValue::CircuitExpression(name, members) => (name, members), value => return Err(ExpressionError::undefined_circuit(value.to_string(), span)), @@ -76,9 +76,9 @@ impl> ConstrainedProgram { ConstrainedValue::Function(ref _circuit_identifier, ref _function) => { // Pass circuit members into function call by value for stored_member in members { - let circuit_scope = new_scope(file_scope.clone(), circuit_name.to_string()); - let self_keyword = new_scope(circuit_scope, SELF_KEYWORD.to_string()); - let variable = new_scope(self_keyword, stored_member.0.to_string()); + let circuit_scope = new_scope(&file_scope, &circuit_name.name); + let self_keyword = new_scope(&circuit_scope, SELF_KEYWORD); + let variable = new_scope(&self_keyword, &stored_member.0.name); self.store(variable, stored_member.1.clone()); } diff --git a/compiler/src/expression/circuit/circuit.rs b/compiler/src/expression/circuit/circuit.rs index 6f70ea5943..366dea8a48 100644 --- a/compiler/src/expression/circuit/circuit.rs +++ b/compiler/src/expression/circuit/circuit.rs @@ -33,22 +33,22 @@ impl> ConstrainedProgram { pub fn enforce_circuit>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, identifier: Identifier, members: Vec, span: Span, ) -> Result, ExpressionError> { // Circuit definitions are located at the minimum file scope let scopes: Vec<&str> = file_scope.split('_').collect(); - let mut program_identifier = new_scope(scopes[0].to_string(), identifier.to_string()); + let mut program_identifier = new_scope(scopes[0], &identifier.name); if identifier.is_self() { - program_identifier = file_scope.clone(); + program_identifier = file_scope.to_string(); } let circuit = match self.get(&program_identifier) { - Some(value) => value.clone().extract_circuit(span.clone())?, + Some(value) => value.clone().extract_circuit(&span)?, None => return Err(ExpressionError::undefined_circuit(identifier.to_string(), span)), }; @@ -67,8 +67,8 @@ impl> ConstrainedProgram { // Resolve and enforce circuit variable let mut variable_value = self.enforce_expression( cs, - file_scope.clone(), - function_scope.clone(), + file_scope, + function_scope, Some(type_.clone()), variable.expression, )?; diff --git a/compiler/src/expression/circuit/static_access.rs b/compiler/src/expression/circuit/static_access.rs index 88cfcf30d8..90fae8ca4f 100644 --- a/compiler/src/expression/circuit/static_access.rs +++ b/compiler/src/expression/circuit/static_access.rs @@ -29,8 +29,8 @@ impl> ConstrainedProgram { pub fn enforce_circuit_static_access>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, circuit_identifier: Box, circuit_member: Identifier, @@ -47,12 +47,12 @@ impl> ConstrainedProgram { circuit.to_owned() } else { - self.evaluate_identifier(file_scope, function_scope, expected_type, identifier)? + self.evaluate_identifier(&file_scope, &function_scope, expected_type, identifier)? } } expression => self.enforce_expression(cs, file_scope, function_scope, expected_type, expression)?, } - .extract_circuit(span.clone())?; + .extract_circuit(&span)?; // Find static circuit function let matched_function = circuit.members.into_iter().find(|member| match member { diff --git a/compiler/src/expression/conditional/conditional.rs b/compiler/src/expression/conditional/conditional.rs index 6cb30660bf..cfae42dbb3 100644 --- a/compiler/src/expression/conditional/conditional.rs +++ b/compiler/src/expression/conditional/conditional.rs @@ -30,35 +30,23 @@ impl> ConstrainedProgram { pub fn enforce_conditional_expression>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, conditional: Expression, first: Expression, second: Expression, - span: Span, + span: &Span, ) -> Result, ExpressionError> { - let conditional_value = match self.enforce_expression( - cs, - file_scope.clone(), - function_scope.clone(), - Some(Type::Boolean), - conditional, - )? { - ConstrainedValue::Boolean(resolved) => resolved, - value => return Err(ExpressionError::conditional_boolean(value.to_string(), span)), - }; + let conditional_value = + match self.enforce_expression(cs, file_scope, function_scope, Some(Type::Boolean), conditional)? { + ConstrainedValue::Boolean(resolved) => resolved, + value => return Err(ExpressionError::conditional_boolean(value.to_string(), span.to_owned())), + }; - let first_value = self.enforce_operand( - cs, - file_scope.clone(), - function_scope.clone(), - expected_type.clone(), - first, - span.clone(), - )?; + let first_value = self.enforce_operand(cs, file_scope, function_scope, expected_type.clone(), first, span)?; - let second_value = self.enforce_operand(cs, file_scope, function_scope, expected_type, second, span.clone())?; + let second_value = self.enforce_operand(cs, file_scope, function_scope, expected_type, second, span)?; let unique_namespace = cs.ns(|| { format!( @@ -68,6 +56,6 @@ impl> ConstrainedProgram { }); ConstrainedValue::conditionally_select(unique_namespace, &conditional_value, &first_value, &second_value) - .map_err(|e| ExpressionError::cannot_enforce("conditional select".to_string(), e, span)) + .map_err(|e| ExpressionError::cannot_enforce("conditional select".to_string(), e, span.to_owned())) } } diff --git a/compiler/src/expression/expression.rs b/compiler/src/expression/expression.rs index e0c108a7fe..c97be901a5 100644 --- a/compiler/src/expression/expression.rs +++ b/compiler/src/expression/expression.rs @@ -39,8 +39,8 @@ impl> ConstrainedProgram { pub(crate) fn enforce_expression>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, expression: Expression, ) -> Result, ExpressionError> { @@ -51,21 +51,21 @@ impl> ConstrainedProgram { } // Values - Expression::Address(address, span) => Ok(ConstrainedValue::Address(Address::constant(address, span)?)), - Expression::Boolean(boolean, span) => Ok(ConstrainedValue::Boolean(new_bool_constant(boolean, span)?)), - Expression::Field(field, span) => Ok(ConstrainedValue::Field(FieldType::constant(field, span)?)), + Expression::Address(address, span) => Ok(ConstrainedValue::Address(Address::constant(address, &span)?)), + Expression::Boolean(boolean, span) => Ok(ConstrainedValue::Boolean(new_bool_constant(boolean, &span)?)), + Expression::Field(field, span) => Ok(ConstrainedValue::Field(FieldType::constant(field, &span)?)), Expression::Group(group_element) => Ok(ConstrainedValue::Group(G::constant(group_element)?)), - Expression::Implicit(value, span) => Ok(enforce_number_implicit(expected_type, value, span)?), - Expression::Integer(type_, integer, span) => { - Ok(ConstrainedValue::Integer(Integer::new_constant(&type_, integer, span)?)) - } + Expression::Implicit(value, span) => Ok(enforce_number_implicit(expected_type, value, &span)?), + Expression::Integer(type_, integer, span) => Ok(ConstrainedValue::Integer(Integer::new_constant( + &type_, integer, &span, + )?)), // Binary operations Expression::Negate(expression, span) => { let resolved_value = self.enforce_expression(cs, file_scope, function_scope, expected_type, *expression)?; - enforce_negate(cs, resolved_value, span) + enforce_negate(cs, resolved_value, &span) } Expression::Add(left, right, span) => { let (resolved_left, resolved_right) = self.enforce_binary_expression( @@ -75,10 +75,10 @@ impl> ConstrainedProgram { expected_type, *left, *right, - span.clone(), + &span, )?; - enforce_add(cs, resolved_left, resolved_right, span) + enforce_add(cs, resolved_left, resolved_right, &span) } Expression::Sub(left, right, span) => { let (resolved_left, resolved_right) = self.enforce_binary_expression( @@ -88,10 +88,10 @@ impl> ConstrainedProgram { expected_type, *left, *right, - span.clone(), + &span, )?; - enforce_sub(cs, resolved_left, resolved_right, span) + enforce_sub(cs, resolved_left, resolved_right, &span) } Expression::Mul(left, right, span) => { let (resolved_left, resolved_right) = self.enforce_binary_expression( @@ -101,10 +101,10 @@ impl> ConstrainedProgram { expected_type, *left, *right, - span.clone(), + &span, )?; - enforce_mul(cs, resolved_left, resolved_right, span) + enforce_mul(cs, resolved_left, resolved_right, &span) } Expression::Div(left, right, span) => { let (resolved_left, resolved_right) = self.enforce_binary_expression( @@ -114,10 +114,10 @@ impl> ConstrainedProgram { expected_type, *left, *right, - span.clone(), + &span, )?; - enforce_div(cs, resolved_left, resolved_right, span) + enforce_div(cs, resolved_left, resolved_right, &span) } Expression::Pow(left, right, span) => { let (resolved_left, resolved_right) = self.enforce_binary_expression( @@ -127,10 +127,10 @@ impl> ConstrainedProgram { expected_type, *left, *right, - span.clone(), + &span, )?; - enforce_pow(cs, resolved_left, resolved_right, span) + enforce_pow(cs, resolved_left, resolved_right, &span) } // Boolean operations @@ -146,10 +146,10 @@ impl> ConstrainedProgram { expected_type, *left, *right, - span.clone(), + &span, )?; - Ok(enforce_or(cs, resolved_left, resolved_right, span)?) + Ok(enforce_or(cs, resolved_left, resolved_right, &span)?) } Expression::And(left, right, span) => { let (resolved_left, resolved_right) = self.enforce_binary_expression( @@ -159,40 +159,40 @@ impl> ConstrainedProgram { expected_type, *left, *right, - span.clone(), + &span, )?; - Ok(enforce_and(cs, resolved_left, resolved_right, span)?) + Ok(enforce_and(cs, resolved_left, resolved_right, &span)?) } Expression::Eq(left, right, span) => { let (resolved_left, resolved_right) = - self.enforce_binary_expression(cs, file_scope, function_scope, None, *left, *right, span.clone())?; + self.enforce_binary_expression(cs, file_scope, function_scope, None, *left, *right, &span)?; - Ok(evaluate_eq(cs, resolved_left, resolved_right, span)?) + Ok(evaluate_eq(cs, resolved_left, resolved_right, &span)?) } Expression::Ge(left, right, span) => { let (resolved_left, resolved_right) = - self.enforce_binary_expression(cs, file_scope, function_scope, None, *left, *right, span.clone())?; + self.enforce_binary_expression(cs, file_scope, function_scope, None, *left, *right, &span)?; - Ok(evaluate_ge(cs, resolved_left, resolved_right, span)?) + Ok(evaluate_ge(cs, resolved_left, resolved_right, &span)?) } Expression::Gt(left, right, span) => { let (resolved_left, resolved_right) = - self.enforce_binary_expression(cs, file_scope, function_scope, None, *left, *right, span.clone())?; + self.enforce_binary_expression(cs, file_scope, function_scope, None, *left, *right, &span)?; - Ok(evaluate_gt(cs, resolved_left, resolved_right, span)?) + Ok(evaluate_gt(cs, resolved_left, resolved_right, &span)?) } Expression::Le(left, right, span) => { let (resolved_left, resolved_right) = - self.enforce_binary_expression(cs, file_scope, function_scope, None, *left, *right, span.clone())?; + self.enforce_binary_expression(cs, file_scope, function_scope, None, *left, *right, &span)?; - Ok(evaluate_le(cs, resolved_left, resolved_right, span)?) + Ok(evaluate_le(cs, resolved_left, resolved_right, &span)?) } Expression::Lt(left, right, span) => { let (resolved_left, resolved_right) = - self.enforce_binary_expression(cs, file_scope, function_scope, None, *left, *right, span.clone())?; + self.enforce_binary_expression(cs, file_scope, function_scope, None, *left, *right, &span)?; - Ok(evaluate_lt(cs, resolved_left, resolved_right, span)?) + Ok(evaluate_lt(cs, resolved_left, resolved_right, &span)?) } // Conditionals @@ -204,7 +204,7 @@ impl> ConstrainedProgram { *conditional, *first, *second, - span, + &span, ), // Arrays @@ -212,7 +212,7 @@ impl> ConstrainedProgram { self.enforce_array(cs, file_scope, function_scope, expected_type, array, span) } Expression::ArrayAccess(array, index, span) => { - self.enforce_array_access(cs, file_scope, function_scope, expected_type, array, *index, span) + self.enforce_array_access(cs, file_scope, function_scope, expected_type, array, *index, &span) } // Tuples @@ -220,7 +220,7 @@ impl> ConstrainedProgram { self.enforce_tuple(cs, file_scope, function_scope, expected_type, tuple, span) } Expression::TupleAccess(tuple, index, span) => { - self.enforce_tuple_access(cs, file_scope, function_scope, expected_type, tuple, index, span) + self.enforce_tuple_access(cs, file_scope, function_scope, expected_type, tuple, index, &span) } // Circuits diff --git a/compiler/src/expression/function/core_circuit.rs b/compiler/src/expression/function/core_circuit.rs index a6eea1fc4e..c0314e775f 100644 --- a/compiler/src/expression/function/core_circuit.rs +++ b/compiler/src/expression/function/core_circuit.rs @@ -29,8 +29,8 @@ impl> ConstrainedProgram { pub fn enforce_core_circuit_call_expression>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, core_circuit: String, arguments: Vec, @@ -39,8 +39,7 @@ impl> ConstrainedProgram { // Get the value of each core function argument let mut argument_values = Vec::with_capacity(arguments.len()); for argument in arguments.into_iter() { - let argument_value = - self.enforce_expression(cs, file_scope.clone(), function_scope.clone(), None, argument)?; + let argument_value = self.enforce_expression(cs, file_scope, function_scope, None, argument)?; let core_function_argument = argument_value.to_value(); argument_values.push(core_function_argument); @@ -62,7 +61,7 @@ impl> ConstrainedProgram { // Check that function returns expected type if let Some(expected) = expected_type { - let actual = return_value.to_type(span.clone())?; + let actual = return_value.to_type(&span)?; if expected.ne(&actual) { return Err(ExpressionError::FunctionError(Box::new( FunctionError::return_argument_type(expected.to_string(), actual.to_string(), span), diff --git a/compiler/src/expression/function/function.rs b/compiler/src/expression/function/function.rs index 395bccce1a..94d75b4386 100644 --- a/compiler/src/expression/function/function.rs +++ b/compiler/src/expression/function/function.rs @@ -29,8 +29,8 @@ impl> ConstrainedProgram { pub fn enforce_function_call_expression>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, function: Box, arguments: Vec, @@ -41,15 +41,15 @@ impl> ConstrainedProgram { // Call a circuit function that can mutate self. // Save a reference to the circuit we are mutating. - let circuit_id_string = format!("{}", circuit_identifier); - let declared_circuit_reference = new_scope(function_scope.clone(), circuit_id_string); + let circuit_id_string = circuit_identifier.to_string(); + let declared_circuit_reference = new_scope(&function_scope, &circuit_id_string); ( declared_circuit_reference, self.enforce_circuit_access( cs, - file_scope.clone(), - function_scope.clone(), + file_scope, + function_scope, expected_type, circuit_identifier, circuit_member, @@ -58,12 +58,12 @@ impl> ConstrainedProgram { ) } function => ( - function_scope.clone(), - self.enforce_expression(cs, file_scope.clone(), function_scope.clone(), expected_type, function)?, + function_scope.to_string(), + self.enforce_expression(cs, file_scope, function_scope, expected_type, function)?, ), }; - let (outer_scope, function_call) = function_value.extract_function(file_scope, span.clone())?; + let (outer_scope, function_call) = function_value.extract_function(file_scope, &span)?; let name_unique = format!( "function call {} {}:{}", @@ -74,11 +74,11 @@ impl> ConstrainedProgram { self.enforce_function( &mut cs.ns(|| name_unique), - outer_scope, + &outer_scope, function_scope, function_call, arguments, - declared_circuit_reference, + &declared_circuit_reference, ) .map_err(|error| ExpressionError::from(Box::new(error))) } diff --git a/compiler/src/expression/identifier/identifier.rs b/compiler/src/expression/identifier/identifier.rs index 8022bb569a..f1c8c4662d 100644 --- a/compiler/src/expression/identifier/identifier.rs +++ b/compiler/src/expression/identifier/identifier.rs @@ -31,14 +31,14 @@ impl> ConstrainedProgram { /// Enforce a variable expression by getting the resolved value pub fn evaluate_identifier( &mut self, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, unresolved_identifier: Identifier, ) -> Result, ExpressionError> { // Evaluate the identifier name in the current function scope - let variable_name = new_scope(function_scope, unresolved_identifier.to_string()); - let identifier_name = new_scope(file_scope, unresolved_identifier.to_string()); + let variable_name = new_scope(function_scope, &unresolved_identifier.name); + let identifier_name = new_scope(file_scope, &unresolved_identifier.name); let mut result_value = if let Some(value) = self.get(&variable_name) { // Reassigning variable to another variable @@ -51,14 +51,14 @@ impl> ConstrainedProgram { value.clone() } else if expected_type.is_some() && expected_type.unwrap() == Type::Address { // If we expect an address type, try to return an address - let address = Address::constant(unresolved_identifier.name, unresolved_identifier.span)?; + let address = Address::constant(unresolved_identifier.name, &unresolved_identifier.span)?; return Ok(ConstrainedValue::Address(address)); } else { return Err(ExpressionError::undefined_identifier(unresolved_identifier)); }; - result_value.resolve_type(expected_type, unresolved_identifier.span)?; + result_value.resolve_type(expected_type, &unresolved_identifier.span)?; Ok(result_value) } diff --git a/compiler/src/expression/logical/and.rs b/compiler/src/expression/logical/and.rs index 579600fbc5..1f901d0e19 100644 --- a/compiler/src/expression/logical/and.rs +++ b/compiler/src/expression/logical/and.rs @@ -28,17 +28,17 @@ pub fn enforce_and, CS: ConstraintSystem< cs: &mut CS, left: ConstrainedValue, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, BooleanError> { let name = format!("{} && {}", left, right); if let (ConstrainedValue::Boolean(left_bool), ConstrainedValue::Boolean(right_bool)) = (left, right) { let name_unique = format!("{} {}:{}", name, span.line, span.start); let result = Boolean::and(cs.ns(|| name_unique), &left_bool, &right_bool) - .map_err(|e| BooleanError::cannot_enforce("&&".to_string(), e, span))?; + .map_err(|e| BooleanError::cannot_enforce("&&".to_string(), e, span.to_owned()))?; return Ok(ConstrainedValue::Boolean(result)); } - Err(BooleanError::cannot_evaluate(name, span)) + Err(BooleanError::cannot_evaluate(name, span.to_owned())) } diff --git a/compiler/src/expression/logical/or.rs b/compiler/src/expression/logical/or.rs index ce425d47f9..644c90fa21 100644 --- a/compiler/src/expression/logical/or.rs +++ b/compiler/src/expression/logical/or.rs @@ -28,17 +28,17 @@ pub fn enforce_or, CS: ConstraintSystem, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, BooleanError> { let name = format!("{} || {}", left, right); if let (ConstrainedValue::Boolean(left_bool), ConstrainedValue::Boolean(right_bool)) = (left, right) { let name_unique = format!("{} {}:{}", name, span.line, span.start); let result = Boolean::or(cs.ns(|| name_unique), &left_bool, &right_bool) - .map_err(|e| BooleanError::cannot_enforce("||".to_string(), e, span))?; + .map_err(|e| BooleanError::cannot_enforce("||".to_string(), e, span.to_owned()))?; return Ok(ConstrainedValue::Boolean(result)); } - Err(BooleanError::cannot_evaluate(name, span)) + Err(BooleanError::cannot_evaluate(name, span.to_owned())) } diff --git a/compiler/src/expression/relational/eq.rs b/compiler/src/expression/relational/eq.rs index b50a87fd16..69d8d1370f 100644 --- a/compiler/src/expression/relational/eq.rs +++ b/compiler/src/expression/relational/eq.rs @@ -31,7 +31,7 @@ pub fn evaluate_eq, CS: ConstraintSystem< cs: &mut CS, left: ConstrainedValue, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, ExpressionError> { let namespace_string = format!("evaluate {} == {} {}:{}", left, right, span.line, span.start); let constraint_result = match (left, right) { @@ -58,14 +58,9 @@ pub fn evaluate_eq, CS: ConstraintSystem< (ConstrainedValue::Array(arr_1), ConstrainedValue::Array(arr_2)) => { let mut current = ConstrainedValue::Boolean(Boolean::constant(true)); for (i, (left, right)) in arr_1.into_iter().zip(arr_2.into_iter()).enumerate() { - let next = evaluate_eq(&mut cs.ns(|| format!("array[{}]", i)), left, right, span.clone())?; + let next = evaluate_eq(&mut cs.ns(|| format!("array[{}]", i)), left, right, span)?; - current = enforce_and( - &mut cs.ns(|| format!("array result {}", i)), - current, - next, - span.clone(), - )?; + current = enforce_and(&mut cs.ns(|| format!("array result {}", i)), current, next, span)?; } return Ok(current); } @@ -73,36 +68,31 @@ pub fn evaluate_eq, CS: ConstraintSystem< let mut current = ConstrainedValue::Boolean(Boolean::constant(true)); for (i, (left, right)) in tuple_1.into_iter().zip(tuple_2.into_iter()).enumerate() { - let next = evaluate_eq(&mut cs.ns(|| format!("tuple_index {}", i)), left, right, span.clone())?; + let next = evaluate_eq(&mut cs.ns(|| format!("tuple_index {}", i)), left, right, span)?; - current = enforce_and( - &mut cs.ns(|| format!("array result {}", i)), - current, - next, - span.clone(), - )?; + current = enforce_and(&mut cs.ns(|| format!("array result {}", i)), current, next, span)?; } return Ok(current); } (ConstrainedValue::Unresolved(string), val_2) => { let mut unique_namespace = cs.ns(|| namespace_string); - let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?; + let val_1 = ConstrainedValue::from_other(string, &val_2, span)?; return evaluate_eq(&mut unique_namespace, val_1, val_2, span); } (val_1, ConstrainedValue::Unresolved(string)) => { let mut unique_namespace = cs.ns(|| namespace_string); - let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?; + let val_2 = ConstrainedValue::from_other(string, &val_1, span)?; return evaluate_eq(&mut unique_namespace, val_1, val_2, span); } (val_1, val_2) => { return Err(ExpressionError::incompatible_types( format!("{} == {}", val_1, val_2,), - span, + span.to_owned(), )); } }; - let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate("==".to_string(), span))?; + let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate("==".to_string(), span.to_owned()))?; Ok(ConstrainedValue::Boolean(boolean)) } diff --git a/compiler/src/expression/relational/ge.rs b/compiler/src/expression/relational/ge.rs index 00212cd157..15e91f9e27 100644 --- a/compiler/src/expression/relational/ge.rs +++ b/compiler/src/expression/relational/ge.rs @@ -29,7 +29,7 @@ pub fn evaluate_ge, CS: ConstraintSystem< cs: &mut CS, left: ConstrainedValue, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, ExpressionError> { let mut unique_namespace = cs.ns(|| format!("evaluate {} >= {} {}:{}", left, right, span.line, span.start)); let constraint_result = match (left, right) { @@ -37,22 +37,22 @@ pub fn evaluate_ge, CS: ConstraintSystem< num_1.greater_than_or_equal(unique_namespace, &num_2) } (ConstrainedValue::Unresolved(string), val_2) => { - let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?; + let val_1 = ConstrainedValue::from_other(string, &val_2, span)?; return evaluate_ge(&mut unique_namespace, val_1, val_2, span); } (val_1, ConstrainedValue::Unresolved(string)) => { - let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?; + let val_2 = ConstrainedValue::from_other(string, &val_1, span)?; return evaluate_ge(&mut unique_namespace, val_1, val_2, span); } (val_1, val_2) => { return Err(ExpressionError::incompatible_types( format!("{} >= {}", val_1, val_2), - span, + span.to_owned(), )); } }; - let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate(">=".to_string(), span))?; + let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate(">=".to_string(), span.to_owned()))?; Ok(ConstrainedValue::Boolean(boolean)) } diff --git a/compiler/src/expression/relational/gt.rs b/compiler/src/expression/relational/gt.rs index ce733b49b7..45351cc999 100644 --- a/compiler/src/expression/relational/gt.rs +++ b/compiler/src/expression/relational/gt.rs @@ -29,7 +29,7 @@ pub fn evaluate_gt, CS: ConstraintSystem< cs: &mut CS, left: ConstrainedValue, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, ExpressionError> { let mut unique_namespace = cs.ns(|| format!("evaluate {} > {} {}:{}", left, right, span.line, span.start)); let constraint_result = match (left, right) { @@ -37,22 +37,22 @@ pub fn evaluate_gt, CS: ConstraintSystem< num_1.greater_than(unique_namespace, &num_2) } (ConstrainedValue::Unresolved(string), val_2) => { - let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?; + let val_1 = ConstrainedValue::from_other(string, &val_2, span)?; return evaluate_gt(&mut unique_namespace, val_1, val_2, span); } (val_1, ConstrainedValue::Unresolved(string)) => { - let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?; + let val_2 = ConstrainedValue::from_other(string, &val_1, span)?; return evaluate_gt(&mut unique_namespace, val_1, val_2, span); } (val_1, val_2) => { return Err(ExpressionError::incompatible_types( format!("{} > {}", val_1, val_2), - span, + span.to_owned(), )); } }; - let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate(">".to_string(), span))?; + let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate(">".to_string(), span.to_owned()))?; Ok(ConstrainedValue::Boolean(boolean)) } diff --git a/compiler/src/expression/relational/le.rs b/compiler/src/expression/relational/le.rs index 6afb209367..91fbd5e6f9 100644 --- a/compiler/src/expression/relational/le.rs +++ b/compiler/src/expression/relational/le.rs @@ -29,7 +29,7 @@ pub fn evaluate_le, CS: ConstraintSystem< cs: &mut CS, left: ConstrainedValue, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, ExpressionError> { let mut unique_namespace = cs.ns(|| format!("evaluate {} <= {} {}:{}", left, right, span.line, span.start)); let constraint_result = match (left, right) { @@ -37,22 +37,22 @@ pub fn evaluate_le, CS: ConstraintSystem< num_1.less_than_or_equal(unique_namespace, &num_2) } (ConstrainedValue::Unresolved(string), val_2) => { - let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?; + let val_1 = ConstrainedValue::from_other(string, &val_2, span)?; return evaluate_le(&mut unique_namespace, val_1, val_2, span); } (val_1, ConstrainedValue::Unresolved(string)) => { - let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?; + let val_2 = ConstrainedValue::from_other(string, &val_1, span)?; return evaluate_le(&mut unique_namespace, val_1, val_2, span); } (val_1, val_2) => { return Err(ExpressionError::incompatible_types( format!("{} <= {}", val_1, val_2), - span, + span.to_owned(), )); } }; - let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate("<=".to_string(), span))?; + let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate("<=".to_string(), span.to_owned()))?; Ok(ConstrainedValue::Boolean(boolean)) } diff --git a/compiler/src/expression/relational/lt.rs b/compiler/src/expression/relational/lt.rs index ef7a7c76f9..ef9dd01e93 100644 --- a/compiler/src/expression/relational/lt.rs +++ b/compiler/src/expression/relational/lt.rs @@ -29,7 +29,7 @@ pub fn evaluate_lt, CS: ConstraintSystem< cs: &mut CS, left: ConstrainedValue, right: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, ExpressionError> { let mut unique_namespace = cs.ns(|| format!("evaluate {} < {} {}:{}", left, right, span.line, span.start)); let constraint_result = match (left, right) { @@ -37,22 +37,22 @@ pub fn evaluate_lt, CS: ConstraintSystem< num_1.less_than(unique_namespace, &num_2) } (ConstrainedValue::Unresolved(string), val_2) => { - let val_1 = ConstrainedValue::from_other(string, &val_2, span.clone())?; + let val_1 = ConstrainedValue::from_other(string, &val_2, span)?; return evaluate_lt(&mut unique_namespace, val_1, val_2, span); } (val_1, ConstrainedValue::Unresolved(string)) => { - let val_2 = ConstrainedValue::from_other(string, &val_1, span.clone())?; + let val_2 = ConstrainedValue::from_other(string, &val_1, span)?; return evaluate_lt(&mut unique_namespace, val_1, val_2, span); } (val_1, val_2) => { return Err(ExpressionError::incompatible_types( format!("{} < {}", val_1, val_2), - span, + span.to_owned(), )); } }; - let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate("<".to_string(), span))?; + let boolean = constraint_result.map_err(|_| ExpressionError::cannot_evaluate("<".to_string(), span.to_owned()))?; Ok(ConstrainedValue::Boolean(boolean)) } diff --git a/compiler/src/expression/tuple/access.rs b/compiler/src/expression/tuple/access.rs index afd3cf9483..b489fb9a81 100644 --- a/compiler/src/expression/tuple/access.rs +++ b/compiler/src/expression/tuple/access.rs @@ -29,20 +29,20 @@ impl> ConstrainedProgram { pub fn enforce_tuple_access>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, tuple: Box, index: usize, - span: Span, + span: &Span, ) -> Result, ExpressionError> { - let tuple = match self.enforce_operand(cs, file_scope, function_scope, expected_type, *tuple, span.clone())? { + let tuple = match self.enforce_operand(cs, file_scope, function_scope, expected_type, *tuple, &span)? { ConstrainedValue::Tuple(tuple) => tuple, - value => return Err(ExpressionError::undefined_array(value.to_string(), span)), + value => return Err(ExpressionError::undefined_array(value.to_string(), span.to_owned())), }; if index > tuple.len() - 1 { - return Err(ExpressionError::index_out_of_bounds(index, span)); + return Err(ExpressionError::index_out_of_bounds(index, span.to_owned())); } Ok(tuple[index].to_owned()) diff --git a/compiler/src/expression/tuple/tuple.rs b/compiler/src/expression/tuple/tuple.rs index 632024ed5d..3295fb4fe3 100644 --- a/compiler/src/expression/tuple/tuple.rs +++ b/compiler/src/expression/tuple/tuple.rs @@ -29,8 +29,8 @@ impl> ConstrainedProgram { pub fn enforce_tuple>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expected_type: Option, tuple: Vec, span: Span, @@ -60,7 +60,7 @@ impl> ConstrainedProgram { Some(expected_types[i].clone()) }; - result.push(self.enforce_expression(cs, file_scope.clone(), function_scope.clone(), type_, expression)?); + result.push(self.enforce_expression(cs, file_scope, function_scope, type_, expression)?); } Ok(ConstrainedValue::Tuple(result)) diff --git a/compiler/src/function/function.rs b/compiler/src/function/function.rs index aa1b67c42c..2c5ab8ea12 100644 --- a/compiler/src/function/function.rs +++ b/compiler/src/function/function.rs @@ -30,10 +30,10 @@ use snarkos_models::{ gadgets::r1cs::ConstraintSystem, }; -pub fn check_arguments_length(expected: usize, actual: usize, span: Span) -> Result<(), FunctionError> { +pub fn check_arguments_length(expected: usize, actual: usize, span: &Span) -> Result<(), FunctionError> { // Make sure we are given the correct number of arguments if expected != actual { - Err(FunctionError::arguments_length(expected, actual, span)) + Err(FunctionError::arguments_length(expected, actual, span.to_owned())) } else { Ok(()) } @@ -43,39 +43,33 @@ impl> ConstrainedProgram { pub(crate) fn enforce_function>( &mut self, cs: &mut CS, - scope: String, - caller_scope: String, + scope: &str, + caller_scope: &str, function: Function, input: Vec, - declared_circuit_reference: String, + declared_circuit_reference: &str, ) -> Result, FunctionError> { - let function_name = new_scope(scope.clone(), function.get_name()); + let function_name = new_scope(scope, function.get_name()); // Make sure we are given the correct number of input variables - check_arguments_length(function.input.len(), input.len(), function.span.clone())?; + check_arguments_length(function.input.len(), input.len(), &function.span)?; // Store input values as new variables in resolved program for (input_model, input_expression) in function.input.iter().zip(input.into_iter()) { let (name, value) = match input_model { InputVariable::InputKeyword(identifier) => { - let input_value = self.enforce_function_input( - cs, - scope.clone(), - caller_scope.clone(), - function_name.clone(), - None, - input_expression, - )?; + let input_value = + self.enforce_function_input(cs, scope, caller_scope, &function_name, None, input_expression)?; - (identifier.name.clone(), input_value) + (&identifier.name, input_value) } InputVariable::FunctionInput(input_model) => { // First evaluate input expression let mut input_value = self.enforce_function_input( cs, - scope.clone(), - caller_scope.clone(), - function_name.clone(), + scope, + caller_scope, + &function_name, Some(input_model.type_.clone()), input_expression, )?; @@ -84,12 +78,12 @@ impl> ConstrainedProgram { input_value = ConstrainedValue::Mutable(Box::new(input_value)) } - (input_model.identifier.name.clone(), input_value) + (&input_model.identifier.name, input_value) } }; // Store input as variable with {function_name}_{input_name} - let input_program_identifier = new_scope(function_name.clone(), name); + let input_program_identifier = new_scope(&function_name, &name); self.store(input_program_identifier, value); } @@ -99,12 +93,12 @@ impl> ConstrainedProgram { for statement in function.statements.iter() { let mut result = self.enforce_statement( cs, - scope.clone(), - function_name.clone(), + scope, + &function_name, None, statement.clone(), function.returns.clone(), - declared_circuit_reference.clone(), + declared_circuit_reference, )?; results.append(&mut result); @@ -113,7 +107,7 @@ impl> ConstrainedProgram { // Conditionally select a result based on returned indicators let mut return_values = ConstrainedValue::Tuple(vec![]); - Self::conditionally_select_result(cs, &mut return_values, results, function.span.clone())?; + Self::conditionally_select_result(cs, &mut return_values, results, &function.span)?; if let ConstrainedValue::Tuple(ref returns) = return_values { let return_types = match function.returns { diff --git a/compiler/src/function/input/array.rs b/compiler/src/function/input/array.rs index 9b988a2d14..6317fde9c3 100644 --- a/compiler/src/function/input/array.rs +++ b/compiler/src/function/input/array.rs @@ -34,11 +34,11 @@ impl> ConstrainedProgram { pub fn allocate_array>( &mut self, cs: &mut CS, - name: String, + name: &str, array_type: Type, array_dimensions: Vec, input_value: Option, - span: Span, + span: &Span, ) -> Result, FunctionError> { let expected_length = array_dimensions[0]; let mut array_value = vec![]; @@ -47,34 +47,33 @@ impl> ConstrainedProgram { Some(InputValue::Array(arr)) => { // Allocate each value in the current row for (i, value) in arr.into_iter().enumerate() { - let value_name = new_scope(name.clone(), i.to_string()); + let value_name = new_scope(&name, &i.to_string()); let value_type = array_type.outer_dimension(&array_dimensions); array_value.push(self.allocate_main_function_input( cs, value_type, - value_name, + &value_name, Some(value), - span.clone(), + span, )?) } } None => { // Allocate all row values as none for i in 0..expected_length { - let value_name = new_scope(name.clone(), i.to_string()); + let value_name = new_scope(&name, &i.to_string()); let value_type = array_type.outer_dimension(&array_dimensions); - array_value.push(self.allocate_main_function_input( - cs, - value_type, - value_name, - None, - span.clone(), - )?); + array_value.push(self.allocate_main_function_input(cs, value_type, &value_name, None, span)?); } } - _ => return Err(FunctionError::invalid_array(input_value.unwrap().to_string(), span)), + _ => { + return Err(FunctionError::invalid_array( + input_value.unwrap().to_string(), + span.to_owned(), + )); + } } Ok(ConstrainedValue::Array(array_value)) diff --git a/compiler/src/function/input/function_input.rs b/compiler/src/function/input/function_input.rs index 2c377c4398..a588d778c8 100644 --- a/compiler/src/function/input/function_input.rs +++ b/compiler/src/function/input/function_input.rs @@ -29,9 +29,9 @@ impl> ConstrainedProgram { pub fn enforce_function_input>( &mut self, cs: &mut CS, - scope: String, - caller_scope: String, - function_name: String, + scope: &str, + caller_scope: &str, + function_name: &str, expected_type: Option, input: Expression, ) -> Result, FunctionError> { diff --git a/compiler/src/function/input/input_section.rs b/compiler/src/function/input/input_section.rs index 2ef7e8c42c..96d24bfbd6 100644 --- a/compiler/src/function/input/input_section.rs +++ b/compiler/src/function/input/input_section.rs @@ -39,9 +39,9 @@ impl> ConstrainedProgram { let member_value = self.allocate_main_function_input( cs, parameter.type_, - parameter.variable.name, + ¶meter.variable.name, option, - parameter.span, + ¶meter.span, )?; let member = ConstrainedCircuitMember(member_name, member_value); diff --git a/compiler/src/function/input/main_function_input.rs b/compiler/src/function/input/main_function_input.rs index 12b848b9a8..ef9db4092a 100644 --- a/compiler/src/function/input/main_function_input.rs +++ b/compiler/src/function/input/main_function_input.rs @@ -42,9 +42,9 @@ impl> ConstrainedProgram { &mut self, cs: &mut CS, type_: Type, - name: String, + name: &str, input_option: Option, - span: Span, + span: &Span, ) -> Result, FunctionError> { match type_ { Type::Address => Ok(Address::from_input(cs, name, input_option, span)?), @@ -59,7 +59,7 @@ impl> ConstrainedProgram { span, )?)), Type::Array(type_, dimensions) => self.allocate_array(cs, name, *type_, dimensions, input_option, span), - Type::Tuple(types) => self.allocate_tuple(cs, name, types, input_option, span), + Type::Tuple(types) => self.allocate_tuple(cs, &name, types, input_option, span), _ => unimplemented!("main function input not implemented for type"), } } diff --git a/compiler/src/function/input/tuple.rs b/compiler/src/function/input/tuple.rs index 2712f2e1db..77ee183309 100644 --- a/compiler/src/function/input/tuple.rs +++ b/compiler/src/function/input/tuple.rs @@ -34,10 +34,10 @@ impl> ConstrainedProgram { pub fn allocate_tuple>( &mut self, cs: &mut CS, - name: String, + name: &str, types: Vec, input_value: Option, - span: Span, + span: &Span, ) -> Result, FunctionError> { let mut tuple_values = vec![]; @@ -45,26 +45,25 @@ impl> ConstrainedProgram { Some(InputValue::Tuple(values)) => { // Allocate each value in the tuple for (i, (value, type_)) in values.into_iter().zip(types.into_iter()).enumerate() { - let value_name = new_scope(name.clone(), i.to_string()); + let value_name = new_scope(name, &i.to_string()); - tuple_values.push(self.allocate_main_function_input( - cs, - type_, - value_name, - Some(value), - span.clone(), - )?) + tuple_values.push(self.allocate_main_function_input(cs, type_, &value_name, Some(value), span)?) } } None => { // Allocate all tuple values as none for (i, type_) in types.into_iter().enumerate() { - let value_name = new_scope(name.clone(), i.to_string()); + let value_name = new_scope(name, &i.to_string()); - tuple_values.push(self.allocate_main_function_input(cs, type_, value_name, None, span.clone())?); + tuple_values.push(self.allocate_main_function_input(cs, type_, &value_name, None, span)?); } } - _ => return Err(FunctionError::invalid_tuple(input_value.unwrap().to_string(), span)), + _ => { + return Err(FunctionError::invalid_tuple( + input_value.unwrap().to_string(), + span.to_owned(), + )); + } } Ok(ConstrainedValue::Tuple(tuple_values)) diff --git a/compiler/src/function/main_function.rs b/compiler/src/function/main_function.rs index 2756b7213d..6e4f0047d9 100644 --- a/compiler/src/function/main_function.rs +++ b/compiler/src/function/main_function.rs @@ -34,11 +34,11 @@ impl> ConstrainedProgram { pub fn enforce_main_function>( &mut self, cs: &mut CS, - scope: String, + scope: &str, function: Function, input: Input, ) -> Result { - let function_name = new_scope(scope.clone(), function.get_name()); + let function_name = new_scope(scope, function.get_name()); let registers = input.get_registers(); // Iterate over main function input variables and allocate new values @@ -55,20 +55,15 @@ impl> ConstrainedProgram { let input_option = input .get(&name) .ok_or_else(|| FunctionError::input_not_found(name.clone(), function.span.clone()))?; - let input_value = self.allocate_main_function_input( - cs, - input_model.type_, - name.clone(), - input_option, - function.span.clone(), - )?; + let input_value = + self.allocate_main_function_input(cs, input_model.type_, &name, input_option, &function.span)?; (input_model.identifier, input_value) } }; // Store input as variable with {function_name}_{identifier_name} - let input_name = new_scope(function_name.clone(), identifier.name.clone()); + let input_name = new_scope(&function_name, &identifier.name); // Store a new variable for every allocated main function input self.store(input_name, value); @@ -77,7 +72,7 @@ impl> ConstrainedProgram { } let span = function.span.clone(); - let result_value = self.enforce_function(cs, scope, function_name, function, input_variables, "".to_owned())?; + let result_value = self.enforce_function(cs, scope, &function_name, function, input_variables, "")?; let output_bytes = OutputBytes::new_from_constrained_value(registers, result_value, span)?; Ok(output_bytes) diff --git a/compiler/src/function/result/result.rs b/compiler/src/function/result/result.rs index 29ace00aed..f4e84ae4c0 100644 --- a/compiler/src/function/result/result.rs +++ b/compiler/src/function/result/result.rs @@ -34,7 +34,7 @@ impl> ConstrainedProgram { cs: &mut CS, return_value: &mut ConstrainedValue, results: Vec<(Option, ConstrainedValue)>, - span: Span, + span: &Span, ) -> Result<(), StatementError> { // if there are no results, continue if results.is_empty() { @@ -65,7 +65,7 @@ impl> ConstrainedProgram { let selected_value = ConstrainedValue::conditionally_select(cs.ns(|| name_unique), &condition, &result, return_value) .map_err(|_| { - StatementError::select_fail(result.to_string(), return_value.to_string(), span.clone()) + StatementError::select_fail(result.to_string(), return_value.to_string(), span.to_owned()) })?; *return_value = selected_value; diff --git a/compiler/src/import/store/core_package.rs b/compiler/src/import/store/core_package.rs index 1fb723da07..2357c1d7b6 100644 --- a/compiler/src/import/store/core_package.rs +++ b/compiler/src/import/store/core_package.rs @@ -21,7 +21,7 @@ use leo_core::{CorePackageList, LeoCoreError}; use snarkos_models::curves::{Field, PrimeField}; impl> ConstrainedProgram { - pub(crate) fn store_core_package(&mut self, scope: String, package: Package) -> Result<(), LeoCoreError> { + pub(crate) fn store_core_package(&mut self, scope: &str, package: Package) -> Result<(), LeoCoreError> { // Create list of imported core packages. let list = CorePackageList::from_package_access(package.access)?; @@ -29,10 +29,10 @@ impl> ConstrainedProgram { let symbol_list = list.to_symbols()?; for (symbol, circuit) in symbol_list.symbols() { - let symbol_name = new_scope(scope.clone(), symbol); + let symbol_name = new_scope(scope, symbol); // store packages - self.store(symbol_name, ConstrainedValue::CircuitDefinition(circuit)) + self.store(symbol_name, ConstrainedValue::CircuitDefinition(circuit.to_owned())) } Ok(()) diff --git a/compiler/src/import/store/import.rs b/compiler/src/import/store/import.rs index bd8f3077e6..7a4bed717e 100644 --- a/compiler/src/import/store/import.rs +++ b/compiler/src/import/store/import.rs @@ -22,7 +22,7 @@ use snarkos_models::curves::{Field, PrimeField}; impl> ConstrainedProgram { pub(crate) fn store_import( &mut self, - scope: String, + scope: &str, import: &Import, imported_programs: &ImportParser, ) -> Result<(), ImportError> { @@ -51,7 +51,7 @@ impl> ConstrainedProgram { self.store_definitions(program.clone(), imported_programs)?; // Store the imported symbol - self.store_symbol(scope.clone(), package, &symbol, program)?; + self.store_symbol(scope, &package, &symbol, program)?; } Ok(()) diff --git a/compiler/src/import/store/symbol.rs b/compiler/src/import/store/symbol.rs index bde98a52da..dfb9244cc2 100644 --- a/compiler/src/import/store/symbol.rs +++ b/compiler/src/import/store/symbol.rs @@ -22,8 +22,8 @@ use snarkos_models::curves::{Field, PrimeField}; impl> ConstrainedProgram { pub(crate) fn store_symbol( &mut self, - scope: String, - program_name: String, + scope: &str, + program_name: &str, symbol: &ImportSymbol, program: &Program, ) -> Result<(), ImportError> { @@ -31,9 +31,9 @@ impl> ConstrainedProgram { if symbol.is_star() { // evaluate and store all circuit definitions program.circuits.iter().for_each(|(identifier, circuit)| { - let name = new_scope(scope.clone(), identifier.to_string()); + let name = new_scope(scope, &identifier.name); let value = ConstrainedValue::Import( - program_name.clone(), + program_name.to_owned(), Box::new(ConstrainedValue::CircuitDefinition(circuit.clone())), ); @@ -42,9 +42,9 @@ impl> ConstrainedProgram { // evaluate and store all function definitions program.functions.iter().for_each(|(identifier, function)| { - let name = new_scope(scope.clone(), identifier.to_string()); + let name = new_scope(scope, &identifier.name); let value = ConstrainedValue::Import( - program_name.clone(), + program_name.to_owned(), Box::new(ConstrainedValue::Function(None, function.clone())), ); @@ -59,7 +59,7 @@ impl> ConstrainedProgram { let value = match matched_circuit { Some((_circuit_name, circuit)) => ConstrainedValue::Import( - program_name, + program_name.to_owned(), Box::new(ConstrainedValue::CircuitDefinition(circuit.clone())), ), None => { @@ -71,17 +71,17 @@ impl> ConstrainedProgram { match matched_function { Some((_function_name, function)) => ConstrainedValue::Import( - program_name, + program_name.to_owned(), Box::new(ConstrainedValue::Function(None, function.clone())), ), - None => return Err(ImportError::unknown_symbol(symbol.to_owned(), program_name)), + None => return Err(ImportError::unknown_symbol(symbol.to_owned(), program_name.to_owned())), } } }; // take the alias if it is present let id = symbol.alias.clone().unwrap_or_else(|| symbol.symbol.clone()); - let name = new_scope(scope, id.to_string()); + let name = new_scope(scope, &id.name); // store imported circuit under imported name self.store(name, value); diff --git a/compiler/src/program/program.rs b/compiler/src/program/program.rs index 8e87702ab8..596c59b23e 100644 --- a/compiler/src/program/program.rs +++ b/compiler/src/program/program.rs @@ -22,7 +22,6 @@ use snarkos_models::curves::{Field, PrimeField}; use std::collections::HashMap; -#[derive(Clone)] pub struct ConstrainedProgram> { pub identifiers: HashMap>, } @@ -35,7 +34,7 @@ impl> Default for ConstrainedProgram String { +pub fn new_scope(outer: &str, inner: &str) -> String { format!("{}_{}", outer, inner) } diff --git a/compiler/src/statement/assign/array.rs b/compiler/src/statement/assign/array.rs index 94d3f88668..dde5ac528b 100644 --- a/compiler/src/statement/assign/array.rs +++ b/compiler/src/statement/assign/array.rs @@ -32,25 +32,25 @@ impl> ConstrainedProgram { pub fn assign_array>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, indicator: Option, - name: String, + name: &str, range_or_expression: RangeOrExpression, mut new_value: ConstrainedValue, - span: Span, + span: &Span, ) -> Result<(), StatementError> { let condition = indicator.unwrap_or(Boolean::Constant(true)); // Resolve index so we know if we are assigning to a single value or a range of values match range_or_expression { RangeOrExpression::Expression(index) => { - let index = self.enforce_index(cs, file_scope, function_scope, index, span.clone())?; + let index = self.enforce_index(cs, file_scope, function_scope, index, span)?; // Modify the single value of the array in place - match self.get_mutable_assignee(name, span.clone())? { + match self.get_mutable_assignee(name, &span)? { ConstrainedValue::Array(old) => { - new_value.resolve_type(Some(old[index].to_type(span.clone())?), span.clone())?; + new_value.resolve_type(Some(old[index].to_type(&span)?), &span)?; let name_unique = format!("select {} {}:{}", new_value, span.line, span.start); let selected_value = ConstrainedValue::conditionally_select( @@ -60,28 +60,26 @@ impl> ConstrainedProgram { &old[index], ) .map_err(|_| { - StatementError::select_fail(new_value.to_string(), old[index].to_string(), span) + StatementError::select_fail(new_value.to_string(), old[index].to_string(), span.to_owned()) })?; old[index] = selected_value; } - _ => return Err(StatementError::array_assign_index(span)), + _ => return Err(StatementError::array_assign_index(span.to_owned())), } } RangeOrExpression::Range(from, to) => { let from_index = match from { - Some(integer) => { - self.enforce_index(cs, file_scope.clone(), function_scope.clone(), integer, span.clone())? - } + Some(integer) => self.enforce_index(cs, file_scope, function_scope, integer, span)?, None => 0usize, }; let to_index_option = match to { - Some(integer) => Some(self.enforce_index(cs, file_scope, function_scope, integer, span.clone())?), + Some(integer) => Some(self.enforce_index(cs, file_scope, function_scope, integer, span)?), None => None, }; // Modify the range of values of the array - let old_array = self.get_mutable_assignee(name, span.clone())?; + let old_array = self.get_mutable_assignee(name, &span)?; let new_array = match (old_array.clone(), new_value) { (ConstrainedValue::Array(mut mutable), ConstrainedValue::Array(new)) => { let to_index = to_index_option.unwrap_or(mutable.len()); @@ -89,12 +87,14 @@ impl> ConstrainedProgram { mutable.splice(from_index..to_index, new.iter().cloned()); ConstrainedValue::Array(mutable) } - _ => return Err(StatementError::array_assign_range(span)), + _ => return Err(StatementError::array_assign_range(span.to_owned())), }; let name_unique = format!("select {} {}:{}", new_array, span.line, span.start); let selected_array = ConstrainedValue::conditionally_select(cs.ns(|| name_unique), &condition, &new_array, old_array) - .map_err(|_| StatementError::select_fail(new_array.to_string(), old_array.to_string(), span))?; + .map_err(|_| { + StatementError::select_fail(new_array.to_string(), old_array.to_string(), span.to_owned()) + })?; *old_array = selected_array; } diff --git a/compiler/src/statement/assign/assign.rs b/compiler/src/statement/assign/assign.rs index 444a9a7c9b..7040f2fab7 100644 --- a/compiler/src/statement/assign/assign.rs +++ b/compiler/src/statement/assign/assign.rs @@ -39,33 +39,34 @@ impl> ConstrainedProgram { pub fn enforce_assign_statement>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, - declared_circuit_reference: String, + file_scope: &str, + function_scope: &str, + declared_circuit_reference: &str, indicator: Option, assignee: Assignee, expression: Expression, - span: Span, + span: &Span, ) -> Result<(), StatementError> { // Get the name of the variable we are assigning to - let variable_name = resolve_assignee(function_scope.clone(), assignee.clone()); + let variable_name = resolve_assignee(function_scope, assignee.clone()); // Evaluate new value - let mut new_value = - self.enforce_expression(cs, file_scope.clone(), function_scope.clone(), None, expression)?; + let mut new_value = self.enforce_expression(cs, file_scope, function_scope, None, expression)?; // Mutate the old value into the new value match assignee { Assignee::Identifier(_identifier) => { let condition = indicator.unwrap_or(Boolean::Constant(true)); - let old_value = self.get_mutable_assignee(variable_name, span.clone())?; + let old_value = self.get_mutable_assignee(&variable_name, &span)?; - new_value.resolve_type(Some(old_value.to_type(span.clone())?), span.clone())?; + new_value.resolve_type(Some(old_value.to_type(&span)?), &span)?; let name_unique = format!("select {} {}:{}", new_value, span.line, span.start); let selected_value = ConstrainedValue::conditionally_select(cs.ns(|| name_unique), &condition, &new_value, old_value) - .map_err(|_| StatementError::select_fail(new_value.to_string(), old_value.to_string(), span))?; + .map_err(|_| { + StatementError::select_fail(new_value.to_string(), old_value.to_string(), span.to_owned()) + })?; *old_value = selected_value; @@ -76,18 +77,18 @@ impl> ConstrainedProgram { file_scope, function_scope, indicator, - variable_name, + &variable_name, *range_or_expression, new_value, span, ), - Assignee::Tuple(_tuple, index) => self.assign_tuple(cs, indicator, variable_name, index, new_value, span), + Assignee::Tuple(_tuple, index) => self.assign_tuple(cs, indicator, &variable_name, index, new_value, span), Assignee::CircuitField(assignee, circuit_variable) => { // Mutate a circuit variable using the self keyword. if let Assignee::Identifier(circuit_name) = *assignee { if circuit_name.is_self() { - let self_circuit_variable_name = new_scope(circuit_name.name, circuit_variable.name.clone()); - let self_variable_name = new_scope(file_scope, self_circuit_variable_name); + let self_circuit_variable_name = new_scope(&circuit_name.name, &circuit_variable.name); + let self_variable_name = new_scope(file_scope, &self_circuit_variable_name); let value = self.mutate_circuit_variable( cs, indicator, @@ -102,7 +103,7 @@ impl> ConstrainedProgram { let _value = self.mutate_circuit_variable( cs, indicator, - variable_name, + &variable_name, circuit_variable, new_value, span, diff --git a/compiler/src/statement/assign/assignee.rs b/compiler/src/statement/assign/assignee.rs index 99716431ea..5c568dd7f9 100644 --- a/compiler/src/statement/assign/assignee.rs +++ b/compiler/src/statement/assign/assignee.rs @@ -21,9 +21,9 @@ use leo_typed::{Assignee, Span}; use snarkos_models::curves::{Field, PrimeField}; -pub fn resolve_assignee(scope: String, assignee: Assignee) -> String { +pub fn resolve_assignee(scope: &str, assignee: Assignee) -> String { match assignee { - Assignee::Identifier(name) => new_scope(scope, name.to_string()), + Assignee::Identifier(name) => new_scope(scope, &name.name), Assignee::Array(array, _index) => resolve_assignee(scope, *array), Assignee::Tuple(tuple, _index) => resolve_assignee(scope, *tuple), Assignee::CircuitField(circuit_name, _member) => resolve_assignee(scope, *circuit_name), @@ -33,16 +33,16 @@ pub fn resolve_assignee(scope: String, assignee: Assignee) -> String { impl> ConstrainedProgram { pub fn get_mutable_assignee( &mut self, - name: String, - span: Span, + name: &str, + span: &Span, ) -> Result<&mut ConstrainedValue, StatementError> { // Check that assignee exists and is mutable - Ok(match self.get_mut(&name) { + Ok(match self.get_mut(name) { Some(value) => match value { ConstrainedValue::Mutable(mutable_value) => mutable_value, - _ => return Err(StatementError::immutable_assign(name, span)), + _ => return Err(StatementError::immutable_assign(name.to_owned(), span.to_owned())), }, - None => return Err(StatementError::undefined_variable(name, span)), + None => return Err(StatementError::undefined_variable(name.to_owned(), span.to_owned())), }) } } diff --git a/compiler/src/statement/assign/circuit_variable.rs b/compiler/src/statement/assign/circuit_variable.rs index 908dd0f50c..099c24adf2 100644 --- a/compiler/src/statement/assign/circuit_variable.rs +++ b/compiler/src/statement/assign/circuit_variable.rs @@ -32,15 +32,15 @@ impl> ConstrainedProgram { &mut self, cs: &mut CS, indicator: Option, - circuit_name: String, + circuit_name: &str, variable_name: Identifier, mut new_value: ConstrainedValue, - span: Span, + span: &Span, ) -> Result, StatementError> { let condition = indicator.unwrap_or(Boolean::Constant(true)); // Get the mutable circuit by name - match self.get_mutable_assignee(circuit_name, span.clone())? { + match self.get_mutable_assignee(circuit_name, span)? { ConstrainedValue::CircuitExpression(_variable, members) => { // Modify the circuit variable in place let matched_variable = members.iter_mut().find(|member| member.0 == variable_name); @@ -51,18 +51,21 @@ impl> ConstrainedProgram { // Throw an error if we try to mutate a circuit function Err(StatementError::immutable_circuit_function( function.identifier.to_string(), - span, + span.to_owned(), )) } ConstrainedValue::Static(_circuit_function) => { // Throw an error if we try to mutate a static circuit function - Err(StatementError::immutable_circuit_function("static".into(), span)) + Err(StatementError::immutable_circuit_function( + "static".into(), + span.to_owned(), + )) } ConstrainedValue::Mutable(value) => { // Mutate the circuit variable's value in place // Check that the new value type == old value type - new_value.resolve_type(Some(value.to_type(span.clone())?), span.clone())?; + new_value.resolve_type(Some(value.to_type(span)?), span)?; // Conditionally select the value if this branch is executed. let name_unique = format!("select {} {}:{}", new_value, span.line, span.start); @@ -73,7 +76,11 @@ impl> ConstrainedProgram { &member.1, ) .map_err(|_| { - StatementError::select_fail(new_value.to_string(), member.1.to_string(), span) + StatementError::select_fail( + new_value.to_string(), + member.1.to_string(), + span.to_owned(), + ) })?; // Make sure the new value is still mutable @@ -85,20 +92,26 @@ impl> ConstrainedProgram { } _ => { // Throw an error if we try to mutate an immutable circuit variable - Err(StatementError::immutable_circuit_variable(variable_name.name, span)) + Err(StatementError::immutable_circuit_variable( + variable_name.name, + span.to_owned(), + )) } }, None => { // Throw an error if the circuit variable does not exist in the circuit Err(StatementError::undefined_circuit_variable( variable_name.to_string(), - span, + span.to_owned(), )) } } } // Throw an error if the circuit definition does not exist in the file - _ => Err(StatementError::undefined_circuit(variable_name.to_string(), span)), + _ => Err(StatementError::undefined_circuit( + variable_name.to_string(), + span.to_owned(), + )), } } } diff --git a/compiler/src/statement/assign/tuple.rs b/compiler/src/statement/assign/tuple.rs index a7ce8a9184..24d7d80f1f 100644 --- a/compiler/src/statement/assign/tuple.rs +++ b/compiler/src/statement/assign/tuple.rs @@ -32,28 +32,28 @@ impl> ConstrainedProgram { &mut self, cs: &mut CS, indicator: Option, - name: String, + name: &str, index: usize, mut new_value: ConstrainedValue, - span: Span, + span: &Span, ) -> Result<(), StatementError> { let condition = indicator.unwrap_or(Boolean::Constant(true)); // Modify the single value of the tuple in place - match self.get_mutable_assignee(name, span.clone())? { + match self.get_mutable_assignee(name, &span)? { ConstrainedValue::Tuple(old) => { - new_value.resolve_type(Some(old[index].to_type(span.clone())?), span.clone())?; + new_value.resolve_type(Some(old[index].to_type(&span)?), &span)?; let name_unique = format!("select {} {}:{}", new_value, span.line, span.start); let selected_value = ConstrainedValue::conditionally_select(cs.ns(|| name_unique), &condition, &new_value, &old[index]) .map_err(|_| { - StatementError::select_fail(new_value.to_string(), old[index].to_string(), span) + StatementError::select_fail(new_value.to_string(), old[index].to_string(), span.to_owned()) })?; old[index] = selected_value; } - _ => return Err(StatementError::tuple_assign_index(span)), + _ => return Err(StatementError::tuple_assign_index(span.to_owned())), } Ok(()) diff --git a/compiler/src/statement/branch/branch.rs b/compiler/src/statement/branch/branch.rs index 200a926b34..ddb6e91838 100644 --- a/compiler/src/statement/branch/branch.rs +++ b/compiler/src/statement/branch/branch.rs @@ -28,8 +28,8 @@ impl> ConstrainedProgram { pub fn evaluate_branch>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, indicator: Option, statements: Vec, return_type: Option, @@ -39,12 +39,12 @@ impl> ConstrainedProgram { for statement in statements.into_iter() { let mut value = self.enforce_statement( cs, - file_scope.clone(), - function_scope.clone(), + file_scope, + function_scope, indicator, statement, return_type.clone(), - "".to_owned(), + "", )?; results.append(&mut value); diff --git a/compiler/src/statement/conditional/conditional.rs b/compiler/src/statement/conditional/conditional.rs index cf3d7dbf5f..db83d57ffb 100644 --- a/compiler/src/statement/conditional/conditional.rs +++ b/compiler/src/statement/conditional/conditional.rs @@ -47,12 +47,12 @@ impl> ConstrainedProgram { pub fn enforce_conditional_statement>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, indicator: Option, statement: ConditionalStatement, return_type: Option, - span: Span, + span: &Span, ) -> StatementResult>> { let statement_string = statement.to_string(); @@ -62,13 +62,13 @@ impl> ConstrainedProgram { // Evaluate the conditional boolean as the inner indicator let inner_indicator = match self.enforce_expression( cs, - file_scope.clone(), - function_scope.clone(), + file_scope, + function_scope, Some(Type::Boolean), statement.condition.clone(), )? { ConstrainedValue::Boolean(resolved) => resolved, - value => return Err(StatementError::conditional_boolean(value.to_string(), span)), + value => return Err(StatementError::conditional_boolean(value.to_string(), span.to_owned())), }; // If outer_indicator && inner_indicator, then select branch 1 @@ -83,15 +83,15 @@ impl> ConstrainedProgram { &outer_indicator, &inner_indicator, ) - .map_err(|_| StatementError::indicator_calculation(branch_1_name, span.clone()))?; + .map_err(|_| StatementError::indicator_calculation(branch_1_name, span.to_owned()))?; let mut results = vec![]; // Evaluate branch 1 let mut branch_1_result = self.evaluate_branch( cs, - file_scope.clone(), - function_scope.clone(), + file_scope, + function_scope, Some(branch_1_indicator), statement.statements, return_type.clone(), @@ -111,7 +111,7 @@ impl> ConstrainedProgram { &outer_indicator, &inner_indicator, ) - .map_err(|_| StatementError::indicator_calculation(branch_2_name, span.clone()))?; + .map_err(|_| StatementError::indicator_calculation(branch_2_name, span.to_owned()))?; // Evaluate branch 2 let mut branch_2_result = match statement.next { diff --git a/compiler/src/statement/definition/definition.rs b/compiler/src/statement/definition/definition.rs index b41420f057..994b9ff6a8 100644 --- a/compiler/src/statement/definition/definition.rs +++ b/compiler/src/statement/definition/definition.rs @@ -28,14 +28,17 @@ impl> ConstrainedProgram { fn enforce_single_definition>( &mut self, cs: &mut CS, - function_scope: String, + function_scope: &str, is_constant: bool, variable_name: VariableName, mut value: ConstrainedValue, - span: Span, + span: &Span, ) -> Result<(), StatementError> { if is_constant && variable_name.mutable { - return Err(StatementError::immutable_assign(variable_name.to_string(), span)); + return Err(StatementError::immutable_assign( + variable_name.to_string(), + span.to_owned(), + )); } else { value.allocate_value(cs, span)? } @@ -48,15 +51,15 @@ impl> ConstrainedProgram { fn enforce_expressions>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, type_: Option, expressions: Vec, - span: Span, + span: &Span, ) -> Result>, StatementError> { let types = match type_ { Some(Type::Tuple(types)) => types, - Some(type_) => return Err(StatementError::tuple_type(type_.to_string(), span)), + Some(type_) => return Err(StatementError::tuple_type(type_.to_string(), span.to_owned())), None => vec![], }; @@ -72,13 +75,7 @@ impl> ConstrainedProgram { let mut values = Vec::with_capacity(expressions.len()); for (expression, expected_type) in expressions.into_iter().zip(expected_types.into_iter()) { - let value = self.enforce_expression( - cs, - file_scope.clone(), - function_scope.clone(), - expected_type, - expression, - )?; + let value = self.enforce_expression(cs, file_scope, function_scope, expected_type, expression)?; values.push(value); } @@ -90,20 +87,20 @@ impl> ConstrainedProgram { fn enforce_tuple_definition>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, is_constant: bool, variables: Variables, expressions: Vec, - span: Span, + span: &Span, ) -> Result<(), StatementError> { let values = self.enforce_expressions( cs, file_scope, - function_scope.clone(), + function_scope, variables.type_.clone(), expressions, - span.clone(), + span, )?; let tuple = ConstrainedValue::Tuple(values); @@ -115,22 +112,22 @@ impl> ConstrainedProgram { fn enforce_multiple_definition>( &mut self, cs: &mut CS, - function_scope: String, + function_scope: &str, is_constant: bool, variables: Variables, values: Vec>, - span: Span, + span: &Span, ) -> Result<(), StatementError> { if values.len() != variables.names.len() { return Err(StatementError::invalid_number_of_definitions( values.len(), variables.names.len(), - span, + span.to_owned(), )); } for (variable, value) in variables.names.into_iter().zip(values.into_iter()) { - self.enforce_single_definition(cs, function_scope.clone(), is_constant, variable, value, span.clone())?; + self.enforce_single_definition(cs, function_scope, is_constant, variable, value, span)?; } Ok(()) @@ -140,12 +137,12 @@ impl> ConstrainedProgram { pub fn enforce_definition_statement>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, declare: Declare, variables: Variables, expressions: Vec, - span: Span, + span: &Span, ) -> Result<(), StatementError> { let num_variables = variables.names.len(); let num_values = expressions.len(); @@ -157,13 +154,8 @@ impl> ConstrainedProgram { if num_variables == 1 && num_values == 1 { // Define a single variable with a single value let variable = variables.names[0].clone(); - let expression = self.enforce_expression( - cs, - file_scope, - function_scope.clone(), - variables.type_, - expressions[0].clone(), - )?; + let expression = + self.enforce_expression(cs, file_scope, function_scope, variables.type_, expressions[0].clone())?; self.enforce_single_definition(cs, function_scope, is_constant, variable, expression, span) } else if num_variables == 1 && num_values > 1 { @@ -184,13 +176,13 @@ impl> ConstrainedProgram { let values = match self.enforce_expression( cs, file_scope, - function_scope.clone(), + function_scope, variables.type_.clone(), expressions[0].clone(), )? { // ConstrainedValue::Return(values) => values, ConstrainedValue::Tuple(values) => values, - value => return Err(StatementError::multiple_definition(value.to_string(), span)), + value => return Err(StatementError::multiple_definition(value.to_string(), span.to_owned())), }; self.enforce_multiple_definition(cs, function_scope, is_constant, variables, values, span) @@ -199,10 +191,10 @@ impl> ConstrainedProgram { let values = self.enforce_expressions( cs, file_scope, - function_scope.clone(), + function_scope, variables.type_.clone(), expressions, - span.clone(), + span, )?; self.enforce_multiple_definition(cs, function_scope, is_constant, variables, values, span) diff --git a/compiler/src/statement/iteration/iteration.rs b/compiler/src/statement/iteration/iteration.rs index 7503f7571c..9bc96ca4be 100644 --- a/compiler/src/statement/iteration/iteration.rs +++ b/compiler/src/statement/iteration/iteration.rs @@ -40,26 +40,26 @@ impl> ConstrainedProgram { pub fn enforce_iteration_statement>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, indicator: Option, index: Identifier, start: Expression, stop: Expression, statements: Vec, return_type: Option, - span: Span, + span: &Span, ) -> StatementResult>> { let mut results = vec![]; - let from = self.enforce_index(cs, file_scope.clone(), function_scope.clone(), start, span.clone())?; - let to = self.enforce_index(cs, file_scope.clone(), function_scope.clone(), stop, span.clone())?; + let from = self.enforce_index(cs, file_scope, function_scope, start, span)?; + let to = self.enforce_index(cs, file_scope, function_scope, stop, span)?; for i in from..to { // Store index in current function scope. // For loop scope is not implemented. - let index_name = new_scope(function_scope.clone(), index.to_string()); + let index_name = new_scope(function_scope, &index.name); self.store( index_name, @@ -70,8 +70,8 @@ impl> ConstrainedProgram { let name_unique = format!("for loop iteration {} {}:{}", i, span.line, span.start); let mut result = self.evaluate_branch( &mut cs.ns(|| name_unique), - file_scope.clone(), - function_scope.clone(), + file_scope, + function_scope, indicator, statements.clone(), return_type.clone(), diff --git a/compiler/src/statement/return_/return_.rs b/compiler/src/statement/return_/return_.rs index 56217532b2..4c1cb083c6 100644 --- a/compiler/src/statement/return_/return_.rs +++ b/compiler/src/statement/return_/return_.rs @@ -24,14 +24,14 @@ use snarkos_models::{ gadgets::r1cs::ConstraintSystem, }; -fn check_return_type(expected: Option, actual: Type, span: Span) -> Result<(), StatementError> { +fn check_return_type(expected: Option, actual: Type, span: &Span) -> Result<(), StatementError> { match expected { Some(expected) => { if expected.ne(&actual) { if (expected.is_self() && actual.is_circuit()) || expected.match_array_types(&actual) { return Ok(()); } else { - return Err(StatementError::arguments_type(&expected, &actual, span)); + return Err(StatementError::arguments_type(&expected, &actual, span.to_owned())); } } Ok(()) @@ -44,24 +44,17 @@ impl> ConstrainedProgram { pub fn enforce_return_statement>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, expression: Expression, return_type: Option, - span: Span, + span: &Span, ) -> Result, StatementError> { // Make sure we return the correct number of values - let result = self.enforce_operand( - cs, - file_scope, - function_scope, - return_type.clone(), - expression, - span.clone(), - )?; + let result = self.enforce_operand(cs, file_scope, function_scope, return_type.clone(), expression, span)?; - check_return_type(return_type, result.to_type(span.clone())?, span)?; + check_return_type(return_type, result.to_type(&span)?, span)?; Ok(result) } diff --git a/compiler/src/statement/statement.rs b/compiler/src/statement/statement.rs index f3e66035aa..b636639380 100644 --- a/compiler/src/statement/statement.rs +++ b/compiler/src/statement/statement.rs @@ -37,12 +37,12 @@ impl> ConstrainedProgram { pub fn enforce_statement>( &mut self, cs: &mut CS, - file_scope: String, - function_scope: String, + file_scope: &str, + function_scope: &str, indicator: Option, statement: Statement, return_type: Option, - declared_circuit_reference: String, + declared_circuit_reference: &str, ) -> StatementResult>> { let mut results = vec![]; @@ -50,7 +50,7 @@ impl> ConstrainedProgram { Statement::Return(expression, span) => { let return_value = ( indicator, - self.enforce_return_statement(cs, file_scope, function_scope, expression, return_type, span)?, + self.enforce_return_statement(cs, file_scope, function_scope, expression, return_type, &span)?, ); results.push(return_value); @@ -63,7 +63,7 @@ impl> ConstrainedProgram { declare, variables, expressions, - span, + &span, )?; } Statement::Assign(variable, expression, span) => { @@ -75,7 +75,7 @@ impl> ConstrainedProgram { indicator, variable, *expression, - span, + &span, )?; } Statement::Conditional(statement, span) => { @@ -86,7 +86,7 @@ impl> ConstrainedProgram { indicator, statement, return_type, - span, + &span, )?; results.append(&mut result); @@ -102,7 +102,7 @@ impl> ConstrainedProgram { *stop, statements, return_type, - span, + &span, )?; results.append(&mut result); diff --git a/compiler/src/value/address/address.rs b/compiler/src/value/address/address.rs index 614e5adae4..b98803d238 100644 --- a/compiler/src/value/address/address.rs +++ b/compiler/src/value/address/address.rs @@ -44,8 +44,9 @@ pub struct Address { } impl Address { - pub(crate) fn constant(address: String, span: Span) -> Result { - let address = AccountAddress::from_str(&address).map_err(|error| AddressError::account_error(error, span))?; + pub(crate) fn constant(address: String, span: &Span) -> Result { + let address = + AccountAddress::from_str(&address).map_err(|error| AddressError::account_error(error, span.to_owned()))?; let mut address_bytes = vec![]; address.write(&mut address_bytes).unwrap(); @@ -70,9 +71,9 @@ impl Address { pub(crate) fn from_input, CS: ConstraintSystem>( cs: &mut CS, - name: String, + name: &str, input_value: Option, - span: Span, + span: &Span, ) -> Result, AddressError> { // Check that the input value is the correct type let address_value = match input_value { @@ -80,7 +81,7 @@ impl Address { if let InputValue::Address(string) = input { Some(string) } else { - return Err(AddressError::invalid_address(name, span)); + return Err(AddressError::invalid_address(name.to_owned(), span.to_owned())); } } None => None, @@ -92,7 +93,7 @@ impl Address { let address = Address::alloc(cs.ns(|| address_namespace), || { address_value.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| AddressError::missing_address(span))?; + .map_err(|_| AddressError::missing_address(span.to_owned()))?; Ok(ConstrainedValue::Address(address)) } diff --git a/compiler/src/value/boolean/input.rs b/compiler/src/value/boolean/input.rs index 6a42697b23..c627e96bcb 100644 --- a/compiler/src/value/boolean/input.rs +++ b/compiler/src/value/boolean/input.rs @@ -28,19 +28,19 @@ use snarkos_models::{ }, }; -pub(crate) fn new_bool_constant(string: String, span: Span) -> Result { +pub(crate) fn new_bool_constant(string: String, span: &Span) -> Result { let boolean = string .parse::() - .map_err(|_| BooleanError::invalid_boolean(string, span))?; + .map_err(|_| BooleanError::invalid_boolean(string, span.to_owned()))?; Ok(Boolean::constant(boolean)) } pub(crate) fn allocate_bool>( cs: &mut CS, - name: String, + name: &str, option: Option, - span: Span, + span: &Span, ) -> Result { let boolean_name = format!("{}: bool", name); let boolean_name_unique = format!("`{}` {}:{}", boolean_name, span.line, span.start); @@ -48,14 +48,14 @@ pub(crate) fn allocate_bool>( Boolean::alloc(cs.ns(|| boolean_name_unique), || { option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| BooleanError::missing_boolean(boolean_name, span)) + .map_err(|_| BooleanError::missing_boolean(boolean_name.to_owned(), span.to_owned())) } pub(crate) fn bool_from_input, CS: ConstraintSystem>( cs: &mut CS, - name: String, + name: &str, input_value: Option, - span: Span, + span: &Span, ) -> Result, BooleanError> { // Check that the input value is the correct type let option = match input_value { @@ -63,7 +63,7 @@ pub(crate) fn bool_from_input, CS: Constr if let InputValue::Boolean(bool) = input { Some(bool) } else { - return Err(BooleanError::invalid_boolean(name, span)); + return Err(BooleanError::invalid_boolean(name.to_owned(), span.to_owned())); } } None => None, diff --git a/compiler/src/value/field/field_type.rs b/compiler/src/value/field/field_type.rs index 95e97145e5..c22bed7600 100644 --- a/compiler/src/value/field/field_type.rs +++ b/compiler/src/value/field/field_type.rs @@ -53,24 +53,26 @@ impl FieldType { } } - pub fn constant(string: String, span: Span) -> Result { - let value = F::from_str(&string).map_err(|_| FieldError::invalid_field(string, span))?; + pub fn constant(string: String, span: &Span) -> Result { + let value = F::from_str(&string).map_err(|_| FieldError::invalid_field(string, span.to_owned()))?; Ok(FieldType::Constant(value)) } - pub fn negate>(&self, cs: CS, span: Span) -> Result { + pub fn negate>(&self, cs: CS, span: &Span) -> Result { match self { FieldType::Constant(field) => Ok(FieldType::Constant(field.neg())), FieldType::Allocated(field) => { - let result = field.negate(cs).map_err(|e| FieldError::negate_operation(e, span))?; + let result = field + .negate(cs) + .map_err(|e| FieldError::negate_operation(e, span.to_owned()))?; Ok(FieldType::Allocated(result)) } } } - pub fn add>(&self, cs: CS, other: &Self, span: Span) -> Result { + pub fn add>(&self, cs: CS, other: &Self, span: &Span) -> Result { match (self, other) { (FieldType::Constant(self_value), FieldType::Constant(other_value)) => { Ok(FieldType::Constant(self_value.add(other_value))) @@ -79,7 +81,7 @@ impl FieldType { (FieldType::Allocated(self_value), FieldType::Allocated(other_value)) => { let result = self_value .add(cs, other_value) - .map_err(|e| FieldError::binary_operation("+".to_string(), e, span))?; + .map_err(|e| FieldError::binary_operation("+".to_string(), e, span.to_owned()))?; Ok(FieldType::Allocated(result)) } @@ -88,12 +90,12 @@ impl FieldType { | (FieldType::Allocated(allocated_value), FieldType::Constant(constant_value)) => Ok(FieldType::Allocated( allocated_value .add_constant(cs, constant_value) - .map_err(|e| FieldError::binary_operation("+".to_string(), e, span))?, + .map_err(|e| FieldError::binary_operation("+".to_string(), e, span.to_owned()))?, )), } } - pub fn sub>(&self, cs: CS, other: &Self, span: Span) -> Result { + pub fn sub>(&self, cs: CS, other: &Self, span: &Span) -> Result { match (self, other) { (FieldType::Constant(self_value), FieldType::Constant(other_value)) => { Ok(FieldType::Constant(self_value.sub(other_value))) @@ -102,7 +104,7 @@ impl FieldType { (FieldType::Allocated(self_value), FieldType::Allocated(other_value)) => { let result = self_value .sub(cs, other_value) - .map_err(|e| FieldError::binary_operation("-".to_string(), e, span))?; + .map_err(|e| FieldError::binary_operation("-".to_string(), e, span.to_owned()))?; Ok(FieldType::Allocated(result)) } @@ -111,12 +113,12 @@ impl FieldType { | (FieldType::Allocated(allocated_value), FieldType::Constant(constant_value)) => Ok(FieldType::Allocated( allocated_value .sub_constant(cs, constant_value) - .map_err(|e| FieldError::binary_operation("+".to_string(), e, span))?, + .map_err(|e| FieldError::binary_operation("+".to_string(), e, span.to_owned()))?, )), } } - pub fn mul>(&self, cs: CS, other: &Self, span: Span) -> Result { + pub fn mul>(&self, cs: CS, other: &Self, span: &Span) -> Result { match (self, other) { (FieldType::Constant(self_value), FieldType::Constant(other_value)) => { Ok(FieldType::Constant(self_value.mul(other_value))) @@ -125,7 +127,7 @@ impl FieldType { (FieldType::Allocated(self_value), FieldType::Allocated(other_value)) => { let result = self_value .mul(cs, other_value) - .map_err(|e| FieldError::binary_operation("*".to_string(), e, span))?; + .map_err(|e| FieldError::binary_operation("*".to_string(), e, span.to_owned()))?; Ok(FieldType::Allocated(result)) } @@ -134,24 +136,24 @@ impl FieldType { | (FieldType::Allocated(allocated_value), FieldType::Constant(constant_value)) => Ok(FieldType::Allocated( allocated_value .mul_by_constant(cs, constant_value) - .map_err(|e| FieldError::binary_operation("*".to_string(), e, span))?, + .map_err(|e| FieldError::binary_operation("*".to_string(), e, span.to_owned()))?, )), } } - pub fn div>(&self, mut cs: CS, other: &Self, span: Span) -> Result { + pub fn div>(&self, mut cs: CS, other: &Self, span: &Span) -> Result { let inverse = match other { FieldType::Constant(constant) => { let constant_inverse = constant .inverse() - .ok_or_else(|| FieldError::no_inverse(constant.to_string(), span.clone()))?; + .ok_or_else(|| FieldError::no_inverse(constant.to_string(), span.to_owned()))?; FieldType::Constant(constant_inverse) } FieldType::Allocated(allocated) => { let allocated_inverse = allocated .inverse(&mut cs) - .map_err(|e| FieldError::binary_operation("+".to_string(), e, span.clone()))?; + .map_err(|e| FieldError::binary_operation("+".to_string(), e, span.to_owned()))?; FieldType::Allocated(allocated_inverse) } diff --git a/compiler/src/value/field/input.rs b/compiler/src/value/field/input.rs index a4f8ba9a87..ae1f092669 100644 --- a/compiler/src/value/field/input.rs +++ b/compiler/src/value/field/input.rs @@ -27,9 +27,9 @@ use snarkos_models::{ pub(crate) fn allocate_field>( cs: &mut CS, - name: String, + name: &str, option: Option, - span: Span, + span: &Span, ) -> Result, FieldError> { let field_name = format!("{}: field", name); let field_name_unique = format!("`{}` {}:{}", field_name, span.line, span.start); @@ -37,14 +37,14 @@ pub(crate) fn allocate_field>( FieldType::alloc(cs.ns(|| field_name_unique), || { option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| FieldError::missing_field(field_name, span)) + .map_err(|_| FieldError::missing_field(field_name, span.to_owned())) } pub(crate) fn field_from_input, CS: ConstraintSystem>( cs: &mut CS, - name: String, + name: &str, input_value: Option, - span: Span, + span: &Span, ) -> Result, FieldError> { // Check that the parameter value is the correct type let option = match input_value { @@ -52,7 +52,7 @@ pub(crate) fn field_from_input, CS: Const if let InputValue::Field(string) = input { Some(string) } else { - return Err(FieldError::invalid_field(input.to_string(), span)); + return Err(FieldError::invalid_field(input.to_string(), span.to_owned())); } } None => None, diff --git a/compiler/src/value/group/group_type.rs b/compiler/src/value/group/group_type.rs index 2cde923985..3dc2a0250d 100644 --- a/compiler/src/value/group/group_type.rs +++ b/compiler/src/value/group/group_type.rs @@ -50,11 +50,11 @@ pub trait GroupType: { fn constant(value: GroupValue) -> Result; - fn to_allocated>(&self, cs: CS, span: Span) -> Result; + fn to_allocated>(&self, cs: CS, span: &Span) -> Result; - fn negate>(&self, cs: CS, span: Span) -> Result; + fn negate>(&self, cs: CS, span: &Span) -> Result; - fn add>(&self, cs: CS, other: &Self, span: Span) -> Result; + fn add>(&self, cs: CS, other: &Self, span: &Span) -> Result; - fn sub>(&self, cs: CS, other: &Self, span: Span) -> Result; + fn sub>(&self, cs: CS, other: &Self, span: &Span) -> Result; } diff --git a/compiler/src/value/group/input.rs b/compiler/src/value/group/input.rs index d85b64e302..1fa9492463 100644 --- a/compiler/src/value/group/input.rs +++ b/compiler/src/value/group/input.rs @@ -27,9 +27,9 @@ use snarkos_models::{ pub(crate) fn allocate_group, CS: ConstraintSystem>( cs: &mut CS, - name: String, + name: &str, option: Option, - span: Span, + span: &Span, ) -> Result { let group_name = format!("{}: group", name); let group_name_unique = format!("`{}` {}:{}", group_name, span.line, span.start); @@ -37,14 +37,14 @@ pub(crate) fn allocate_group, CS: Constra G::alloc(cs.ns(|| group_name_unique), || { option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| GroupError::missing_group(group_name, span)) + .map_err(|_| GroupError::missing_group(group_name, span.to_owned())) } pub(crate) fn group_from_input, CS: ConstraintSystem>( cs: &mut CS, - name: String, + name: &str, input_value: Option, - span: Span, + span: &Span, ) -> Result, GroupError> { // Check that the parameter value is the correct type let option = match input_value { @@ -52,7 +52,7 @@ pub(crate) fn group_from_input, CS: Const if let InputValue::Group(string) = input { Some(string) } else { - return Err(GroupError::invalid_group(input.to_string(), span)); + return Err(GroupError::invalid_group(input.to_string(), span.to_owned())); } } None => None, diff --git a/compiler/src/value/group/targets/edwards_bls12.rs b/compiler/src/value/group/targets/edwards_bls12.rs index 2e45baf4d1..8c06509361 100644 --- a/compiler/src/value/group/targets/edwards_bls12.rs +++ b/compiler/src/value/group/targets/edwards_bls12.rs @@ -58,25 +58,25 @@ impl GroupType for EdwardsGroupType { Ok(EdwardsGroupType::Constant(value)) } - fn to_allocated>(&self, mut cs: CS, span: Span) -> Result { + fn to_allocated>(&self, mut cs: CS, span: &Span) -> Result { self.allocated(cs.ns(|| format!("allocate affine point {}:{}", span.line, span.start))) .map(|ebg| EdwardsGroupType::Allocated(Box::new(ebg))) - .map_err(|error| GroupError::synthesis_error(error, span)) + .map_err(|error| GroupError::synthesis_error(error, span.to_owned())) } - fn negate>(&self, cs: CS, span: Span) -> Result { + fn negate>(&self, cs: CS, span: &Span) -> Result { match self { EdwardsGroupType::Constant(group) => Ok(EdwardsGroupType::Constant(group.neg())), EdwardsGroupType::Allocated(group) => { let result = , Fq>>::negate(group, cs) - .map_err(|e| GroupError::negate_operation(e, span))?; + .map_err(|e| GroupError::negate_operation(e, span.to_owned()))?; Ok(EdwardsGroupType::Allocated(Box::new(result))) } } } - fn add>(&self, cs: CS, other: &Self, span: Span) -> Result { + fn add>(&self, cs: CS, other: &Self, span: &Span) -> Result { match (self, other) { (EdwardsGroupType::Constant(self_value), EdwardsGroupType::Constant(other_value)) => { Ok(EdwardsGroupType::Constant(self_value.add(other_value))) @@ -88,7 +88,7 @@ impl GroupType for EdwardsGroupType { cs, other_value, ) - .map_err(|e| GroupError::binary_operation("+".to_string(), e, span))?; + .map_err(|e| GroupError::binary_operation("+".to_string(), e, span.to_owned()))?; Ok(EdwardsGroupType::Allocated(Box::new(result))) } @@ -98,13 +98,13 @@ impl GroupType for EdwardsGroupType { Ok(EdwardsGroupType::Allocated(Box::new( allocated_value .add_constant(cs, constant_value) - .map_err(|e| GroupError::binary_operation("+".to_string(), e, span))?, + .map_err(|e| GroupError::binary_operation("+".to_string(), e, span.to_owned()))?, ))) } } } - fn sub>(&self, cs: CS, other: &Self, span: Span) -> Result { + fn sub>(&self, cs: CS, other: &Self, span: &Span) -> Result { match (self, other) { (EdwardsGroupType::Constant(self_value), EdwardsGroupType::Constant(other_value)) => { Ok(EdwardsGroupType::Constant(self_value.sub(other_value))) @@ -116,7 +116,7 @@ impl GroupType for EdwardsGroupType { cs, other_value, ) - .map_err(|e| GroupError::binary_operation("-".to_string(), e, span))?; + .map_err(|e| GroupError::binary_operation("-".to_string(), e, span.to_owned()))?; Ok(EdwardsGroupType::Allocated(Box::new(result))) } @@ -126,7 +126,7 @@ impl GroupType for EdwardsGroupType { Ok(EdwardsGroupType::Allocated(Box::new( allocated_value .sub_constant(cs, constant_value) - .map_err(|e| GroupError::binary_operation("-".to_string(), e, span))?, + .map_err(|e| GroupError::binary_operation("-".to_string(), e, span.to_owned()))?, ))) } } diff --git a/compiler/src/value/implicit/implicit.rs b/compiler/src/value/implicit/implicit.rs index 8256471832..27b6cb76c6 100644 --- a/compiler/src/value/implicit/implicit.rs +++ b/compiler/src/value/implicit/implicit.rs @@ -24,7 +24,7 @@ use snarkos_models::curves::{Field, PrimeField}; pub fn enforce_number_implicit>( expected_type: Option, value: String, - span: Span, + span: &Span, ) -> Result, ValueError> { match expected_type { Some(type_) => Ok(ConstrainedValue::from_type(value, &type_, span)?), diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index ef47e00cc5..3d79ac8bdf 100644 --- a/compiler/src/value/integer/integer.rs +++ b/compiler/src/value/integer/integer.rs @@ -67,40 +67,40 @@ impl fmt::Display for Integer { } impl Integer { - pub fn new_constant(integer_type: &IntegerType, string: String, span: Span) -> Result { + pub fn new_constant(integer_type: &IntegerType, string: String, span: &Span) -> Result { match integer_type { IntegerType::U8 => { let number = string .parse::() - .map_err(|_| IntegerError::invalid_integer(string, span))?; + .map_err(|_| IntegerError::invalid_integer(string, span.to_owned()))?; Ok(Integer::U8(UInt8::constant(number))) } IntegerType::U16 => { let number = string .parse::() - .map_err(|_| IntegerError::invalid_integer(string, span))?; + .map_err(|_| IntegerError::invalid_integer(string, span.to_owned()))?; Ok(Integer::U16(UInt16::constant(number))) } IntegerType::U32 => { let number = string .parse::() - .map_err(|_| IntegerError::invalid_integer(string, span))?; + .map_err(|_| IntegerError::invalid_integer(string, span.to_owned()))?; Ok(Integer::U32(UInt32::constant(number))) } IntegerType::U64 => { let number = string .parse::() - .map_err(|_| IntegerError::invalid_integer(string, span))?; + .map_err(|_| IntegerError::invalid_integer(string, span.to_owned()))?; Ok(Integer::U64(UInt64::constant(number))) } IntegerType::U128 => { let number = string .parse::() - .map_err(|_| IntegerError::invalid_integer(string, span))?; + .map_err(|_| IntegerError::invalid_integer(string, span.to_owned()))?; Ok(Integer::U128(UInt128::constant(number))) } @@ -108,35 +108,35 @@ impl Integer { IntegerType::I8 => { let number = string .parse::() - .map_err(|_| IntegerError::invalid_integer(string, span))?; + .map_err(|_| IntegerError::invalid_integer(string, span.to_owned()))?; Ok(Integer::I8(Int8::constant(number))) } IntegerType::I16 => { let number = string .parse::() - .map_err(|_| IntegerError::invalid_integer(string, span))?; + .map_err(|_| IntegerError::invalid_integer(string, span.to_owned()))?; Ok(Integer::I16(Int16::constant(number))) } IntegerType::I32 => { let number = string .parse::() - .map_err(|_| IntegerError::invalid_integer(string, span))?; + .map_err(|_| IntegerError::invalid_integer(string, span.to_owned()))?; Ok(Integer::I32(Int32::constant(number))) } IntegerType::I64 => { let number = string .parse::() - .map_err(|_| IntegerError::invalid_integer(string, span))?; + .map_err(|_| IntegerError::invalid_integer(string, span.to_owned()))?; Ok(Integer::I64(Int64::constant(number))) } IntegerType::I128 => { let number = string .parse::() - .map_err(|_| IntegerError::invalid_integer(string, span))?; + .map_err(|_| IntegerError::invalid_integer(string, span.to_owned()))?; Ok(Integer::I128(Int128::constant(number))) } @@ -153,14 +153,14 @@ impl Integer { match_integer!(integer => integer.get_value()) } - pub fn to_usize(&self, span: Span) -> Result { + pub fn to_usize(&self, span: &Span) -> Result { let unsigned_integer = self; let value_option: Option = match_unsigned_integer!(unsigned_integer => unsigned_integer.get_value()); - let value = value_option.ok_or_else(|| IntegerError::invalid_index(span.clone()))?; + let value = value_option.ok_or_else(|| IntegerError::invalid_index(span.to_owned()))?; let value_usize = value .parse::() - .map_err(|_| IntegerError::invalid_integer(value, span))?; + .map_err(|_| IntegerError::invalid_integer(value, span.to_owned()))?; Ok(value_usize) } @@ -183,9 +183,9 @@ impl Integer { pub fn allocate_type>( cs: &mut CS, integer_type: IntegerType, - name: String, + name: &str, option: Option, - span: Span, + span: &Span, ) -> Result { Ok(match integer_type { IntegerType::U8 => { @@ -194,14 +194,14 @@ impl Integer { let u8_option = option.map(|s| { s.parse::() - .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .map_err(|_| IntegerError::invalid_integer(s, span.to_owned())) .unwrap() }); let u8_result = UInt8::alloc(cs.ns(|| u8_name_unique), || { u8_option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| IntegerError::missing_integer(u8_name, span))?; + .map_err(|_| IntegerError::missing_integer(u8_name, span.to_owned()))?; Integer::U8(u8_result) } @@ -210,13 +210,13 @@ impl Integer { let u16_name_unique = format!("`{}` {}:{}", u16_name, span.line, span.start); let u16_option = option.map(|s| { s.parse::() - .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .map_err(|_| IntegerError::invalid_integer(s, span.to_owned())) .unwrap() }); let u16_result = UInt16::alloc(cs.ns(|| u16_name_unique), || { u16_option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| IntegerError::missing_integer(u16_name, span))?; + .map_err(|_| IntegerError::missing_integer(u16_name, span.to_owned()))?; Integer::U16(u16_result) } @@ -225,13 +225,13 @@ impl Integer { let u32_name_unique = format!("`{}` {}:{}", u32_name, span.line, span.start); let u32_option = option.map(|s| { s.parse::() - .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .map_err(|_| IntegerError::invalid_integer(s, span.to_owned())) .unwrap() }); let u32_result = UInt32::alloc(cs.ns(|| u32_name_unique), || { u32_option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| IntegerError::missing_integer(u32_name, span))?; + .map_err(|_| IntegerError::missing_integer(u32_name, span.to_owned()))?; Integer::U32(u32_result) } @@ -240,13 +240,13 @@ impl Integer { let u64_name_unique = format!("`{}` {}:{}", u64_name, span.line, span.start); let u64_option = option.map(|s| { s.parse::() - .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .map_err(|_| IntegerError::invalid_integer(s, span.to_owned())) .unwrap() }); let u64_result = UInt64::alloc(cs.ns(|| u64_name_unique), || { u64_option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| IntegerError::missing_integer(u64_name, span))?; + .map_err(|_| IntegerError::missing_integer(u64_name, span.to_owned()))?; Integer::U64(u64_result) } @@ -255,13 +255,13 @@ impl Integer { let u128_name_unique = format!("`{}` {}:{}", u128_name, span.line, span.start); let u128_option = option.map(|s| { s.parse::() - .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .map_err(|_| IntegerError::invalid_integer(s, span.to_owned())) .unwrap() }); let u128_result = UInt128::alloc(cs.ns(|| u128_name_unique), || { u128_option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| IntegerError::missing_integer(u128_name, span))?; + .map_err(|_| IntegerError::missing_integer(u128_name, span.to_owned()))?; Integer::U128(u128_result) } @@ -271,13 +271,13 @@ impl Integer { let i8_name_unique = format!("`{}` {}:{}", i8_name, span.line, span.start); let i8_option = option.map(|s| { s.parse::() - .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .map_err(|_| IntegerError::invalid_integer(s, span.to_owned())) .unwrap() }); let i8_result = Int8::alloc(cs.ns(|| i8_name_unique), || { i8_option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| IntegerError::missing_integer(i8_name, span))?; + .map_err(|_| IntegerError::missing_integer(i8_name, span.to_owned()))?; Integer::I8(i8_result) } @@ -286,13 +286,13 @@ impl Integer { let i16_name_unique = format!("`{}` {}:{}", i16_name, span.line, span.start); let i16_option = option.map(|s| { s.parse::() - .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .map_err(|_| IntegerError::invalid_integer(s, span.to_owned())) .unwrap() }); let i16_result = Int16::alloc(cs.ns(|| i16_name_unique), || { i16_option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| IntegerError::missing_integer(i16_name, span))?; + .map_err(|_| IntegerError::missing_integer(i16_name, span.to_owned()))?; Integer::I16(i16_result) } @@ -301,13 +301,13 @@ impl Integer { let i32_name_unique = format!("`{}` {}:{}", i32_name, span.line, span.start); let i32_option = option.map(|s| { s.parse::() - .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .map_err(|_| IntegerError::invalid_integer(s, span.to_owned())) .unwrap() }); let i32_result = Int32::alloc(cs.ns(|| i32_name_unique), || { i32_option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| IntegerError::missing_integer(i32_name, span))?; + .map_err(|_| IntegerError::missing_integer(i32_name, span.to_owned()))?; Integer::I32(i32_result) } @@ -316,13 +316,13 @@ impl Integer { let i64_name_unique = format!("`{}` {}:{}", i64_name, span.line, span.start); let i64_option = option.map(|s| { s.parse::() - .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .map_err(|_| IntegerError::invalid_integer(s, span.to_owned())) .unwrap() }); let i64_result = Int64::alloc(cs.ns(|| i64_name_unique), || { i64_option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| IntegerError::missing_integer(i64_name, span))?; + .map_err(|_| IntegerError::missing_integer(i64_name, span.to_owned()))?; Integer::I64(i64_result) } @@ -331,13 +331,13 @@ impl Integer { let i128_name_unique = format!("`{}` {}:{}", i128_name, span.line, span.start); let i128_option = option.map(|s| { s.parse::() - .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .map_err(|_| IntegerError::invalid_integer(s, span.to_owned())) .unwrap() }); let i128_result = Int128::alloc(cs.ns(|| i128_name_unique), || { i128_option.ok_or(SynthesisError::AssignmentMissing) }) - .map_err(|_| IntegerError::missing_integer(i128_name, span))?; + .map_err(|_| IntegerError::missing_integer(i128_name, span.to_owned()))?; Integer::I128(i128_result) } @@ -347,9 +347,9 @@ impl Integer { pub fn from_input>( cs: &mut CS, integer_type: IntegerType, - name: String, + name: &str, integer_value: Option, - span: Span, + span: &Span, ) -> Result { // Check that the input value is the correct type let option = match integer_value { @@ -357,7 +357,7 @@ impl Integer { if let InputValue::Integer(_type_, number) = input { Some(number) } else { - return Err(IntegerError::invalid_integer(input.to_string(), span)); + return Err(IntegerError::invalid_integer(input.to_string(), span.to_owned())); } } None => None, @@ -369,101 +369,95 @@ impl Integer { pub fn negate>( self, cs: &mut CS, - span: Span, + span: &Span, ) -> Result { let unique_namespace = format!("enforce -{} {}:{}", self, span.line, span.start); let a = self; - let s = span.clone(); - let result = match_signed_integer!(a, s => a.neg(cs.ns(|| unique_namespace))); + let result = match_signed_integer!(a, span => a.neg(cs.ns(|| unique_namespace))); - result.ok_or_else(|| IntegerError::negate_operation(span)) + result.ok_or_else(|| IntegerError::negate_operation(span.to_owned())) } pub fn add>( self, cs: &mut CS, other: Self, - span: Span, + span: &Span, ) -> Result { let unique_namespace = format!("enforce {} + {} {}:{}", self, other, span.line, span.start); let a = self; let b = other; - let s = span.clone(); - let result = match_integers_span!((a, b), s => a.add(cs.ns(|| unique_namespace), &b)); + let result = match_integers_span!((a, b), span => a.add(cs.ns(|| unique_namespace), &b)); - result.ok_or_else(|| IntegerError::binary_operation("+".to_string(), span)) + result.ok_or_else(|| IntegerError::binary_operation("+".to_string(), span.to_owned())) } pub fn sub>( self, cs: &mut CS, other: Self, - span: Span, + span: &Span, ) -> Result { let unique_namespace = format!("enforce {} - {} {}:{}", self, other, span.line, span.start); let a = self; let b = other; - let s = span.clone(); - let result = match_integers_span!((a, b), s => a.sub(cs.ns(|| unique_namespace), &b)); + let result = match_integers_span!((a, b), span => a.sub(cs.ns(|| unique_namespace), &b)); - result.ok_or_else(|| IntegerError::binary_operation("-".to_string(), span)) + result.ok_or_else(|| IntegerError::binary_operation("-".to_string(), span.to_owned())) } pub fn mul>( self, cs: &mut CS, other: Self, - span: Span, + span: &Span, ) -> Result { let unique_namespace = format!("enforce {} * {} {}:{}", self, other, span.line, span.start); let a = self; let b = other; - let s = span.clone(); - let result = match_integers_span!((a, b), s => a.mul(cs.ns(|| unique_namespace), &b)); + let result = match_integers_span!((a, b), span => a.mul(cs.ns(|| unique_namespace), &b)); - result.ok_or_else(|| IntegerError::binary_operation("*".to_string(), span)) + result.ok_or_else(|| IntegerError::binary_operation("*".to_string(), span.to_owned())) } pub fn div>( self, cs: &mut CS, other: Self, - span: Span, + span: &Span, ) -> Result { let unique_namespace = format!("enforce {} ÷ {} {}:{}", self, other, span.line, span.start); let a = self; let b = other; - let s = span.clone(); - let result = match_integers_span!((a, b), s => a.div(cs.ns(|| unique_namespace), &b)); + let result = match_integers_span!((a, b), span => a.div(cs.ns(|| unique_namespace), &b)); - result.ok_or_else(|| IntegerError::binary_operation("÷".to_string(), span)) + result.ok_or_else(|| IntegerError::binary_operation("÷".to_string(), span.to_owned())) } pub fn pow>( self, cs: &mut CS, other: Self, - span: Span, + span: &Span, ) -> Result { let unique_namespace = format!("enforce {} ** {} {}:{}", self, other, span.line, span.start); let a = self; let b = other; - let s = span.clone(); - let result = match_integers_span!((a, b), s => a.pow(cs.ns(|| unique_namespace), &b)); + let result = match_integers_span!((a, b), span => a.pow(cs.ns(|| unique_namespace), &b)); - result.ok_or_else(|| IntegerError::binary_operation("**".to_string(), span)) + result.ok_or_else(|| IntegerError::binary_operation("**".to_string(), span.to_owned())) } } diff --git a/compiler/src/value/integer/macros.rs b/compiler/src/value/integer/macros.rs index be8e979280..3d7018f8d1 100644 --- a/compiler/src/value/integer/macros.rs +++ b/compiler/src/value/integer/macros.rs @@ -84,11 +84,21 @@ macro_rules! match_unsigned_integer { macro_rules! match_signed_integer { ($integer: ident, $span: ident => $expression: expr) => { match $integer { - Integer::I8($integer) => Some(Integer::I8($expression.map_err(|e| IntegerError::signed(e, $span))?)), - Integer::I16($integer) => Some(Integer::I16($expression.map_err(|e| IntegerError::signed(e, $span))?)), - Integer::I32($integer) => Some(Integer::I32($expression.map_err(|e| IntegerError::signed(e, $span))?)), - Integer::I64($integer) => Some(Integer::I64($expression.map_err(|e| IntegerError::signed(e, $span))?)), - Integer::I128($integer) => Some(Integer::I128($expression.map_err(|e| IntegerError::signed(e, $span))?)), + Integer::I8($integer) => Some(Integer::I8( + $expression.map_err(|e| IntegerError::signed(e, $span.to_owned()))?, + )), + Integer::I16($integer) => Some(Integer::I16( + $expression.map_err(|e| IntegerError::signed(e, $span.to_owned()))?, + )), + Integer::I32($integer) => Some(Integer::I32( + $expression.map_err(|e| IntegerError::signed(e, $span.to_owned()))?, + )), + Integer::I64($integer) => Some(Integer::I64( + $expression.map_err(|e| IntegerError::signed(e, $span.to_owned()))?, + )), + Integer::I128($integer) => Some(Integer::I128( + $expression.map_err(|e| IntegerError::signed(e, $span.to_owned()))?, + )), _ => None, } @@ -119,37 +129,37 @@ macro_rules! match_integers { macro_rules! match_integers_span { (($a: ident, $b: ident), $span: ident => $expression:expr) => { match ($a, $b) { - (Integer::U8($a), Integer::U8($b)) => { - Some(Integer::U8($expression.map_err(|e| IntegerError::synthesis(e, $span))?)) - } + (Integer::U8($a), Integer::U8($b)) => Some(Integer::U8( + $expression.map_err(|e| IntegerError::synthesis(e, $span.to_owned()))?, + )), (Integer::U16($a), Integer::U16($b)) => Some(Integer::U16( - $expression.map_err(|e| IntegerError::synthesis(e, $span))?, + $expression.map_err(|e| IntegerError::synthesis(e, $span.to_owned()))?, )), (Integer::U32($a), Integer::U32($b)) => Some(Integer::U32( - $expression.map_err(|e| IntegerError::synthesis(e, $span))?, + $expression.map_err(|e| IntegerError::synthesis(e, $span.to_owned()))?, )), (Integer::U64($a), Integer::U64($b)) => Some(Integer::U64( - $expression.map_err(|e| IntegerError::synthesis(e, $span))?, + $expression.map_err(|e| IntegerError::synthesis(e, $span.to_owned()))?, )), (Integer::U128($a), Integer::U128($b)) => Some(Integer::U128( - $expression.map_err(|e| IntegerError::synthesis(e, $span))?, + $expression.map_err(|e| IntegerError::synthesis(e, $span.to_owned()))?, )), - (Integer::I8($a), Integer::I8($b)) => { - Some(Integer::I8($expression.map_err(|e| IntegerError::signed(e, $span))?)) - } - (Integer::I16($a), Integer::I16($b)) => { - Some(Integer::I16($expression.map_err(|e| IntegerError::signed(e, $span))?)) - } - (Integer::I32($a), Integer::I32($b)) => { - Some(Integer::I32($expression.map_err(|e| IntegerError::signed(e, $span))?)) - } - (Integer::I64($a), Integer::I64($b)) => { - Some(Integer::I64($expression.map_err(|e| IntegerError::signed(e, $span))?)) - } - (Integer::I128($a), Integer::I128($b)) => { - Some(Integer::I128($expression.map_err(|e| IntegerError::signed(e, $span))?)) - } + (Integer::I8($a), Integer::I8($b)) => Some(Integer::I8( + $expression.map_err(|e| IntegerError::signed(e, $span.to_owned()))?, + )), + (Integer::I16($a), Integer::I16($b)) => Some(Integer::I16( + $expression.map_err(|e| IntegerError::signed(e, $span.to_owned()))?, + )), + (Integer::I32($a), Integer::I32($b)) => Some(Integer::I32( + $expression.map_err(|e| IntegerError::signed(e, $span.to_owned()))?, + )), + (Integer::I64($a), Integer::I64($b)) => Some(Integer::I64( + $expression.map_err(|e| IntegerError::signed(e, $span.to_owned()))?, + )), + (Integer::I128($a), Integer::I128($b)) => Some(Integer::I128( + $expression.map_err(|e| IntegerError::signed(e, $span.to_owned()))?, + )), (_, _) => None, } }; diff --git a/compiler/src/value/value.rs b/compiler/src/value/value.rs index 15ac98a508..febfb8c9f7 100644 --- a/compiler/src/value/value.rs +++ b/compiler/src/value/value.rs @@ -74,19 +74,22 @@ pub enum ConstrainedValue> { } impl> ConstrainedValue { - pub(crate) fn from_other(value: String, other: &ConstrainedValue, span: Span) -> Result { - let other_type = other.to_type(span.clone())?; + pub(crate) fn from_other(value: String, other: &ConstrainedValue, span: &Span) -> Result { + let other_type = other.to_type(span)?; ConstrainedValue::from_type(value, &other_type, span) } - pub(crate) fn from_type(value: String, type_: &Type, span: Span) -> Result { + pub(crate) fn from_type(value: String, type_: &Type, span: &Span) -> Result { match type_ { // Data types Type::Address => Ok(ConstrainedValue::Address(Address::constant(value, span)?)), Type::Boolean => Ok(ConstrainedValue::Boolean(new_bool_constant(value, span)?)), Type::Field => Ok(ConstrainedValue::Field(FieldType::constant(value, span)?)), - Type::Group => Ok(ConstrainedValue::Group(G::constant(GroupValue::Single(value, span))?)), + Type::Group => Ok(ConstrainedValue::Group(G::constant(GroupValue::Single( + value, + span.to_owned(), + ))?)), Type::IntegerType(integer_type) => Ok(ConstrainedValue::Integer(Integer::new_constant( integer_type, value, @@ -99,7 +102,7 @@ impl> ConstrainedValue { } } - pub(crate) fn to_type(&self, span: Span) -> Result { + pub(crate) fn to_type(&self, span: &Span) -> Result { Ok(match self { // Data types ConstrainedValue::Address(_address) => Type::Address, @@ -125,7 +128,7 @@ impl> ConstrainedValue { let mut types = Vec::with_capacity(tuple.len()); for value in tuple { - let type_ = value.to_type(span.clone())?; + let type_ = value.to_type(span)?; types.push(type_) } @@ -133,7 +136,7 @@ impl> ConstrainedValue { } ConstrainedValue::CircuitExpression(id, _members) => Type::Circuit(id.clone()), ConstrainedValue::Mutable(value) => return value.to_type(span), - value => return Err(ValueError::implicit(value.to_string(), span)), + value => return Err(ValueError::implicit(value.to_string(), span.to_owned())), }) } @@ -168,7 +171,7 @@ impl> ConstrainedValue { } } - pub(crate) fn resolve_type(&mut self, type_: Option, span: Span) -> Result<(), ValueError> { + pub(crate) fn resolve_type(&mut self, type_: Option, span: &Span) -> Result<(), ValueError> { if let ConstrainedValue::Unresolved(ref string) = self { if type_.is_some() { *self = ConstrainedValue::from_type(string.clone(), &type_.unwrap(), span)? @@ -183,45 +186,45 @@ impl> ConstrainedValue { &mut self, other: &mut Self, type_: Option, - span: Span, + span: &Span, ) -> Result<(), ValueError> { if type_.is_some() { - self.resolve_type(type_.clone(), span.clone())?; + self.resolve_type(type_.clone(), span)?; return other.resolve_type(type_, span); } match (&self, &other) { (ConstrainedValue::Unresolved(_), ConstrainedValue::Unresolved(_)) => Ok(()), - (ConstrainedValue::Unresolved(_), _) => self.resolve_type(Some(other.to_type(span.clone())?), span), - (_, ConstrainedValue::Unresolved(_)) => other.resolve_type(Some(self.to_type(span.clone())?), span), + (ConstrainedValue::Unresolved(_), _) => self.resolve_type(Some(other.to_type(span)?), span), + (_, ConstrainedValue::Unresolved(_)) => other.resolve_type(Some(self.to_type(span)?), span), _ => Ok(()), } } - pub(crate) fn extract_function(self, scope: String, span: Span) -> Result<(String, Function), ExpressionError> { + pub(crate) fn extract_function(self, scope: &str, span: &Span) -> Result<(String, Function), ExpressionError> { match self { ConstrainedValue::Function(circuit_identifier, function) => { - let mut outer_scope = scope.clone(); + let mut outer_scope = scope.to_string(); // If this is a circuit function, evaluate inside the circuit scope if let Some(identifier) = circuit_identifier { // avoid creating recursive scope if !is_in_scope(&scope, &identifier.name) { - outer_scope = new_scope(scope, identifier.name); + outer_scope = new_scope(scope, &identifier.name); } } Ok((outer_scope, function)) } - ConstrainedValue::Import(import_scope, function) => function.extract_function(import_scope, span), - value => Err(ExpressionError::undefined_function(value.to_string(), span)), + ConstrainedValue::Import(import_scope, function) => function.extract_function(&import_scope, span), + value => Err(ExpressionError::undefined_function(value.to_string(), span.to_owned())), } } - pub(crate) fn extract_circuit(self, span: Span) -> Result { + pub(crate) fn extract_circuit(self, span: &Span) -> Result { match self { ConstrainedValue::CircuitDefinition(circuit) => Ok(circuit), ConstrainedValue::Import(_import_scope, circuit) => circuit.extract_circuit(span), - value => Err(ExpressionError::undefined_circuit(value.to_string(), span)), + value => Err(ExpressionError::undefined_circuit(value.to_string(), span.to_owned())), } } @@ -231,7 +234,11 @@ impl> ConstrainedValue { } } - pub(crate) fn allocate_value>(&mut self, mut cs: CS, span: Span) -> Result<(), ValueError> { + pub(crate) fn allocate_value>( + &mut self, + mut cs: CS, + span: &Span, + ) -> Result<(), ValueError> { match self { // Data types ConstrainedValue::Address(_address) => { @@ -243,12 +250,12 @@ impl> ConstrainedValue { .map(|b| b.to_string()) .unwrap_or_else(|| "[allocated]".to_string()); - *boolean = allocate_bool(&mut cs, name, option, span)?; + *boolean = allocate_bool(&mut cs, &name, option, span)?; } ConstrainedValue::Field(field) => { let gadget = field .allocated(cs.ns(|| format!("allocate field {}:{}", span.line, span.start))) - .map_err(|error| ValueError::FieldError(FieldError::synthesis_error(error, span)))?; + .map_err(|error| ValueError::FieldError(FieldError::synthesis_error(error, span.to_owned())))?; *field = FieldType::Allocated(gadget) } @@ -260,7 +267,7 @@ impl> ConstrainedValue { let option = integer.get_value(); let name = option.clone().unwrap_or_else(|| "[allocated]".to_string()); - *integer = Integer::allocate_type(&mut cs, integer_type, name, option, span)?; + *integer = Integer::allocate_type(&mut cs, integer_type, &name, option, span)?; } // Data type wrappers @@ -271,7 +278,7 @@ impl> ConstrainedValue { .map(|(i, value)| { let unique_name = format!("allocate array member {} {}:{}", i, span.line, span.start); - value.allocate_value(cs.ns(|| unique_name), span.clone()) + value.allocate_value(cs.ns(|| unique_name), span) }) .collect::>()?; } @@ -282,7 +289,7 @@ impl> ConstrainedValue { .map(|(i, value)| { let unique_name = format!("allocate tuple member {} {}:{}", i, span.line, span.start); - value.allocate_value(cs.ns(|| unique_name), span.clone()) + value.allocate_value(cs.ns(|| unique_name), span) }) .collect::>()?; } @@ -293,7 +300,7 @@ impl> ConstrainedValue { .map(|(i, member)| { let unique_name = format!("allocate circuit member {} {}:{}", i, span.line, span.start); - member.1.allocate_value(cs.ns(|| unique_name), span.clone()) + member.1.allocate_value(cs.ns(|| unique_name), span) }) .collect::>()?; } @@ -311,7 +318,7 @@ impl> ConstrainedValue { // Cannot allocate an unresolved value ConstrainedValue::Unresolved(value) => { - return Err(ValueError::implicit(value.to_string(), span)); + return Err(ValueError::implicit(value.to_string(), span.to_owned())); } } diff --git a/core/src/types/core_circuit_struct_list.rs b/core/src/types/core_circuit_struct_list.rs index 46b432e956..4a93867bf4 100644 --- a/core/src/types/core_circuit_struct_list.rs +++ b/core/src/types/core_circuit_struct_list.rs @@ -32,7 +32,7 @@ impl CoreCircuitStructList { self.symbols.push((name, circuit)) } - pub fn symbols(&self) -> Vec<(String, Circuit)> { - self.symbols.clone() + pub fn symbols(&self) -> impl Iterator { + self.symbols.iter() } } diff --git a/typed/src/functions/function.rs b/typed/src/functions/function.rs index b68526cbe6..595c95c453 100644 --- a/typed/src/functions/function.rs +++ b/typed/src/functions/function.rs @@ -47,8 +47,8 @@ impl<'ast> From> for Function { } impl Function { - pub fn get_name(&self) -> String { - self.identifier.name.clone() + pub fn get_name(&self) -> &str { + &self.identifier.name } fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { From 7c9a5ad8aa38cd19121b818ab58ac7ec041dfe51 Mon Sep 17 00:00:00 2001 From: collin Date: Wed, 21 Oct 2020 00:19:30 -0700 Subject: [PATCH 2/2] fix reference error --- compiler/src/expression/expression.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/expression/expression.rs b/compiler/src/expression/expression.rs index c97be901a5..042e5ec422 100644 --- a/compiler/src/expression/expression.rs +++ b/compiler/src/expression/expression.rs @@ -54,7 +54,7 @@ impl> ConstrainedProgram { Expression::Address(address, span) => Ok(ConstrainedValue::Address(Address::constant(address, &span)?)), Expression::Boolean(boolean, span) => Ok(ConstrainedValue::Boolean(new_bool_constant(boolean, &span)?)), Expression::Field(field, span) => Ok(ConstrainedValue::Field(FieldType::constant(field, &span)?)), - Expression::Group(group_element) => Ok(ConstrainedValue::Group(G::constant(group_element)?)), + Expression::Group(group_element) => Ok(ConstrainedValue::Group(G::constant(*group_element)?)), Expression::Implicit(value, span) => Ok(enforce_number_implicit(expected_type, value, &span)?), Expression::Integer(type_, integer, span) => Ok(ConstrainedValue::Integer(Integer::new_constant( &type_, integer, &span,