Skip to content

Commit 54c0574

Browse files
author
José Valim
committed
Explicitly announce structs
Before this patch, we could announce a struct too early, only when __struct__/0 was defined (and not __struct__/1) leading to deadlocks in the compiler. We fixed the bug and made struct announcements to avoid future bugs. Closes #4844 Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
1 parent 8d50e1f commit 54c0574

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3473,7 +3473,7 @@ defmodule Kernel do
34733473
end
34743474

34753475
unquote(builder)
3476-
3476+
Kernel.Utils.announce_struct(__MODULE__)
34773477
fields
34783478
end
34793479
end

lib/elixir/lib/kernel/utils.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,11 @@ defmodule Kernel.Utils do
8484
List.wrap(Module.get_attribute(module, :enforce_keys)),
8585
Module.get_attribute(module, :derive)}
8686
end
87+
88+
def announce_struct(module) do
89+
case :erlang.get(:elixir_compiler_pid) do
90+
:undefined -> :ok
91+
pid -> send(pid, {:struct_available, module})
92+
end
93+
end
8794
end

lib/elixir/src/elixir_def.erl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ store_definition(Meta, Line, Kind, CheckClauses, Name, Args, Guards, Body, KeepL
105105
store_each(CheckClauses, Kind, File, Location, Module, DefaultsLength, Function),
106106
[store_each(false, Kind, File, Location, Module, 0,
107107
default_function_for(Kind, Name, Default)) || Default <- Defaults],
108-
109-
make_struct_available(Kind, Module, Name, Args),
110108
{Name, Arity}.
111109

112110
%% @on_definition
@@ -117,16 +115,6 @@ run_on_definition_callbacks(Kind, Line, Module, Name, Args, Guards, Expr, E) ->
117115
_ = [Mod:Fun(Env, Kind, Name, Args, Guards, Expr) || {Mod, Fun} <- Callbacks],
118116
ok.
119117

120-
make_struct_available(def, Module, '__struct__', []) ->
121-
case erlang:get(elixir_compiler_pid) of
122-
undefined -> ok;
123-
Pid ->
124-
Pid ! {struct_available, Module},
125-
ok
126-
end;
127-
make_struct_available(_, _, _, _) ->
128-
ok.
129-
130118
%% Retrieve location from meta file (if Key == keep)
131119
%% or @file, otherwise nil
132120

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
defmodule Bar do
2-
defstruct name: ""
3-
def foo?(%Foo{}), do: true
2+
defstruct name: "", foo: %Foo{}
43
end

0 commit comments

Comments
 (0)