From 085a756ed3dce9106ca690e0db5392fe047ec44d Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Tue, 3 Dec 2019 10:49:58 -0800 Subject: [PATCH 1/2] Fix bug where tuples are destructured with the members in the wrong order --- src/passes/2-simplify/ligodity.ml | 5 +++-- src/test/contracts/let_multiple.mligo | 2 +- src/test/integration_tests.ml | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/passes/2-simplify/ligodity.ml b/src/passes/2-simplify/ligodity.ml index 39c60639c..d524901ad 100644 --- a/src/passes/2-simplify/ligodity.ml +++ b/src/passes/2-simplify/ligodity.ml @@ -593,14 +593,15 @@ and simpl_declaration : Raw.declaration -> declaration Location.wrap list result if i = (List.length variables) then accesses else let accesses = - build_access_expr :: accesses + build_access_expr :: accesses in gen_access_tuple name ~i: (i + 1) ~accesses in ok (gen_access_tuple name) (* TODO: Improve this error message *) | other -> fail @@ simplifying_expr other in let%bind decls = - bind_map_list process_variable (List.combine variables expr_bind_lst) + (* TODO: Rewrite the gen_access_tuple so there's no List.rev *) + bind_map_list process_variable (List.combine variables (List.rev expr_bind_lst)) in ok @@ decls | PPar {region = _ ; value = { lpar = _ ; inside = pt; rpar = _; } } -> (* Extract parenthetical multi-bind *) diff --git a/src/test/contracts/let_multiple.mligo b/src/test/contracts/let_multiple.mligo index 588bce0ab..c399f2952 100644 --- a/src/test/contracts/let_multiple.mligo +++ b/src/test/contracts/let_multiple.mligo @@ -11,4 +11,4 @@ let main_paren (p: unit) : int = x + y let foobar : (int * int) = (23 , 42) let (foo : int) , (bar : int) = foobar -let non_tuple_rhs (p: unit) : int = foo + bar +let non_tuple_rhs (p: unit) : int = bar - foo diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index eb8b36535..afa219205 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -1215,7 +1215,7 @@ let mligo_let_multiple () : unit result = in let%bind () = let input = e_unit () in - let expected = e_int 65 in + let expected = e_int 19 in expect_eq program "non_tuple_rhs" input expected in ok () From 6a4d58d46720965fd6f22c274f9ea56f4eb5f056 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Tue, 3 Dec 2019 11:10:53 -0800 Subject: [PATCH 2/2] Overkill the tuple ordering bug --- src/test/contracts/let_multiple.mligo | 26 +++++++++++++++++++++++++- src/test/integration_tests.ml | 15 +++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/test/contracts/let_multiple.mligo b/src/test/contracts/let_multiple.mligo index c399f2952..08cb6dc3c 100644 --- a/src/test/contracts/let_multiple.mligo +++ b/src/test/contracts/let_multiple.mligo @@ -11,4 +11,28 @@ let main_paren (p: unit) : int = x + y let foobar : (int * int) = (23 , 42) let (foo : int) , (bar : int) = foobar -let non_tuple_rhs (p: unit) : int = bar - foo +(* Here to prevent a regression of https://gitlab.com/ligolang/ligo/issues/63#note_254106580 *) + +let correct_values_bound (p: unit) : int * int = + foo, bar + +let non_tuple_rhs (p: unit) : int = + bar - foo + +(* Here to prevent a regression of https://gitlab.com/ligolang/ligo/issues/63#note_254106580 *) + +let big_tuple : (int * int * int * int * int) = (10, 20, 30, 40, 50) + +let (a: int), (b: int), (c: int), (d: int), (e: int) = big_tuple + +let correct_values_big_tuple (p: unit) : int * int * int * int * int = + a, b, c, d, e + +(* Here to prevent a regression of https://gitlab.com/ligolang/ligo/issues/63#note_254106580 *) + +let different_types: (int * string) = 10, "hello" + +let (greet_num: int), (greeting: string) = different_types + +let correct_values_different_types (p: unit) : int * string = + greet_num, greeting diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index afa219205..725db8ddd 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -1213,11 +1213,26 @@ let mligo_let_multiple () : unit result = let expected = e_int 6 in expect_eq program "main_paren" input expected in + let%bind () = + let input = e_unit () in + let expected = e_tuple [e_int 23 ; e_int 42] in + expect_eq program "correct_values_bound" input expected + in let%bind () = let input = e_unit () in let expected = e_int 19 in expect_eq program "non_tuple_rhs" input expected in + let%bind () = + let input = e_unit () in + let expected = e_tuple [e_int 10; e_int 20; e_int 30; e_int 40; e_int 50] in + expect_eq program "correct_values_big_tuple" input expected + in + let%bind () = + let input = e_unit () in + let expected = e_tuple [e_int 10 ; e_string "hello"] in + expect_eq program "correct_values_different_types" input expected + in ok () let balance_constant () : unit result =