Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 6 additions & 28 deletions lib/elixir/lib/inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -662,35 +662,13 @@ 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

Inspect.Map.inspect_as_struct(struct, Macro.inspect_atom(:literal, module), info, opts)
else
Inspect.Map.inspect_as_map(struct, opts)
end
end
end
info =
for %{field: field} = map <- module.__info__(:struct),
field != :__exception__,
do: map

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
Inspect.Map.inspect_as_struct(struct, Macro.inspect_atom(:literal, module), info, opts)
end

def inspect_as_struct(map, name, infos, opts) do
open = color_doc("#" <> name <> "<", :map, opts)
Expand Down
22 changes: 21 additions & 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 valid_struct?(struct) do
try do
fun.(struct, opts)
rescue
Expand Down Expand Up @@ -453,6 +453,26 @@ defmodule Inspect.Algebra do
fun.(arg, opts) |> pack_opts(opts)
end

defp 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 pack_opts({_doc, %Inspect.Opts{}} = doc_opts, _opts), do: doc_opts
defp pack_opts(doc, opts), do: {doc, opts}

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