Skip to content

Commit 4911916

Browse files
committed
Do not warn when comparing literals
1 parent 546a0db commit 4911916

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

lib/elixir/lib/kernel.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -1972,7 +1972,7 @@ defmodule Kernel do
19721972

19731973
defp build_boolean_check(operator, check, true_clause, false_clause) do
19741974
annotate_case(
1975-
[optimize_boolean: true, type_check: :expr],
1975+
[optimize_boolean: true],
19761976
quote do
19771977
case unquote(check) do
19781978
false -> unquote(false_clause)
@@ -2006,7 +2006,7 @@ defmodule Kernel do
20062006
assert_no_match_or_guard_scope(__CALLER__.context, "!")
20072007

20082008
annotate_case(
2009-
[optimize_boolean: true, type_check: :expr],
2009+
[optimize_boolean: true],
20102010
quote do
20112011
case unquote(value) do
20122012
x when :"Elixir.Kernel".in(x, [false, nil]) -> false
@@ -2020,7 +2020,7 @@ defmodule Kernel do
20202020
assert_no_match_or_guard_scope(__CALLER__.context, "!")
20212021

20222022
annotate_case(
2023-
[optimize_boolean: true, type_check: :expr],
2023+
[optimize_boolean: true],
20242024
quote do
20252025
case unquote(value) do
20262026
x when :"Elixir.Kernel".in(x, [false, nil]) -> true
@@ -3910,7 +3910,7 @@ defmodule Kernel do
39103910

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

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

+10-2
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

+3-2
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

+16-11
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)