From 6ee761126d1f24a9114160e5bc77983a4d167df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 30 Oct 2024 08:21:30 +0100 Subject: [PATCH 1/2] Store record metadata in the documentation chunk --- lib/elixir/lib/record.ex | 11 +++++++---- lib/elixir/test/elixir/record_test.exs | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/elixir/lib/record.ex b/lib/elixir/lib/record.ex index 6ee963677d5..a57f218e22f 100644 --- a/lib/elixir/lib/record.ex +++ b/lib/elixir/lib/record.ex @@ -36,10 +36,9 @@ defmodule Record do ## Reflection - A list of all records in a module, if any, can be retrieved by reading the - `@__records__` module attribute. It returns a list of maps with the record - kind, name, tag, and fields. The attribute is only available inside the - module definition. + The record tag and its fields are stored as metadata in the "Docs" chunk + of the record definition macro. You can retrieve the documentation for + a module by calling `Code.fetch_docs/1`. """ @doc """ @@ -253,6 +252,7 @@ defmodule Record do tag = tag || name fields = Record.__record__(__MODULE__, :defrecord, name, tag, kv) + @doc record: {tag, fields} defmacro unquote(name)(args \\ []) do Record.__access__(unquote(tag), unquote(fields), args, __CALLER__) end @@ -312,6 +312,9 @@ defmodule Record do error_on_duplicate_record(module, name) fields = fields(kind, kv) + + # TODO: Deprecate reading me on Elixir v1.22 + # TODO: Remove me on Elixir v2.0 Module.register_attribute(module, :__records__, accumulate: true) Module.put_attribute(module, :__records__, %{ diff --git a/lib/elixir/test/elixir/record_test.exs b/lib/elixir/test/elixir/record_test.exs index 39f02ac3492..ae8f8f1ebf9 100644 --- a/lib/elixir/test/elixir/record_test.exs +++ b/lib/elixir/test/elixir/record_test.exs @@ -341,6 +341,20 @@ defmodule RecordTest do end end + test "docs metadata" do + import PathHelpers + + write_beam( + defmodule Metadata do + Record.defrecord(:user, foo: 0, bar: "baz") + end + ) + + {:docs_v1, 348, :elixir, "text/markdown", _, %{}, docs} = Code.fetch_docs(RecordTest.Metadata) + {{:macro, :user, 1}, _meta, _sig, _docs, metadata} = List.keyfind(docs, {:macro, :user, 1}, 0) + assert %{record: {:user, [foo: 0, bar: "baz"]}} = metadata + end + describe "warnings" do import ExUnit.CaptureIO From fbb60c871511fd904f6f794da84ed5aa8188f190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 30 Oct 2024 09:24:20 +0100 Subject: [PATCH 2/2] Update lib/elixir/lib/record.ex --- lib/elixir/lib/record.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/elixir/lib/record.ex b/lib/elixir/lib/record.ex index a57f218e22f..4a5dd294c08 100644 --- a/lib/elixir/lib/record.ex +++ b/lib/elixir/lib/record.ex @@ -313,7 +313,6 @@ defmodule Record do fields = fields(kind, kv) - # TODO: Deprecate reading me on Elixir v1.22 # TODO: Remove me on Elixir v2.0 Module.register_attribute(module, :__records__, accumulate: true)