Skip to content

Commit 56e7de3

Browse files
committed
Also pretty print structs with no fields in types
1 parent 063a673 commit 56e7de3

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/elixir/lib/module/types/descr.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,7 +2855,7 @@ defmodule Module.Types.Descr do
28552855
:closed ->
28562856
with %{__struct__: struct_descr} <- fields,
28572857
{_, [struct]} <- atom_fetch(struct_descr),
2858-
[_ | _] = info <- maybe_struct(struct),
2858+
info when is_list(info) <- maybe_struct(struct),
28592859
true <- map_size(fields) == length(info) + 1,
28602860
true <- Enum.all?(info, &is_map_key(fields, &1.field)) do
28612861
collapse? = Keyword.get(opts, :collapse_structs, true)
@@ -2873,7 +2873,8 @@ defmodule Module.Types.Descr do
28732873
{:%{}, [], map_fields_to_quoted(tag, fields, opts)}
28742874
]}
28752875
else
2876-
_ -> {:%{}, [], map_fields_to_quoted(tag, Enum.sort(fields), opts)}
2876+
_ ->
2877+
{:%{}, [], map_fields_to_quoted(tag, Enum.sort(fields), opts)}
28772878
end
28782879

28792880
:open ->

lib/elixir/test/elixir/module/types/descr_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
Code.require_file("type_helper.exs", __DIR__)
55

6+
defmodule NoFieldsStruct do
7+
defstruct []
8+
end
9+
610
defmodule Decimal do
711
defstruct [:sign, :coef, :exp]
812
end
@@ -1995,6 +1999,9 @@ defmodule Module.Types.DescrTest do
19951999
assert closed_map(__struct__: atom([URI])) |> to_quoted_string() ==
19962000
"%{__struct__: URI}"
19972001

2002+
assert closed_map(__struct__: atom([NoFieldsStruct])) |> to_quoted_string() ==
2003+
"%NoFieldsStruct{}"
2004+
19982005
assert closed_map(__struct__: atom([URI, Another])) |> to_quoted_string() ==
19992006
"%{__struct__: Another or URI}"
20002007

0 commit comments

Comments
 (0)