Skip to content

Commit 17853e6

Browse files
josevalimJosé Valim
authored andcommitted
Ensure bitstring comprehensions work on OTP 20.3 (#7453)
Closes #7452 Signed-off-by: José Valim <[email protected]>
1 parent 338e242 commit 17853e6

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/elixir/lib/collectable.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ defimpl Collectable, for: BitString do
101101
acc, {:cont, x} when is_bitstring(x) ->
102102
<<IO.iodata_to_binary(acc)::bitstring, x::bitstring>>
103103

104+
acc, :done when is_bitstring(acc) ->
105+
acc
106+
104107
acc, :done ->
105108
IO.iodata_to_binary(acc)
106109

lib/elixir/src/elixir_erl_for.erl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ translate(Meta, Args, Return, S) ->
2121

2222
case comprehension_expr(TInto, TExpr) of
2323
{inline, TIntoExpr} ->
24-
{build_inline(Ann, TCases, TIntoExpr, TInto, TUniq, SF), SF};
24+
build_inline(Ann, TCases, TIntoExpr, TInto, TUniq, SF);
2525
{into, TIntoExpr} ->
2626
build_into(Ann, TCases, TIntoExpr, TInto, TUniq, SF)
2727
end.
@@ -91,17 +91,17 @@ collect_filters([], Acc) ->
9191

9292
build_inline(Ann, Clauses, Expr, Into, Uniq, S) ->
9393
case not Uniq and lists:all(fun(Clause) -> element(1, Clause) == bin end, Clauses) of
94-
true -> build_comprehension(Ann, Clauses, Expr, Into);
94+
true -> {build_comprehension(Ann, Clauses, Expr, Into), S};
9595
false -> build_inline_each(Ann, Clauses, Expr, Into, Uniq, S)
9696
end.
9797

9898
build_inline_each(Ann, Clauses, Expr, false, Uniq, S) ->
9999
InnerFun = fun(InnerExpr, _InnerAcc) -> InnerExpr end,
100-
build_reduce(Ann, Clauses, InnerFun, Expr, {nil, Ann}, Uniq, S);
100+
{build_reduce(Ann, Clauses, InnerFun, Expr, {nil, Ann}, Uniq, S), S};
101101
build_inline_each(Ann, Clauses, Expr, {nil, _} = Into, Uniq, S) ->
102102
InnerFun = fun(InnerExpr, InnerAcc) -> {cons, Ann, InnerExpr, InnerAcc} end,
103103
ReduceExpr = build_reduce(Ann, Clauses, InnerFun, Expr, Into, Uniq, S),
104-
elixir_erl:remote(Ann, lists, reverse, [ReduceExpr]);
104+
{elixir_erl:remote(Ann, lists, reverse, [ReduceExpr]), S};
105105
build_inline_each(Ann, Clauses, Expr, {bin, _, []}, Uniq, S) ->
106106
{InnerValue, SV} = build_var(Ann, S),
107107

@@ -135,11 +135,21 @@ build_inline_each(Ann, Clauses, Expr, {bin, _, []}, Uniq, S) ->
135135
end,
136136

137137
ReduceExpr = build_reduce(Ann, Clauses, InnerFun, Expr, {nil, Ann}, Uniq, SV),
138-
elixir_erl:remote(Ann, erlang, iolist_to_binary, [ReduceExpr]).
138+
139+
{{'case', Ann, ReduceExpr, [
140+
{clause, Ann,
141+
[InnerValue],
142+
[[elixir_erl:remote(Ann, erlang, is_bitstring, [InnerValue])]],
143+
[InnerValue]},
144+
{clause, Ann,
145+
[InnerValue],
146+
[],
147+
[elixir_erl:remote(Ann, erlang, iolist_to_binary, [InnerValue])]}
148+
]}, SV}.
139149

140150
build_into(Ann, Clauses, Expr, {map, _, []}, Uniq, S) ->
141-
ReduceExpr = build_inline_each(Ann, Clauses, Expr, {nil, Ann}, Uniq, S),
142-
{elixir_erl:remote(Ann, maps, from_list, [ReduceExpr]), S};
151+
{ReduceExpr, SR} = build_inline_each(Ann, Clauses, Expr, {nil, Ann}, Uniq, S),
152+
{elixir_erl:remote(Ann, maps, from_list, [ReduceExpr]), SR};
143153
build_into(Ann, Clauses, Expr, Into, Uniq, S) ->
144154
{Fun, SF} = build_var(Ann, S),
145155
{Acc, SA} = build_var(Ann, SF),

0 commit comments

Comments
 (0)