Weird errors in non-matching clause in anonymous function

@tmbb I think you could report your findings to OTP.

Here’s a similar issue that has been reported a few days ago
Inconsistent error messages when errors are optimized away by the compiler · Issue #5440 · erlang/otp · GitHub

The Erlang code:

-module(foo).
-export([f1/2, f2/1]).

f1(G, X) ->
    Fun =
        fun(Value) when is_integer(Value) ->
               G(Value)
        end,
    Fun(X).

f2(X) ->
    Fun =
        fun(Value) when is_integer(Value) ->
               Value
        end,
    Fun(X).

The console outputs:

Erlang/OTP 24 [erts-12.1.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Eshell V12.1.2  (abort with ^G)
1> c('foo').                        
{ok,foo}

2> foo:f1(invalid, fun(X) -> X end).
** exception error: no case clause matching {#Fun<erl_eval.44.65746770>}
     in function  foo:f1/2 (foo.erl, line 31)

3> foo:f2(invalid).                 
** exception error: no function clause matching foo:f2(invalid) (foo.erl, line 38)


Erlang/OTP 23 [erts-11.2.2.8] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]
1> c('foo').

2> foo:f1(invalid, fun(X) -> X end).
** exception error: no function clause matching foo:'-f1/2-fun-0-'(#Fun<erl_eval.44.79398840>) (foo.erl, line 31)

3> foo:f2(invalid).                 
** exception error: no function clause matching foo:'-f2/1-fun-0-'(invalid) (foo.erl, line 38)