Skip to content

Crash when defining a function without args via unquote fragment #14422

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

Closed
lukaszsamson opened this issue Apr 10, 2025 · 1 comment
Closed

Crash when defining a function without args via unquote fragment #14422

lukaszsamson opened this issue Apr 10, 2025 · 1 comment

Comments

@lukaszsamson
Copy link
Contributor

Elixir and Erlang/OTP versions

Erlang/OTP 27 [erts-15.2.5] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]

Elixir 1.18.3 (compiled with Erlang/OTP 27)

Operating system

any

Current behavior

defmodule A do; def unquote(:"foo-bar"), do: :ok; end

error: invalid syntax in def :"foo-bar"
└─ iex:20: A (module)

** (CompileError) iex: cannot compile module A (errors have been logged)
    (elixir 1.18.3) src/elixir_def.erl:149: :elixir_def.store_definition/5
    iex:20: (module)
    (elixir 1.18.3) src/elixir_compiler.erl:77: :elixir_compiler.dispatch/4
    (elixir 1.18.3) src/elixir_compiler.erl:52: :elixir_compiler.compile/4
    (elixir 1.18.3) src/elixir_module.erl:454: :elixir_module.eval_form/7
    (elixir 1.18.3) src/elixir_module.erl:156: :elixir_module.compile/7
    iex:20: (file)

The version with params works as expected

defmodule A do; def unquote(:"foo-bar")(), do: :ok; end

The docs in https://hexdocs.pm/elixir/Kernel.SpecialForms.html#quote/2-binding-and-unquote-fragments does not state that it is forbidden
The docs in https://hexdocs.pm/elixir/1.18.3/Kernel.html#def/2 encourage to use paramsless form in the first example

Expected behavior

No crash or docs updated to mention that params are required on defs with unquote fragments

@josevalim
Copy link
Member

I can update the docs to provide be a bit clearer though but the behaviour is expected as you are literally defining:

def :foo do

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants