Skip to content

Commit a211223

Browse files
author
José Valim
committed
Keep struct fields ordered in types
Closes elixir-lang/ex_doc#1016
1 parent 8bc3c82 commit a211223

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/elixir/lib/kernel/typespec.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ defmodule Kernel.Typespec do
557557
types =
558558
:lists.map(
559559
fn {field, _} -> {field, Keyword.get(fields, field, quote(do: term()))} end,
560-
struct
560+
:lists.sort(struct)
561561
)
562562

563563
fun = fn {field, _} ->

lib/elixir/test/elixir/typespec_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,23 @@ defmodule TypespecTest do
480480
assert {:type, _, :map_field_exact, [{:atom, _, :other}, {:type, _, :term, []}]} = arg2
481481
end
482482

483+
@fields Enum.map(10..42, &{:"f#{&1}", :ok})
484+
485+
test "@type with a large struct" do
486+
bytecode =
487+
test_module do
488+
defstruct unquote(@fields)
489+
@type my_type :: %TypespecSample{unquote_splicing(@fields)}
490+
end
491+
492+
assert [type: {:my_type, type, []}] = types(bytecode)
493+
assert {:type, _, :map, [struct, arg1, arg2 | _]} = type
494+
assert {:type, _, :map_field_exact, struct_args} = struct
495+
assert [{:atom, _, :__struct__}, {:atom, _, TypespecSample}] = struct_args
496+
assert {:type, _, :map_field_exact, [{:atom, _, :f10}, {:atom, _, :ok}]} = arg1
497+
assert {:type, _, :map_field_exact, [{:atom, _, :f11}, {:atom, _, :ok}]} = arg2
498+
end
499+
483500
test "@type with struct does not @enforce_keys" do
484501
bytecode =
485502
test_module do

0 commit comments

Comments
 (0)