Skip to content

Commit 469acb1

Browse files
gldubcjosevalim
authored andcommitted
Fix guard equality with short-circuit operators
Signed-off-by: José Valim <jose.valim@gmail.com> Closes #15253. Closes #15257.
1 parent 7061c37 commit 469acb1

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

lib/elixir/lib/module/types/pattern.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,8 @@ defmodule Module.Types.Pattern do
12821282
# only be true if both clauses are executed, so we know the first
12831283
# argument has to be true and the second has to be expected.
12841284
cond do
1285-
subtype?(expected, both_domain) ->
1286-
of_logical_all([left | right], true, both_domain, abort_domain, stack, context)
1285+
is_nil(context.pattern_info) or subtype?(expected, both_domain) ->
1286+
of_logical_all([left | right], true, expected, abort_domain, stack, context)
12871287

12881288
right == [] ->
12891289
of_guard(left, expected, left, stack, context)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,24 @@ defmodule Module.Types.PatternTest do
896896
"""
897897
end
898898

899+
test "nested conditional checks" do
900+
assert typecheck!([a, b], true == (b and a), a) == dynamic(atom([true]))
901+
assert typecheck!([a, b], false == (b and a), a) == dynamic(term())
902+
903+
assert typecheck!([a, b, c], a != (b or c), a) == dynamic(term())
904+
assert typecheck!([a, b, c], a == (b or c), a) == dynamic(term())
905+
assert typecheck!([a, b, c], a !== (b or c), a) == dynamic(term())
906+
assert typecheck!([a, b, c], a === (b or c), a) == dynamic(term())
907+
assert typecheck!([a, b, c], a == hd(b or c), a) == dynamic(term())
908+
assert typecheck!([a, b, c], a != (b and c), a) == dynamic(term())
909+
assert typecheck!([a, b, c], a == (b and c), a) == dynamic(term())
910+
911+
assert typecheck!([a, b, c], a == :erlang.and(b, c), a) == dynamic(boolean())
912+
assert typecheck!([a, b, c], a == :erlang.or(b, c), a) == dynamic(boolean())
913+
assert typecheck!([a, b, c], c and a === (b or c), a) == dynamic(atom([true]))
914+
assert typecheck!([a, b, c], c and a === (b and c), a) == dynamic(boolean())
915+
end
916+
899917
test "domain checks" do
900918
# Regular domain check
901919
assert typecheck!([x, z], length(x) == z, x) == dynamic(list(term()))

0 commit comments

Comments
 (0)