Skip to content

Commit 6eca976

Browse files
sabiwarajosevalim
authored andcommitted
Deterministic struct expansion (#12418)
1 parent e977f02 commit 6eca976

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

lib/elixir/lib/module/types.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ defmodule Module.Types do
252252
"undefined field \"#{atom}\" ",
253253
format_expr(expr, location),
254254
"expected one of the following fields: ",
255-
Enum.map_join(Enum.sort(known_atoms), ", ", & &1),
255+
Enum.join(Enum.sort(known_atoms), ", "),
256256
"\n\n",
257257
traces,
258258
format_message_hints(hints),

lib/elixir/src/elixir_map.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ expand_struct(Meta, Left, {'%{}', MapMeta, MapArgs}, S, #{context := Context} =
2525
AssocKeys = [K || {K, _} <- Assocs],
2626
Struct = load_struct(Meta, ELeft, [Assocs], AssocKeys, EE),
2727
Keys = ['__struct__'] ++ AssocKeys,
28-
WithoutKeys = maps:to_list(maps:without(Keys, Struct)),
28+
WithoutKeys = lists:sort(maps:to_list(maps:without(Keys, Struct))),
2929
StructAssocs = elixir_quote:escape(WithoutKeys, none, false),
3030
{{'%', Meta, [ELeft, {'%{}', MapMeta, StructAssocs ++ Assocs}]}, SE, EE};
3131

lib/elixir/test/elixir/module/types/pattern_test.exs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,25 @@ defmodule Module.Types.PatternTest do
140140
end
141141

142142
test "struct" do
143-
assert quoted_pattern(%:"Elixir.Module.Types.PatternTest.Struct"{}) ==
144-
{:ok,
145-
{:map,
146-
[
147-
{:required, {:atom, :__struct__}, {:atom, Module.Types.PatternTest.Struct}},
148-
{:required, {:atom, :bar}, :dynamic},
149-
{:required, {:atom, :baz}, :dynamic},
150-
{:required, {:atom, :foo}, :dynamic}
151-
]}}
152-
153-
assert quoted_pattern(%:"Elixir.Module.Types.PatternTest.Struct"{foo: 123, bar: :atom}) ==
154-
{:ok,
155-
{:map,
156-
[
157-
{:required, {:atom, :foo}, :integer},
158-
{:required, {:atom, :bar}, {:atom, :atom}},
159-
{:required, {:atom, :__struct__}, {:atom, Module.Types.PatternTest.Struct}},
160-
{:required, {:atom, :baz}, :dynamic}
161-
]}}
143+
assert {:ok, {:map, fields}} = quoted_pattern(%:"Elixir.Module.Types.PatternTest.Struct"{})
144+
145+
assert Enum.sort(fields) == [
146+
{:required, {:atom, :__struct__}, {:atom, Module.Types.PatternTest.Struct}},
147+
{:required, {:atom, :bar}, :dynamic},
148+
{:required, {:atom, :baz}, :dynamic},
149+
{:required, {:atom, :foo}, :dynamic}
150+
]
151+
152+
assert {:ok, {:map, fields}} =
153+
quoted_pattern(%:"Elixir.Module.Types.PatternTest.Struct"{foo: 123, bar: :atom})
154+
155+
assert Enum.sort(fields) ==
156+
[
157+
{:required, {:atom, :__struct__}, {:atom, Module.Types.PatternTest.Struct}},
158+
{:required, {:atom, :bar}, {:atom, :atom}},
159+
{:required, {:atom, :baz}, :dynamic},
160+
{:required, {:atom, :foo}, :integer}
161+
]
162162
end
163163

164164
test "struct var" do

0 commit comments

Comments
 (0)