ex_type icon indicating copy to clipboard operation
ex_type copied to clipboard

Recursive types cause endless loop

Open turion opened this issue 5 years ago • 4 comments

Discovered by @evnu.

defmodule IWantToSeeTheWorldBurn do
  @type a :: a
  @spec foo(a) :: a()
  def foo(a) do
    a
  end

  @type forest :: [tree]
  @type tree :: {integer, forest}
  @spec flatten(forest) :: [integer]
  def flatten(forest) do
    Enum.map(forest, fn {int, subforest} -> [int | flatten(subforest)] end)
    |> Enum.concat()
  end
end

The first example is a minimal example that should type check, but hangs. The second one is an actually useful thing people might do in practice.

Like in #39 , the problem is in the treatment of local types. They are evaluated directly, causing an endless loop of variable lookup. Maybe it's possible to stop this by making references to local types lazy. Instead of evaluating them, insert a closure, and only compute it when you need to compare the type spec to something else.

turion avatar Dec 10 '20 10:12 turion

In fact, this issue needs to be solved before ex_type can check itself, since ExType.Type contains mutual recursion.

turion avatar Dec 10 '20 10:12 turion

Maybe it's possible to stop this by making references to local types lazy. Instead of evaluating them, insert a closure, and only compute it when you need to compare the type spec to something else.

Simpler even, the new named types should be added to the context, and there should be a "context reference type" that points to an entry in the context instead of getting that value. Whenever an already known type is encountered, this reference should be returned by eval_type.

turion avatar Dec 10 '20 10:12 turion

@turion thanks for creating issues and pull requests!

I will look into them over weekend.

gyson avatar Dec 10 '20 14:12 gyson

Glad to hear from you :) I'm trying to solve this issue here, but I'm not so sure yet whether I can.

turion avatar Dec 10 '20 15:12 turion