Show error message when using pattern matching for consts

This commit is contained in:
Gaith Hallak 2022-12-16 18:54:17 +03:00
parent 28b26ea4cc
commit 0c2421c528

View File

@ -1093,7 +1093,7 @@ infer_contract(Env0, What, Defs0, Options) ->
({fun_clauses, _, _, _, _}) -> function;
({fun_decl, _, _, _}) -> prototype;
({using, _, _, _, _}) -> using;
({letval, _, {id, _, _}, _}) -> constant;
({letval, _, _, _}) -> constant;
(_) -> unexpected
end,
Get = fun(K, In) -> [ Def || Def <- In, Kind(Def) == K ] end,
@ -1286,7 +1286,12 @@ opposite_variance(bivariant) -> bivariant.
-spec check_constants(env(), [aeso_syntax:decl()]) -> {env(), [aeso_syntax:decl()]}.
check_constants(Env, Consts) ->
ConstMap = maps:from_list([ {name(Id), Const} || Const = {letval, _, Id, _} <- Consts ]),
HasValidId = fun({letval, _, {id, _, _}, _}) -> true;
(_) -> false
end,
{ValidConsts, InvalidConsts} = lists:partition(HasValidId, Consts),
[ type_error({invalid_const_id, aeso_syntax:get_ann(Pat)}) || {letval, _, Pat, _} <- InvalidConsts ],
ConstMap = maps:from_list([ {name(Id), Const} || Const = {letval, _, Id, _} <- ValidConsts ]),
DepGraph = maps:map(fun(_, Const) -> aeso_syntax_utils:used_ids(Const) end, ConstMap),
SCCs = aeso_utils:scc(DepGraph),
bind_consts(Env, ConstMap, SCCs, []).
@ -3903,6 +3908,9 @@ mk_error({mutually_recursive_constants, Consts}) ->
|| {letval, Ann, Id, _} <- Consts ] ],
[{letval, Ann, _, _} | _] = Consts,
mk_t_err(pos(Ann), Msg);
mk_error({invalid_const_id, Ann}) ->
Msg = "Pattern matching is not allowed when defining compile-time constants",
mk_t_err(pos(Ann), Msg);
mk_error({invalid_const_expr, ConstId}) ->
Msg = io_lib:format("Invalid expression in the definition of the constant `~s`", [name(ConstId)]),
mk_t_err(pos(aeso_syntax:get_ann(ConstId)), Msg);