@@ -5455,21 +5455,33 @@ defmodule Kernel do
5455
5455
use `@type`.
5456
5456
"""
5457
5457
defmacro defstruct ( fields ) do
5458
- quote bind_quoted: [ fields: fields , bootstrapped?: bootstrapped? ( Enum ) ] do
5459
- { struct , derive , kv , body } =
5460
- Kernel.Utils . defstruct ( __MODULE__ , fields , bootstrapped? , __ENV__ )
5458
+ header =
5459
+ quote bind_quoted: [ fields: fields , bootstrapped?: bootstrapped? ( Enum ) ] do
5460
+ { struct , derive , kv , body } =
5461
+ Kernel.Utils . defstruct ( __MODULE__ , fields , bootstrapped? , __ENV__ )
5462
+
5463
+ case derive do
5464
+ [ ] -> :ok
5465
+ _ -> Protocol . __derive__ ( derive , __MODULE__ , __ENV__ )
5466
+ end
5467
+ end
5461
5468
5462
- case derive do
5463
- [ ] -> :ok
5464
- _ -> Protocol . __derive__ ( derive , __MODULE__ , __ENV__ )
5469
+ # We attach the line: 0 to struct functions because we don't want
5470
+ # the generated callbacks to count towards code coverage and metrics,
5471
+ # especially since they are often expanded at compile-time.
5472
+ functions =
5473
+ quote line: 0 , unquote: false do
5474
+ def __struct__ ( ) , do: @ __struct__
5475
+ def __struct__ ( unquote ( kv ) ) , do: unquote ( body )
5465
5476
end
5466
5477
5467
- def __struct__ ( ) , do: @ __struct__
5468
- def __struct__ ( unquote ( kv ) ) , do: unquote ( body )
5478
+ footer =
5479
+ quote do
5480
+ Kernel.Utils . announce_struct ( __MODULE__ )
5481
+ struct
5482
+ end
5469
5483
5470
- Kernel.Utils . announce_struct ( __MODULE__ )
5471
- struct
5472
- end
5484
+ { :__block__ , [ ] , [ header , functions , footer ] }
5473
5485
end
5474
5486
5475
5487
@ doc ~S"""
0 commit comments