From 5c98317a5a19801a00e2e9a8f10cbc56be728a99 Mon Sep 17 00:00:00 2001 From: Hans Svensson Date: Mon, 27 May 2019 15:57:46 +0200 Subject: [PATCH] Make 'indexed' keyword optional --- src/aeso_ast_infer_types.erl | 22 ++++++++++------------ test/aeso_compiler_tests.erl | 11 +++-------- test/contracts/bad_events.aes | 6 ++---- test/contracts/bad_events2.aes | 1 - 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/aeso_ast_infer_types.erl b/src/aeso_ast_infer_types.erl index 5c97c5d..b92d701 100644 --- a/src/aeso_ast_infer_types.erl +++ b/src/aeso_ast_infer_types.erl @@ -738,25 +738,23 @@ check_event(Env, "event", Ann, Def) -> check_event(_Env, _Name, _Ann, Def) -> Def. check_event_con(Env, {constr_t, Ann, Con, Args}) -> - IsIndexed = fun(T) -> case aeso_syntax:get_ann(indexed, T, false) of - true -> indexed; - false -> notindexed - end end, + IsIndexed = fun(T) -> + T1 = unfold_types_in_type(Env, T), + %% `indexed` is optional but if used it has to be correctly used + case {is_word_type(T1), is_string_type(T1), aeso_syntax:get_ann(indexed, T, false)} of + {true, _, _} -> indexed; + {false, true, true} -> type_error({indexed_type_must_be_word, T, T1}); + {false, true, _} -> notindexed; + {false, false, _} -> type_error({event_arg_type_word_or_string, T, T1}), error + end + end, Indices = lists:map(IsIndexed, Args), Indexed = [ T || T <- Args, IsIndexed(T) == indexed ], NonIndexed = Args -- Indexed, - [ check_event_arg_type(Env, Ix, Type) || {Ix, Type} <- lists:zip(Indices, Args) ], [ type_error({event_0_to_3_indexed_values, Con}) || length(Indexed) > 3 ], [ type_error({event_0_to_1_string_values, Con}) || length(NonIndexed) > 1 ], {constr_t, [{indices, Indices} | Ann], Con, Args}. -check_event_arg_type(Env, Ix, Type0) -> - Type = unfold_types_in_type(Env, Type0), - case Ix of - indexed -> [ type_error({indexed_type_must_be_word, Type0, Type}) || not is_word_type(Type) ]; - notindexed -> [ type_error({payload_type_must_be_string, Type0, Type}) || not is_string_type(Type) ] - end. - %% Not so nice. is_word_type({id, _, Name}) -> lists:member(Name, ["int", "address", "hash", "bits", "bool"]); diff --git a/test/aeso_compiler_tests.erl b/test/aeso_compiler_tests.erl index 2a6ac0e..bddfa32 100644 --- a/test/aeso_compiler_tests.erl +++ b/test/aeso_compiler_tests.erl @@ -211,16 +211,11 @@ failing_contracts() -> , {"namespace_clash", [<<"The contract Call (at line 4, column 10) has the same name as a namespace at (builtin location)">>]} , {"bad_events", - [<<"The payload type int (at line 10, column 30) should be string">>, - <<"The payload type alias_address (at line 12, column 30) equals address but it should be string">>, - <<"The indexed type string (at line 9, column 25) is not a word type">>, - <<"The indexed type alias_string (at line 11, column 25) equals string which is not a word type">>]} + [<<"The indexed type string (at line 9, column 25) is not a word type">>, + <<"The indexed type alias_string (at line 10, column 25) equals string which is not a word type">>]} , {"bad_events2", [<<"The event constructor BadEvent1 (at line 9, column 7) has too many non-indexed values (max 1)">>, - <<"The event constructor BadEvent2 (at line 10, column 7) has too many indexed values (max 3)">>, - <<"The event constructor BadEvent3 (at line 11, column 7) has too many non-indexed values (max 1)">>, - <<"The payload type address (at line 11, column 17) should be string">>, - <<"The payload type int (at line 11, column 26) should be string">>]} + <<"The event constructor BadEvent2 (at line 10, column 7) has too many indexed values (max 3)">>]} , {"type_clash", [<<"Cannot unify int\n" " and string\n" diff --git a/test/contracts/bad_events.aes b/test/contracts/bad_events.aes index 5f0d17c..5f9cfec 100644 --- a/test/contracts/bad_events.aes +++ b/test/contracts/bad_events.aes @@ -6,10 +6,8 @@ contract Events = datatype event = Event1(indexed alias_int, indexed int, string) | Event2(alias_string, indexed alias_address) - | BadEvent1(indexed string, string) - | BadEvent2(indexed int, int) - | BadEvent3(indexed alias_string, string) - | BadEvent4(indexed int, alias_address) + | BadEvent1(indexed string) + | BadEvent2(indexed alias_string) function f1(x : int, y : string) = Chain.event(Event1(x, x+1, y)) diff --git a/test/contracts/bad_events2.aes b/test/contracts/bad_events2.aes index 42843f3..02842e3 100644 --- a/test/contracts/bad_events2.aes +++ b/test/contracts/bad_events2.aes @@ -8,7 +8,6 @@ contract Events = | Event2(alias_string, indexed alias_address) | BadEvent1(string, string) | BadEvent2(indexed int, indexed int, indexed int, indexed address) - | BadEvent3(address, int) function f1(x : int, y : string) = Chain.event(Event1(x, x+1, y))