Skip to content

Commit c5cbe7c

Browse files
committed
Infer return types for for+:reduce
1 parent 984620a commit c5cbe7c

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,10 @@ defmodule Module.Types.Expr do
346346
# We handle reduce and into accordingly instead.
347347
if Keyword.has_key?(opts, :reduce) do
348348
reduce = Keyword.fetch!(opts, :reduce)
349-
{_, context} = of_expr(reduce, stack, context)
350-
{_, context} = of_clauses(block, [dynamic()], :for_reduce, stack, {none(), context})
351-
{dynamic(), context}
349+
{reduce_type, context} = of_expr(reduce, stack, context)
350+
# TODO: We need to type check against dynamic() instead of using reduce_type
351+
# because this is recursive. We need to infer the block type first.
352+
of_clauses(block, [dynamic()], :for_reduce, stack, {reduce_type, context})
352353
else
353354
into = Keyword.get(opts, :into, [])
354355
{into_wrapper, gradual?, context} = for_into(into, meta, stack, context)

lib/elixir/lib/module/types/pattern.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ defmodule Module.Types.Pattern do
838838
end
839839
end
840840

841-
defp badpattern(:default, pattern_or_expr, _) do
841+
defp badpattern(_, pattern_or_expr, _) do
842842
{pattern_or_expr,
843843
"""
844844
the following pattern will never match:

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,16 @@ defmodule Module.Types.ExprTest do
14601460
)
14611461
) == dynamic(union(binary(), list(float())))
14621462
end
1463+
1464+
test ":reduce" do
1465+
assert typecheck!(
1466+
[list],
1467+
for _ <- list, reduce: :ok do
1468+
:ok -> 1
1469+
_ -> 2.0
1470+
end
1471+
) == union(atom([:ok]), union(integer(), float()))
1472+
end
14631473
end
14641474

14651475
describe "info" do

0 commit comments

Comments
 (0)