Skip to content

Commit 127691d

Browse files
author
José Valim
committed
Ensure structs can be expanded in dynamic module names, closes #4894
Signed-off-by: José Valim <[email protected]>
1 parent e848e87 commit 127691d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/elixir/src/elixir_map.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ expand_struct(Meta, Left, Right, #{context := Context} = E) ->
3131
end,
3232

3333
EMeta =
34-
case lists:member(ELeft, ?m(E, context_modules)) of
34+
%% We also include the current module because it won't be present
35+
%% in context module in case the module name is defined dynamically.
36+
case lists:member(ELeft, [?m(E, module) | ?m(E, context_modules)]) of
3537
true -> [{struct, context} | Meta];
3638
false -> Meta
3739
end,

lib/elixir/test/elixir/map_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ defmodule MapTest do
147147
%^struct{name: "john"} = user
148148
end
149149

150+
test "structs when using dynamic modules" do
151+
defmodule Module.concat(MapTest, DynamicUser) do
152+
defstruct [:name, :age]
153+
154+
def sample do
155+
%__MODULE__{}
156+
end
157+
end
158+
end
159+
150160
test "structs when quoted" do
151161
assert (quote do
152162
%User{foo: 1}

0 commit comments

Comments
 (0)