Skip to content

Commit e17069e

Browse files
committed
Protocol should not add deps on implementations, closes #15202
1 parent 4aa837d commit e17069e

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

lib/elixir/lib/protocol.ex

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -818,11 +818,9 @@ defmodule Protocol do
818818
@fallback_to_any false
819819
@undefined_impl_description ""
820820

821-
# Invoke the user given block
822-
_ = unquote(block)
823-
824-
# Finalize expansion
821+
res = unquote(block)
825822
unquote(after_defprotocol())
823+
res
826824
end
827825
end
828826
end
@@ -932,7 +930,7 @@ defmodule Protocol do
932930
quote bind_quoted: [built_in: built_in()] do
933931
any_impl_for =
934932
if @fallback_to_any do
935-
__MODULE__.Any
933+
Protocol.__concat__(__MODULE__, "Any")
936934
else
937935
nil
938936
end
@@ -1225,10 +1223,12 @@ defmodule Protocol do
12251223
end
12261224

12271225
@doc false
1228-
def __concat__(left, right) do
1229-
String.to_atom(
1230-
ensure_prefix(Atom.to_string(left)) <> "." <> remove_prefix(Atom.to_string(right))
1231-
)
1226+
def __concat__(left, right) when is_atom(right) do
1227+
__concat__(left, remove_prefix(Atom.to_string(right)))
1228+
end
1229+
1230+
def __concat__(left, right) when is_binary(right) do
1231+
String.to_atom(ensure_prefix(Atom.to_string(left)) <> "." <> right)
12321232
end
12331233

12341234
defp ensure_prefix("Elixir." <> _ = left), do: left

lib/elixir/test/elixir/kernel/lexical_tracker_test.exs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ defmodule Kernel.LexicalTrackerTest do
594594
refute Config in runtime
595595
end
596596

597-
test "defimpl does not add dependencies on for only on impl" do
597+
test "defimpl does not add dependencies on for, only on impl" do
598598
{{compile, exports, runtime, _}, _binding} =
599599
Code.eval_string("""
600600
defimpl String.Chars, for: Kernel.LexicalTrackerTest do
@@ -610,5 +610,19 @@ defmodule Kernel.LexicalTrackerTest do
610610
refute Kernel.LexicalTrackerTest in exports
611611
refute Kernel.LexicalTrackerTest in runtime
612612
end
613+
614+
test "defprotocol does not add dependencies on implementations" do
615+
{{compile, _exports, _runtime, _}, _binding} =
616+
Code.eval_string("""
617+
defprotocol Kernel.LexicalTracker.ProtocolTest do
618+
@fallback_to_any true
619+
def example(val)
620+
Kernel.LexicalTracker.references(__ENV__.lexical_tracker)
621+
end |> elem(3)
622+
""")
623+
624+
refute Kernel.LexicalTracker.ProtocolTest.Any in compile
625+
refute Kernel.LexicalTracker.ProtocolTest.Tuple in compile
626+
end
613627
end
614628
end

0 commit comments

Comments
 (0)