Skip to content

Commit 1bee85e

Browse files
author
José Valim
committed
Merge pull request #2734 from gjaldon/clearer-unquote-splicing-error
Friendlier unquote_splicing error with hints
2 parents 15fdae4 + 66b2119 commit 1bee85e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/elixir/src/elixir_quote.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ escape(Expr, Unquote) ->
151151

152152
%% Quotes an expression and return its quoted Elixir AST.
153153

154+
quote({unquote_splicing, _, [_]}, Binding, #elixir_quote{unquote=true}, E) ->
155+
argument_error(<<"unquote_splicing only works inside arguments and block contexts, "
156+
"wrap it in parens if you want it to work with one-liners">>);
157+
154158
quote(Expr, nil, Q, E) ->
155159
do_quote(Expr, Q, E);
156160

lib/elixir/test/elixir/kernel/errors_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,23 @@ defmodule Kernel.ErrorsTest do
290290
'unquote 1'
291291
end
292292

293+
test :invalid_unquote_splicing_in_oneliners do
294+
assert_compile_fail ArgumentError,
295+
"unquote_splicing only works inside arguments and block contexts, " <>
296+
"wrap it in parens if you want it to work with one-liners",
297+
'''
298+
defmodule Foo do
299+
defmacro oneliner2 do
300+
quote do: unquote_splicing 1
301+
end
302+
303+
def callme do
304+
oneliner2
305+
end
306+
end
307+
'''
308+
end
309+
293310
test :invalid_quote_args do
294311
assert_compile_fail CompileError,
295312
"nofile:1: invalid arguments for quote",

0 commit comments

Comments
 (0)