Elixir: Map pattern matching with pinned key parts doesn't compile

Created on 10 Apr 2018  路  4Comments  路  Source: elixir-lang/elixir

Environment

Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Elixir 1.6.4 (compiled with OTP 19)
CentOS 7 x86_64

Current behavior

This code doesn't compile

m = %{{1, 2} => 3}
a = 1
b = 2
%{{^a, ^b} => c} = m
** (CompileError) main.ex:4: illegal map key in pattern

Expected behavior

It should compile.

Elixir Bug Advanced

Most helpful comment

@liveforeverx as far as we are concerned, it is an Erlang compiler limitation. We need to ask them why. :)

All 4 comments

FYI it works inside IEx:

$ iex
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.6.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> m = %{{1, 2} => 3}
%{{1, 2} => 3}
iex(2)> a = 1
1
iex(3)> b = 2
2
iex(4)> %{{^a, ^b} => c} = m
%{{1, 2} => 3}
iex(5)> c
3
iex(6)>

but not when compiling.

Thank you for the report @japplegame!

The raising is correct but the error message is not good. We will fix that.

@fertapric good catch. It works on IEx by accident. The Elixir expansion pass was supposed to catch it and raise. This is part of #5800.

@josevalim Why it shouldn't work?

This works:

d = {a, b}
%{^d => c} = m # the same as %{{^a, ^b} => c} = m

@liveforeverx as far as we are concerned, it is an Erlang compiler limitation. We need to ask them why. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

devonestes picture devonestes  路  33Comments

josevalim picture josevalim  路  44Comments

josevalim picture josevalim  路  42Comments

conradwt picture conradwt  路  34Comments

wojtekmach picture wojtekmach  路  34Comments