Skip to content

Commit

Permalink
Display file:line in already defined type error message (elixir-lang#…
Browse files Browse the repository at this point in the history
  • Loading branch information
eksperimental authored Jul 3, 2020
1 parent 11c3c66 commit b792ae5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
8 changes: 7 additions & 1 deletion lib/elixir/lib/kernel/typespec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,13 @@ defmodule Kernel.Typespec do
end

if Map.has_key?(type_pairs, type_pair) do
compile_error(env, "type #{name}/#{arity} is already defined")
{error_full_path, error_line} = type_pairs[type_pair]
error_relative_path = Path.relative_to_cwd(error_full_path)

compile_error(
env,
"type #{name}/#{arity} is already defined in #{error_relative_path}:#{error_line}"
)
end

Map.put(type_pairs, type_pair, {file, line})
Expand Down
48 changes: 27 additions & 21 deletions lib/elixir/test/elixir/typespec_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,33 @@ defmodule TypespecTest do
end

test "redefined type" do
assert_raise CompileError, ~r"type foo/0 is already defined", fn ->
test_module do
@type foo :: atom
@type foo :: integer
end
end

assert_raise CompileError, ~r"type foo/2 is already defined", fn ->
test_module do
@type foo :: atom
@type foo(var1, var2) :: {var1, var2}
@type foo(x, y) :: {x, y}
end
end

assert_raise CompileError, ~r"type foo/0 is already defined", fn ->
test_module do
@type foo :: atom
@typep foo :: integer
end
end
assert_raise CompileError,
~r"type foo/0 is already defined in test/elixir/typespec_test.exs:110",
fn ->
test_module do
@type foo :: atom
@type foo :: integer
end
end

assert_raise CompileError,
~r"type foo/2 is already defined in test/elixir/typespec_test.exs:120",
fn ->
test_module do
@type foo :: atom
@type foo(var1, var2) :: {var1, var2}
@type foo(x, y) :: {x, y}
end
end

assert_raise CompileError,
~r"type foo/0 is already defined in test/elixir/typespec_test.exs:129",
fn ->
test_module do
@type foo :: atom
@typep foo :: integer
end
end
end

test "type variable unused (singleton type variable)" do
Expand Down

0 comments on commit b792ae5

Please sign in to comment.