ex_type icon indicating copy to clipboard operation
ex_type copied to clipboard

Support inline modules

Open turion opened this issue 4 years ago • 1 comments

Consider this minimal example:

defmodule Foo do
  defmodule Bar do
    defstruct [:bar]
  end

  @spec hello(%Bar{}) :: :world
  def hello(_) do
    :world
  end
end
$ mix type
==> ex_type
Compiling 2 files (.ex)
✅  Foo.hello/1
** (CompileError) lib/foo.ex:6: Bar.__struct__/0 is undefined, cannot expand struct Bar. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code
    (elixir 1.10.4) lib/kernel/typespec.ex:549: Kernel.Typespec.typespec/4
    (stdlib 3.12) lists.erl:1354: :lists.mapfoldl/3
    (elixir 1.10.4) lib/kernel/typespec.ex:950: Kernel.Typespec.fn_args/5
    (elixir 1.10.4) lib/kernel/typespec.ex:936: Kernel.Typespec.fn_args/6
    (elixir 1.10.4) lib/kernel/typespec.ex:377: Kernel.Typespec.translate_spec/8
    (stdlib 3.12) lists.erl:1354: :lists.mapfoldl/3
    (elixir 1.10.4) lib/kernel/typespec.ex:229: Kernel.Typespec.translate_typespecs_for_module/2

I'll try and submit a PR.

turion avatar Dec 08 '20 15:12 turion

Here is a more sensible program that works as intended and shows the same error:

defmodule Foo do
  defmodule Bar do
    @enforce_keys [:bar]
    defstruct @enforce_keys

    @type t(bar) :: %Bar{
      bar: bar
    }
  end

  @spec get_bar(Bar.t(bar)) :: bar when bar: any()
  def get_bar(%Bar{bar: bar}) do
    bar
  end
end

turion avatar Dec 08 '20 15:12 turion