Skip to content

Commit 94e0e38

Browse files
committed
Do not crash for missing line info on type warnings
Closes #12263.
1 parent bfa799b commit 94e0e38

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/elixir/lib/module/types.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ defmodule Module.Types do
135135
# Collect relevant information from context and traces to report error
136136
def error_to_warning(:unable_apply, {mfa, args, expected, signature, stack}, context) do
137137
{fun, arity} = context.function
138-
line = get_meta(stack.last_expr)[:line]
139-
location = {context.file, line, {context.module, fun, arity}}
138+
location = {context.file, get_line(stack), {context.module, fun, arity}}
140139

141140
traces = type_traces(stack, context)
142141
{[signature | args], traces} = lift_all_types([signature | args], traces, context)
@@ -146,15 +145,16 @@ defmodule Module.Types do
146145

147146
def error_to_warning(:unable_unify, {left, right, stack}, context) do
148147
{fun, arity} = context.function
149-
line = get_meta(stack.last_expr)[:line]
150-
location = {context.file, line, {context.module, fun, arity}}
148+
location = {context.file, get_line(stack), {context.module, fun, arity}}
151149

152150
traces = type_traces(stack, context)
153151
{[left, right], traces} = lift_all_types([left, right], traces, context)
154152
error = {:unable_unify, left, right, {location, stack.last_expr, traces}}
155153
{Module.Types, error, location}
156154
end
157155

156+
defp get_line(stack), do: stack.last_expr |> get_meta() |> Keyword.get(:line, 0)
157+
158158
# Collect relevant traces from context.traces using stack.unify_stack
159159
defp type_traces(stack, context) do
160160
# TODO: Do we need the unify_stack or is enough to only get the last variable

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,22 @@ defmodule Module.Types.IntegrationTest do
562562
end
563563
end
564564

565+
describe "regressions" do
566+
test "handle missing location info from quoted" do
567+
assert capture_io(:stderr, fn ->
568+
quote do
569+
defmodule X do
570+
def f() do
571+
x = %{}
572+
%{x | key: :value}
573+
end
574+
end
575+
end
576+
|> Code.compile_quoted()
577+
end) =~ "warning:"
578+
end
579+
end
580+
565581
describe "after_verify" do
566582
test "reports functions" do
567583
files = %{

0 commit comments

Comments
 (0)