Skip to content

Commit ed478e8

Browse files
authored
Don't crash when inspecting OTP 29 native records (#15278)
1 parent 370e7db commit ed478e8

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/elixir/lib/inspect.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,11 @@ defimpl Inspect, for: Any do
669669
Inspect.Map.inspect_as_struct(struct, Macro.inspect_atom(:literal, module), info, opts)
670670
end
671671

672+
# A temporary clause to deal with native records until they are officially supported
673+
def inspect(native_record, _opts) do
674+
:io_lib.format("~p", [native_record]) |> IO.iodata_to_binary()
675+
end
676+
672677
def inspect_as_struct(map, name, infos, opts) do
673678
open = color_doc("#" <> name <> "<", :map, opts)
674679
sep = color_doc(",", :map, opts)

lib/elixir/test/elixir/inspect_test.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,36 @@ defmodule Inspect.TupleTest do
268268
end
269269
end
270270

271+
defmodule Inspect.NativeRecordTest do
272+
use ExUnit.Case, async: true
273+
274+
@moduletag skip: System.otp_release() < "29"
275+
276+
@tag :tmp_dir
277+
test "basic", %{tmp_dir: tmp_dir} do
278+
File.write!("#{tmp_dir}/native_records_test.erl", """
279+
-module(native_records_test).
280+
-export([test/0]).
281+
-record #point{ x = 0.0, y = 0.0 }.
282+
283+
test() ->
284+
#point{}.
285+
""")
286+
287+
{"", 0} = System.cmd("erlc", ["native_records_test.erl"], cd: tmp_dir, stderr_to_stdout: true)
288+
289+
{:module, _} =
290+
:code.load_binary(
291+
:native_records_test,
292+
~c"nofile",
293+
File.read!("#{tmp_dir}/native_records_test.beam")
294+
)
295+
296+
assert inspect(apply(:native_records_test, :test, [])) ==
297+
"#native_records_test:point{x = 0.0,y = 0.0}"
298+
end
299+
end
300+
271301
defmodule Inspect.ListTest do
272302
use ExUnit.Case, async: true
273303

0 commit comments

Comments
 (0)