From 6a3ab09f7ca6b671e2652eba3d86c79f55d57d0b Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Wed, 1 Jul 2020 15:37:49 +0200 Subject: [PATCH] get_scope: scopes for records update/access, constants and applications --- src/bin/expect_tests/get_scope.ml | 161 +++++++++++++++--- src/main/scopes/scopes.ml | 32 +++- .../get_scope_tests/application.mligo | 3 + .../contracts/get_scope_tests/constant.mligo | 6 + .../contracts/get_scope_tests/records.mligo | 16 ++ 5 files changed, 191 insertions(+), 27 deletions(-) create mode 100644 src/test/contracts/get_scope_tests/application.mligo create mode 100644 src/test/contracts/get_scope_tests/constant.mligo create mode 100644 src/test/contracts/get_scope_tests/records.mligo diff --git a/src/bin/expect_tests/get_scope.ml b/src/bin/expect_tests/get_scope.ml index f6ce0d0b4..7d9d4cfd3 100644 --- a/src/bin/expect_tests/get_scope.ml +++ b/src/bin/expect_tests/get_scope.ml @@ -6,10 +6,21 @@ let%expect_test _ = run_ligo_good [ "get-scope" ; gs "lambda_letin.mligo" ; "--format=dev" ] ; [%expect {| Scopes: - [ f#5 a#0 ] in file "lambda_letin.mligo", line 9, characters 2-7 - [ k#4 j#2 i#1 g#3 a#0 ] in file "lambda_letin.mligo", line 7, characters 4-21 - [ j#2 i#1 g#3 a#0 ] in file "lambda_letin.mligo", line 6, character 4 to line 7, character 21 - [ j#2 i#1 a#0 ] in file "lambda_letin.mligo", line 5, character 4 to line 7, character 21 + [ f#5 a#0 ] in file "lambda_letin.mligo", line 9, characters 6-7 + [ f#5 a#0 ] in file "lambda_letin.mligo", line 9, characters 4-5 + [ f#5 a#0 ] in file "lambda_letin.mligo", line 9, characters 2-3 + [ k#4 j#2 i#1 g#3 a#0 ] in file "lambda_letin.mligo", line 7, characters 20-21 + [ k#4 j#2 i#1 g#3 a#0 ] in file "lambda_letin.mligo", line 7, characters 16-17 + [ k#4 j#2 i#1 g#3 a#0 ] in file "lambda_letin.mligo", line 7, characters 12-13 + [ k#4 j#2 i#1 g#3 a#0 ] in file "lambda_letin.mligo", line 7, characters 8-9 + [ k#4 j#2 i#1 g#3 a#0 ] in file "lambda_letin.mligo", line 7, characters 4-5 + [ j#2 i#1 g#3 a#0 ] in file "lambda_letin.mligo", line 6, characters 24-25 + [ j#2 i#1 g#3 a#0 ] in file "lambda_letin.mligo", line 6, characters 20-21 + [ j#2 i#1 g#3 a#0 ] in file "lambda_letin.mligo", line 6, characters 16-17 + [ j#2 i#1 g#3 a#0 ] in file "lambda_letin.mligo", line 6, characters 12-13 + [ j#2 i#1 a#0 ] in file "lambda_letin.mligo", line 5, characters 20-21 + [ j#2 i#1 a#0 ] in file "lambda_letin.mligo", line 5, characters 16-17 + [ j#2 i#1 a#0 ] in file "lambda_letin.mligo", line 5, characters 12-13 [ ] in file "lambda_letin.mligo", line 1, characters 0-9 Variable definitions: @@ -25,11 +36,20 @@ let%expect_test _ = run_ligo_good [ "get-scope" ; gs "letin.mligo" ; "--format=dev" ] ; [%expect {| Scopes: - [ d#4 c#1 a#0 ] in file "letin.mligo", line 10, characters 2-11 - [ f#3 e#2 c#1 a#0 ] in file "letin.mligo", line 8, characters 4-17 - [ e#2 c#1 a#0 ] in file "letin.mligo", line 7, character 4 to line 8, character 17 - [ c#1 a#0 ] in file "letin.mligo", line 6, character 4 to line 8, character 17 - [ a#0 ] in file "letin.mligo", line 4, character 2 to line 10, character 11 + [ d#4 c#1 a#0 ] in file "letin.mligo", line 10, characters 10-11 + [ d#4 c#1 a#0 ] in file "letin.mligo", line 10, characters 6-7 + [ d#4 c#1 a#0 ] in file "letin.mligo", line 10, characters 2-3 + [ f#3 e#2 c#1 a#0 ] in file "letin.mligo", line 8, characters 16-17 + [ f#3 e#2 c#1 a#0 ] in file "letin.mligo", line 8, characters 12-13 + [ f#3 e#2 c#1 a#0 ] in file "letin.mligo", line 8, characters 8-9 + [ f#3 e#2 c#1 a#0 ] in file "letin.mligo", line 8, characters 4-5 + [ e#2 c#1 a#0 ] in file "letin.mligo", line 7, characters 20-21 + [ e#2 c#1 a#0 ] in file "letin.mligo", line 7, characters 16-17 + [ e#2 c#1 a#0 ] in file "letin.mligo", line 7, characters 12-13 + [ c#1 a#0 ] in file "letin.mligo", line 6, characters 16-17 + [ c#1 a#0 ] in file "letin.mligo", line 6, characters 12-13 + [ a#0 ] in file "letin.mligo", line 4, characters 14-15 + [ a#0 ] in file "letin.mligo", line 4, characters 10-11 [ ] in file "letin.mligo", line 1, characters 0-9 Variable definitions: @@ -44,8 +64,11 @@ let%expect_test _ = run_ligo_good [ "get-scope" ; gs "lambda.mligo" ; "--format=dev" ] ; [%expect {| Scopes: - [ f#3 a#0 ] in file "lambda.mligo", line 5, characters 2-7 - [ j#2 i#1 a#0 ] in file "lambda.mligo", line 4, characters 58-63 + [ f#3 a#0 ] in file "lambda.mligo", line 5, characters 6-7 + [ f#3 a#0 ] in file "lambda.mligo", line 5, characters 4-5 + [ f#3 a#0 ] in file "lambda.mligo", line 5, characters 2-3 + [ j#2 i#1 a#0 ] in file "lambda.mligo", line 4, characters 62-63 + [ j#2 i#1 a#0 ] in file "lambda.mligo", line 4, characters 58-59 [ ] in file "lambda.mligo", line 1, characters 0-9 Variable definitions: @@ -59,17 +82,23 @@ let%expect_test _ = run_ligo_good [ "get-scope" ; gs "match.mligo" ; "--format=dev" ] ; [%expect {| Scopes: - [ s#11 mytype#0 c#9 b#5 a#1 ] in file "match.mligo", line 19, characters 16-21 + [ s#11 mytype#0 c#9 b#5 a#1 ] in file "match.mligo", line 19, characters 20-21 + [ s#11 mytype#0 c#9 b#5 a#1 ] in file "match.mligo", line 19, characters 16-17 [ mytype#0 c#9 b#5 a#1 ] in file "match.mligo", line 20, characters 12-13 - [ mytype#0 d#10 c#9 b#5 a#1 ] in file "match.mligo", line 18, characters 22-32 + [ mytype#0 d#10 c#9 b#5 a#1 ] in file "match.mligo", line 18, characters 30-31 + [ mytype#0 d#10 c#9 b#5 a#1 ] in file "match.mligo", line 18, characters 28-29 [ mytype#0 c#9 b#5 a#1 ] in file "match.mligo", line 18, characters 9-32 [ tl#8 mytype#0 hd#7 b#5 a#1 ] in file "match.mligo", line 15, characters 14-15 [ mytype#0 c#6 b#5 a#1 ] in file "match.mligo", line 14, characters 4-5 [ mytype#0 b#5 a#1 ] in file "match.mligo", line 13, character 4 to line 14, character 5 - [ mytype#0 b#5 a#1 ] in file "match.mligo", line 11, characters 9-21 - [ y#4 mytype#0 a#1 ] in file "match.mligo", line 8, characters 13-18 - [ x#3 mytype#0 a#1 ] in file "match.mligo", line 7, characters 13-18 - [ mytype#0 c#2 a#1 ] in file "match.mligo", line 6, characters 22-27 + [ mytype#0 b#5 a#1 ] in file "match.mligo", line 11, characters 18-19 + [ mytype#0 b#5 a#1 ] in file "match.mligo", line 11, characters 15-16 + [ mytype#0 b#5 a#1 ] in file "match.mligo", line 11, characters 11-12 + [ y#4 mytype#0 a#1 ] in file "match.mligo", line 8, characters 17-18 + [ y#4 mytype#0 a#1 ] in file "match.mligo", line 8, characters 13-14 + [ x#3 mytype#0 a#1 ] in file "match.mligo", line 7, characters 17-18 + [ x#3 mytype#0 a#1 ] in file "match.mligo", line 7, characters 13-14 + [ mytype#0 c#2 a#1 ] in file "match.mligo", line 6, characters 26-27 [ mytype#0 a#1 ] in file "match.mligo", line 6, characters 9-27 [ mytype#0 ] in file "match.mligo", line 3, characters 0-9 @@ -92,10 +121,16 @@ let%expect_test _ = run_ligo_good [ "get-scope" ; gs "rec.mligo" ; "--format=dev" ] ; [%expect {| Scopes: - [ c#5 b#6 a#0 ] in file "rec.mligo", line 9, characters 2-10 + [ c#5 b#6 a#0 ] in file "rec.mligo", line 9, characters 5-6 + [ c#5 b#6 a#0 ] in file "rec.mligo", line 9, characters 8-9 + [ c#5 b#6 a#0 ] in file "rec.mligo", line 9, characters 2-3 [ c#5 a#0 ] in file "rec.mligo", line 8, character 2 to line 9, character 10 - [ k#4 j#3 i#2 c#1 a#0 ] in file "rec.mligo", line 6, characters 4-11 - [ j#3 i#2 c#1 a#0 ] in file "rec.mligo", line 5, character 4 to line 6, character 11 + [ k#4 j#3 i#2 c#1 a#0 ] in file "rec.mligo", line 6, characters 7-8 + [ k#4 j#3 i#2 c#1 a#0 ] in file "rec.mligo", line 6, characters 9-10 + [ k#4 j#3 i#2 c#1 a#0 ] in file "rec.mligo", line 6, characters 4-5 + [ j#3 i#2 c#1 a#0 ] in file "rec.mligo", line 5, characters 20-21 + [ j#3 i#2 c#1 a#0 ] in file "rec.mligo", line 5, characters 16-17 + [ j#3 i#2 c#1 a#0 ] in file "rec.mligo", line 5, characters 12-13 [ i#2 c#1 a#0 ] in file "rec.mligo", line 4, characters 36-49 [ c#1 a#0 ] in file "rec.mligo", line 4, characters 36-49 [ c#1 a#0 ] @@ -115,11 +150,19 @@ let%expect_test _ = run_ligo_good [ "get-scope" ; gs "shadowing.mligo" ; "--format=dev" ] ; [%expect {| Scopes: - [ d#4 c#1 a#0 ] in file "shadowing.mligo", line 10, characters 2-11 - [ e#2 c#1 a#3 ] in file "shadowing.mligo", line 8, characters 4-13 - [ e#2 c#1 a#0 ] in file "shadowing.mligo", line 7, character 4 to line 8, character 13 - [ c#1 a#0 ] in file "shadowing.mligo", line 6, character 4 to line 8, character 13 - [ a#0 ] in file "shadowing.mligo", line 4, character 2 to line 10, character 11 + [ d#4 c#1 a#0 ] in file "shadowing.mligo", line 10, characters 10-11 + [ d#4 c#1 a#0 ] in file "shadowing.mligo", line 10, characters 6-7 + [ d#4 c#1 a#0 ] in file "shadowing.mligo", line 10, characters 2-3 + [ e#2 c#1 a#3 ] in file "shadowing.mligo", line 8, characters 12-13 + [ e#2 c#1 a#3 ] in file "shadowing.mligo", line 8, characters 8-9 + [ e#2 c#1 a#3 ] in file "shadowing.mligo", line 8, characters 4-5 + [ e#2 c#1 a#0 ] in file "shadowing.mligo", line 7, characters 20-21 + [ e#2 c#1 a#0 ] in file "shadowing.mligo", line 7, characters 16-17 + [ e#2 c#1 a#0 ] in file "shadowing.mligo", line 7, characters 12-13 + [ c#1 a#0 ] in file "shadowing.mligo", line 6, characters 16-17 + [ c#1 a#0 ] in file "shadowing.mligo", line 6, characters 12-13 + [ a#0 ] in file "shadowing.mligo", line 4, characters 14-15 + [ a#0 ] in file "shadowing.mligo", line 4, characters 10-11 [ ] in file "shadowing.mligo", line 1, characters 0-9 Variable definitions: @@ -129,4 +172,72 @@ let%expect_test _ = (b#5 -> b) in file "shadowing.mligo", line 3, characters 4-5 (a#3 -> a) in file "shadowing.mligo", line 7, characters 8-9 (a#0 -> a) in file "shadowing.mligo", line 1, characters 4-5 + Type definitions: |} ] ; + + run_ligo_good [ "get-scope" ; gs "records.mligo" ; "--format=dev" ] ; + [%expect {| + Scopes: + [ myrec#0 g#5 b#4 a#1 ] in file "records.mligo", line 16, characters 40-41 + [ myrec#0 g#5 b#4 a#1 ] in file "records.mligo", line 16, characters 32-37 + [ myrec#0 g#5 b#4 a#1 ] in file "records.mligo", line 16, characters 28-29 + [ myrec#0 b#4 a#1 ] in file "records.mligo", line 16, characters 15-41 + [ myrec#0 b#4 a#1 ] + [ myrec#0 a#1 ] in file "records.mligo", line 6, characters 53-55 + [ myrec#0 j#3 i#2 a#1 ] in file "records.mligo", line 6, characters 44-45 + [ myrec#0 j#3 i#2 a#1 ] in file "records.mligo", line 6, characters 42-43 + [ myrec#0 j#3 i#2 a#1 ] in file "records.mligo", line 6, characters 40-41 + [ myrec#0 i#2 a#1 ] in file "records.mligo", line 6, characters 27-45 + [ myrec#0 a#1 ] in file "records.mligo", line 6, characters 14-45 + [ myrec#0 ] in file "records.mligo", line 3, characters 0-9 + + Variable definitions: + (j#3 -> j) in file "records.mligo", line 6, characters 31-32 + (i#2 -> i) in file "records.mligo", line 6, characters 18-19 + (g#5 -> g) in file "records.mligo", line 16, characters 19-20 + (e#6 -> e) in file "records.mligo", line 15, characters 4-5 + (b#4 -> b) in file "records.mligo", line 6, characters 4-5 + (a#1 -> a) in file "records.mligo", line 3, characters 4-5 + Type definitions: + (myrec#0 -> myrec) in file "records.mligo", line 1, characters 0-36 |} ] ; + + run_ligo_good [ "get-scope" ; gs "constant.mligo" ; "--format=dev" ] ; + [%expect {| + Scopes: + [ e#3 a#0 ] in file "constant.mligo", line 6, characters 29-30 + [ e#3 a#0 ] in file "constant.mligo", line 6, characters 27-28 + [ e#3 a#0 ] in file "constant.mligo", line 6, characters 22-23 + [ e#3 a#0 ] in file "constant.mligo", line 6, characters 20-21 + [ a#0 ] in file "constant.mligo", line 6, characters 5-32 + [ d#2 c#1 a#0 ] in file "constant.mligo", line 5, characters 43-44 + [ d#2 c#1 a#0 ] in file "constant.mligo", line 5, characters 39-40 + [ d#2 c#1 a#0 ] in file "constant.mligo", line 5, characters 35-36 + [ c#1 a#0 ] in file "constant.mligo", line 5, characters 22-44 + [ ] in file "constant.mligo", line 1, characters 0-9 + + Variable definitions: + (e#3 -> e) in file "constant.mligo", line 6, characters 9-10 + (d#2 -> d) in file "constant.mligo", line 5, characters 26-27 + (c#1 -> c) in file "constant.mligo", line 5, characters 10-11 + (b#4 -> b) in file "constant.mligo", line 3, characters 4-5 + (a#0 -> a) in file "constant.mligo", line 1, characters 4-5 + Type definitions: |} ] ; + + run_ligo_good [ "get-scope" ; gs "application.mligo" ; "--format=dev" ] ; + [%expect {| + Scopes: + [ f#2 c#4 ] in file "application.mligo", line 3, characters 35-36 + [ f#2 ] in file "application.mligo", line 3, characters 22-36 + [ f#2 b#3 ] in file "application.mligo", line 3, characters 18-19 + [ f#2 b#3 ] in file "application.mligo", line 3, characters 16-17 + [ f#2 ] in file "application.mligo", line 3, characters 3-19 + [ j#1 i#0 ] in file "application.mligo", line 2, characters 62-63 + [ j#1 i#0 ] in file "application.mligo", line 2, characters 58-59 + + Variable definitions: + (j#1 -> j) in file "application.mligo", line 2, characters 46-47 + (i#0 -> i) in file "application.mligo", line 2, characters 36-37 + (f#2 -> f) in file "application.mligo", line 2, characters 6-7 + (c#4 -> c) in file "application.mligo", line 3, characters 26-27 + (b#3 -> b) in file "application.mligo", line 3, characters 7-8 + (a#5 -> a) in file "application.mligo", line 1, characters 4-5 Type definitions: |} ] ; \ No newline at end of file diff --git a/src/main/scopes/scopes.ml b/src/main/scopes/scopes.ml index 9f8461796..d0d27ed71 100644 --- a/src/main/scopes/scopes.ml +++ b/src/main/scopes/scopes.ml @@ -77,10 +77,38 @@ let scopes : with_types:bool -> string -> string -> ((def_map * scopes), Main_er (i,all_defs,env,scopes) ) ) - | E_ascription { anno_expr ; _ } -> find_scopes' (i,all_defs,env,scopes,anno_expr.location) anno_expr - | _ -> + | E_record emap -> ( + let aux = fun (i,all_defs,scopes) (exp:Ast_core.expression) -> + let (i,all_defs,_,scopes) = find_scopes' (i,all_defs,env,scopes,exp.location) exp in + (i,all_defs,scopes) + in + let (i,all_defs,scopes) = List.fold_left aux (i,all_defs,scopes) (Ast_core.LMap.to_list emap) in + (i,all_defs,env,scopes) + ) + | E_record_update { record ; update ; _ } -> ( + (*TODO: here record has a virtual location, check this out.. not normal *) + let (i,all_defs,_,scopes) = find_scopes' (i,all_defs,env,scopes,record.location) record in + find_scopes' (i,all_defs,env,scopes,update.location) update + ) + | E_constant { arguments ; _ } -> ( + let aux = fun (i,all_defs,scopes) (exp:Ast_core.expression) -> + let (i,all_defs,_,scopes) = find_scopes' (i,all_defs,env,scopes,exp.location) exp in + (i,all_defs,scopes) + in + let (i,all_defs,scopes) = List.fold_left aux (i,all_defs,scopes) arguments in + (i,all_defs,env,scopes) + ) + | E_application { lamb ; args } -> ( + let (i,all_defs,_,scopes) = find_scopes' (i,all_defs,env,scopes,lamb.location) lamb in + find_scopes' (i,all_defs,env,scopes,args.location) args + ) + | E_ascription { anno_expr=e;_ } | E_record_accessor { record=e;_ } | E_constructor { element=e;_ } -> ( + find_scopes' (i,all_defs,env,scopes,e.location) e + ) + | E_literal _ | E_raw_code _ | E_variable _ -> ( let scopes = add_scope (lastloc, env) scopes in (i,all_defs,env,scopes) + ) in let find_scopes (i,top_lvl_defs,scopes,loc) e = let (i,defs,_,scopes) = find_scopes' (i,top_lvl_defs,top_lvl_defs,scopes,loc) e in diff --git a/src/test/contracts/get_scope_tests/application.mligo b/src/test/contracts/get_scope_tests/application.mligo new file mode 100644 index 000000000..cc17da8c0 --- /dev/null +++ b/src/test/contracts/get_scope_tests/application.mligo @@ -0,0 +1,3 @@ +let a = + let f : (int-> int -> int) = fun (i : int) (j : int) -> j + i in + (let b = 1 in f 1) (let c = 2 in c) \ No newline at end of file diff --git a/src/test/contracts/get_scope_tests/constant.mligo b/src/test/contracts/get_scope_tests/constant.mligo new file mode 100644 index 000000000..35bca3cfc --- /dev/null +++ b/src/test/contracts/get_scope_tests/constant.mligo @@ -0,0 +1,6 @@ +let a = 1 + +let b = + List.map + (fun (c : int) -> let d = 1 in d + c + a) + (let e = 1 in [ e+a ; e+a ]) \ No newline at end of file diff --git a/src/test/contracts/get_scope_tests/records.mligo b/src/test/contracts/get_scope_tests/records.mligo new file mode 100644 index 000000000..af526f1b9 --- /dev/null +++ b/src/test/contracts/get_scope_tests/records.mligo @@ -0,0 +1,16 @@ +type myrec = {foo : int ; bar : int} + +let a = 1 + +// record +let b = {foo=(let i = 1 in let j = 2 in a+i+j) ; bar=24} + +// record accessor +// let c = (let d = 1 in b).bar + +// record update +// let e = +// {(let f = 2 in b) with bar=(let g = a in g);} + +let e = + {b with bar=(let g = a in g + b.bar + a);}