Skip to content

Commit 2e9981d

Browse files
committed
Raise when splicing cons without head, closes #15282
1 parent ed478e8 commit 2e9981d

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

lib/elixir/src/elixir_quote.erl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,10 @@ tail_list(Left, Right, Tail) when is_list(Right), is_list(Tail) ->
583583

584584
tail_list(Left, Right, Tail) when is_list(Left) ->
585585
validate_list(Left),
586-
[H | T] = lists:reverse(Tail ++ Left),
587-
lists:reverse([{'|', [], [H, Right]} | T]).
586+
case lists:reverse(Tail ++ Left) of
587+
[H | T] -> lists:reverse([{'|', [], [H, Right]} | T]);
588+
[] -> argument_error(<<"unquote_splicing/1 failed because it attempted to splice an empty list before tail position">>)
589+
end.
588590

589591
validate_list(List) ->
590592
case valid_ast_list(List) of

lib/elixir/test/elixir/kernel/quote_test.exs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,17 @@ defmodule Kernel.QuoteTest do
200200
contents = [1, 2, 3]
201201

202202
assert quote(do: [unquote_splicing(contents) | [1, 2, 3]]) == [1, 2, 3, 1, 2, 3]
203-
204203
assert quote(do: [unquote_splicing(contents) | val]) == quote(do: [1, 2, 3 | val])
205-
206204
assert quote(do: [unquote_splicing(contents) | unquote([4])]) == quote(do: [1, 2, 3, 4])
205+
206+
# Empty splicing
207+
assert quote(do: [unquote_splicing([]) | [1, 2, 3]]) == [1, 2, 3]
208+
assert quote(do: [0, unquote_splicing([]) | [1, 2, 3]]) == [0, 1, 2, 3]
209+
assert quote(do: [:head, unquote_splicing([]) | :tail]) == quote(do: [:head | :tail])
210+
211+
assert_raise ArgumentError,
212+
~r"unquote_splicing/1 failed because it attempted to splice an empty list",
213+
fn -> quote(do: [unquote_splicing([]) | :tail]) end
207214
end
208215

209216
test "splice on stab" do

0 commit comments

Comments
 (0)