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
5 changes: 5 additions & 0 deletions lib/elixir/lib/inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,11 @@ defimpl Inspect, for: Any do
Inspect.Map.inspect_as_struct(struct, Macro.inspect_atom(:literal, module), info, opts)
end

# A temporary clause to deal with native records until they are officially supported
def inspect(native_record, _opts) do
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we could add a comment that this is only temporary code, that we'll need to revisit if we add proper support for native record?

Comment thread
josevalim marked this conversation as resolved.
:io_lib.format("~p", [native_record]) |> IO.iodata_to_binary()
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is obviously very naive implementation just to prevent crashes, a more robust one would use :record.get_module/1, :record.get_name/1, :record.get_field_names/1, :record.get/2, and maybe the new guard :erlang.is_record/2 like here:

https://github.com/erlang/otp/blob/OTP-29.0-rc3/lib/stdlib/src/io_lib.erl#L727:L750

end

def inspect_as_struct(map, name, infos, opts) do
open = color_doc("#" <> name <> "<", :map, opts)
sep = color_doc(",", :map, opts)
Expand Down
30 changes: 30 additions & 0 deletions lib/elixir/test/elixir/inspect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,36 @@ defmodule Inspect.TupleTest do
end
end

defmodule Inspect.NativeRecordTest do
use ExUnit.Case, async: true

@moduletag skip: System.otp_release() < "29"

@tag :tmp_dir
test "basic", %{tmp_dir: tmp_dir} do
File.write!("#{tmp_dir}/native_records_test.erl", """
-module(native_records_test).
-export([test/0]).
-record #point{ x = 0.0, y = 0.0 }.

test() ->
#point{}.
""")

{"", 0} = System.cmd("erlc", ["native_records_test.erl"], cd: tmp_dir, stderr_to_stdout: true)

{:module, _} =
:code.load_binary(
:native_records_test,
~c"nofile",
File.read!("#{tmp_dir}/native_records_test.beam")
)

assert inspect(apply(:native_records_test, :test, [])) ==
"#native_records_test:point{x = 0.0,y = 0.0}"
end
end

defmodule Inspect.ListTest do
use ExUnit.Case, async: true

Expand Down
Loading