Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias %__MODULE__ breaks submodules #27

Open
turion opened this issue Dec 9, 2020 · 1 comment
Open

Alias %__MODULE__ breaks submodules #27

turion opened this issue Dec 9, 2020 · 1 comment

Comments

@turion
Copy link
Contributor

turion commented Dec 9, 2020

I have two files:

defmodule Foo.Bar do
  alias __MODULE__

  @type t :: %Bar.Baz{}
end
defmodule Foo.Bar.Baz do
  defstruct []
end

mix compile works, but mix type doesn't:

$ mix type
Compiling 1 file (.ex)
** (CompileError) lib/foo/bar.ex:4: ExType.Module.Foo.Bar.Baz.__struct__/0 is undefined, cannot expand struct ExType.Module.Foo.Bar.Baz. 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
    (elixir 1.10.4) lib/kernel/typespec.ex:294: Kernel.Typespec.translate_type/2
    (stdlib 3.12) lists.erl:1354: :lists.mapfoldl/3
    (elixir 1.10.4) lib/kernel/typespec.ex:228: Kernel.Typespec.translate_typespecs_for_module/2
    (elixir 1.10.4) src/elixir_erl_compiler.erl:12: anonymous fn/3 in :elixir_erl_compiler.spawn/2

It seems like mix compile creates the modules in a cleverer order.

More context

When we put both modules in one file, some variants work and others don't:

Doesn't work

defmodule Foo.Bar do
  alias Foo.Bar # Also with __MODULE__

  @type t :: %Bar.Baz{}
end

defmodule Foo.Bar.Baz do
  defstruct []
end

Works

defmodule Foo.Bar.Baz do
  defstruct []
end

defmodule Foo.Bar do
  alias Foo.Bar # Also with __MODULE__

  @type t :: %Bar.Baz{}
end
defmodule Foo.Bar do
  defmodule Baz do
    defstruct []
  end

  alias __MODULE__

  @type t :: %Bar.Baz{}
end
defmodule Foo.Bar do
  alias __MODULE__

  defmodule Baz do
    defstruct []
  end

  @type t :: %Bar.Baz{}
end
defmodule Foo.Bar do
  alias __MODULE__

  @type t :: %Bar.Baz{}

  defmodule Baz do
    defstruct []
  end
end
@turion
Copy link
Contributor Author

turion commented Dec 9, 2020

An acceptable workaround might be manually expanding __MODULE__.

turion pushed a commit to turion/ex_type that referenced this issue Dec 9, 2020
@turion turion mentioned this issue Dec 9, 2020
turion pushed a commit to turion/ex_type that referenced this issue Dec 9, 2020
turion pushed a commit to turion/ex_type that referenced this issue Dec 25, 2020
turion pushed a commit to turion/ex_type that referenced this issue Dec 25, 2020
turion pushed a commit to turion/ex_type that referenced this issue Dec 26, 2020
turion pushed a commit to turion/ex_type that referenced this issue Dec 26, 2020
turion pushed a commit to turion/ex_type that referenced this issue Feb 24, 2021
turion pushed a commit to turion/ex_type that referenced this issue Feb 24, 2021
turion pushed a commit to turion/ex_type that referenced this issue Feb 24, 2021
turion pushed a commit to turion/ex_type that referenced this issue Feb 14, 2022
turion pushed a commit to turion/ex_type that referenced this issue Feb 14, 2022
turion pushed a commit to turion/ex_type that referenced this issue Feb 14, 2022
turion added a commit to turion/ex_type that referenced this issue Feb 14, 2022
Expand `alias __MODULE__` manually. Fixes gyson#27
turion pushed a commit to turion/ex_type that referenced this issue Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant