Skip to content

Type system crashes when callbacks are injected into protocols #15456

Description

@ityonemo

Existing issue

  • I have searched existing issues and could not find a duplicate.

Elixir and Erlang/OTP versions

Erlang/OTP 28 [erts-16.3] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Elixir 1.19.5 (compiled with Erlang/OTP 28)

and

Erlang/OTP 29 [erts-17.0.1] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Elixir 1.20.1 (compiled with Erlang/OTP 29)

Operating system

Linux

Current behavior

The following code crashes with the following stacktrace:

== Compilation error in file test/after_callback_test.exs ==
** (FunctionClauseError) no function clause matching in List.duplicate/2    
    
    The following arguments were given to List.duplicate/2:
    
        # 1
        %{dynamic: :term}
    
        # 2
        -1
    
    Attempted function clauses (showing 1 out of 1):
    
        def duplicate(elem, n) when is_integer(n) and n >= 0
    
    (elixir 1.20.1) lib/list.ex:190: List.duplicate/2
    (elixir 1.20.1) lib/module/types.ex:155: Module.Types.default_domain/4
    (elixir 1.20.1) lib/module/types.ex:301: Module.Types.local_handler/5
    (elixir 1.20.1) lib/module/types.ex:86: anonymous fn/5 in Module.Types.infer/7
    (elixir 1.20.1) lib/enum.ex:2622: Enum."-reduce/3-lists^foldl/2-0-"/3
    (elixir 1.20.1) lib/module/types.ex:78: Module.Types.infer/7

Code:

  defmodule InjectCallback do
    defmacro __before_compile__(_env) do
      quote do
        @callback extra() :: term()
      end
    end
  end

  defprotocol P do
    @before_compile InjectCallback
    def f(x)
  end

  defmodule S do
    defstruct []
  end

  defimpl P, for: S do
    def f(_), do: :ok
    def extra(), do: :extra
  end

The InjectCallback exists to suppress the warning to not create additional non-protocol callbacks. The following code will also crash the type checker (does not crash the type checker in <= 1.18), though it does emit a warning:

  defprotocol P do
    @callback extra() :: term
    def f(x)
  end

  defmodule S do
    defstruct []
  end

  defimpl P, for: S do
    def f(_), do: :ok
    def extra(), do: :extra
  end

Expected behavior

Expected behavior: No failure (as with 1.18.x)

Anyways, this is obviously behavior that "is discouraged", but not obvious that it should be banned (or crash the type checker in any case)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions