Skip to content

Clause cannot be reached error #52

@Fl4m3Ph03n1x

Description

@Fl4m3Ph03n1x

Background

I am trying to implement a couple of protocols in a module but Gradualizer is complaining saying my code is not reachable. The code works without issues, so I am confused.

Code

Here is a simplification of the source code:

defmodule Option do

  defmodule Some do
    @type t(elem) :: %__MODULE__{val: elem}

    defstruct [:val]

    defimpl Collectable do
      @impl Collectable
      def into(option), do: {option, fn acc, _command -> {:done, acc} end}
    end

    defimpl Enumerable do
      @impl Enumerable
      def count(_some), do: {:ok, 1}

      @impl Enumerable
      def member?(some, element), do: {:ok, some.val == element}

      @impl Enumerable
      def reduce(some, acc, fun)

      def reduce(_some, {:halt, acc}, _fun), do: {:halted, acc}
      def reduce(some, {:suspend, acc}, fun), do: {:suspended, acc, &reduce(some, &1, fun)}
      def reduce([], {:cont, acc}, _fun), do: {:done, acc}

      def reduce(%Option.Some{} = some, {:cont, acc}, fun),
        do: reduce([], fun.(some.val, acc), fun)

      def reduce(val, {:cont, acc}, fun), do: reduce([], fun.(val, acc), fun)

      @impl Enumerable
      @spec slice(%Option.Some{}) :: {:error, Enumerable.Option.Some}
      def slice(_option), do: {:error, __MODULE__}
    end
  end

end

Here the errors:

$ mix gradient
Compiling 1 file (.ex)
Generated simple_monads app
Typechecking files...
lib/option.ex: The clause on line 9 cannot be reached

lib/option.ex: The clause on line 14 cannot be reached

lib/option.ex: The pattern %Option.Some{} on line 28 doesn't have the type any()

line 9: defimpl Collectable do
line 14: defimpl Enumerable do
line 28 does not accept any, but there is a clause for that after. By that logic, the line def reduce([], {:cont, acc}, _fun), do: {:done, acc} does not accept any as the first parameter and yet the tool wont complain.

Simply put, I cover all cases of the spec.

I would expect to see no errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions