Skip to content

Commit f053df8

Browse files
author
José Valim
committed
Move raise checks to runtime callback, closes #5257
Signed-off-by: José Valim <[email protected]>
1 parent 0caa757 commit f053df8

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,31 +1350,9 @@ defmodule Kernel do
13501350
:erlang.error unquote(alias).exception([])
13511351
end
13521352
_ ->
1353-
generated = fn fun, var ->
1354-
{fun, [generated: true, line: -1], [{var, [], __MODULE__}]}
1353+
quote do
1354+
:erlang.error Kernel.Utils.raise(unquote(msg))
13551355
end
1356-
1357-
{fun, meta, [arg, [do: clauses]]} =
1358-
quote do
1359-
case unquote(msg) do
1360-
msg when unquote(generated.(:is_binary, :msg)) ->
1361-
:erlang.error RuntimeError.exception(msg)
1362-
atom when unquote(generated.(:is_atom, :atom)) ->
1363-
:erlang.error atom.exception([])
1364-
%{__struct__: struct, __exception__: true} = other when is_atom(struct) ->
1365-
:erlang.error other
1366-
other ->
1367-
message = "raise/1 expects an alias, string or exception as the first argument, got: #{inspect other}"
1368-
:erlang.error ArgumentError.exception(message)
1369-
end
1370-
end
1371-
1372-
clauses =
1373-
:lists.map(fn {:->, meta, args} ->
1374-
{:->, [generated: true] ++ Keyword.put(meta, :line, -1), args}
1375-
end, clauses)
1376-
1377-
{fun, meta, [arg, [do: clauses]]}
13781356
end
13791357
end
13801358

lib/elixir/lib/kernel/utils.ex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import Kernel, except: [destructure: 2, defdelegate: 2, defstruct: 2]
33
defmodule Kernel.Utils do
44
@moduledoc false
55

6+
@doc """
7+
Callback for destructure.
8+
"""
69
def destructure(list, count) when is_list(list), do: destructure_list(list, count)
710
def destructure(nil, count), do: destructure_nil(count)
811

@@ -13,6 +16,9 @@ defmodule Kernel.Utils do
1316
defp destructure_nil(0), do: []
1417
defp destructure_nil(count), do: [nil | destructure_nil(count - 1)]
1518

19+
@doc """
20+
Callback for defdelegate.
21+
"""
1622
def defdelegate(fun, opts) do
1723
append_first = Keyword.get(opts, :append_first, false)
1824

@@ -56,6 +62,9 @@ defmodule Kernel.Utils do
5662
"defdelegate/2 only accepts function parameters, got: #{Macro.to_string(code)}"
5763
end
5864

65+
@doc """
66+
Callback for defstruct.
67+
"""
5968
def defstruct(module, fields) do
6069
case fields do
6170
fs when is_list(fs) ->
@@ -85,10 +94,30 @@ defmodule Kernel.Utils do
8594
Module.get_attribute(module, :derive)}
8695
end
8796

97+
@doc """
98+
Announcing callback for defstruct.
99+
"""
88100
def announce_struct(module) do
89101
case :erlang.get(:elixir_compiler_pid) do
90102
:undefined -> :ok
91103
pid -> send(pid, {:struct_available, module})
92104
end
93105
end
106+
107+
@doc """
108+
Callback for raise.
109+
"""
110+
def raise(msg) when is_binary(msg) do
111+
RuntimeError.exception(msg)
112+
end
113+
def raise(atom) when is_atom(atom) do
114+
atom.exception([])
115+
end
116+
def raise(%{__struct__: struct, __exception__: true} = exception) when is_atom(struct) do
117+
exception
118+
end
119+
def raise(other) do
120+
ArgumentError.exception("raise/1 expects an alias, string or exception as " <>
121+
"the first argument, got: #{inspect other}")
122+
end
94123
end

0 commit comments

Comments
 (0)