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

Unable to reference __MODULE__ in type declarations #66

Closed
baldwindavid opened this issue Oct 12, 2021 · 1 comment · Fixed by #68
Closed

Unable to reference __MODULE__ in type declarations #66

baldwindavid opened this issue Oct 12, 2021 · 1 comment · Fixed by #68

Comments

@baldwindavid
Copy link
Contributor

It appears that referencing __MODULE__ in a @type! declaration results in a specification that can never be met. For example, suppose t is declared as a type...

defmodule Typetest do
  use TypeCheck

  @type! t :: %__MODULE__{name: String.t()}
  defstruct [:name]

  @spec! hello(String.t()) :: t()
  def hello(name) do
    %__MODULE__{name: name}
  end
end

Calling Typetest.hello("Jane") results in the following error:

** (TypeCheck.TypeError) The call to `hello/1` failed,
   because the returned result does not adhere to the spec `Typetest.t`.
   Rather, its value is: `%Typetest{name: "Jane"}`.
   Details:
     The result of calling `hello("Jane")`
     does not adhere to spec `hello(String.t()) :: Typetest.t`. Reason:
       Returned result:
         `%Typetest{name: "Jane"}` does not match the definition of the named type `Typetest.t`
         which is: `Typetest.t
         ::
         %{__struct__: TypeCheck.Internals.UserTypes.Typetest, name: String.t()}`. Reason:
           `%Typetest{name: "Jane"}` does not check against `%{__struct__: TypeCheck.Internals.UserTypes.Typetest, name: String.t()}`. Reason:
             under key `:__struct__`:
               `Typetest` is not the same value as `TypeCheck.Internals.UserTypes.Typetest`.

It seems that TypeCheck has converted it to an internal module and is then comparing the module against that. Changing the type to @type! t :: %Typetest{name: String.t()} works, but referencing __MODULE__ I believe to be quite common.

@Qqwy
Copy link
Owner

Qqwy commented Oct 13, 2021

Thank you for reporting this bug!

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.

2 participants