Skip to content

Commit ffbcc00

Browse files
committed
Do not crash when :reduce is set to nil in comprehensions, closes #10411
1 parent e0d61c7 commit ffbcc00

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

lib/elixir/lib/module/types/expr.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ defmodule Module.Types.Expr do
289289

290290
with {:ok, context} <- reduce_ok(clauses, context, &for_clause(&1, stack, &2)),
291291
{:ok, context} <- reduce_ok(opts, context, &for_option(&1, stack, &2)) do
292-
if opts[:reduce] do
292+
if Keyword.has_key?(opts, :reduce) do
293293
with {:ok, context} <- of_clauses(block, stack, context) do
294294
{:ok, :dynamic, context}
295295
end

lib/elixir/test/elixir/module/types/expr_test.exs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -250,27 +250,43 @@ defmodule Module.Types.ExprTest do
250250
) == {:ok, {:var, 0}}
251251
end
252252

253-
test "for" do
254-
assert quoted_expr(
255-
[list],
256-
for(
257-
foo <- list,
258-
is_integer(foo),
259-
do: foo == 123
260-
)
261-
) == {:ok, :dynamic}
262-
263-
assert quoted_expr(
264-
[list, bar],
265-
(
253+
describe "for comprehension" do
254+
test "with generators and filters" do
255+
assert quoted_expr(
256+
[list],
266257
for(
267258
foo <- list,
268-
is_integer(bar),
259+
is_integer(foo),
269260
do: foo == 123
270261
)
262+
) == {:ok, :dynamic}
263+
end
271264

272-
bar
273-
)
274-
) == {:ok, {:var, 0}}
265+
test "with unused return" do
266+
assert quoted_expr(
267+
[list, bar],
268+
(
269+
for(
270+
foo <- list,
271+
is_integer(bar),
272+
do: foo == 123
273+
)
274+
275+
bar
276+
)
277+
) == {:ok, {:var, 0}}
278+
end
279+
280+
test "with reduce" do
281+
assert quoted_expr(
282+
[],
283+
for(i <- [1, 2, 3], do: (acc -> i + acc), reduce: 0)
284+
) == {:ok, :dynamic}
285+
286+
assert quoted_expr(
287+
[],
288+
for(i <- [1, 2, 3], do: (_ -> i), reduce: nil)
289+
) == {:ok, :dynamic}
290+
end
275291
end
276292
end

0 commit comments

Comments
 (0)