Skip to content

Commit 9598d55

Browse files
committed
Type Tuple.delete_at/2
1 parent c1ef879 commit 9598d55

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lib/elixir/lib/module/types/of.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,24 @@ defmodule Module.Types.Of do
326326
end
327327
end
328328

329+
def apply(
330+
:erlang,
331+
:delete_element,
332+
[_, tuple],
333+
{_, meta, [index, _]} = expr,
334+
stack,
335+
context
336+
)
337+
when is_integer(index) do
338+
case tuple_delete_at(tuple, index - 1) do
339+
value_type when is_descr(value_type) ->
340+
{value_type, context}
341+
342+
reason ->
343+
{error_type(), error({reason, expr, tuple, index - 1, context}, meta, stack, context)}
344+
end
345+
end
346+
329347
def apply(:erlang, name, [left, right], expr, stack, context)
330348
when name in [:>=, :"=<", :>, :<, :min, :max] do
331349
result = if name in [:min, :max], do: union(left, right), else: boolean()

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,43 @@ defmodule Module.Types.ExprTest do
327327
{:ok, integer()}
328328
"""}
329329
end
330+
331+
test "Tuple.delete_at/2" do
332+
assert typecheck!(Tuple.delete_at({:ok, 123}, 0)) == tuple([integer()])
333+
assert typecheck!(Tuple.delete_at({:ok, 123}, 1)) == tuple([atom([:ok])])
334+
assert typecheck!([x], Tuple.delete_at({:ok, x}, 0)) == dynamic(tuple([term()]))
335+
assert typecheck!([x], Tuple.delete_at({:ok, x}, 1)) == dynamic(tuple([atom([:ok])]))
336+
337+
assert typewarn!([<<x::integer>>], Tuple.delete_at(x, 0)) ==
338+
{dynamic(),
339+
~l"""
340+
expected a tuple in expression:
341+
342+
Tuple.delete_at(x, 0)
343+
344+
but got type:
345+
346+
integer()
347+
348+
where "x" was given the type:
349+
350+
# type: integer()
351+
# from: types_test.ex:LINE-2
352+
<<x::integer>>
353+
"""}
354+
355+
assert typewarn!(Tuple.delete_at({:ok, 123}, 2)) ==
356+
{dynamic(),
357+
~l"""
358+
expected a tuple with at least 3 elements in expression:
359+
360+
Tuple.delete_at({:ok, 123}, 2)
361+
362+
the given type does not have the given index:
363+
364+
{:ok, integer()}
365+
"""}
366+
end
330367
end
331368

332369
describe "maps/structs" do

0 commit comments

Comments
 (0)