elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications

Home Page:https://elixir-lang.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Compilation error when `min` appears in guard of function head on Elixir 1.16

crkent opened this issue · comments

Elixir and Erlang/OTP versions

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

Elixir 1.16.3 (compiled with Erlang/OTP 26)

Operating system

macOS 14.5

Current behavior

Attempting to compile the module

defmodule ErrorGeneratingModule do
  defp problematic_head(value) when min(value, 0) >= 0, do: value
end

produces the following error

** (FunctionClauseError) no function clause matching in Module.Types.Pattern.guard_signature/2
The exception happened while checking this code:

defp problematic_head(value) when :erlang.>=(:erlang.min(value, 0), 0) do
  value
end

In case it is a bug, please report it at: https://github.com/elixir-lang/elixir/issues

        (elixir 1.16.3) lib/module/types/pattern.ex:631: Module.Types.Pattern.guard_signature(:min, 2)
        (elixir 1.16.3) lib/module/types/pattern.ex:260: Module.Types.Pattern.of_guard/4
        (elixir 1.16.3) lib/module/types/helpers.ex:93: Module.Types.Helpers.do_map_reduce_ok/3
        (elixir 1.16.3) lib/module/types/pattern.ex:273: Module.Types.Pattern.of_guard/4
        (elixir 1.16.3) lib/module/types/pattern.ex:14: Module.Types.Pattern.of_head/4
        (elixir 1.16.3) lib/module/types.ex:64: Module.Types.warnings_from_clause/6
        (elixir 1.16.3) lib/module/types.ex:22: anonymous fn/8 in Module.Types.warnings/5
        (elixir 1.16.3) lib/enum.ex:4326: Enum.flat_map_list/2

Expected behavior

The module ErrorGeneratingModule appears to be syntactically valid and should compile given that the following code compiles under the same runtime

case 10 do
  value when min(value, 0) >= 0 -> :ok
  _ -> :error
end

I have not done an exhaustive search of previously-filed issues to understand if this is related to a previous (and possibly closed/fixed) issue.

This happens because min/max were added as guards in Erlang but they are not treated as guards by Elixir. However the code above has been rewritten, so it no longer happens in main. Thank you. For now, do not use min/max in guards, as it is Elixir specific.