Skip to content

Commit 32c2a2d

Browse files
committed
Expand :for in protocols with the appropriate env
1 parent a285e58 commit 32c2a2d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/elixir/lib/protocol.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,8 @@ defmodule Protocol do
931931
raise ArgumentError, "defimpl/3 expects a :for option when declared outside a module"
932932
end)
933933

934-
for = Macro.expand_literals(for, %{env | module: Kernel, function: {:defimpl, 3}})
934+
for =
935+
Macro.expand_literals(for, %{env | module: env.module || Elixir, function: {:__impl__, 1}})
935936

936937
case opts do
937938
[] -> raise ArgumentError, "defimpl expects a do-end block"

lib/elixir/test/elixir/protocol_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ defmodule ProtocolTest do
7474
end
7575
end
7676

77+
defmodule ImplStructExplicitFor do
78+
defstruct a: 0, b: 0
79+
80+
defimpl Sample, for: __MODULE__ do
81+
def ok(_struct), do: true
82+
end
83+
end
84+
7785
test "protocol implementations without any" do
7886
assert is_nil(Sample.impl_for(:foo))
7987
assert is_nil(Sample.impl_for(fn x -> x end))
@@ -91,6 +99,7 @@ defmodule ProtocolTest do
9199
assert is_nil(Sample.impl_for(make_ref()))
92100

93101
assert Sample.impl_for(%ImplStruct{}) == Sample.ProtocolTest.ImplStruct
102+
assert Sample.impl_for(%ImplStructExplicitFor{}) == Sample.ProtocolTest.ImplStructExplicitFor
94103
assert Sample.impl_for(%NoImplStruct{}) == nil
95104
end
96105

0 commit comments

Comments
 (0)