Skip to content

Fix errors with ref/4 IEx helper #8016

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

Merged
merged 4 commits into from
Aug 4, 2018
Merged

Fix errors with ref/4 IEx helper #8016

merged 4 commits into from
Aug 4, 2018

Conversation

fertapric
Copy link
Member

@fertapric fertapric commented Jul 31, 2018

In addition, I've included the documentation for that function.

Before:

$ iex
Erlang/OTP 21 [erts-10.0.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.7.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> make_ref
#Reference<0.2597424430.1465122823.223385>
iex(2)> ref("0.2597424430.1465122823.1")
#Reference<0.2597424430.1465122823.1>
iex(3)> ref(0, 2597424430, 1465122823, 1)
** (ArgumentError) argument error
    :erlang.list_to_ref('<0.2597424430..1465122823..1.>')
    (iex) lib/iex/helpers.ex:1220: IEx.Helpers.ref/4
iex(3)>

After:

$ bin/iex
Erlang/OTP 21 [erts-10.0.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.8.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> ref("0.2597424430.1465122823.1")
#Reference<0.2597424430.1465122823.1>
iex(2)> ref(0, 2597424430, 1465122823, 1)
#Reference<0.2597424430.1465122823.1>
iex(3)>

These functions were included in this commit

fertapric and others added 2 commits July 31, 2018 11:21
In addition, include documentation for that function.
@josevalim
Copy link
Member

@fertapric we don't have doctests enabled for this module, so we need to add some unit tests too.

Copy link
Member

@josevalim josevalim left a comment

Choose a reason for hiding this comment

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

@fertapric ping!

@fertapric
Copy link
Member Author

fertapric commented Aug 3, 2018

I just wanted to mention that I did not forget about this one :)

I was doing some tests using the new ref/1 and I got curious due to some unexpected? results:

iex(1)> make_ref()
#Reference<0.3013689594.4010541059.29671>
iex(2)> :erlang.list_to_ref('#Ref<0.3013689594.4010541059.29671>')
#Reference<0.3013689594.4010541059.29671>
iex(3)> :erlang.list_to_ref('#Ref<3.3013689594.4010541059.29671>')
#Reference<0.3013689594.4010541059.29671>
iex(4)> :erlang.list_to_ref('#Ref<0.301.401054.29671>')
** (ArgumentError) argument error
    :erlang.list_to_ref('#Ref<0.301.401054.29671>')

(4) shows that not all possible number combinations are valid to create a Reference. I guessed references were not just random numbers, and the numbers had to follow some set of rules. Everything good here.

However, I was not expecting (3) to return a modified reference. I would expect an ArgumentError, but not a different set of numbers.

I looked a bit in the Erlang/OTP codebase, and I'm currently checking these parts of the codebase:

According to that, Ref only contains 3 numbers, so I'm not sure if the first one should always be 0.

I also suspect that ref[0] is not related to the first number #Reference<num1.num2.num3.num4>, and num1 is more related to the marker, but I haven't looked that internal code that much yet.

I was inspecting the code to check if it would be an error that should be reported upstream. But I don't have that much time to investigate :(

I thought maybe you know a little more about it and you could give me some more details or tips? cc @michalmuskala

@fertapric
Copy link
Member Author

Either way, I'll finish this PR today. Bug or not, Elixir is not responsible of it :)

@josevalim
Copy link
Member

@fertapric I believe the first part is the node reference. 0 is local node.

@michalmuskala
Copy link
Member

Yeah, I believe it is. I would expect it to fail, similar to what happens for external PIDs or ports

@fertapric
Copy link
Member Author

@josevalim Tests added.

Btw, I found out that :erlang.list_to_pid('<3.21.32>') also behaves the same:

iex(1)> :erlang.list_to_pid('<0.21.32>')
#PID<0.21.32>
iex(2)> :erlang.list_to_pid('<3.21.32>')
#PID<0.21.32>

However, if I do the same experiments in a cluster of nodes:

iex(n2@127.0.0.1)37> :erlang.list_to_pid('<3.21.32>')
** (ArgumentError) argument error
    :erlang.list_to_pid('<3.21.32>')
iex(n2@127.0.0.1)37> :erlang.list_to_pid('<0.21.32>')
#PID<0.21.32>
iex(n2@127.0.0.1)38> :erlang.list_to_ref('#Ref<3.1.2.3>')
** (ArgumentError) argument error
    :erlang.list_to_ref('#Ref<3.1.2.3>')
iex(n2@127.0.0.1)38> :erlang.list_to_ref('#Ref<0.1.2.3>')
#Reference<0.1.2.3>

🤷‍♂️

@michalmuskala michalmuskala merged commit 3f94113 into elixir-lang:master Aug 4, 2018
@michalmuskala
Copy link
Member

Thank you! ❤️

josevalim pushed a commit that referenced this pull request Aug 4, 2018
* Fix errors with ref/4 IEx helper

In addition, include documentation for that function.

* Update helpers.ex

* Add IEx.Helpers.ref/* tests

* Use examples that do not return ArgumentError

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
josevalim pushed a commit that referenced this pull request Aug 4, 2018
@fertapric fertapric deleted the fertapric-fix-ref-4-iex-helper branch August 5, 2018 21:49
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