Skip to content

Commit

Permalink
Dog food __STACKTRACE__ (elixir-lang#7308)
Browse files Browse the repository at this point in the history
  • Loading branch information
michalmuskala authored and josevalim committed Feb 3, 2018
1 parent 097de7d commit a044559
Show file tree
Hide file tree
Showing 21 changed files with 41 additions and 70 deletions.
2 changes: 1 addition & 1 deletion lib/elixir/lib/dynamic_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ defmodule DynamicSupervisor do
apply(m, f, a)
catch
kind, reason ->
{:error, exit_reason(kind, reason, System.stacktrace())}
{:error, exit_reason(kind, reason, __STACKTRACE__)}
else
{:ok, pid, extra} when is_pid(pid) -> {:ok, pid, extra}
{:ok, pid} when is_pid(pid) -> {:ok, pid}
Expand Down
5 changes: 2 additions & 3 deletions lib/elixir/lib/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1234,9 +1234,8 @@ defmodule Enum do
reduce(enumerable, initial, callback)
catch
kind, reason ->
stacktrace = System.stacktrace()
fun.(initial, :halt)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
acc -> fun.(acc, :done)
end
Expand Down Expand Up @@ -2819,7 +2818,7 @@ defmodule Enum do
Enumerable.slice(args)
catch
:error, :undef ->
case System.stacktrace() do
case __STACKTRACE__ do
[{module, :slice, [^args], _} | _] -> {:error, module}
stack -> :erlang.raise(:error, :undef, stack)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/lib/gen_event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ defmodule GenEvent do
apply(mod, fun, args)
catch
:throw, val -> {:ok, val}
:error, val -> {:error, {val, System.stacktrace()}}
:error, val -> {:error, {val, __STACKTRACE__}}
:exit, val -> {:error, val}
else
res -> {:ok, res}
Expand Down
4 changes: 1 addition & 3 deletions lib/elixir/lib/inspect/algebra.ex
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,6 @@ defmodule Inspect.Algebra do
Inspect.inspect(struct, opts)
rescue
caught_exception ->
stacktrace = System.stacktrace()

# Because we try to raise a nice error message in case
# we can't inspect a struct, there is a chance the error
# message itself relies on the struct being printed, so
Expand All @@ -305,7 +303,7 @@ defmodule Inspect.Algebra do
if opts.safe do
Inspect.inspect(exception, opts)
else
reraise(exception, stacktrace)
reraise(exception, __STACKTRACE__)
end
after
Process.delete(:inspect_trap)
Expand Down
5 changes: 2 additions & 3 deletions lib/elixir/lib/kernel/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ defmodule Kernel.CLI do
exit(reason)

kind, reason ->
stack = System.stacktrace()
print_error(kind, reason, stack)
print_error(kind, reason, __STACKTRACE__)
send(parent, {self(), {:shutdown, 1}})
exit(to_exit(kind, reason, stack))
exit(to_exit(kind, reason, __STACKTRACE__))
else
_ ->
send(parent, {self(), res})
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/lib/kernel/parallel_compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ defmodule Kernel.ParallelCompiler do
:ok
catch
kind, reason ->
{kind, reason, System.stacktrace()}
{kind, reason, __STACKTRACE__}
end

send(parent, {:file_done, self(), file, result})
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/lib/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ defmodule Registry do
apply(module, function, [pid])
catch
kind, reason ->
formatted = Exception.format(kind, reason, System.stacktrace)
formatted = Exception.format(kind, reason, __STACKTRACE__)
Logger.error "Registry.dispatch/3 failed with #{formatted}"
end
end
Expand Down
27 changes: 9 additions & 18 deletions lib/elixir/lib/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,8 @@ defmodule Stream do
reduce.({command, [acc | collectable]})
catch
kind, reason ->
stacktrace = System.stacktrace()
into.(collectable, :halt)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:suspended, [acc | collectable], continuation} ->
{:suspended, acc, &do_into(continuation, collectable, into, &1)}
Expand Down Expand Up @@ -850,9 +849,8 @@ defmodule Stream do
next.({:cont, []})
catch
kind, reason ->
stacktrace = System.stacktrace()
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:suspended, vals, next} ->
do_transform_user(:lists.reverse(vals), user_acc, :cont, next, inner_acc, funs)
Expand All @@ -873,10 +871,9 @@ defmodule Stream do
user.(val, user_acc)
catch
kind, reason ->
stacktrace = System.stacktrace()
next.({:halt, []})
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{[], user_acc} ->
do_transform_user(vals, user_acc, next_op, next, inner_acc, funs)
Expand All @@ -903,10 +900,9 @@ defmodule Stream do
reduce.(inner_acc)
catch
kind, reason ->
stacktrace = System.stacktrace()
next.({:halt, []})
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:done, acc} ->
do_transform_user(vals, user_acc, next_op, next, {:cont, acc}, funs)
Expand All @@ -929,10 +925,9 @@ defmodule Stream do
reduce.({op, [:outer | inner_acc]})
catch
kind, reason ->
stacktrace = System.stacktrace()
next.({:halt, []})
do_after(after_fun, user_acc)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
# Only take into account outer halts when the op is not halt itself.
# Otherwise, we were the ones wishing to halt, so we should just stop.
Expand Down Expand Up @@ -1149,9 +1144,8 @@ defmodule Stream do
do_zip_next_tuple(zips, acc, callback, [], [])
catch
kind, reason ->
stacktrace = System.stacktrace()
do_zip_close(zips)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:next, buffer, acc} ->
do_zip(buffer, acc, callback)
Expand Down Expand Up @@ -1372,9 +1366,8 @@ defmodule Stream do
end
catch
kind, reason ->
stacktrace = System.stacktrace()
after_fun.(next_acc)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:opt, acc, next_acc} ->
do_resource(next_acc, next_fun, acc, fun, after_fun)
Expand All @@ -1398,9 +1391,8 @@ defmodule Stream do
reduce.(acc)
catch
kind, reason ->
stacktrace = System.stacktrace()
after_fun.(next_acc)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:done, acc} ->
do_resource(next_acc, next_fun, {:cont, acc}, fun, after_fun)
Expand All @@ -1418,9 +1410,8 @@ defmodule Stream do
reduce.({op, [:outer | acc]})
catch
kind, reason ->
stacktrace = System.stacktrace()
after_fun.(next_acc)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:halted, [:outer | acc]} ->
do_resource(next_acc, next_fun, {:cont, acc}, fun, after_fun)
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/lib/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ defmodule Supervisor do
module.child_spec(arg)
rescue
e in UndefinedFunctionError ->
case System.stacktrace() do
case __STACKTRACE__ do
[{^module, :child_spec, [^arg], _} | _] ->
raise ArgumentError, child_spec_error(module)

Expand Down
3 changes: 1 addition & 2 deletions lib/elixir/lib/system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,8 @@ defmodule System do
do_cmd(Port.open({:spawn_executable, cmd}, opts), initial, fun)
catch
kind, reason ->
stacktrace = System.stacktrace()
fun.(initial, :halt)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{acc, status} -> {fun.(acc, :done), status}
end
Expand Down
15 changes: 6 additions & 9 deletions lib/elixir/lib/task/supervised.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ defmodule Task.Supervised do
apply(module, fun, args)
catch
:error, value ->
reason = {value, System.stacktrace()}
reason = {value, __STACKTRACE__}
exit(info, mfa, reason, reason)

:throw, value ->
reason = {{:nocatch, value}, System.stacktrace()}
reason = {{:nocatch, value}, __STACKTRACE__}
exit(info, mfa, reason, reason)

:exit, value ->
exit(info, mfa, {value, System.stacktrace()}, value)
exit(info, mfa, {value, __STACKTRACE__}, value)
end
end

Expand Down Expand Up @@ -295,9 +295,8 @@ defmodule Task.Supervised do
next.({:cont, []})
catch
kind, reason ->
stacktrace = System.stacktrace()
stream_close(monitor_pid, monitor_ref, timeout)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
{:suspended, [value], next} ->
waiting = stream_spawn(value, spawned, waiting, monitor_pid, monitor_ref, timeout)
Expand All @@ -324,10 +323,9 @@ defmodule Task.Supervised do
reducer.(reply, acc)
catch
kind, reason ->
stacktrace = System.stacktrace()
is_function(next) && next.({:halt, []})
stream_close(monitor_pid, monitor_ref, timeout)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
end
end

Expand All @@ -354,10 +352,9 @@ defmodule Task.Supervised do
reducer.(reply, acc)
catch
kind, reason ->
stacktrace = System.stacktrace()
is_function(next) && next.({:halt, []})
stream_close(monitor_pid, monitor_ref, timeout)
:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
else
pair ->
stream_deliver(
Expand Down
11 changes: 4 additions & 7 deletions lib/ex_unit/lib/ex_unit/assertions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,6 @@ defmodule ExUnit.Assertions do
function.()
rescue
error ->
stacktrace = System.stacktrace()
name = error.__struct__

cond do
Expand All @@ -673,14 +672,14 @@ defmodule ExUnit.Assertions do
error

name == ExUnit.AssertionError ->
reraise(error, stacktrace)
reraise(error, __STACKTRACE__)

true ->
message =
"Expected exception #{inspect(exception)} " <>
"but got #{inspect(name)} (#{Exception.message(error)})"

reraise ExUnit.AssertionError, [message: message], stacktrace
reraise ExUnit.AssertionError, [message: message], __STACKTRACE__
end
else
_ -> flunk("Expected exception #{inspect(exception)} but nothing was raised")
Expand All @@ -691,11 +690,9 @@ defmodule ExUnit.Assertions do
module.message(error)
catch
kind, reason ->
stacktrace = System.stacktrace()

message =
"Got exception #{inspect(module)} but it failed to produce a message with:\n\n" <>
Exception.format(kind, reason, stacktrace)
Exception.format(kind, reason, __STACKTRACE__)

flunk(message)
end
Expand Down Expand Up @@ -776,7 +773,7 @@ defmodule ExUnit.Assertions do
flunk("Expected to catch #{unquote(kind)}, got nothing")
rescue
e in [ExUnit.AssertionError] ->
reraise(e, System.stacktrace())
reraise(e, __STACKTRACE__)
catch
unquote(kind), we_got -> we_got
end
Expand Down
3 changes: 1 addition & 2 deletions lib/ex_unit/lib/ex_unit/capture_io.ex
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ defmodule ExUnit.CaptureIO do
fun.()
catch
kind, reason ->
stack = System.stacktrace()
_ = StringIO.close(string_io)
:erlang.raise(kind, reason, stack)
:erlang.raise(kind, reason, __STACKTRACE__)
else
_ ->
{:ok, output} = StringIO.close(string_io)
Expand Down
5 changes: 2 additions & 3 deletions lib/iex/lib/iex/evaluator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,8 @@ defmodule IEx.Evaluator do
%{state | binding: binding, env: :elixir.env_for_eval(env, file: "iex", line: 1)}
catch
kind, error ->
stacktrace = System.stacktrace()
io_result("Error while evaluating: #{path}")
print_error(kind, error, stacktrace)
print_error(kind, error, __STACKTRACE__)
System.halt(1)
end
end
Expand All @@ -208,7 +207,7 @@ defmodule IEx.Evaluator do
do_eval(String.to_charlist(code), iex_state, state)
catch
kind, error ->
print_error(kind, error, System.stacktrace())
print_error(kind, error, __STACKTRACE__)
{%{iex_state | cache: ''}, state}
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/mix/lib/mix/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,12 @@ defmodule Mix.CLI do
# We only rescue exceptions in the Mix namespace, all
# others pass through and will explode on the users face
exception ->
stacktrace = System.stacktrace()

if Map.get(exception, :mix) && not Mix.debug?() do
mod = exception.__struct__ |> Module.split() |> Enum.at(0, "Mix")
Mix.shell().error("** (#{mod}) #{Exception.message(exception)}")
exit({:shutdown, 1})
else
reraise exception, stacktrace
reraise exception, __STACKTRACE__
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/mix/lib/mix/compilers/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ defmodule Mix.Compilers.Test do
{:ok, results}
catch
kind, reason ->
stack = System.stacktrace()
# In case there is an error, shutdown the runner task
# before the error propagates up and trigger links.
Task.shutdown(task)
:erlang.raise(kind, reason, stack)
:erlang.raise(kind, reason, __STACKTRACE__)
after
agent_stop(stale_manifest_pid)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mix/lib/mix/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ defmodule Mix.Config do
validate!(config)
config
rescue
e in [LoadError] -> reraise(e, System.stacktrace())
e -> reraise(LoadError, [file: file, error: e], System.stacktrace())
e in [LoadError] -> reraise(e, __STACKTRACE__)
e -> reraise(LoadError, [file: file, error: e], __STACKTRACE__)
end
end

Expand Down
4 changes: 1 addition & 3 deletions lib/mix/lib/mix/hex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@ defmodule Mix.Hex do
Hex.start()
catch
kind, reason ->
stacktrace = System.stacktrace()

Mix.shell().error(
"Could not start Hex. Try fetching a new version with " <>
"\"mix local.hex\" or uninstalling it with \"mix archive.uninstall hex.ez\""
)

:erlang.raise(kind, reason, stacktrace)
:erlang.raise(kind, reason, __STACKTRACE__)
end
end

Expand Down
Loading

0 comments on commit a044559

Please sign in to comment.