Skip to content

Commit

Permalink
Do not emit already consolidated warnings during mix xref trace, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jul 6, 2022
1 parent 564b691 commit 2325732
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
11 changes: 8 additions & 3 deletions lib/elixir/lib/code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ defmodule Code do
@boolean_compiler_options [
:docs,
:debug_info,
:ignore_already_consolidated,
:ignore_module_conflict,
:relative_paths,
:warnings_as_errors
Expand Down Expand Up @@ -1350,11 +1351,15 @@ defmodule Code do
module. This allows a developer to reconstruct the original source
code. Defaults to `true`.
* `:ignore_module_conflict` - when `true`, override modules that were
already defined without raising errors. Defaults to `false`.
* `:ignore_already_consolidated` - when `true`, does not warn when a protocol
has already been consolidated and a new implementation is added. Defaults
to `false`.
* `:ignore_module_conflict` - when `true`, does not warn when a module has
already been defined. Defaults to `false`.
* `:relative_paths` - when `true`, use relative paths in quoted nodes,
warnings and errors generated by the compiler. Note disabling this option
warnings, and errors generated by the compiler. Note disabling this option
won't affect runtime warnings and errors. Defaults to `true`.
* `:warnings_as_errors` - causes compilation to fail when warnings are
Expand Down
3 changes: 2 additions & 1 deletion lib/elixir/lib/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,8 @@ defmodule Protocol do

@doc false
def __ensure_defimpl__(protocol, for, env) do
if Protocol.consolidated?(protocol) do
if not Code.get_compiler_option(:ignore_already_consolidated) and
Protocol.consolidated?(protocol) do
message =
"the #{inspect(protocol)} protocol has already been consolidated, an " <>
"implementation for #{inspect(for)} has no effect. If you want to " <>
Expand Down
1 change: 1 addition & 0 deletions lib/elixir/src/elixir.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ start(_Type, _Args) ->

%% Compiler options
{docs, true},
{ignore_already_consolidated, false},
{ignore_module_conflict, false},
{parser_options, []},
{debug_info, true},
Expand Down
14 changes: 13 additions & 1 deletion lib/elixir/test/elixir/protocol/consolidation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ defmodule Protocol.ConsolidationTest do
refute Protocol.consolidated?(Enumerable)
end

test "consolidation prevents new implementations" do
test "consolidation warns on new implementations" do
output =
ExUnit.CaptureIO.capture_io(:stderr, fn ->
defimpl WithAny, for: Integer do
Expand All @@ -74,6 +74,18 @@ defmodule Protocol.ConsolidationTest do
:code.delete(WithAny.Integer)
end

test "consolidation warns on new implementations unless disabled" do
Code.put_compiler_option(:ignore_already_consolidated, true)

defimpl WithAny, for: Integer do
def ok(_any), do: :ok
end
after
Code.put_compiler_option(:ignore_already_consolidated, false)
:code.purge(WithAny.Integer)
:code.delete(WithAny.Integer)
end

test "consolidated implementations without any" do
assert is_nil(Sample.impl_for(:foo))
assert is_nil(Sample.impl_for(fn x -> x end))
Expand Down
3 changes: 2 additions & 1 deletion lib/mix/lib/mix/tasks/xref.ex
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ defmodule Mix.Tasks.Xref do
into: MapSet.new(),
do: module

old = Code.compiler_options(ignore_module_conflict: true, tracers: [__MODULE__])
new = [ignore_already_consolidated: true, ignore_module_conflict: true, tracers: [__MODULE__]]
old = Code.compiler_options(new)
ets = :ets.new(__MODULE__, [:named_table, :duplicate_bag, :public])
:ets.insert(ets, [{:config, set, trace_label(opts[:label])}])

Expand Down

0 comments on commit 2325732

Please sign in to comment.