Skip to content

Commit 5858e7c

Browse files
authored
Fix wrong intersection with union of dynamic() (#15340)
1 parent 2fd3506 commit 5858e7c

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

lib/elixir/lib/module/types/descr.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,6 @@ defmodule Module.Types.Descr do
448448
"""
449449
def intersection(:term, other), do: remove_optional(other)
450450
def intersection(other, :term), do: remove_optional(other)
451-
def intersection(%{dynamic: :term}, other), do: dynamic(remove_optional(other))
452-
def intersection(other, %{dynamic: :term}), do: dynamic(remove_optional(other))
453451

454452
def intersection(left, right) do
455453
is_gradual_left = gradual?(left)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,11 @@ defmodule Module.Types.DescrTest do
15001500
assert truthiness(dynamic(type)) == :always_false
15011501
end
15021502

1503+
assert equal?(
1504+
intersection(union(atom(), dynamic()), union(atom(), dynamic())),
1505+
union(atom(), dynamic())
1506+
)
1507+
15031508
for type <-
15041509
[negation(atom()), atom([true]), negation(atom([false, nil])), atom([:ok]), integer()] do
15051510
assert truthiness(type) == :always_true

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,20 @@ defmodule Module.Types.ExprTest do
14251425
assert typecheck!([x = 123, y = 456.0], x == y) == boolean()
14261426
end
14271427

1428+
test "preserves static components across gradual self-intersections" do
1429+
assert typecheck!(
1430+
[x],
1431+
(
1432+
# y :: :foo or dynamic()
1433+
y = if :rand.uniform() > 0.5, do: :foo, else: x
1434+
# y's type intersected with itself
1435+
y = y
1436+
1437+
y
1438+
)
1439+
) == union(atom([:foo]), dynamic())
1440+
end
1441+
14281442
test "in dynamic mode" do
14291443
assert typedyn!([x = 123, y = 456.0], x < y) == dynamic(boolean())
14301444
assert typedyn!([x = 123, y = 456.0], x == y) == dynamic(boolean())

0 commit comments

Comments
 (0)