From af2fe4515ca741977e88b8bcc75722b33ab423df Mon Sep 17 00:00:00 2001 From: Gaith Hallak Date: Mon, 23 May 2022 20:33:13 +0400 Subject: [PATCH] Ban empty record declarations --- CHANGELOG.md | 1 + src/aeso_ast_infer_types.erl | 6 ++++++ test/aeso_compiler_tests.erl | 4 ++++ test/contracts/empty_record_declaration.aes | 3 +++ 4 files changed, 14 insertions(+) create mode 100644 test/contracts/empty_record_declaration.aes diff --git a/CHANGELOG.md b/CHANGELOG.md index 925053c..86bcadb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ``` ### Changed - Error messages have been restructured (less newlines) to provide more unified errors. Also `pp_oneline/1` has been added. +- Ban empty record declarations (e.g. `record r = {}` would give an error). ### Removed - Support for AEVM has been entirely wiped diff --git a/src/aeso_ast_infer_types.erl b/src/aeso_ast_infer_types.erl index 9490117..ef94924 100644 --- a/src/aeso_ast_infer_types.erl +++ b/src/aeso_ast_infer_types.erl @@ -1018,6 +1018,9 @@ check_typedef_sccs(Env, TypeMap, [{acyclic, Name} | SCCs], Acc) -> Env1 = bind_type(Name, Ann, {Xs, Def}, Env), case Def of {alias_t, _} -> check_typedef_sccs(Env1, TypeMap, SCCs, Acc1); + {record_t, []} -> + type_error({empty_record_declaration, Ann, Name}), + check_typedef_sccs(Env1, TypeMap, SCCs, Acc1); {record_t, Fields} -> %% check_type to get qualified name RecTy = check_type(Env1, app_t(Ann, D, Xs)), @@ -3279,6 +3282,9 @@ mk_error({using_undefined_namespace_parts, Ann, Namespace, Parts}) -> mk_error({unknown_warning, Warning}) -> Msg = io_lib:format("Trying to report unknown warning: ~p", [Warning]), mk_t_err(pos(0, 0), Msg); +mk_error({empty_record_declaration, Ann, Name}) -> + Msg = io_lib:format("Empty record declarations are not allowed. Cannot declare the record `~s`", [Name]), + mk_t_err(pos(Ann), Msg); mk_error(Err) -> Msg = io_lib:format("Unknown error: ~p", [Err]), mk_t_err(pos(0, 0), Msg). diff --git a/test/aeso_compiler_tests.erl b/test/aeso_compiler_tests.erl index 6e547b3..6db76f5 100644 --- a/test/aeso_compiler_tests.erl +++ b/test/aeso_compiler_tests.erl @@ -791,6 +791,10 @@ failing_contracts() -> "when checking the type of the expression `\"y\" : string` " "against the expected type `bool`">> ]) + , ?TYPE_ERROR(empty_record_declaration, + [<> + ]) , ?TYPE_ERROR(warnings, [<>, diff --git a/test/contracts/empty_record_declaration.aes b/test/contracts/empty_record_declaration.aes new file mode 100644 index 0000000..df45e8c --- /dev/null +++ b/test/contracts/empty_record_declaration.aes @@ -0,0 +1,3 @@ +contract C = + record r = {} + entrypoint init() = ()