Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions lib/elixir/lib/inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,26 @@ defimpl Inspect, for: Map do
map_container_doc(infos, name, opts, fun)
end

def valid_struct?(%module{} = struct) do
try do
module.__info__(:struct)
rescue
_ -> false
else
info ->
valid_struct?(info, struct, map_size(struct) - 1)
end
end

defp valid_struct?([%{field: field} | info], struct, count) when is_map_key(struct, field),
do: valid_struct?(info, struct, count - 1)

defp valid_struct?([], _struct, 0),
do: true

defp valid_struct?(_fields, _struct, _count),
do: false

defp to_assoc({key, value}, opts, sep) do
{key_doc, opts} = to_doc_with_opts(key, opts)
{value_doc, opts} = to_doc_with_opts(value, opts)
Expand Down Expand Up @@ -662,36 +682,14 @@ end

defimpl Inspect, for: Any do
def inspect(%module{} = struct, opts) do
try do
module.__info__(:struct)
rescue
_ -> Inspect.Map.inspect_as_map(struct, opts)
else
info ->
if valid_struct?(info, struct) do
info =
for %{field: field} = map <- info,
field != :__exception__,
do: map
info =
for %{field: field} = map <- module.__info__(:struct),
field != :__exception__,
do: map

Inspect.Map.inspect_as_struct(struct, Macro.inspect_atom(:literal, module), info, opts)
else
Inspect.Map.inspect_as_map(struct, opts)
end
end
Inspect.Map.inspect_as_struct(struct, Macro.inspect_atom(:literal, module), info, opts)
end

defp valid_struct?(info, struct), do: valid_struct?(info, struct, map_size(struct) - 1)

defp valid_struct?([%{field: field} | info], struct, count) when is_map_key(struct, field),
do: valid_struct?(info, struct, count - 1)

defp valid_struct?([], _struct, 0),
do: true

defp valid_struct?(_fields, _struct, _count),
do: false

def inspect_as_struct(map, name, infos, opts) do
open = color_doc("#" <> name <> "<", :map, opts)
sep = color_doc(",", :map, opts)
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/lib/inspect/algebra.ex
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ defmodule Inspect.Algebra do
def to_doc_with_opts(term, opts)

def to_doc_with_opts(%_{} = struct, %Inspect.Opts{inspect_fun: fun} = opts) do
if opts.structs do
if opts.structs and Inspect.Map.valid_struct?(struct) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move the function to this module instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed makes sense, I initially thought I'd need it at several places but not anymore 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try do
fun.(struct, opts)
rescue
Expand Down
21 changes: 0 additions & 21 deletions lib/elixir/test/elixir/calendar/date_range_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,6 @@ defmodule Date.RangeTest do
end

describe "old date ranges" do
test "inspect" do
asc = %{
__struct__: Date.Range,
first: ~D[2021-07-14],
first_in_iso_days: 738_350,
last: ~D[2021-07-17],
last_in_iso_days: 738_353
}

desc = %{
__struct__: Date.Range,
first: ~D[2021-07-17],
first_in_iso_days: 738_353,
last: ~D[2021-07-14],
last_in_iso_days: 738_350
}

assert inspect(asc) == "Date.range(~D[2021-07-14], ~D[2021-07-17])"
assert inspect(desc) == "Date.range(~D[2021-07-17], ~D[2021-07-14], -1)"
end

test "enumerable" do
asc = %{
__struct__: Date.Range,
Expand Down
7 changes: 7 additions & 0 deletions lib/elixir/test/elixir/inspect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,13 @@ defmodule Inspect.MapTest do
"%{__struct__: Inspect.MapTest.Public, foo: :bar, key: 1}"
end

test "public modified struct with defimpl" do
map_set = MapSet.new([1, 2])

assert inspect(Map.put(map_set, :foo, :bar), custom_options: [sort_maps: true]) ==
"%{__struct__: MapSet, foo: :bar, map: %{1 => [], 2 => []}}"
end

test "private struct" do
assert inspect(%{__struct__: Private, key: 1}, custom_options: [sort_maps: true]) ==
"%{__struct__: Inspect.MapTest.Private, key: 1}"
Expand Down
8 changes: 0 additions & 8 deletions lib/elixir/test/elixir/range_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,6 @@ defmodule RangeTest do
end

describe "old ranges" do
test "inspect" do
asc = %{__struct__: Range, first: 1, last: 3}
desc = %{__struct__: Range, first: 3, last: 1}

assert inspect(asc) == "1..3"
assert inspect(desc) == "3..1//-1"
end
Comment on lines -203 to -209
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These will be rendered as maps, which I think is acceptable now?
It's been a while since stepped ranges.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is!


test "enum" do
asc = %{__struct__: Range, first: 1, last: 3}
desc = %{__struct__: Range, first: 3, last: 1}
Expand Down