Skip to content

Commit 46cab56

Browse files
michalmuskalaJosé Valim
authored andcommitted
Mark struct update syntax as generated (#5067)
With changes to OTP 19, dialyzer started emitting warnings for the struct update syntax where variable could only be that struct. For example: def foo(%Foo{} = struct), do: %Foo{struct | bar: :baz} Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
1 parent 7aebe55 commit 46cab56

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

lib/elixir/src/elixir_map.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ translate_struct(Meta, Name, {'%{}', MapMeta, Args}, S) ->
9292

9393
{TMap, TS} = translate_map(MapMeta, Assocs, Var, VS),
9494

95-
{{'case', Ann, TUpdate, [
95+
{{'case', ?generated, TUpdate, [
9696
{clause, Ann, [Match], [], [TMap]},
97-
{clause, Ann, [Var], [], [elixir_utils:erl_call(Ann, erlang, error, [Error])]}
97+
{clause, ?generated, [Var], [], [elixir_utils:erl_call(Ann, erlang, error, [Error])]}
9898
]}, TS};
9999
match ->
100100
translate_map(MapMeta, Assocs ++ [{'__struct__', Name}], nil, US);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule Dialyzer.StructUpdate do
2+
defstruct [:foo]
3+
4+
def update(%__MODULE__{} = struct) do
5+
%__MODULE__{struct | foo: :bar}
6+
end
7+
end

lib/elixir/test/elixir/kernel/dialyzer_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ defmodule Kernel.DialyzerTest do
7676
assert_dialyze_no_warnings! context
7777
end
7878

79+
test "no warnings on struct update", context do
80+
copy_beam! context, Dialyzer.StructUpdate
81+
assert_dialyze_no_warnings! context
82+
end
83+
7984
defp copy_beam!(context, module) do
8085
name = "#{module}.beam"
8186
File.cp! Path.join(context[:base_dir], name),

0 commit comments

Comments
 (0)