Skip to content

Commit 90c8327

Browse files
committed
Support bitstring specifies as map keys in pattern, closes #12586
1 parent 8d5b213 commit 90c8327

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/elixir/src/elixir_map.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ clean_struct_key_from_map_assocs(Meta, Assocs, E) ->
6262

6363
validate_match_key(Meta, {Name, _, Context}, E) when is_atom(Name), is_atom(Context) ->
6464
file_error(Meta, E, ?MODULE, {invalid_variable_in_map_key_match, Name});
65+
validate_match_key(Meta, {'::', _, [Left, _]}, E) ->
66+
validate_match_key(Meta, Left, E);
6567
validate_match_key(_, {'^', _, [{Name, _, Context}]}, _) when is_atom(Name), is_atom(Context) ->
6668
ok;
6769
validate_match_key(_, {'%{}', _, [_ | _]}, _) ->

lib/elixir/test/elixir/kernel/expansion_test.exs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ defmodule Kernel.ExpansionTest do
483483
quote(do: %{a: after_expansion = 1, b: a()})
484484
end
485485

486-
test "with variables on keys" do
486+
test "with variables on keys inside patterns" do
487487
ast =
488488
quote do
489489
%{(x = 1) => 1}
@@ -516,6 +516,34 @@ defmodule Kernel.ExpansionTest do
516516
end)
517517
end
518518

519+
test "with binaries in keys inside patterns" do
520+
before_ast =
521+
quote do
522+
%{<<0>> => nil} = %{<<0>> => nil}
523+
end
524+
525+
after_ast =
526+
quote do
527+
%{<<0::integer>> => nil} = %{<<0::integer>> => nil}
528+
end
529+
530+
assert expand(before_ast) |> clean_meta([:alignment]) == clean_bit_modifiers(after_ast)
531+
assert expand(after_ast) |> clean_meta([:alignment]) == clean_bit_modifiers(after_ast)
532+
533+
ast =
534+
quote do
535+
x = 8
536+
%{<<0::integer-size(x)>> => nil} = %{<<0::integer>> => nil}
537+
end
538+
539+
assert expand(ast) |> clean_meta([:alignment]) ==
540+
clean_bit_modifiers(ast) |> clean_meta([:context, :imports])
541+
542+
assert_compile_error(~r"cannot use variable x as map key inside a pattern", fn ->
543+
expand(quote(do: %{<<x::integer>> => 1} = %{}), [])
544+
end)
545+
end
546+
519547
test "expects key-value pairs" do
520548
assert_compile_error(~r"expected key-value pairs in a map, got: :foo", fn ->
521549
expand(quote(do: unquote({:%{}, [], [:foo]})))

0 commit comments

Comments
 (0)