Skip to content

Commit

Permalink
Merge pull request erlang#9069 from bjorng/bjorn/compiler/zip-coverage
Browse files Browse the repository at this point in the history
Fix code coverage for zip generators
  • Loading branch information
bjorng authored Nov 22, 2024
2 parents cdd61f5 + 56240a8 commit 270cec6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/compiler/src/compile.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,7 @@ abstr_passes(AbstrStatus) ->
{delay,[{iff,debug_info,?pass(save_abstract_code)}]},

{delay,[{iff,line_coverage,{pass,sys_coverage}}]},
{iff,'dcover',{src_listing,"cover"}},

?pass(expand_records),
{iff,'dexp',{listing,"expand"}},
Expand Down
28 changes: 23 additions & 5 deletions lib/compiler/src/sys_coverage.erl
Original file line number Diff line number Diff line change
Expand Up @@ -574,18 +574,36 @@ munge_qs([{m_generate_strict,Anno,Pattern,Expr}|Qs], Vars0, MQs) ->
{MExpr, Vars1} = munge_expr(Expr, Vars0),
munge_qs1(Qs, A, {m_generate_strict,Anno,Pattern,MExpr}, Vars0, Vars1, MQs);
munge_qs([{zip,Anno,Gs0}|Qs], Vars0, MQs) ->
{Gs1, Vars1} = munge_qualifiers(Gs0, Vars0),
%% Get rid of dummy filters inserted by munge_qualifiers/2 --
%% they are not allowed in the zip construct.
Gs = [G || G <- Gs1, element(1, G) =/= block],
munge_qs1(Qs, Anno, {zip,Anno,Gs}, Vars0, Vars1, MQs);
{Gs, Pre, Vars1} = munge_zip(Gs0, Vars0),
munge_qs1(Qs, Anno, {zip,Anno,Gs}, Vars0, Vars1, Pre ++ MQs);
munge_qs([Expr|Qs], Vars0, MQs) ->
A = element(2, Expr),
{MungedExpr, Vars1} = munge_expr(Expr, Vars0),
munge_qs1(Qs, A, MungedExpr, Vars0, Vars1, MQs);
munge_qs([], Vars0, MQs) ->
{reverse(MQs), Vars0}.

munge_zip([G0|Gs0], Vars0) ->
{Gen,Anno,Pattern,Expr} = G0,
{MungedExpr, Vars1} = munge_expr(Expr, Vars0),
G1 = {Gen,Anno,Pattern,MungedExpr},
case munge_qs1([], Anno, G1, Vars0, Vars1, []) of
{[{block,_,_}=Blk,G], Vars2} ->
{Gs, Vars3} = munge_zip_1(Gs0, Vars2, [G]),
{Gs, [Blk], Vars3};
{[G], Vars2} ->
{Gs, Vars3} = munge_zip_1(Gs0, Vars2, [G]),
{Gs, [], Vars3}
end.

munge_zip_1([G0|Gs], Vars0, Acc) ->
{Gen,Anno,Pattern,Expr} = G0,
{MungedExpr, Vars1} = munge_expr(Expr, Vars0),
G1 = {Gen,Anno,Pattern,MungedExpr},
munge_zip_1(Gs, Vars1, [G1|Acc]);
munge_zip_1([], Vars, Acc) ->
{reverse(Acc), Vars}.

munge_qs1(Qs, Anno, NQ, Vars0, Vars1, MQs) ->
case new_bumps(Vars1, Vars0) of
[_] ->
Expand Down
36 changes: 33 additions & 3 deletions lib/compiler/test/zlc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
init_per_testcase/2,end_per_testcase/2,
basic/1,mixed_zlc/1,zmc/1,filter_guard/1,
filter_pattern/1,cartesian/1,nomatch/1,bad_generators/1,
strict_list/1,strict_binary/1]).
strict_list/1,strict_binary/1,
cover/1]).

-include_lib("common_test/include/ct.hrl").
-include_lib("stdlib/include/assert.hrl").
Expand All @@ -47,7 +48,8 @@ groups() ->
nomatch,
bad_generators,
strict_list,
strict_binary
strict_binary,
cover
]}].

init_per_suite(Config) ->
Expand Down Expand Up @@ -287,7 +289,7 @@ strict_list(Config) when is_list(Config) ->
NaN = <<-1:64>>,
[] = strict_list_5(<<>>, <<>>),
[3.14] = strict_list_5(<<0:1,1:1>>, <<32,0.0:32/float, 64,3.14:64/float>>),
[0.0,3.14] = strict_list_5(<<1:1,1:1>>, <<32,0.0:32/float, 64,3.14:64/float>>),
[+0.0,3.14] = strict_list_5(<<1:1,1:1>>, <<32,0.0:32/float, 64,3.14:64/float>>),
{'EXIT',{{bad_generators,{<<>>,<<64,42.0/float>>}},_}} =
catch strict_list_5(<<>>, <<64,42.0/float>>),
{'EXIT',{{bad_generators,{<<0:1,1:1>>,
Expand Down Expand Up @@ -440,6 +442,34 @@ bad_generators(Config) when is_list(Config) ->
end,
ok.

%% Cover some code in sys_coverage.
cover(Config) when is_list(Config) ->
[] = do_cover_1([], []),
[11,12,13] = do_cover_1([1,2,3], [10,10,10]),

ok.

do_cover_1(L1, L2) ->
Res = [A + B || A <- begin L1 end && B <- L2],
Res = [A + B || A <-
begin L1 end &&
B <- L2],
Res = [A + B ||
A <-
begin L1 end &&
B <-
begin L2 end],
Res = [A + B ||
A <-
begin
L1
end &&
B <-
begin
L2
end],
Res.

-file("bad_zlc.erl", 1).
bad_generators(L1,L2) -> %Line 2
[{I1, I2} || %Line 3
Expand Down

0 comments on commit 270cec6

Please sign in to comment.