From a5b6780c3be58f10baa3a7c186d3f99062703b0b Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Wed, 25 Aug 2021 08:55:26 -0700 Subject: [PATCH] big self canonicalization fixes --- ast/src/reducer/canonicalization.rs | 47 +++++++++++++++++-- test-framework/src/bin/tgc.rs | 4 +- .../big_self_in_circuit_replacement.leo | 32 ++++++++----- .../big_self_in_circuit_replacement.leo.out | 6 +-- 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/ast/src/reducer/canonicalization.rs b/ast/src/reducer/canonicalization.rs index d657c17206..da6a881ff9 100644 --- a/ast/src/reducer/canonicalization.rs +++ b/ast/src/reducer/canonicalization.rs @@ -125,6 +125,19 @@ impl Canonicalizer { } } + fn canonicalize_circuit_implied_variable_definition( + &mut self, + member: &CircuitImpliedVariableDefinition, + ) -> CircuitImpliedVariableDefinition { + CircuitImpliedVariableDefinition { + identifier: member.identifier.clone(), + expression: member + .expression + .as_ref() + .map(|expr| self.canonicalize_expression(expr)), + } + } + fn canonicalize_expression(&mut self, expression: &Expression) -> Expression { match expression { Expression::Unary(unary) => { @@ -262,7 +275,11 @@ impl Canonicalizer { return Expression::CircuitInit(CircuitInitExpression { name, - members: circuit_init.members.clone(), + members: circuit_init + .members + .iter() + .map(|member| self.canonicalize_circuit_implied_variable_definition(member)) + .collect(), span: circuit_init.span.clone(), }); } @@ -284,7 +301,11 @@ impl Canonicalizer { Expression::Call(call) => { return Expression::Call(CallExpression { function: Box::new(self.canonicalize_expression(&call.function)), - arguments: call.arguments.clone(), + arguments: call + .arguments + .iter() + .map(|arg| self.canonicalize_expression(dbg!(arg))) + .collect(), span: call.span.clone(), }); } @@ -439,11 +460,31 @@ impl Canonicalizer { } } + fn canonicalize_function_input(&mut self, input: &FunctionInput) -> FunctionInput { + if let FunctionInput::Variable(variable) = input { + if variable.type_.is_self() { + return FunctionInput::Variable(FunctionInputVariable { + identifier: variable.identifier.clone(), + const_: variable.const_, + mutable: variable.mutable, + type_: Type::Circuit(self.circuit_name.as_ref().unwrap().clone()), + span: variable.span.clone(), + }); + } + } + + input.clone() + } + fn canonicalize_circuit_member(&mut self, circuit_member: &CircuitMember) -> CircuitMember { match circuit_member { CircuitMember::CircuitVariable(_, _) => {} CircuitMember::CircuitFunction(function) => { - let input = function.input.clone(); + let input = function + .input + .iter() + .map(|input| self.canonicalize_function_input(input)) + .collect(); let output = self.canonicalize_self_type(function.output.as_ref()); let block = self.canonicalize_block(&function.block); diff --git a/test-framework/src/bin/tgc.rs b/test-framework/src/bin/tgc.rs index fc62bcb413..799e3d0ba4 100644 --- a/test-framework/src/bin/tgc.rs +++ b/test-framework/src/bin/tgc.rs @@ -107,7 +107,7 @@ fn run_with_args(opt: Opt) -> Result<(), Box> { cwd.pop(); cwd.join(&val.as_str().unwrap()) }) - .unwrap_or(PathBuf::from(path)); + .unwrap_or_else(|| PathBuf::from(path)); // Write all files into the directory. let (initial, canonicalized, type_inferenced) = generate_asts(cwd, text)?; @@ -129,7 +129,7 @@ fn run_with_args(opt: Opt) -> Result<(), Box> { } /// Do what Compiler does - prepare 3 stages of AST: initial, canonicalized and type_inferenced -fn generate_asts(path: PathBuf, text: &String) -> Result<(String, String, String), Box> { +fn generate_asts(path: PathBuf, text: &str) -> Result<(String, String, String), Box> { let mut ast = leo_parser::parse_ast(path.clone().into_os_string().into_string().unwrap(), text)?; let initial = ast.to_json_string()?; diff --git a/tests/compiler/circuits/big_self_in_circuit_replacement.leo b/tests/compiler/circuits/big_self_in_circuit_replacement.leo index 7dee75cedf..7f0e58b378 100644 --- a/tests/compiler/circuits/big_self_in_circuit_replacement.leo +++ b/tests/compiler/circuits/big_self_in_circuit_replacement.leo @@ -5,23 +5,29 @@ input_file: input/dummy.in */ circuit Foo { - x: u32; + x: u32; - function new() -> Self { - let new: Self = Self { - x: 1u32 - }; + function new() -> Self { + let new: Self = Self { + x: 1u32 + }; - return new; - } + return new; + } - function etc() { - let y = [0u32, 1, 2, 3]; - y[Self {x: 0}.x] += 2; - } + function etc() { + let y = [0u32, 1, 2, 3]; + y[Self {x: 0}.x] += 2; + } + + function func() { + const x: Self = Foo {x: Self {x: 1}.x}; + } + + function self_circ(x: Self) {} } function main(y: bool) -> bool { - let foo: Foo = Foo::new(); - return foo.x == 1u32 && y; + let foo: Foo = Foo::new(); + return foo.x == 1u32 && y; } \ No newline at end of file diff --git a/tests/expectations/compiler/compiler/circuits/big_self_in_circuit_replacement.leo.out b/tests/expectations/compiler/compiler/circuits/big_self_in_circuit_replacement.leo.out index 11d7c42755..7eb8dbfaf7 100644 --- a/tests/expectations/compiler/compiler/circuits/big_self_in_circuit_replacement.leo.out +++ b/tests/expectations/compiler/compiler/circuits/big_self_in_circuit_replacement.leo.out @@ -16,6 +16,6 @@ outputs: r0: type: bool value: "true" - initial_ast: bc330763338677f23601b03f5665bd8f42f8143f59cc9b4803fb693b3cfa0311 - canonicalized_ast: fbff610ef772ee7f997b4bc4cd7c2a3f2024d70af35b94a966ca6a0f19f15194 - type_inferenced_ast: f6b0159f6bffeff8e3cde7f13c97ac5d537b40855271a4a13d07a84d24d78504 + initial_ast: f5211a78cc2ceca171cc594466a95d12e9240d552dbdb0c65ea718eb07a756fc + canonicalized_ast: 787dffcc703941dd35266c066dc5fdf5d15490ccc60c95ade650366463390239 + type_inferenced_ast: 96dba011f40d5e05148f41d78de45801277a54fea727fc476b5d8cefae6e3679