From 325d69e96df119880e7d4074728d47bb7f9b04fa Mon Sep 17 00:00:00 2001 From: Ulf Norell Date: Tue, 3 Sep 2019 17:24:06 +0200 Subject: [PATCH] Fail gracefully on bad top-level declaration --- src/aeso_ast_infer_types.erl | 12 +++++++++++- test/aeso_compiler_tests.erl | 2 ++ test/contracts/bad_top_level_decl.aes | 3 +++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/contracts/bad_top_level_decl.aes diff --git a/src/aeso_ast_infer_types.erl b/src/aeso_ast_infer_types.erl index 8c33012..58c60e3 100644 --- a/src/aeso_ast_infer_types.erl +++ b/src/aeso_ast_infer_types.erl @@ -706,7 +706,8 @@ check_modifiers(Env, Contracts) -> {true, []} -> type_error({contract_has_no_entrypoints, Con}); _ -> ok end; - {namespace, _, _, Decls} -> check_modifiers1(namespace, Decls) + {namespace, _, _, Decls} -> check_modifiers1(namespace, Decls); + Decl -> type_error({bad_top_level_decl, Decl}) end || C <- Contracts ], destroy_and_report_type_errors(Env). @@ -2301,6 +2302,15 @@ mk_error({cannot_call_init_function, Ann}) -> Msg = "The 'init' function is called exclusively by the create contract transaction\n" "and cannot be called from the contract code.\n", mk_t_err(pos(Ann), Msg); +mk_error({bad_top_level_decl, Decl}) -> + What = case element(1, Decl) of + letval -> "function or entrypoint"; + _ -> "contract or namespace" + end, + Id = element(3, Decl), + Msg = io_lib:format("The definition of '~s' must appear inside a ~s.\n", + [pp_expr("", Id), What]), + mk_t_err(pos(Decl), Msg); mk_error(Err) -> Msg = io_lib:format("Unknown error: ~p\n", [Err]), mk_t_err(pos(0, 0), Msg). diff --git a/test/aeso_compiler_tests.erl b/test/aeso_compiler_tests.erl index c63c54d..9956ce6 100644 --- a/test/aeso_compiler_tests.erl +++ b/test/aeso_compiler_tests.erl @@ -493,6 +493,8 @@ failing_contracts() -> [<>]) + , ?TEST(bad_top_level_decl, + [<>]) ]. -define(Path(File), "code_errors/" ??File). diff --git a/test/contracts/bad_top_level_decl.aes b/test/contracts/bad_top_level_decl.aes new file mode 100644 index 0000000..5475fb6 --- /dev/null +++ b/test/contracts/bad_top_level_decl.aes @@ -0,0 +1,3 @@ +function square(x) = x ^ 2 +contract Main = + entrypoint main() = square(10)