diff --git a/lib/elixir/lib/record.ex b/lib/elixir/lib/record.ex index 6ee963677d5..4a5dd294c08 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,8 @@ defmodule Record do error_on_duplicate_record(module, name) fields = fields(kind, kv) + + # 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