Skip to content

Commit

Permalink
fix for binary tail_pat
Browse files Browse the repository at this point in the history
  • Loading branch information
lucioleKi committed Nov 4, 2024
1 parent 4cf83d5 commit 5e527b8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
10 changes: 10 additions & 0 deletions lib/compiler/src/v3_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,7 @@ preprocess_quals(Line, [{zip,Anno,Gens}|Qs], St, Acc) ->
acc_guard=AccGuard},
Zip1 = preprocess_zip_generators(Gens1, Zip0),
Zip2 = Zip1#izip{skip_pats=preprocess_skip(Zip1#izip.nomatch_total,Zip1#izip.nomatch_pats,Zip1#izip.acc_pats),
tail_pats=preprocess_tail(Zip1#izip.nomatch_total,Zip1#izip.nomatch_pats,Zip1#izip.tail_pats,[]),
nomatch_total=get_nomatch_total(Zip1#izip.nomatch_total)},
preprocess_quals(Line, Qs, St1, [Zip2|Acc]);
preprocess_quals(Line, [Q|Qs0], St0, Acc) ->
Expand Down Expand Up @@ -2035,6 +2036,15 @@ preprocess_skip(NomatchModes, NomatchPats, AccPats) ->
end || {NomatchMode, NomatchPat, AccPat} <:-
lists:zip3(NomatchModes, NomatchPats, AccPats)].

preprocess_tail([skip|NomatchModes], [_|NomatchPats], [TailPat|TailPats], Acc) ->
preprocess_tail(NomatchModes, NomatchPats, TailPats, [TailPat|Acc]);
preprocess_tail([_|NomatchModes], [_|NomatchPats], [#ibinary{}=A|TailPats], Acc) ->
preprocess_tail(NomatchModes, NomatchPats, TailPats, [A#ibinary{segments=[]}|Acc]);
preprocess_tail([_|NomatchModes], [_|NomatchPats], [TailPat|TailPats], Acc) ->
preprocess_tail(NomatchModes, NomatchPats, TailPats, [TailPat|Acc]);
preprocess_tail([], [], [], Acc) ->
reverse(Acc).

is_generator({generate,_,_,_}) -> true;
is_generator({generate_strict,_,_,_}) -> true;
is_generator({b_generate,_,_,_}) -> true;
Expand Down
22 changes: 22 additions & 0 deletions lib/compiler/test/zlc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ strict_list(Config) when is_list(Config) ->
{'EXIT',{{bad_generators,{[a],[],<<>>}},_}} =
catch strict_list_strict_2([a], [], <<>>),

[] = strict_list_strict_3([], <<>>),
[45] = strict_list_strict_3([{i,42}], <<3>>),
{'EXIT',{{bad_generators,{[],<<0:7>>}},_}} = catch strict_list_strict_3([], <<0:7>>),
[] = strict_list_strict_4([], <<>>),
[100] = strict_list_strict_4([{i,100}], <<42>>),
{'EXIT',{{bad_generators,{[{i,100}],<<0>>}},_}} = catch strict_list_strict_4([{i,100}], <<0>>),
{'EXIT',{{bad_generators,{[{i,100}],<<>>}},_}} = catch strict_list_strict_4([{i,100}], <<>>),

ok.

strict_list_mixed_1(X, Y) ->
Expand All @@ -264,13 +272,27 @@ strict_list_strict_1(X, Y) ->
strict_list_strict_2(X, Y, Z) ->
[A * B + C || {i,A} <:- X && {i,B} <:- Y && <<C:8>> <:= Z].

strict_list_strict_3(List, Bin) ->
[A + B || {i,A} <:- List && <<B:8>> <:= Bin].

strict_list_strict_4(List, Bin) ->
[A || {i,A} <:- List && <<42:8>> <:= Bin].

strict_binary(Config) when is_list(Config) ->
<<2,4,6>> = << <<(X+Y)>> || X <:- [1,2,3] && <<Y>> <= <<1,2,3>>>>,
<<2,4>> = << <<(X+Y)>> || <<X>> <:= <<1,2,3>> && {X, Y} <- [{1,1},{2,2},{2,3}]>>,
<<2,24>> = << <<(X*Y*Z)>> || X := Y <:- #{1 => 2, 3 => 4} && <<Z>> <:= <<1,2>> >>,

<<>> = strict_binary_1(#{}, <<>>),
<<24:64>> = strict_binary_1(#{2 => {val,3}}, <<4:8>>),
{'EXIT',{{bad_generators,{none,<<42:8>>}},_}} = catch strict_binary_1(#{}, <<42:8>>),
{'EXIT',{{bad_generators,{none,<<42:7>>}},_}} = catch strict_binary_1(#{}, <<42:7>>),
{'EXIT',{{bad_generators,{none,<<0:4>>}},_}} = catch strict_binary_1(#{2 => {val,3}}, <<0,0:4>>),
ok.

strict_binary_1(Map, Bin) ->
<< <<(X*Y*Z):64>> || X := {val,Y} <:- Map && <<Z:8>> <:= Bin >>.

nomatch(Config) when is_list(Config) ->
[] = do_nomatch_1([], []),
[] = do_nomatch_1([1], [a]),
Expand Down
22 changes: 22 additions & 0 deletions lib/debugger/test/zlc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ strict_list(Config) when is_list(Config) ->
{'EXIT',{{bad_generators,{[a],[],<<>>}},_}} =
catch strict_list_strict_2([a], [], <<>>),

[] = strict_list_strict_3([], <<>>),
[45] = strict_list_strict_3([{i,42}], <<3>>),
{'EXIT',{{bad_generators,{[],<<0:7>>}},_}} = catch strict_list_strict_3([], <<0:7>>),
[] = strict_list_strict_4([], <<>>),
[100] = strict_list_strict_4([{i,100}], <<42>>),
{'EXIT',{{bad_generators,{[{i,100}],<<0>>}},_}} = catch strict_list_strict_4([{i,100}], <<0>>),
{'EXIT',{{bad_generators,{[{i,100}],<<>>}},_}} = catch strict_list_strict_4([{i,100}], <<>>),

ok.

strict_list_mixed_1(X, Y) ->
Expand All @@ -261,13 +269,27 @@ strict_list_strict_1(X, Y) ->
strict_list_strict_2(X, Y, Z) ->
[A * B + C || {i,A} <:- X && {i,B} <:- Y && <<C:8>> <:= Z].

strict_list_strict_3(List, Bin) ->
[A + B || {i,A} <:- List && <<B:8>> <:= Bin].

strict_list_strict_4(List, Bin) ->
[A || {i,A} <:- List && <<42:8>> <:= Bin].

strict_binary(Config) when is_list(Config) ->
<<2,4,6>> = << <<(X+Y)>> || X <:- [1,2,3] && <<Y>> <= <<1,2,3>>>>,
<<2,4>> = << <<(X+Y)>> || <<X>> <:= <<1,2,3>> && {X, Y} <- [{1,1},{2,2},{2,3}]>>,
<<2,24>> = << <<(X*Y*Z)>> || X := Y <:- #{1 => 2, 3 => 4} && <<Z>> <:= <<1,2>> >>,

<<>> = strict_binary_1(#{}, <<>>),
<<24:64>> = strict_binary_1(#{2 => {val,3}}, <<4:8>>),
{'EXIT',{{bad_generators,{none,<<42:8>>}},_}} = catch strict_binary_1(#{}, <<42:8>>),
{'EXIT',{{bad_generators,{none,<<42:7>>}},_}} = catch strict_binary_1(#{}, <<42:7>>),
{'EXIT',{{bad_generators,{none,<<0:4>>}},_}} = catch strict_binary_1(#{2 => {val,3}}, <<0,0:4>>),
ok.

strict_binary_1(Map, Bin) ->
<< <<(X*Y*Z):64>> || X := {val,Y} <:- Map && <<Z:8>> <:= Bin >>.

nomatch(Config) when is_list(Config) ->
[] = do_nomatch_1([], []),
[] = do_nomatch_1([1], [a]),
Expand Down
3 changes: 0 additions & 3 deletions lib/stdlib/src/erl_error.erl
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,6 @@ explain_reason({badfun,Term}, error=Cl, [], PF, S, _Enc, CL) ->
explain_reason({badmatch,Term}, error=Cl, [], PF, S, _Enc, CL) ->
Str = <<"no match of right hand side value ">>,
format_value(Term, Str, Cl, PF, S, CL);
explain_reason({badmatches,Terms}, error=Cl, [], PF, S, _Enc, CL) ->
Str = <<"no match of right hand side values ">>,
format_value(Terms, Str, Cl, PF, S, CL);
explain_reason({case_clause,V}, error=Cl, [], PF, S, _Enc, CL) ->
%% "there is no case clause with a true guard sequence and a
%% pattern matching..."
Expand Down

0 comments on commit 5e527b8

Please sign in to comment.