Skip to content

Commit 1cd67c9

Browse files
committed
Do not warn when comparing literals
1 parent 9df0fe5 commit 1cd67c9

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,7 +1974,7 @@ defmodule Kernel do
19741974

19751975
defp build_boolean_check(operator, check, true_clause, false_clause) do
19761976
annotate_case(
1977-
[optimize_boolean: true, type_check: :expr],
1977+
[optimize_boolean: true],
19781978
quote do
19791979
case unquote(check) do
19801980
false -> unquote(false_clause)
@@ -2008,7 +2008,7 @@ defmodule Kernel do
20082008
assert_no_match_or_guard_scope(__CALLER__.context, "!")
20092009

20102010
annotate_case(
2011-
[optimize_boolean: true, type_check: :expr],
2011+
[optimize_boolean: true],
20122012
quote do
20132013
case unquote(value) do
20142014
x when :"Elixir.Kernel".in(x, [false, nil]) -> false
@@ -2022,7 +2022,7 @@ defmodule Kernel do
20222022
assert_no_match_or_guard_scope(__CALLER__.context, "!")
20232023

20242024
annotate_case(
2025-
[optimize_boolean: true, type_check: :expr],
2025+
[optimize_boolean: true],
20262026
quote do
20272027
case unquote(value) do
20282028
x when :"Elixir.Kernel".in(x, [false, nil]) -> true
@@ -3912,7 +3912,7 @@ defmodule Kernel do
39123912

39133913
defp build_if(condition, do: do_clause, else: else_clause) do
39143914
annotate_case(
3915-
[optimize_boolean: true, type_check: :expr],
3915+
[optimize_boolean: true],
39163916
quote do
39173917
case unquote(condition) do
39183918
x when :"Elixir.Kernel".in(x, [false, nil]) -> unquote(else_clause)

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,19 @@ defmodule Module.Types.Apply do
400400
end
401401
end
402402

403-
def remote(:erlang, name, [left, right] = args_types, expr, stack, context)
403+
def remote(
404+
:erlang,
405+
name,
406+
[left, right] = args_types,
407+
{_, _, args} = expr,
408+
stack,
409+
context
410+
)
404411
when name in [:==, :"/=", :"=:=", :"=/="] do
405412
context =
406413
cond do
407-
stack.mode == :infer ->
414+
# We ignore quoted literals as they most likely come from generated code.
415+
stack.mode == :infer or Macro.quoted_literal?(args) ->
408416
context
409417

410418
name in [:==, :"/="] and number_type?(left) and number_type?(right) ->

lib/elixir/lib/module/types/expr.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ defmodule Module.Types.Expr do
263263
{case_type, context} = of_expr(case_expr, stack, context)
264264

265265
# If we are only type checking the expression and the expression is a literal,
266-
# let's mark it as generated, as it is most likely a macro code.
267-
if is_atom(case_expr) and {:type_check, :expr} in meta do
266+
# let's mark it as generated, as it is most likely a macro code. However, if
267+
# no clause is matched, we should still check for that.
268+
if Macro.quoted_literal?(case_expr) do
268269
for {:->, meta, args} <- clauses, do: {:->, [generated: true] ++ meta, args}
269270
else
270271
clauses

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,11 @@ defmodule Module.Types.ExprTest do
860860
test "in dynamic mode" do
861861
assert typedyn!([x = 123, y = 456.0], x < y) == dynamic(boolean())
862862
assert typedyn!([x = 123, y = 456.0], x == y) == dynamic(boolean())
863-
assert typedyn!(123 == 456) == boolean()
863+
assert typedyn!([x = 123, y = 456], x == y) == dynamic(boolean())
864+
end
865+
866+
test "using literals" do
867+
assert typecheck!(:foo == :bar) == boolean()
864868
end
865869

866870
test "min/max" do
@@ -1060,6 +1064,15 @@ defmodule Module.Types.ExprTest do
10601064
end
10611065

10621066
describe "case" do
1067+
test "does not type check literals" do
1068+
assert typecheck!(
1069+
case :dev do
1070+
:dev -> :ok
1071+
:prod -> :error
1072+
end
1073+
) == atom([:ok, :error])
1074+
end
1075+
10631076
test "returns unions of all clauses" do
10641077
assert typecheck!(
10651078
[x],
@@ -1115,25 +1128,17 @@ defmodule Module.Types.ExprTest do
11151128
end
11161129

11171130
describe "conditionals" do
1118-
test "if does not report on literal atoms" do
1131+
test "if does not report on literals" do
11191132
assert typecheck!(
11201133
if true do
11211134
:ok
11221135
end
11231136
) == atom([:ok, nil])
11241137
end
11251138

1126-
test "and does not report on literal atoms" do
1139+
test "and does not report on literals" do
11271140
assert typecheck!(false and true) == boolean()
11281141
end
1129-
1130-
test "and reports on non-atom literals" do
1131-
assert typeerror!(1 and true) == ~l"""
1132-
the following conditional expression will always evaluate to integer():
1133-
1134-
1
1135-
"""
1136-
end
11371142
end
11381143

11391144
describe "receive" do

0 commit comments

Comments
 (0)