Skip to content

Commit edafcc4

Browse files
committed
Handle improper lists on apply, closes #11465
1 parent 233fc09 commit edafcc4

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

lib/elixir/lib/exception.ex

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,10 @@ defmodule ArgumentError do
732732
) do
733733
message =
734734
cond do
735+
not proper_list?(args) ->
736+
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)} " <>
737+
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a proper list"
738+
735739
# Note that args may be an empty list even if they were supplied
736740
not is_atom(module) and is_atom(function) and args == [] ->
737741
"you attempted to apply a function named #{inspect(function)} on #{inspect(module)}. " <>
@@ -747,10 +751,6 @@ defmodule ArgumentError do
747751
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)}. " <>
748752
"However #{inspect(function)} is not a valid function name. Function names (the second argument " <>
749753
"of apply) must always be an atom"
750-
751-
not is_list(args) ->
752-
"you attempted to apply a function named #{inspect(function)} on module #{inspect(module)} " <>
753-
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a list"
754754
end
755755

756756
{%{exception | message: message}, stacktrace}
@@ -759,6 +759,9 @@ defmodule ArgumentError do
759759
def blame(exception, stacktrace) do
760760
{exception, stacktrace}
761761
end
762+
763+
defp proper_list?(list) when length(list) >= 0, do: true
764+
defp proper_list?(_), do: false
762765
end
763766

764767
defmodule ArithmeticError do

lib/elixir/test/elixir/exception_test.exs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,11 @@ defmodule ExceptionTest do
423423

424424
assert blame_message(123, &apply(Kernel, :+, &1)) ==
425425
"you attempted to apply a function named :+ on module Kernel with arguments 123. " <>
426-
"Arguments (the third argument of apply) must always be a list"
426+
"Arguments (the third argument of apply) must always be a proper list"
427+
428+
assert blame_message(123, &apply(Kernel, :+, [&1 | 456])) ==
429+
"you attempted to apply a function named :+ on module Kernel with arguments [123 | 456]. " <>
430+
"Arguments (the third argument of apply) must always be a proper list"
427431
end
428432

429433
test "annotates function clause errors" do

0 commit comments

Comments
 (0)