Skip to content

Commit 7844c98

Browse files
committed
Ensure escaping works within struct fields, closes #14817
1 parent 519eff5 commit 7844c98

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/elixir/src/elixir_map.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ expand_struct(Meta, Left, {'%{}', MapMeta, MapArgs}, S, #{context := Context} =
3838
Keys = ['__struct__'] ++ AssocKeys,
3939
WithoutKeys = lists:sort(maps:to_list(maps:without(Keys, Struct))),
4040
StructAssocs = elixir_quote:escape(WithoutKeys, none, false),
41-
{{'%', Meta, [ELeft, {'%{}', MapMeta, StructAssocs ++ Assocs}]}, SE, EE};
41+
{EStructAssocs, SA, EA} = elixir_expand:expand(StructAssocs, SE, EE),
42+
{{'%', Meta, [ELeft, {'%{}', MapMeta, EStructAssocs ++ Assocs}]}, SA, EA};
4243

4344
{'%{}', MapMeta, Assocs} ->
4445
_ = load_struct_info(Meta, ELeft, Assocs, EE),

lib/elixir/test/elixir/macro_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ defmodule MacroTest do
176176
} = Macro.escape(~r/foo/)
177177
end
178178

179+
@tag :re_import
180+
test "escape works within structs fields" do
181+
defmodule Test do
182+
# Structs have their own escape logic, which now needs to be expanded
183+
defstruct my_regex: ~r/^hi$/
184+
def init, do: %__MODULE__{}
185+
end
186+
187+
assert %Regex{} = Test.init().my_regex
188+
end
189+
179190
defmodule EscapedStruct do
180191
defstruct [:ast, :ref]
181192

0 commit comments

Comments
 (0)