Skip to content

Commit d0ebaa7

Browse files
cleidianojosevalim
authored andcommitted
Allow user to define a type named union (#10055)
1 parent e9ea9de commit d0ebaa7

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/elixir/lib/code/typespec.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ defmodule Code.Typespec do
217217
end
218218

219219
defp typespec_to_quoted({:user_type, line, name, args}) do
220-
typespec_to_quoted({:type, line, name, args})
220+
args = for arg <- args, do: typespec_to_quoted(arg)
221+
{name, [line: line], args}
221222
end
222223

223224
defp typespec_to_quoted({:type, line, :tuple, :any}) do

lib/elixir/test/elixir/typespec_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,12 @@ defmodule TypespecTest do
11831183
quoted =
11841184
Enum.sort([
11851185
quote(do: @spec(foo() :: integer())),
1186+
quote(do: @spec(foo() :: union())),
1187+
quote(do: @spec(foo() :: union(integer()))),
1188+
quote(do: @spec(foo() :: truly_union())),
1189+
quote(do: @spec(foo(union()) :: union())),
1190+
quote(do: @spec(foo(union(integer())) :: union(integer()))),
1191+
quote(do: @spec(foo(truly_union()) :: truly_union())),
11861192
quote(do: @spec(foo(atom()) :: integer() | [{}])),
11871193
quote(do: @spec(foo(arg) :: integer() when [arg: integer()])),
11881194
quote(do: @spec(foo(arg) :: arg when [arg: var])),
@@ -1191,6 +1197,10 @@ defmodule TypespecTest do
11911197

11921198
bytecode =
11931199
test_module do
1200+
@type union :: any()
1201+
@type union(t) :: t
1202+
@type truly_union :: list | map | union
1203+
11941204
def foo(), do: 1
11951205
def foo(arg), do: arg
11961206
Module.eval_quoted(__MODULE__, quote(do: (unquote_splicing(quoted))))

0 commit comments

Comments
 (0)