Ensure escaping works within struct fields, closes #14817

This commit is contained in:
José Valim
2025-10-07 13:07:42 +02:00
parent 519eff5e29
commit 7844c98fe7
2 changed files with 13 additions and 1 deletions
+2 -1
View File
@@ -38,7 +38,8 @@ expand_struct(Meta, Left, {'%{}', MapMeta, MapArgs}, S, #{context := Context} =
Keys = ['__struct__'] ++ AssocKeys,
WithoutKeys = lists:sort(maps:to_list(maps:without(Keys, Struct))),
StructAssocs = elixir_quote:escape(WithoutKeys, none, false),
{{'%', Meta, [ELeft, {'%{}', MapMeta, StructAssocs ++ Assocs}]}, SE, EE};
{EStructAssocs, SA, EA} = elixir_expand:expand(StructAssocs, SE, EE),
{{'%', Meta, [ELeft, {'%{}', MapMeta, EStructAssocs ++ Assocs}]}, SA, EA};
{'%{}', MapMeta, Assocs} ->
_ = load_struct_info(Meta, ELeft, Assocs, EE),
+11
View File
@@ -176,6 +176,17 @@ defmodule MacroTest do
} = Macro.escape(~r/foo/)
end
@tag :re_import
test "escape works within structs fields" do
defmodule Test do
# Structs have their own escape logic, which now needs to be expanded
defstruct my_regex: ~r/^hi$/
def init, do: %__MODULE__{}
end
assert %Regex{} = Test.init().my_regex
end
defmodule EscapedStruct do
defstruct [:ast, :ref]