Skip to content

Commit c20a874

Browse files
committed
is_map_key/2
1 parent 41b4b00 commit c20a874

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

lib/elixir/lib/module/types/apply.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,20 @@ defmodule Module.Types.Apply do
339339
{@is_function_info, [arg, integer()], context}
340340
end
341341

342+
@is_map_key_info {:strong, nil, [{[term(), open_map()], boolean()}]}
343+
344+
def remote_domain(:erlang, :is_map_key, [key, _map], expected, _meta, _stack, context)
345+
when is_atom(key) do
346+
arg =
347+
case booleaness(expected) do
348+
:always_true -> open_map([{key, term()}])
349+
:always_false -> open_map([{key, not_set()}])
350+
:undefined -> open_map()
351+
end
352+
353+
{@is_map_key_info, [term(), arg], context}
354+
end
355+
342356
def remote_domain(:erlang, :element, [index, _], expected, _meta, _stack, context)
343357
when is_integer(index) do
344358
tuple = open_tuple(List.duplicate(term(), max(index - 1, 0)) ++ [expected])

lib/elixir/test/elixir/module/types/pattern_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,11 @@ defmodule Module.Types.PatternTest do
424424
assert typecheck!([x], not is_function(x, 3), x) == dynamic(negation(fun(3)))
425425
end
426426

427+
test "is_map_key/2" do
428+
assert typecheck!([x], is_map_key(x, :foo), x) == dynamic(open_map(foo: term()))
429+
assert typecheck!([x], not is_map_key(x, :foo), x) == dynamic(open_map(foo: not_set()))
430+
end
431+
427432
test "elem" do
428433
assert typecheck!([x], elem(x, 1), x) ==
429434
dynamic(open_tuple([term(), atom([true, false, :fail])]))

0 commit comments

Comments
 (0)