Skip to content

Commit 65a0554

Browse files
amatalaijosevalim
authored andcommitted
Run code formatter on Kernel.ParallelCompiler (#6796)
1 parent 6ca2fd1 commit 65a0554

File tree

1 file changed

+84
-51
lines changed

1 file changed

+84
-51
lines changed

lib/elixir/lib/kernel/parallel_compiler.ex

+84-51
Original file line numberDiff line numberDiff line change
@@ -93,36 +93,41 @@ defmodule Kernel.ParallelCompiler do
9393
:elixir_code_server.cast({:reset_warnings, compiler_pid})
9494
schedulers = max(:erlang.system_info(:schedulers_online), 2)
9595

96-
result = spawn_workers(%{
97-
entries: files,
98-
original: files,
99-
output: output,
100-
options: options,
101-
waiting: [],
102-
queued: [],
103-
schedulers: schedulers,
104-
result: [],
105-
warnings: [],
106-
})
96+
result =
97+
spawn_workers(%{
98+
entries: files,
99+
original: files,
100+
output: output,
101+
options: options,
102+
waiting: [],
103+
queued: [],
104+
schedulers: schedulers,
105+
result: [],
106+
warnings: []
107+
})
107108

108109
# In case --warning-as-errors is enabled and there was a warning,
109110
# compilation status will be set to error.
110111
compilation_status = :elixir_code_server.call({:compilation_status, compiler_pid})
111112

112113
case {result, compilation_status} do
113114
{{:ok, _, warnings}, :error} ->
114-
IO.puts :stderr, "Compilation failed due to warnings while using the --warnings-as-errors option"
115+
message = "Compilation failed due to warnings while using the --warnings-as-errors option"
116+
IO.puts(:stderr, message)
117+
115118
{:error, warnings, []}
119+
116120
{{:error, errors, warnings}, :error} ->
117121
{:error, errors ++ warnings, []}
122+
118123
_ ->
119124
result
120125
end
121126
end
122127

123128
# We already have n=schedulers currently running, don't spawn new ones
124129
defp spawn_workers(%{queued: queued, waiting: waiting, schedulers: schedulers} = state)
125-
when length(queued) - length(waiting) >= schedulers do
130+
when length(queued) - length(waiting) >= schedulers do
126131
wait_for_messages(state)
127132
end
128133

@@ -131,11 +136,13 @@ defmodule Kernel.ParallelCompiler do
131136
waiting =
132137
case List.keytake(waiting, ref, 2) do
133138
{{_kind, pid, ^ref, _on, _defining}, waiting} ->
134-
send pid, {ref, found}
139+
send(pid, {ref, found})
135140
waiting
141+
136142
nil ->
137143
waiting
138144
end
145+
139146
spawn_workers(%{state | entries: t, waiting: waiting})
140147
end
141148

@@ -144,7 +151,7 @@ defmodule Kernel.ParallelCompiler do
144151
parent = self()
145152

146153
{pid, ref} =
147-
:erlang.spawn_monitor fn ->
154+
:erlang.spawn_monitor(fn ->
148155
:erlang.put(:elixir_compiler_pid, parent)
149156
:erlang.put(:elixir_compiler_file, file)
150157

@@ -155,23 +162,26 @@ defmodule Kernel.ParallelCompiler do
155162
{:compile, path} ->
156163
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
157164
:elixir_compiler.file_to_path(file, path)
165+
158166
:compile ->
159167
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
160168
:elixir_compiler.file(file, Keyword.get(options, :dest))
169+
161170
:require ->
162171
Code.require_file(file)
163172
end
173+
164174
:ok
165175
catch
166176
kind, reason ->
167-
{kind, reason, System.stacktrace}
177+
{kind, reason, System.stacktrace()}
168178
end
169179

170180
send(parent, {:file_done, self(), file, result})
171181
exit(:shutdown)
172-
end
182+
end)
173183

174-
timeout = Keyword.get(options, :long_compilation_threshold, 10) * 1_000
184+
timeout = Keyword.get(options, :long_compilation_threshold, 10) * 1000
175185
timer_ref = Process.send_after(self(), {:timed_out, pid}, timeout)
176186

177187
new_queued = [{pid, ref, file, timer_ref} | queued]
@@ -186,11 +196,13 @@ defmodule Kernel.ParallelCompiler do
186196
end
187197

188198
# Queued x, waiting for x: POSSIBLE ERROR! Release processes so we get the failures
189-
defp spawn_workers(%{entries: [], waiting: waiting, queued: queued, warnings: warnings} = state) when length(waiting) == length(queued) do
190-
entries = for {pid, _, _, _} <- queued,
191-
entry = waiting_on_without_definition(waiting, pid),
192-
{_, _, ref, on, _} = entry,
193-
do: {on, {ref, :not_found}}
199+
defp spawn_workers(%{entries: [], waiting: waiting, queued: queued, warnings: warnings} = state)
200+
when length(waiting) == length(queued) do
201+
entries =
202+
for {pid, _, _, _} <- queued,
203+
entry = waiting_on_without_definition(waiting, pid),
204+
{_, _, ref, on, _} = entry,
205+
do: {on, {ref, :not_found}}
194206

195207
# Instead of releasing all files at once, we release them in groups
196208
# based on the module they are waiting on. We pick the module being
@@ -205,12 +217,13 @@ defmodule Kernel.ParallelCompiler do
205217
|> Enum.group_by(&elem(&1, 0), &elem(&1, 1))
206218
|> Enum.sort_by(&length(elem(&1, 1)))
207219
|> case do
208-
[{_on, refs} | _] ->
209-
spawn_workers(%{state | entries: refs})
210-
[] ->
211-
errors = handle_deadlock(waiting, queued)
212-
{:error, errors, warnings}
213-
end
220+
[{_on, refs} | _] ->
221+
spawn_workers(%{state | entries: refs})
222+
223+
[] ->
224+
errors = handle_deadlock(waiting, queued)
225+
{:error, errors, warnings}
226+
end
214227
end
215228

216229
# No more files, but queue and waiting are not full or do not match
@@ -220,6 +233,7 @@ defmodule Kernel.ParallelCompiler do
220233

221234
defp waiting_on_without_definition(waiting, pid) do
222235
{_, ^pid, _, on, _} = entry = List.keyfind(waiting, pid, 1)
236+
223237
if Enum.any?(waiting, fn {_, _, _, _, defining} -> on in defining end) do
224238
nil
225239
else
@@ -241,30 +255,41 @@ defmodule Kernel.ParallelCompiler do
241255

242256
receive do
243257
{:struct_available, module} ->
244-
available = for {:struct, _, ref, waiting_module, _defining} <- waiting,
245-
module == waiting_module,
246-
do: {ref, :found}
258+
available =
259+
for {:struct, _, ref, waiting_module, _defining} <- waiting,
260+
module == waiting_module,
261+
do: {ref, :found}
247262

248-
spawn_workers(%{state | entries: available ++ entries, result: [{:struct, module} | result]})
263+
spawn_workers(%{
264+
state
265+
| entries: available ++ entries,
266+
result: [{:struct, module} | result]
267+
})
249268

250269
{:module_available, child, ref, file, module, binary} ->
251270
if callback = Keyword.get(options, :each_module) do
252271
callback.(file, module, binary)
253272
end
254273

255274
# Release the module loader which is waiting for an ack
256-
send child, {ref, :ack}
275+
send(child, {ref, :ack})
257276

258-
available = for {:module, _, ref, waiting_module, _defining} <- waiting,
259-
module == waiting_module,
260-
do: {ref, :found}
277+
available =
278+
for {:module, _, ref, waiting_module, _defining} <- waiting,
279+
module == waiting_module,
280+
do: {ref, :found}
261281

262282
cancel_waiting_timer(queued, child)
263283

264-
spawn_workers(%{state | entries: available ++ entries, result: [{:module, module} | result]})
284+
spawn_workers(%{
285+
state
286+
| entries: available ++ entries,
287+
result: [{:module, module} | result]
288+
})
265289

266290
# If we are simply requiring files, we do not add to waiting.
267-
{:waiting, _kind, child, ref, _on, _defining} when output == :require ->
291+
{:waiting, _kind, child, ref, _on, _defining}
292+
when output == :require ->
268293
send(child, {ref, :not_found})
269294
spawn_workers(state)
270295

@@ -274,7 +299,7 @@ defmodule Kernel.ParallelCompiler do
274299
# send :found so that we can crash with a better error.
275300
waiting =
276301
if :lists.any(&match?({^kind, ^on}, &1), result) or on in defining do
277-
send child, {ref, :found}
302+
send(child, {ref, :found})
278303
waiting
279304
else
280305
[{kind, child, ref, on, defining} | waiting]
@@ -284,12 +309,15 @@ defmodule Kernel.ParallelCompiler do
284309

285310
{:timed_out, child} ->
286311
callback = Keyword.get(options, :each_long_compilation)
312+
287313
case List.keyfind(queued, child, 0) do
288314
{^child, _, file, _} when not is_nil(callback) ->
289315
callback.(file)
316+
290317
_ ->
291318
:ok
292319
end
320+
293321
spawn_workers(state)
294322

295323
{:warning, file, line, message} ->
@@ -310,7 +338,7 @@ defmodule Kernel.ParallelCompiler do
310338
# Sometimes we may have spurious entries in the waiting
311339
# list because someone invoked try/rescue UndefinedFunctionError
312340
new_entries = List.delete(entries, child_pid)
313-
new_queued = List.keydelete(queued, child_pid, 0)
341+
new_queued = List.keydelete(queued, child_pid, 0)
314342
new_waiting = List.keydelete(waiting, child_pid, 1)
315343
spawn_workers(%{state | entries: new_entries, waiting: new_waiting, queued: new_queued})
316344

@@ -337,12 +365,14 @@ defmodule Kernel.ParallelCompiler do
337365
defp handle_down(_queued, _ref, :normal) do
338366
:ok
339367
end
368+
340369
defp handle_down(queued, ref, reason) do
341370
case List.keyfind(queued, ref, 1) do
342371
{_child, ^ref, file, _timer_ref} ->
343372
print_error(file, :exit, reason, [])
344373
terminate(queued)
345374
{:error, [to_error(file, :exit, reason, [])]}
375+
346376
_ ->
347377
:ok
348378
end
@@ -355,29 +385,29 @@ defmodule Kernel.ParallelCompiler do
355385
Process.exit(pid, :kill)
356386

357387
{_kind, ^pid, _, on, _} = List.keyfind(waiting, pid, 1)
358-
description = "deadlocked waiting on module #{inspect on}"
388+
description = "deadlocked waiting on module #{inspect(on)}"
359389
error = CompileError.exception(description: description, file: nil, line: nil)
360390
print_error(file, :error, error, stacktrace)
361391

362392
{file, on, description}
363393
end
364394

365-
IO.puts """
366-
395+
IO.puts("""
396+
367397
Compilation failed because of a deadlock between files.
368398
The following files depended on the following modules:
369-
"""
399+
""")
370400

371401
max =
372402
deadlock
373-
|> Enum.map(& &1 |> elem(0) |> String.length)
374-
|> Enum.max
403+
|> Enum.map(&(&1 |> elem(0) |> String.length()))
404+
|> Enum.max()
375405

376406
for {file, mod, _} <- deadlock do
377-
IO.puts [" ", String.pad_leading(file, max), " => " | inspect(mod)]
407+
IO.puts([" ", String.pad_leading(file, max), " => " | inspect(mod)])
378408
end
379409

380-
IO.puts ""
410+
IO.puts("")
381411

382412
for {file, _, description} <- deadlock, do: {Path.absname(file), nil, description}
383413
end
@@ -389,8 +419,10 @@ defmodule Kernel.ParallelCompiler do
389419
end
390420

391421
defp print_error(file, kind, reason, stack) do
392-
IO.write ["\n== Compilation error in file #{Path.relative_to_cwd(file)} ==\n",
393-
Kernel.CLI.format_error(kind, reason, stack)]
422+
IO.write([
423+
"\n== Compilation error in file #{Path.relative_to_cwd(file)} ==\n",
424+
Kernel.CLI.format_error(kind, reason, stack)
425+
])
394426
end
395427

396428
defp cancel_waiting_timer(queued, child_pid) do
@@ -404,6 +436,7 @@ defmodule Kernel.ParallelCompiler do
404436
after
405437
0 -> :ok
406438
end
439+
407440
nil ->
408441
:ok
409442
end

0 commit comments

Comments
 (0)