Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose getting process dictionary value for specific key in Process.info/2 #13505

Merged

Conversation

michallepicki
Copy link
Contributor

This functionality is present since OTP 26.2:
erlang/otp#7707

Discussed at #12857 (comment)

@michallepicki michallepicki changed the title Expose getting process dictionary value in Process.info/2 Expose getting process dictionary value for specific key in Process.info/2 Apr 22, 2024
@michallepicki michallepicki force-pushed the michal/relax-process-info-guard-specs branch from 9fefe58 to 8e53a61 Compare April 22, 2024 18:10
Copy link
Contributor

@sabiwara sabiwara left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -834,7 +837,7 @@ defmodule Process do

See `:erlang.process_info/1` for more information.
"""
@spec info(pid) :: keyword | nil
@spec info(pid) :: [process_info_result_item] | nil
Copy link
Contributor

@sabiwara sabiwara Apr 22, 2024

Choose a reason for hiding this comment

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

[nit] Maybe this one is too imprecise here? Since the key will always be an atom, keyword might be more accurate.

Or should we refer :erlang.process_info_result_item() which is a bit more precise? But dialyzer would generalize to atom anyway.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this function can actually return individual dictionary keys? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

[nit] Maybe this one is too imprecise here? Since the key will always be an atom, keyword might be more accurate.

I don't think this function can actually return individual dictionary keys? 🤔

Good points about this function clause, I will revert this change to info/1 spec

Or should we refer :erlang.process_info_result_item() which is a bit more precise? But dialyzer would generalize to atom anyway.

Unfortunately the type is not exported, Dialyzer says:

lib/process.ex:840:30: Unknown type erlang:process_info_result_item/0

Comment on lines 863 to 868
def info(pid, {:dictionary, key}) do
nilify(:erlang.process_info(pid, {:dictionary, key}))
end

def info(pid, spec) when is_atom(spec) or is_list(spec) do
nilify(:erlang.process_info(pid, spec))
Copy link
Member

Choose a reason for hiding this comment

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

I would actually remove the specific clauses. Now that Erlang/OTP has good error messages, they will probably be more decent than function clause error?

Suggested change
def info(pid, {:dictionary, key}) do
nilify(:erlang.process_info(pid, {:dictionary, key}))
end
def info(pid, spec) when is_atom(spec) or is_list(spec) do
nilify(:erlang.process_info(pid, spec))
def info(pid, spec) do
nilify(:erlang.process_info(pid, spec))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On Erlang 26.2.2 the error looks like:

iex(1)> Process.info(self(), :unknown)
** (ArgumentError) errors were found at the given arguments:

  * 2nd argument: invalid item or item list

    :erlang.process_info(#PID<0.114.0>, :unknown)
    (elixir 1.17.0-dev) lib/process.ex:864: Process.info/2
    iex:1: (file)

We can rely on better ArgumentError messages on newer OTP
Comment on lines 851 to 852
@spec info(pid, process_info_item | [process_info_item]) ::
process_info_result_item | [process_info_result_item] | nil
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
@spec info(pid, process_info_item | [process_info_item]) ::
process_info_result_item | [process_info_result_item] | nil
@spec info(pid, process_info_item) :: process_info_result_item | nil
@spec info(pid, [process_info_item]) :: [process_info_result_item] | nil

I think this could work as well and be better for documentation (althoguh maybe Dialyzer would unify these types internally?)

Copy link
Contributor Author

@michallepicki michallepicki Apr 22, 2024

Choose a reason for hiding this comment

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

Pleasantly surprised to report that this improves the spec for Dialyzer as well:
for {:should_warn, _} = Process.info(self(), [:some_atom]):

The pattern
          {'should_warn', _} can never match the type
          'nil' | [{atom() | {'dictionary', _}, _}]

and for [{:should_warn, _}] = Process.info(self(), :some_atom)

The pattern
          [{'should_warn', _}] can never match the type
          'nil' | {atom() | {'dictionary', _}, _}

@josevalim josevalim merged commit fb4dd54 into elixir-lang:main Apr 22, 2024
8 of 9 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@michallepicki michallepicki deleted the michal/relax-process-info-guard-specs branch April 22, 2024 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants