-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Macro in rescue fails to compile #12209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Plus there is no documentation for this behaviour. So, I'd expect a fix or at least some notice in documentation that macros are not allowed in |
It seems because macro won't be expanded inside the defmodule A do
defmacro m do
quote do
:expanded
end
end
end import A
quote do
m()
end
|> Macro.expand(__ENV__)
# :expanded import A
quote do
m() -> 1
end
|> Macro.expand(__ENV__)
# [{:->, [], [[{:m, [context: Elixir, imports: [{0, A}]], []}], 1]}] import A
quote do
a -> m()
end
|> Macro.expand(__ENV__)
# [{:->, [], [[{:a, [], Elixir}], {:m, [context: Elixir, imports: [{0, A}]], []}]}] |
@hissssst macros are allowed either on the left side or the right side of |
Yes, I can see that. Perhaps we can add new Otherwise, we can come across some strange bugs, like defmodule SampleTest do
use ExUnit.Case
doctest Sample
test "greets the world" do
q =
quote do
try do
x
rescue
e in ArgumentError -> e
end
end
assert (quote do
try do
x
rescue
e in ArgumentError -> e
end
end = q)
end
end Results in $ mix test
== Compilation error in file test/sample_test.exs ==
** (ArgumentError) invalid right argument for operator "in", it expects a compile-time proper list or compile-time range on the right side when used in guard expressions, got: ArgumentError
(elixir 1.13.4) lib/kernel.ex:4245: Kernel.raise_on_invalid_args_in_2/1
(elixir 1.13.4) expanding macro: Kernel.in/2
test/sample_test.exs:19: SampleTest."test greets the world"/1
(ex_unit 1.13.4) expanding macro: ExUnit.Assertions.assert/1
test/sample_test.exs:15: SampleTest."test greets the world"/1 |
It is not really behaving like a special form. Any macro has control over what it may expand on or rewrite or keep as is, which is what
That's a separate bug ( |
Yeah, but this kind of limitation is strange and it is not difficult to fix. I don't know what do you mean by special form, I just think that And I'd suggest adding Or I'd suggest to modify |
The thing about macros is that defmodule MyMacro do
defmacro new_add({:in, _, [x, y]}) do
quote do
unquote(x) + unquote(y)
end
end
end And then: import MyMacro
new_add 1 in 3
#=> 4 My point is that you are trying to say this behaviour is caused or a responsibility of
Yes, this is most likely the appropriate fix! |
You're right, I was not completely correct. After expansion of all user-defined macros, |
You're almost right, but |
The fact |
Yes, that's true |
❤️ |
Uh oh!
There was an error while loading. Please reload this page.
Elixir and Erlang/OTP versions
Operating system
$ uname -a Linux 5.15.72 #1-NixOS SMP Wed Oct 5 08:39:44 UTC 2022 x86_64 GNU/Linux
Current behavior
File
bug.ex
isExpected behavior
Macro is expanded properly.
Originally, I came by this error when I've found out that there is no suitable
Macro.Env.context
for expanding parts of AST which are meant to be used asrescue
clausesThe text was updated successfully, but these errors were encountered: