From 14d479d583872e62e96713b15021cb656ec488be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Thu, 17 Nov 2022 06:23:43 +0100 Subject: Don't allow local calls from record init in guards Calling functions in guards is forbidden, but the compiler would fail to reject a function call from a default record initializer used in a guard. For example: -record(test, {a = mk_a()}). mk_a() -> 1. test_rec(Rec) when Rec =:= #test{} -> true. Before this commit, the compiler would fail to reject the illegal call to `mk_a/0`, and crash in a later pass. The compiler will now give the following error message: t.erl:7:36: call to local/imported function mk_a/0 is illegal in guard % 7| test_rec(Rec) when Rec =:= #test{} -> % | ^ Closes #6465 Closes #6466 --- lib/stdlib/test/erl_lint_SUITE.erl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'lib/stdlib/test') diff --git a/lib/stdlib/test/erl_lint_SUITE.erl b/lib/stdlib/test/erl_lint_SUITE.erl index c54efc20c6..c8c07de212 100644 --- a/lib/stdlib/test/erl_lint_SUITE.erl +++ b/lib/stdlib/test/erl_lint_SUITE.erl @@ -1645,8 +1645,20 @@ guard(Config) when is_list(Config) -> [], {error, [{{2,26},erl_lint,{obsolete_guard_overridden,port}}], - [{{2,26},erl_lint,{obsolete_guard,{port,1}}}]}} - ], + [{{2,26},erl_lint,{obsolete_guard,{port,1}}}]}}, + {guard11, + <<"-record(bar, {a = mk_a()}). + mk_a() -> 1. + + test_rec(Rec) when Rec =:= #bar{} -> true. + map_pattern(#{#bar{} := _}) -> ok. + ">>, + [], + {errors, + [{{4,43},erl_lint,{illegal_guard_local_call,{mk_a,0}}}, + {{5,30},erl_lint,{illegal_guard_local_call,{mk_a,0}}}], + []}} + ], [] = run(Config, Ts1), ok. -- cgit v1.2.1