Skip to content

IEx: break! works strange with pattern match on new guards #7155

@Eiji7

Description

@Eiji7

Environment

$ asdf current
asdf current
elixir ref-v1.6.0-rc.0 (set by /home/eiji/.tool-versions)
erlang 20.2 (set by /home/eiji/.tool-versions)

Example code

Like in last issue I'm still testing RGB(a) checks (as real world example):

defmodule Example do
  defguardp is_rgb_integer(tuple, index) when elem(tuple, index) in 0..255

  defguardp is_rgb_base(rgb)
            when is_rgb_integer(rgb, 0) and is_rgb_integer(rgb, 1) and is_rgb_integer(rgb, 2)

  defguard is_css_alpha(alpha) when alpha >= 0 and alpha <= 1

  defguardp is_rba_css_alhpa(tuple) when tuple |> elem(3) |> is_css_alpha()

  defguard is_rgba(rgba)
           when is_tuple(rgba) and tuple_size(rgba) == 4 and is_rgb_base(rgba) and
                  is_rba_css_alhpa(rgba)

  defguardp check_if_has_alpha(rgba)
            when tuple_size(rgba) == 3 or (tuple_size(rgba) == 4 and is_rba_css_alhpa(rgba))

  defguard is_rgb(rgba)
           when is_tuple(rgba) and check_if_has_alpha(rgba) and is_rgb_base(rgba)

  def check_rgb(rgba) when is_rgb(rgba), do: :ok
  def check_rgb(_rgba), do: :error
end

Current behavior

iex(1)> break! Example.check_rgb({255, _, _})
1
iex(2)> Example.check_rgb({255, 255, 255, 1})
Break reached: Example.check_rgb/1 (lib/example.ex:21)

   19:            when is_tuple(rgba) and check_if_has_alpha(rgba) and is_rgb_base(rgba)
   20: 
   21:   def check_rgb(rgba) when is_rgb(rgba), do: :ok
   22:   def check_rgb(_rgba), do: :error
   23: end

Expected behavior

Call with 4-element tuple should not reach breakpoint that was set for 3-element tuple.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions