Skip to content

Commit adc9a28

Browse files
committed
Warn on structs even if both sides have numbers
1 parent 53b7e55 commit adc9a28

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -429,21 +429,21 @@ defmodule Module.Types.Apply do
429429
return(boolean(), [left, right], stack)
430430
end
431431

432-
cond do
433-
not check? ->
434-
{:ok, result}
432+
if not check? do
433+
{:ok, result}
434+
else
435+
common = intersection(left, right)
435436

436-
number_type?(left) and number_type?(right) ->
437-
{:ok, result}
437+
cond do
438+
empty?(common) and not (number_type?(left) and number_type?(right)) ->
439+
{:error, :mismatched_comparison}
438440

439-
true ->
440-
common = intersection(left, right)
441+
match?({false, _}, map_fetch(dynamic(common), :__struct__)) ->
442+
{:error, :struct_comparison}
441443

442-
cond do
443-
empty?(common) -> {:error, :mismatched_comparison}
444-
match?({false, _}, map_fetch(common, :__struct__)) -> {:error, :struct_comparison}
445-
true -> {:ok, result}
446-
end
444+
true ->
445+
{:ok, result}
446+
end
447447
end
448448
end
449449

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -1103,18 +1103,27 @@ defmodule Module.Types.ExprTest do
11031103
assert typeerror!(
11041104
[x = %Point{}, mod = Kernel, condition],
11051105
(
1106-
y = if condition, do: 123, else: %Point{}
1106+
y = if condition, do: 456, else: %Point{}
11071107
mod.<=(x, y)
11081108
)
11091109
) =~ "comparison with structs found:"
11101110

11111111
assert typecheck!(
1112-
[x = 456, mod = Kernel, condition],
1112+
[x = 123, mod = Kernel, condition],
11131113
(
1114-
y = if condition, do: 123, else: %Point{}
1114+
y = if condition, do: 456, else: %Point{}
11151115
mod.<=(x, y)
11161116
)
11171117
) == boolean()
1118+
1119+
assert typeerror!(
1120+
[mod = Kernel, condition],
1121+
(
1122+
x = if condition, do: 123, else: %Point{}
1123+
y = if condition, do: 456, else: %Point{}
1124+
mod.<=(x, y)
1125+
)
1126+
) =~ "comparison with structs found:"
11181127
end
11191128
end
11201129

0 commit comments

Comments
 (0)