Skip to content

Commit 276b0d3

Browse files
author
José Valim
committed
Ensure comprehensions with guards and filters keep proper ordering, closes #5509
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
1 parent abb8817 commit 276b0d3

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/elixir/src/elixir_for.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ translate_gen(_Meta, Left, Right, T, S) ->
114114
SF = SL#elixir_scope{extra=S#elixir_scope.extra, extra_guards=nil},
115115

116116
{TT, {TFilters, TS}} = translate_filters(T, SF),
117-
Guards = ExtraGuards ++ translate_guards(TLeftGuards) ++ TFilters,
117+
118+
%% The list of guards is kept in reverse order
119+
Guards = TFilters ++ translate_guards(TLeftGuards) ++ ExtraGuards,
118120
{TLeft, TRight, Guards, TT, TS}.
119121

120122
translate_guards([]) ->

lib/elixir/test/elixir/kernel/comprehension_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ defmodule Kernel.ComprehensionTest do
5151
assert for(x when x == 3 when x == 7 <- 1..10, do: x) == [3, 7]
5252
end
5353

54+
test "for comprehensions with guards and filters" do
55+
assert for({var, _} when is_atom(var) <- [{:foo, 1}, {2, :bar}],
56+
var = Atom.to_string(var),
57+
do: var) == ["foo"]
58+
end
59+
5460
test "for comprehensions with map key matching" do
5561
maps = [%{x: 1}, %{y: 2}, %{x: 3}]
5662
assert for(%{x: v} <- maps, do: v * 2) == [2, 6]

0 commit comments

Comments
 (0)