Skip to content

dilayzer warning when using :erlang.exit in a with else clause #10385

@albertored

Description

@albertored

Environment

  • Elixir & Erlang/OTP versions (elixir --version): Elixir 1.10.4 (compiled with Erlang/OTP 21)

Current behavior

I get a strange dialyzer warning when writing a custom mix task.

I don't know if it is really a bug or if is related to elixir so feel free to close it.

I want the task to exit with different exit codes based on the error, this is a minimal example showing my issue

defmodule A do
  def a do
    with :ok <- File.cp("a", "b"),
         {:ok, _} <- File.rm_rf("a") do
      :ok
    else
      {:error, _err} ->
        IO.puts("1")
        exit({:shutdown, 1})

      _ ->
        IO.puts("2")
        exit({:shutdown, 2})
    end
  end
end

and this is the warning I get on the File.cp line

The pattern can never match the type.

Pattern:
{:error, __err}

Type:
{:error, atom(), binary()}

The warning is related to the use of :erlang.exit/1, removing those calls the warning disappears. It seems that after encountering an :erlang.exit/1 dialyzer stops and doesn't see the next clauses of the else.

I can remove the warning by moving the :erlang.exit/1 call in a case statement but I was wondering why this happens and if it can be some sort of bug.

defmodule A do
  def a do
    with :ok <- File.cp("a", "b"),
         {:ok, _} <- File.rm_rf("a") do
      :ok
    else
      {:error, _err} -> {:error, 1}
      _ -> {:error, 2}
    end
    |> case do
      :ok -> :ok
      {:error, exit_code} -> exit({:shutdown, exit_code})
    end
  end
end

Expected behavior

No dialyzer warnings

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions