Merge pull request #3 from aeternity/PT-163053747-missing-init-type-check

PT-163053747 Add missing type check of init function
This commit is contained in:
Ulf Norell 2019-01-08 16:35:23 +01:00 committed by GitHub
commit 05b80a5517
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 2 deletions

View File

@ -352,12 +352,25 @@ check_fundecl(_, {fun_decl, _Attrib, {id, _, Name}, Type}) ->
infer_nonrec(Env, LetFun) ->
create_constraints(),
NewLetFun = infer_letfun(Env, LetFun),
check_special_funs(Env, NewLetFun),
solve_constraints(),
destroy_and_report_unsolved_constraints(),
Result = {TypeSig, _} = instantiate(NewLetFun),
print_typesig(TypeSig),
Result.
%% Currenty only the init function.
check_special_funs(_Env, {{"init", Type}, _}) ->
{type_sig, Ann, _Named, _Args, Res} = Type,
State =
%% We might have implicit (no) state.
case lookup_type({id, [], "state"}) of
false -> {tuple_t, [{origin, system}], []};
_ -> {id, [{origin, system}], "state"}
end,
unify(Res, State, {checking_init_type, Ann});
check_special_funs(_, _) -> ok.
typesig_to_fun_t({type_sig, Ann, Named, Args, Res}) -> {fun_t, Ann, Named, Args, Res}.
infer_letrec(Env, {letrec, Attrs, Defs}) ->
@ -1502,6 +1515,9 @@ pp_when({check_expr, Expr, Inferred0, Expected0}) ->
"against the expected type\n~s\n",
[pp_loc(Expr), pp_typed(" ", Expr, Inferred),
pp_type(" ", Expected)]);
pp_when({checking_init_type, Ann}) ->
io_lib:format("when checking that 'init' returns a value of type 'state' at ~s\n",
[pp_loc(Ann)]);
pp_when(unknown) -> "".
-spec pp_why_record(why_record()) -> iolist().

View File

@ -133,4 +133,12 @@ failing_contracts() ->
"Repeated name x in pattern\n"
" x :: x (at line 26, column 7)\n",
"No record type with fields y, z (at line 14, column 22)\n"]}
, {"init_type_error",
["Cannot unify string\n"
" and map(int, int)\n"
"when checking that 'init' returns a value of type 'state' at line 7, column 3\n"]}
, {"missing_state_type",
["Cannot unify string\n"
" and ()\n"
"when checking that 'init' returns a value of type 'state' at line 5, column 3\n"]}
].

View File

@ -0,0 +1,8 @@
contract InitTypeError =
type state = map(int, int)
// Check that the compiler catches ill-typed init function
function init() = "not the right type!"

View File

@ -0,0 +1,6 @@
contract MissingStateType =
// Check that we get a type error also for implicit state
function init() = "should be ()"