Skip to content

Commit

Permalink
Add compiler errors for non-supported syntax keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodirlam committed Dec 21, 2024
1 parent 19ca71a commit 7cb1214
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/dx/defd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ defmodule Dx.Defd do
define_defd(:def, call, block, __CALLER__)
end

defmacro defd(_call, do: _block, rescue: _rescue_block) do
compile_error!(
__CALLER__,
"`rescue` blocks are not supported in defd functions. Please use them only in non-defd functions."
)
end

@doc false
defmacro defdp(call) do
define_defd(:defp, call, __CALLER__)
Expand All @@ -238,6 +245,13 @@ defmodule Dx.Defd do
define_defd(:defp, call, block, __CALLER__)
end

defmacro defdp(_call, do: _block, rescue: _rescue_block) do
compile_error!(
__CALLER__,
"`rescue` blocks are not supported in defd functions. Please use them only in non-defd functions."
)
end

@doc """
Used to wrap calls to non-Dx defined functions within a `defd` function.
Expand Down
24 changes: 24 additions & 0 deletions lib/dx/defd/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,30 @@ defmodule Dx.Defd.Compiler do
Dx.Defd.Cond.normalize(ast, state)
end

def normalize({:for, meta, _args}, state) do
compile_error!(meta, state, """
`for` comprehensions are not yet supported in defd functions. Please use `Enum` functions.
""")
end

def normalize({:receive, meta, _args}, state) do
compile_error!(meta, state, """
`receive` blocks are not supported in defd functions. Please use them only in non-defd functions.
""")
end

def normalize({:try, meta, _args}, state) do
compile_error!(meta, state, """
`try` blocks are not supported in defd functions. Please use them only in non-defd functions.
""")
end

def normalize({:with, meta, _args}, state) do
compile_error!(meta, state, """
`with` clauses are not yet supported in defd functions. Please use `case` clauses.
""")
end

def normalize({:__block__, _meta, _lines} = ast, state) do
Dx.Defd.Block.normalize(ast, state)
end
Expand Down
82 changes: 82 additions & 0 deletions test/dx/defd_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -622,4 +622,86 @@ defmodule Dx.DefdTest do
)
end
end

test "for comprehension raises error" do
assert_raise CompileError, ~r/#{location(+5)}/, fn ->
defmodule ForTest do
import Dx.Defd

defd run() do
for i <- [1, 2, 3], do: i
end
end
end
end

test "receive raises error" do
assert_raise CompileError, ~r/#{location(+5)}/, fn ->
defmodule ReceiveTest do
import Dx.Defd

defd run() do
receive do
msg -> msg
end
end
end
end
end

test "rescue in defd raises error" do
assert_raise CompileError, ~r/#{location(+4)}/, fn ->
defmodule DefdRescueTest do
import Dx.Defd

defd run(not_fun) do
not_fun.()
rescue
_e -> :boom
end
end
end
end

test "rescue in defdp raises error" do
assert_raise CompileError, ~r/#{location(+4)}/, fn ->
defmodule DefdpRescueTest do
import Dx.Defd

defdp run(not_fun) do
not_fun.()
rescue
_e -> :boom
end
end
end
end

test "try raises error" do
assert_raise CompileError, ~r/#{location(+5)}/, fn ->
defmodule TryTest do
import Dx.Defd

defd run(not_fun) do
try do
not_fun.()
after
:boom
end
end
end
end
end

test "with clauses raise error" do
assert_raise CompileError, ~r/#{location(+5)}/, fn ->
defmodule WithTest do
import Dx.Defd

defd run(result) do
with {:ok, result} <- result, do: result
end
end
end
end
end

0 comments on commit 7cb1214

Please sign in to comment.