From 062efc0da37edde30289796a019aad4474687409 Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Wed, 18 Nov 2026 10:59:41 +0100 Subject: [PATCH 1/2] Preserve cons pattern tails in Kernel.destructure/2 Assisted-by: Codex:gpt-6.2-sol --- lib/elixir/lib/kernel.ex | 20 +++++++++++++++++- lib/elixir/lib/kernel/utils.ex | 19 +++++++++-------- lib/elixir/test/elixir/kernel_test.exs | 28 ++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex index 44bad71893..5d86ddd8cc 100644 --- a/lib/elixir/lib/kernel.ex +++ b/lib/elixir/lib/kernel.ex @@ -4153,10 +4153,28 @@ defmodule Kernel do The example above will only work if `x` matches the first value in the right list. Otherwise, it will raise a `MatchError` (like the `=` operator would do). + + A cons pattern captures the remaining elements in its tail. Only the + elements before the tail are padded with `nil` if the right list is smaller: + + iex> destructure([x, y | tail], [1, 2, 3, 4]) + iex> {x, y, tail} + {1, 2, [3, 4]} + + iex> destructure([x, y | tail], [1]) + iex> {x, y, tail} + {1, nil, []} """ defmacro destructure(left, right) when is_list(left) do + tail? = + case :lists.reverse(left) do + [{:|, _, [_, _]} | _] -> true + _ -> false + end + quote do - unquote(left) = Kernel.Utils.destructure(unquote(right), unquote(length(left))) + unquote(left) = + Kernel.Utils.destructure(unquote(right), unquote(length(left)), unquote(tail?)) end end diff --git a/lib/elixir/lib/kernel/utils.ex b/lib/elixir/lib/kernel/utils.ex index 9a822785fb..895a216373 100644 --- a/lib/elixir/lib/kernel/utils.ex +++ b/lib/elixir/lib/kernel/utils.ex @@ -10,17 +10,20 @@ defmodule Kernel.Utils do @doc """ Callback for destructure. """ - def destructure(list, count) - when is_list(list) and is_integer(count) and count >= 0, - do: destructure_list(list, count) + def destructure(list, count, tail? \\ false) - def destructure(nil, count) - when is_integer(count) and count >= 0, + def destructure(list, count, tail?) + when is_list(list) and is_integer(count) and count >= 0 and is_boolean(tail?), + do: destructure_list(list, count, tail?) + + def destructure(nil, count, tail?) + when is_integer(count) and count >= 0 and is_boolean(tail?), do: destructure_nil(count) - defp destructure_list(_, 0), do: [] - defp destructure_list([], count), do: destructure_nil(count) - defp destructure_list([h | t], count), do: [h | destructure_list(t, count - 1)] + defp destructure_list(list, 0, true), do: list + defp destructure_list(_, 0, false), do: [] + defp destructure_list([], count, _tail?), do: destructure_nil(count) + defp destructure_list([h | t], count, tail?), do: [h | destructure_list(t, count - 1, tail?)] defp destructure_nil(0), do: [] defp destructure_nil(count), do: [nil | destructure_nil(count - 1)] diff --git a/lib/elixir/test/elixir/kernel_test.exs b/lib/elixir/test/elixir/kernel_test.exs index 4470e29e9f..acf4fc5b53 100644 --- a/lib/elixir/test/elixir/kernel_test.exs +++ b/lib/elixir/test/elixir/kernel_test.exs @@ -1238,6 +1238,34 @@ defmodule KernelTest do end describe "destructure" do + test "cons pattern preserves the tail" do + assert destructure([a, b | c], [1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5] + assert {a, b, c} == {1, 2, [3, 4, 5]} + + destructure [head | tail], [1 | :tail] + assert {head, tail} == {1, :tail} + end + + test "cons pattern pads only the fixed prefix" do + for {values, expected} <- [ + {[1], {1, nil, []}}, + {nil, {nil, nil, []}} + ] do + destructure [a, b | c], values + assert {a, b, c} == expected + end + end + + test "cons pattern matches the tail" do + tail = [3, 4] + destructure [a, b | ^tail], [1, 2, 3, 4] + assert {a, b} == {1, 2} + + assert_raise MatchError, fn -> + destructure [_, _ | ^tail], [1, 2, 3] + end + end + test "less args" do destructure [x, y, z], [1, 2, 3, 4, 5] assert x == 1 From 614a9a84c9865d24c5616f1c9ce079eee8ab1295 Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Wed, 18 Nov 2026 14:53:36 +0100 Subject: [PATCH 2/2] Reject explicit tails in Kernel.destructure/2 Assisted-by: Codex:gpt-6.2-sol --- lib/elixir/lib/kernel.ex | 32 ++++++++++---------------- lib/elixir/lib/kernel/utils.ex | 19 +++++++-------- lib/elixir/test/elixir/kernel_test.exs | 32 +++++--------------------- 3 files changed, 26 insertions(+), 57 deletions(-) diff --git a/lib/elixir/lib/kernel.ex b/lib/elixir/lib/kernel.ex index 5d86ddd8cc..184d2a2572 100644 --- a/lib/elixir/lib/kernel.ex +++ b/lib/elixir/lib/kernel.ex @@ -4128,6 +4128,9 @@ defmodule Kernel do and right lists don't match, destructuring simply stops instead of raising an error. + An explicit tail in the left-hand list, such as `[head | tail]`, raises + an `ArgumentError` at compile time. + ## Examples iex> destructure([x, y, z], [1, 2, 3, 4, 5]) @@ -4142,8 +4145,7 @@ defmodule Kernel do iex> {x, y, z} {1, nil, nil} - The left-hand side supports any expression you would use - on the left-hand side of a match: + Each element of the left-hand list can be any pattern: iex> x = 1 iex> destructure([^x, y, z], [1, 2, 3]) @@ -4153,28 +4155,18 @@ defmodule Kernel do The example above will only work if `x` matches the first value in the right list. Otherwise, it will raise a `MatchError` (like the `=` operator would do). - - A cons pattern captures the remaining elements in its tail. Only the - elements before the tail are padded with `nil` if the right list is smaller: - - iex> destructure([x, y | tail], [1, 2, 3, 4]) - iex> {x, y, tail} - {1, 2, [3, 4]} - - iex> destructure([x, y | tail], [1]) - iex> {x, y, tail} - {1, nil, []} """ defmacro destructure(left, right) when is_list(left) do - tail? = - case :lists.reverse(left) do - [{:|, _, [_, _]} | _] -> true - _ -> false - end + case :lists.reverse(left) do + [{:|, _, [_, _]} | _] -> + raise ArgumentError, "destructure/2 does not support a tail in the left-hand list" + + _ -> + :ok + end quote do - unquote(left) = - Kernel.Utils.destructure(unquote(right), unquote(length(left)), unquote(tail?)) + unquote(left) = Kernel.Utils.destructure(unquote(right), unquote(length(left))) end end diff --git a/lib/elixir/lib/kernel/utils.ex b/lib/elixir/lib/kernel/utils.ex index 895a216373..9a822785fb 100644 --- a/lib/elixir/lib/kernel/utils.ex +++ b/lib/elixir/lib/kernel/utils.ex @@ -10,20 +10,17 @@ defmodule Kernel.Utils do @doc """ Callback for destructure. """ - def destructure(list, count, tail? \\ false) + def destructure(list, count) + when is_list(list) and is_integer(count) and count >= 0, + do: destructure_list(list, count) - def destructure(list, count, tail?) - when is_list(list) and is_integer(count) and count >= 0 and is_boolean(tail?), - do: destructure_list(list, count, tail?) - - def destructure(nil, count, tail?) - when is_integer(count) and count >= 0 and is_boolean(tail?), + def destructure(nil, count) + when is_integer(count) and count >= 0, do: destructure_nil(count) - defp destructure_list(list, 0, true), do: list - defp destructure_list(_, 0, false), do: [] - defp destructure_list([], count, _tail?), do: destructure_nil(count) - defp destructure_list([h | t], count, tail?), do: [h | destructure_list(t, count - 1, tail?)] + defp destructure_list(_, 0), do: [] + defp destructure_list([], count), do: destructure_nil(count) + defp destructure_list([h | t], count), do: [h | destructure_list(t, count - 1)] defp destructure_nil(0), do: [] defp destructure_nil(count), do: [nil | destructure_nil(count - 1)] diff --git a/lib/elixir/test/elixir/kernel_test.exs b/lib/elixir/test/elixir/kernel_test.exs index acf4fc5b53..273c97436a 100644 --- a/lib/elixir/test/elixir/kernel_test.exs +++ b/lib/elixir/test/elixir/kernel_test.exs @@ -1238,32 +1238,12 @@ defmodule KernelTest do end describe "destructure" do - test "cons pattern preserves the tail" do - assert destructure([a, b | c], [1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5] - assert {a, b, c} == {1, 2, [3, 4, 5]} - - destructure [head | tail], [1 | :tail] - assert {head, tail} == {1, :tail} - end - - test "cons pattern pads only the fixed prefix" do - for {values, expected} <- [ - {[1], {1, nil, []}}, - {nil, {nil, nil, []}} - ] do - destructure [a, b | c], values - assert {a, b, c} == expected - end - end - - test "cons pattern matches the tail" do - tail = [3, 4] - destructure [a, b | ^tail], [1, 2, 3, 4] - assert {a, b} == {1, 2} - - assert_raise MatchError, fn -> - destructure [_, _ | ^tail], [1, 2, 3] - end + test "rejects a top-level cons pattern" do + assert_raise ArgumentError, + "destructure/2 does not support a tail in the left-hand list", + fn -> + Macro.expand_once(quote(do: destructure([a, b | tail], [1, 2, 3])), __ENV__) + end end test "less args" do