Skip to content

Commit ada2817

Browse files
authored
Fix invalid type inference on size compare in guards (#15324)
1 parent 89e0106 commit ada2817

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,8 @@ defmodule Module.Types.Apply do
726726

727727
{size, {{:., _, [:erlang, fun]}, _, [arg]}} when is_data_size(fun, size) ->
728728
case booleaness(expected) do
729-
{true, _} -> sized_order(invert_order(name), fun, size, arg, @atom_true)
730-
{false, _} -> sized_order(name, fun, size, arg, @atom_false)
729+
{true, _} -> sized_order(mirror_order(name), fun, size, arg, @atom_true)
730+
{false, _} -> sized_order(invert_order(mirror_order(name)), fun, size, arg, @atom_false)
731731
_ -> :none
732732
end
733733

@@ -779,6 +779,11 @@ defmodule Module.Types.Apply do
779779
defp invert_order(:>), do: :"=<"
780780
defp invert_order(:<), do: :>=
781781

782+
defp mirror_order(:>=), do: :"=<"
783+
defp mirror_order(:"=<"), do: :>=
784+
defp mirror_order(:>), do: :<
785+
defp mirror_order(:<), do: :>
786+
782787
@doc """
783788
Returns the domain of an unknown module.
784789

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,10 +1166,10 @@ defmodule Module.Types.PatternTest do
11661166
assert typecheck!([x], length(x) >= 0, x) == dynamic(list(term()))
11671167
assert typecheck!([x], length(x) <= 0, x) == dynamic(empty_list())
11681168

1169-
assert typecheck!([x], 0 <= length(x), x) == dynamic(non_empty_list(term()))
1170-
assert typecheck!([x], 0 >= length(x), x) == dynamic(list(term()))
1171-
assert typecheck!([x], 0 < length(x), x) == dynamic(list(term()))
1172-
assert typecheck!([x], 0 > length(x), x) == dynamic(empty_list())
1169+
assert typecheck!([x], 0 <= length(x), x) == dynamic(list(term()))
1170+
assert typecheck!([x], 0 >= length(x), x) == dynamic(empty_list())
1171+
assert typecheck!([x], 0 < length(x), x) == dynamic(non_empty_list(term()))
1172+
assert typecheck!([x], 0 > length(x), x) == dynamic(list(term()))
11731173

11741174
assert typecheck!([x], not (length(x) > 0), x) == dynamic(empty_list())
11751175
assert typecheck!([x], not (length(x) < 0), x) == dynamic(list(term()))
@@ -1214,10 +1214,10 @@ defmodule Module.Types.PatternTest do
12141214
assert typecheck!([x], map_size(x) >= 0, x) == dynamic(open_map())
12151215
assert typecheck!([x], map_size(x) <= 0, x) == dynamic(empty_map())
12161216

1217-
assert typecheck!([x], 0 <= map_size(x), x) == dynamic(@non_empty_map)
1218-
assert typecheck!([x], 0 >= map_size(x), x) == dynamic(open_map())
1219-
assert typecheck!([x], 0 < map_size(x), x) == dynamic(open_map())
1220-
assert typecheck!([x], 0 > map_size(x), x) == dynamic(empty_map())
1217+
assert typecheck!([x], 0 <= map_size(x), x) == dynamic(open_map())
1218+
assert typecheck!([x], 0 >= map_size(x), x) == dynamic(empty_map())
1219+
assert typecheck!([x], 0 < map_size(x), x) == dynamic(@non_empty_map)
1220+
assert typecheck!([x], 0 > map_size(x), x) == dynamic(open_map())
12211221

12221222
assert typecheck!([x], not (map_size(x) > 0), x) == dynamic(empty_map())
12231223
assert typecheck!([x], not (map_size(x) < 0), x) == dynamic(open_map())
@@ -1278,10 +1278,10 @@ defmodule Module.Types.PatternTest do
12781278
assert typecheck!([x], tuple_size(x) >= 0, x) == dynamic(open_tuple([]))
12791279
assert typecheck!([x], tuple_size(x) <= 0, x) == dynamic(tuple([]))
12801280

1281-
assert typecheck!([x], 0 <= tuple_size(x), x) == dynamic(open_tuple([term()]))
1282-
assert typecheck!([x], 0 >= tuple_size(x), x) == dynamic(open_tuple([]))
1283-
assert typecheck!([x], 0 < tuple_size(x), x) == dynamic(open_tuple([]))
1284-
assert typecheck!([x], 0 > tuple_size(x), x) == dynamic(tuple([]))
1281+
assert typecheck!([x], 0 <= tuple_size(x), x) == dynamic(open_tuple([]))
1282+
assert typecheck!([x], 0 >= tuple_size(x), x) == dynamic(tuple([]))
1283+
assert typecheck!([x], 0 < tuple_size(x), x) == dynamic(open_tuple([term()]))
1284+
assert typecheck!([x], 0 > tuple_size(x), x) == dynamic(open_tuple([]))
12851285

12861286
assert typecheck!([x], not (tuple_size(x) > 0), x) == dynamic(tuple([]))
12871287
assert typecheck!([x], not (tuple_size(x) < 0), x) == dynamic(open_tuple([]))
@@ -1293,10 +1293,10 @@ defmodule Module.Types.PatternTest do
12931293
assert typecheck!([x], tuple_size(x) >= 2, x) == dynamic(@open_binary_tuple)
12941294
assert typecheck!([x], tuple_size(x) <= 2, x) == dynamic(@non_open_ternary_tuple)
12951295

1296-
assert typecheck!([x], 2 <= tuple_size(x), x) == dynamic(@open_ternary_tuple)
1297-
assert typecheck!([x], 2 >= tuple_size(x), x) == dynamic(@non_open_binary_tuple)
1298-
assert typecheck!([x], 2 < tuple_size(x), x) == dynamic(@open_binary_tuple)
1299-
assert typecheck!([x], 2 > tuple_size(x), x) == dynamic(@non_open_ternary_tuple)
1296+
assert typecheck!([x], 2 <= tuple_size(x), x) == dynamic(@open_binary_tuple)
1297+
assert typecheck!([x], 2 >= tuple_size(x), x) == dynamic(@non_open_ternary_tuple)
1298+
assert typecheck!([x], 2 < tuple_size(x), x) == dynamic(@open_ternary_tuple)
1299+
assert typecheck!([x], 2 > tuple_size(x), x) == dynamic(@non_open_binary_tuple)
13001300

13011301
assert typecheck!([x], not (tuple_size(x) > 2), x) == dynamic(@non_open_ternary_tuple)
13021302
assert typecheck!([x], not (tuple_size(x) < 2), x) == dynamic(@open_binary_tuple)

0 commit comments

Comments
 (0)