Skip to content

Reject explicit tails in Kernel.destructure/2 - #16145

Open
lukaszsamson wants to merge 2 commits into
elixir-lang:mainfrom
lukaszsamson:codex/fix-destructure-cons-tail
Open

lukaszsamson wants to merge 2 commits into
elixir-lang:mainfrom
lukaszsamson:codex/fix-destructure-cons-tail

Conversation

@lukaszsamson

@lukaszsamson lukaszsamson commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Kernel.destructure/2 used to silently discard the remainder when the left-hand list had an explicit tail. It now raises ArgumentError at compile time for patterns such as [head | tail]. Each element in the list can still be any match pattern.

@josevalim

Copy link
Copy Markdown
Member

I don't believe this was ever intended to match on cons patterns and we should raise instead.

@lukaszsamson

Copy link
Copy Markdown
Contributor Author

I don't believe this was ever intended to match on cons patterns and we should raise instead.

The docs https://elixir.hexdocs.pm/Kernel.html#destructure/2 make me believe cons operator was intended to be supported

The left-hand side supports any expression you would use on the left-hand side of a match

@josevalim

Copy link
Copy Markdown
Member

I believe the intention there was to say things like defstructure [{:ok, x}, <<"foo">>], [....]. To me it is a bit weird to fill everything with nil and then the tail with an empty list, especially because for improper lists the tail can be any term anyway, so even the default of [] is not guaranteed.

@lukaszsamson

lukaszsamson commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

To me it is a bit weird to fill everything with nil and then the tail with an empty list

I agree. Maybe we should raise in this case?

destructure([a | b], [1, 2, 3]) # [1, [2, 3]]
destructure([a, b, c, d | e], [1, 2, 3]) # ArgumentError

@josevalim

Copy link
Copy Markdown
Member

Let's just raise if we have a tail on the left-side at compile-time! Same check as today, except it raises!

Assisted-by: Codex:gpt-6.2-sol
@lukaszsamson lukaszsamson changed the title Preserve cons pattern tails in Kernel.destructure/2 Reject explicit tails in Kernel.destructure/2 Nov 18, 2026
@lukaszsamson

Copy link
Copy Markdown
Contributor Author

Let's just raise if we have a tail on the left-side at compile-time!

Done

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants