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

Start counting pids, ports, and refs from 1 #9395

Closed
josevalim opened this issue Feb 5, 2025 · 1 comment
Closed

Start counting pids, ports, and refs from 1 #9395

josevalim opened this issue Feb 5, 2025 · 1 comment
Labels
enhancement team:VM Assigned to OTP team VM

Comments

@josevalim
Copy link
Contributor

Is your feature request related to a problem? Please describe.

One of the features we are exploring for Elixir's type system, which could also apply to TypeSpecs and Dialyzer, is witness generation. If a function call fails due to types, we can generate a value that reproduces the failure at runtime. While we can generate witnesses for most runtime values, the issue arises with pids and ports (and slightly with refs).

Describe the solution you'd like

I could call open_port({spawn, "true"}, []) or spawn(fun() -> ok end) to generate values, but it would be best if we could have a safe deterministic value we could emit. One potential suggestion is to always count from 1, and leave pid(0, 0, 0), port(0, 0) and ref(0, 0, 0, 0) always unused.

Additional context

Given this may be useful in other contexts, I have decided to open up an issue to see if there is any interest on this change. However, feel free to close this if undesired, as I do have a workaround for now. :)

@IngelaAndin IngelaAndin added the team:VM Assigned to OTP team VM label Feb 5, 2025
@josevalim
Copy link
Contributor Author

@jhogberg pointed out I could use the node name as a better guarantee the entry does not exist:

$ erl -name "dummy_node@elixir-type-system"
...
(dummy_node@elixir-type-system)1> io:format("~p~n", [erlang:term_to_binary(whereis(init))]).
<<131,88,119,29,100,117,109,109,121,95,110,111,100,101,64,101,108,105,120,105,
  114,45,116,121,112,101,45,115,121,115,116,101,109,0,0,0,0,0,0,0,0,103,163,50,
  140>>
$ erl
...
1> erlang:binary_to_term(<<131,88,119,29,100,117,109,109,121,95,110,111,100,101,64,101,108,105,120,105,
     114,45,116,121,112,101,45,115,121,115,116,101,109,0,0,0,0,0,0,0,0,103,163,50,
     140>>).
<10419.0.0>

And sure, there is a chance of conflict, but that's very unlikely here, so I am happy with this direction. :) Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement team:VM Assigned to OTP team VM
Projects
None yet
Development

No branches or pull requests

2 participants