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

Map comprehension difference between compiler & interpreter #9348

Closed
michalmuskala opened this issue Jan 27, 2025 · 1 comment · Fixed by #9358
Closed

Map comprehension difference between compiler & interpreter #9348

michalmuskala opened this issue Jan 27, 2025 · 1 comment · Fixed by #9358
Assignees
Labels
bug Issue is reported as a bug team:VM Assigned to OTP team VM

Comments

@michalmuskala
Copy link
Contributor

Describe the bug
Given the following module:

-module(test).

-export([test/0]).

test() ->
  #{A => B || X <- [1, 5], {A, B} <- [{X, X+1}, {X, X+3}]}.

Running in a shell:

1> c(test).
{ok,test}
2> test:test().
#{1 => 4,5 => 8}
3> #{A => B || X <- [1, 5], {A, B} <- [{X, X+1}, {X, X+3}]}.
#{1 => 2,5 => 6}

Expected behavior
I'm not actually sure which result is correct - I expect the one from compiled code. Either way, the two should agree

Affected versions
Tested on latest master

@michalmuskala michalmuskala added the bug Issue is reported as a bug label Jan 27, 2025
@hajduakos
Copy link

hajduakos commented Jan 27, 2025

I think you can further simplify the example:

-module(test).

-export([test/0]).

test() ->
  #{A => B || {A, B} <- [{1, 2}, {1, 3}]}.

And then:

1> c(test).
{ok,test}
2> test:test().
#{1 => 3}
3> #{A => B || {A, B} <- [{1, 2}, {1, 3}]}.
#{1 => 2}

Edit: https://www.erlang.org/eeps/eep-0058#semantics says that it should be equivalent to maps:from_list/1, and that seems to agree with the compiled version:

4> maps:from_list([{1, 2}, {1, 3}]).
#{1 => 3}

@IngelaAndin IngelaAndin added the team:VM Assigned to OTP team VM label Jan 28, 2025
@lucioleKi lucioleKi self-assigned this Jan 29, 2025
lucioleKi added a commit that referenced this issue Jan 31, 2025
* isabell/stdlib/map-fix/GH-9348/OTP-19459:
  stdlib: Fix map comprehension result when a key value is replaced
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is reported as a bug team:VM Assigned to OTP team VM
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants