Skip to content

Commit c0fb275

Browse files
author
José Valim
committed
Properly handle eval_failure warnings, closes #2835
Signed-off-by: José Valim <[email protected]>
1 parent 24f46be commit c0fb275

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/elixir/src/elixir_errors.erl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ handle_file_warning(_, File, {Line,erl_lint,{unused_var,Var}}) ->
140140
Message = format_error(erl_lint, {unused_var, format_var(Var)}),
141141
warn(Line, File, Message);
142142

143+
%% Handle literal eval failures
144+
handle_file_warning(_, File, {Line,sys_core_fold,{eval_failure, Error}}) ->
145+
#{'__struct__' := Struct} = 'Elixir.Exception':normalize(error, Error),
146+
warn(Line, File, ["this expression will fail with ", elixir_aliases:inspect(Struct)]);
147+
143148
%% Default behaviour
144149
handle_file_warning(_, File, {Line,Module,Desc}) ->
145150
Message = format_error(Module, Desc),

lib/elixir/test/elixir/kernel/warning_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,20 @@ defmodule Kernel.WarningTest do
375375
purge Sample
376376
end
377377

378+
test :badarg_warning do
379+
assert capture_err(fn ->
380+
assert_raise ArgumentError, fn ->
381+
Code.eval_string """
382+
defmodule Sample do
383+
Atom.to_string "abc"
384+
end
385+
"""
386+
end
387+
end) =~ "warning: this expression will fail with ArgumentError"
388+
after
389+
purge [Sample]
390+
end
391+
378392
test :undefined_function_for_behaviour do
379393
assert capture_err(fn ->
380394
Code.eval_string """

0 commit comments

Comments
 (0)