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

record matches do not work in KeyPattern of a map generator in a comprehension #7875

Closed
RoadRunnr opened this issue Nov 16, 2023 · 1 comment · Fixed by #7878
Closed

record matches do not work in KeyPattern of a map generator in a comprehension #7875

RoadRunnr opened this issue Nov 16, 2023 · 1 comment · Fixed by #7878
Assignees
Labels
bug Issue is reported as a bug team:VM Assigned to OTP team VM

Comments

@RoadRunnr
Copy link
Contributor

Describe the bug

According to the documentation of the map generator the KeyPattern can be any pattern. However, using a partial record as pattern doesn't work. However, using the same record pattern in a list generator works as expected.

To Reproduce

Sample code:

-record(foo, {f1, f2}).

t4() ->
    List = [{#foo{f1 = X, f2 = X*2}, X} || X <- lists:seq(1, 10)],
    FilteredList = [V || {#foo{f1 = M}, V} <- List, M rem 2 =:= 0],
    io:format("List Test~nList: ~0p~nFiltered: ~0p~n", [List, FilteredList]),

    Map = maps:from_list(List),
    FilteredMap = [V || #foo{f1 = N} := V <- Map, N rem 2 =:= 0],
    io:format("~nMap Test~nMap: ~0p~nFiltered: ~0p~n", [Map, FilteredMap]),
    ok.

Resulting output:

List Test
List: [{{foo,1,2},1},{{foo,2,4},2},{{foo,3,6},3},{{foo,4,8},4},{{foo,5,10},5},{{foo,6,12},6},{{foo,7,14},7},{{foo,8,16},8},{{foo,9,18},9},{{foo,10,20},10}]
Filtered: [2,4,6,8,10]

Map Test
Map: #{{foo,1,2} => 1,{foo,2,4} => 2,{foo,3,6} => 3,{foo,4,8} => 4,{foo,5,10} => 5,{foo,6,12} => 6,{foo,7,14} => 7,{foo,8,16} => 8,{foo,9,18} => 9,{foo,10,20} => 10}
Filtered: []

When the KeyPattern is modified to #foo{f1 = N} = _ := V, it works. However, there is nothing in the documentation that would explain why that is so.

Expected behavior

The Filtered list should be the same for the list and map example.

Affected versions

OTP 26.1.2: Erlang/OTP 26 [erts-14.1.1] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit:ns]

@RoadRunnr RoadRunnr added the bug Issue is reported as a bug label Nov 16, 2023
@bjorng bjorng self-assigned this Nov 17, 2023
@bjorng bjorng added the team:VM Assigned to OTP team VM label Nov 17, 2023
@bjorng bjorng linked a pull request Nov 17, 2023 that will close this issue
@bjorng
Copy link
Contributor

bjorng commented Nov 17, 2023

Yes, it is a bug. Fixed in the linked pull request.

bjorng added a commit that referenced this issue Nov 22, 2023
…o maint

* bjorn/compiler/fix-mc-record-bug/GH-7875/OTP-18866:
  Fix partial record matching in map generator
@bjorng bjorng closed this as completed in 3de9ae0 Nov 22, 2023
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.

2 participants