Recursive types cause endless loop
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.
In fact, this issue needs to be solved before ex_type can check itself, since ExType.Type contains mutual recursion.
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 thanks for creating issues and pull requests!
I will look into them over weekend.
Glad to hear from you :) I'm trying to solve this issue here, but I'm not so sure yet whether I can.