elixir icon indicating copy to clipboard operation
elixir copied to clipboard

Type checker errantly warns when `then/1` fn has a fallback clause

Open remotecom opened this issue 5 months ago • 1 comments

Elixir and Erlang/OTP versions

Erlang/OTP 27 [erts-15.2.6] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]

Elixir 1.19.0-rc.0 (2a9a4f2) (compiled with Erlang/OTP 27)

Operating system

macOS 14.7.6 (23H626)

Current behavior

When we pass a fn with a fallback clause into then/1, the type checker is confused :)

defmodule A do
  def a() do
    case b() do
      :ok -> :ok
      :error -> :error
    end
  end

  defp b() do
    [true, false]
    |> Enum.random()
    |> then(fn
      true -> :ok
      _ -> :error
    end)
  end
end
    warning: the following clause will never match:

        :ok

    because it attempts to match on the result of:

        b()

    which has type:

        dynamic(:error)

    typing violation found at:
    │
  4 │       :ok -> :ok
    │       ~~~~~~~~~~
    │
    └─ a.ex:4: A.a/0

Expected behavior

Shouldn't be a violation

remotecom avatar Jun 21 '25 14:06 remotecom

Thank you! We are already working on it on a branch!

josevalim avatar Jun 21 '25 14:06 josevalim