mudasobwa

mudasobwa

Creator of Cure

I need to get specs for the function which may be located in the external module, like Process or :math.

Dialyzer source code does roughly this:

{:ok, core} = Process |> :code.which() |> :dialyzer_utils.get_core_from_beam([])
{:ok, rec_dict} = :dialyzer_utils.get_record_and_type_info(core)
:dialyzer_utils.get_spec_info(Process, core, rec_dict)

and it kinda works. Is there any better documented and/or less hacky way to get the spec? Is there any reason specs are not there in beam chunks? Should not Code.fetch_docs/2 (or maybe another not yet existing Code.fetch_specs/2) retrieve specs? What am I missing?

Showing Posts 1 to 6

D4no0

D4no0

Should they be there? If you think about it, @spec is nothing more than a specialized module attribute.

mudasobwa

mudasobwa OP

Creator of Cure

Same as @doc is, btw.

Specs are there in generated documentation for a reason.

LostKobrakai

LostKobrakai

Code.fetch_docs is about fetching the “Docs” chunk of a beam file (as proposed initially here: Eep 0048 - Erlang/OTP), which specifically includes only documentation text. There is no chunk for types though. E.g. ex_doc seems to parse them from the abstract code provided in the debug_info (“Dbgi”) chunk. Dialyzer also fetches the what seems like core erlang code from the debug_info chunk. I’m wondering if there’s also some history around where and how that data is fetched (seems like there is). So the answer might be that there’s multiple ways to skin the cat and nobody bothered normalizing one of them within the elixir core codebase.

mudasobwa

mudasobwa OP

Creator of Cure

For the record, I used this code to fetch the spec as a string.

  @spec fetch_spec(module(), atom(), arity()) :: {:ok, String.t()} | {:error, any()}
  def fetch_spec(mod, fun, arity) do
    with {:ok, core} <- mod |> :code.which() |> :dialyzer_utils.get_core_from_beam(),
         {:ok, rec_dict} <- :dialyzer_utils.get_record_and_type_info(core),
         {:ok, spec_info, %{}} <- :dialyzer_utils.get_spec_info(mod, core, rec_dict),
         {:ok, {{_file, _line}, {_tmp_contract, [_fun], type}, []}} <-
           fetch_spec_info(spec_info, {mod, fun, arity}),
         do: {:ok, type_to_string(fun, type)}
  end

  defp fetch_spec_info(spec_info, {mod, fun, arity}) do
    with :error <- Map.fetch(spec_info, {mod, fun, arity}), do: {:error, :no_mfa_info}
  end

  defp type_to_quoted(fun, type) do
    for {{:type, _, _, _} = type, _} <- type do
      Code.Typespec.spec_to_quoted(fun, type)
    end
  end

  defp type_to_string(fun, type) do
    fun
    |> type_to_quoted(type)
    |> Enum.map_join(" ", &Macro.to_string/1)
  end

The first naïve attempt was to use :erl_types.t_form_to_string/1 from :dialyzer and it worked to some extent, returning somewhat alongside

~c"fun((['Elixir.Task.Supervisor':option()]) -> 'Elixir.Task.Supervisor':on_start())"

I struggled to find an elixir helper to parse this into an elixir format and reached for Code.Typespec.spec_to_quoted/2 which kinda worked but not without glitches.

The type it returned for Process.send_after/4 did not recornize the when option: {:abs, boolean()} guard and gave me when option: var which resulted in compile error. I will probably file a bug to the core.

Another glitch I found is function spec syntax. While (binary() -> boolean()) is translated to the expected erlang ~c"fun((binary()) -> boolean())", the any-arity notation lacks parentheses around the fun as ~c"fun(...) -> integer()". That I am not sure whether it’s expected or not.

Anyway.

LostKobrakai

LostKobrakai

Seems like ex_doc also reaches into the private API of Code.Typespec. Sounds like there could be an effort made to lift functionality into proper public APIs.

mudasobwa

mudasobwa OP

Creator of Cure

I am positive that specs should be optionally added to “Docs” chunks.

Now when Erlang uses ex_doc to generate docs, at least two different toolings reach for two different private APIs to retrieve what is de facto a part of docs.

Or, alternatively, the new “Spec” chunk should be added to unify the representation of specs in the chunks.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews