Skip to content

Variable cycle detection in patterns rejects valid patterns since 1.18 #15462

Description

@lukaszsamson

Existing issue

  • I have searched existing issues and could not find a duplicate.

Elixir and Erlang/OTP versions

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

Elixir 1.20.1 (compiled with Erlang/OTP 28)
Affected versions: all since 1.18.0

Operating system

any

Current behavior

Pattern variable cycle validation introduced in commit 860f485 rejects valid code when the same variable appears on the left and right

The following code reproduces the issue on 1.18+

fn %{a: v} = %{b: v} -> v end
error: recursive variable definition in patterns:

[%{a: v} = %{b: v}]

the variable "v" is defined in function of itself
└─ iex:1

** (CompileError) cannot compile code (errors have been logged)

Whereas on 1.17.3:

f = fn %{a: v} = %{b: v} -> v end
#Function<42.105768164/1 in :erl_eval.expr/6>
f.(%{a: 1, b: 1})
1

Similar behaviour

fn {v, _} = {_, v} -> v end

Expected behavior

patterns-and-guards.md does not document this as not supported so I expect it to compile fine. Equivalent code works and unifies in erlang (tested on OTP 28)

F = fun(#{a := V} = #{b := V}) -> V end.
#Fun<erl_eval.42.113135111>
F(#{a => 1, b => 1}).
1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions