Nicer error for missing event type

This commit is contained in:
Ulf Norell 2019-09-04 11:03:33 +02:00
parent d8adfce465
commit 97d58fcacd
3 changed files with 22 additions and 10 deletions

View File

@ -205,18 +205,18 @@ bind_state(Env) ->
{S, _} -> {qid, Ann, S}; {S, _} -> {qid, Ann, S};
false -> Unit false -> Unit
end, end,
Event =
case lookup_type(Env, {id, Ann, "event"}) of
{E, _} -> {qid, Ann, E};
false -> {id, Ann, "event"} %% will cause type error if used(?)
end,
Env1 = bind_funs([{"state", State}, Env1 = bind_funs([{"state", State},
{"put", {type_sig, [stateful | Ann], [], [State], Unit}}], Env), {"put", {type_sig, [stateful | Ann], [], [State], Unit}}], Env),
%% We bind Chain.event in a local 'Chain' namespace. case lookup_type(Env, {id, Ann, "event"}) of
pop_scope( {E, _} ->
bind_fun("event", {fun_t, Ann, [], [Event], Unit}, %% We bind Chain.event in a local 'Chain' namespace.
push_scope(namespace, {con, Ann, "Chain"}, Env1))). Event = {qid, Ann, E},
pop_scope(
bind_fun("event", {fun_t, Ann, [], [Event], Unit},
push_scope(namespace, {con, Ann, "Chain"}, Env1)));
false -> Env1
end.
-spec bind_field(name(), field_info(), env()) -> env(). -spec bind_field(name(), field_info(), env()) -> env().
bind_field(X, Info, Env = #env{ fields = Fields }) -> bind_field(X, Info, Env = #env{ fields = Fields }) ->
@ -2112,7 +2112,12 @@ mk_error({cannot_unify, A, B, When}) ->
mk_t_err(Pos, Msg, Ctxt); mk_t_err(Pos, Msg, Ctxt);
mk_error({unbound_variable, Id}) -> mk_error({unbound_variable, Id}) ->
Msg = io_lib:format("Unbound variable ~s at ~s\n", [pp(Id), pp_loc(Id)]), Msg = io_lib:format("Unbound variable ~s at ~s\n", [pp(Id), pp_loc(Id)]),
mk_t_err(pos(Id), Msg); case Id of
{qid, _, ["Chain", "event"]} ->
Cxt = "Did you forget to define the event type?",
mk_t_err(pos(Id), Msg, Cxt);
_ -> mk_t_err(pos(Id), Msg)
end;
mk_error({undefined_field, Id}) -> mk_error({undefined_field, Id}) ->
Msg = io_lib:format("Unbound field ~s at ~s\n", [pp(Id), pp_loc(Id)]), Msg = io_lib:format("Unbound field ~s at ~s\n", [pp(Id), pp_loc(Id)]),
mk_t_err(pos(Id), Msg); mk_t_err(pos(Id), Msg);

View File

@ -496,6 +496,10 @@ failing_contracts() ->
"and cannot be called from the contract code.">>]) "and cannot be called from the contract code.">>])
, ?TEST(bad_top_level_decl, , ?TEST(bad_top_level_decl,
[<<?Pos(1, 1) "The definition of 'square' must appear inside a contract or namespace.">>]) [<<?Pos(1, 1) "The definition of 'square' must appear inside a contract or namespace.">>])
, ?TEST(missing_event_type,
[<<?Pos(3, 5)
"Unbound variable Chain.event at line 3, column 5\n"
"Did you forget to define the event type?">>])
]. ].
-define(Path(File), "code_errors/" ??File). -define(Path(File), "code_errors/" ??File).

View File

@ -0,0 +1,3 @@
contract MissingEventType =
entrypoint main() =
Chain.event("MAIN")