Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/elixir/src/elixir_erl_compiler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ custom_format(sys_core_fold, nomatch_guard) ->
"this check/guard will always yield the same result";

%% Handle literal eval failures
custom_format(sys_core_fold, {eval_failure, {Mod, Name, Arity}, Error}) ->
#{'__struct__' := Struct} = 'Elixir.Exception':normalize(error, Error),
{ExMod, ExName, ExArgs} = elixir_rewrite:erl_to_ex(Mod, Name, lists:duplicate(Arity, nil)),
Call = 'Elixir.Exception':format_mfa(ExMod, ExName, length(ExArgs)),
Trimmed = case Call of
<<"Kernel.", Rest/binary>> -> Rest;
_ -> Call
end,
Comment on lines +141 to +145
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check ExMod and call Exception.format_mfa/3 if not Kernel and then Exception.format_fa/2 if Kernel.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, ignore me. This is good.

["the call to ", Trimmed, " will fail with ", elixir_aliases:inspect(Struct)];

%% TODO: remove when we require OTP 24
custom_format(sys_core_fold, {eval_failure, Error}) ->
#{'__struct__' := Struct} = 'Elixir.Exception':normalize(error, Error),
["this expression will fail with ", elixir_aliases:inspect(Struct)];
Expand Down
13 changes: 11 additions & 2 deletions lib/elixir/test/elixir/kernel/warning_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -974,22 +974,31 @@ defmodule Kernel.WarningTest do
purge(Sample)
end

# TODO: Simplify when we require OTP 24
if System.otp_release() >= "24" do
@argument_error_message "the call to :erlang.atom_to_binary/2"
@arithmetic_error_message "the call to +/2"
else
@argument_error_message "this expression"
@arithmetic_error_message "this expression"
end

test "eval failure warning" do
assert capture_err(fn ->
Code.eval_string("""
defmodule Sample1 do
def foo, do: Atom.to_string "abc"
end
""")
end) =~ ~r"this expression will fail with ArgumentError\n.*nofile:2"
end) =~ "#{@argument_error_message} will fail with ArgumentError\n nofile:2"

assert capture_err(fn ->
Code.eval_string("""
defmodule Sample2 do
def foo, do: 1 + nil
end
""")
end) =~ ~r"this expression will fail with ArithmeticError\n.*nofile:2"
end) =~ "#{@arithmetic_error_message} will fail with ArithmeticError\n nofile:2"
after
purge([Sample1, Sample2])
end
Expand Down