Erlang Blog Posts

Excellent blog post.

I will take the opportunity to explain what is happening in the compiler when it warns or remains silent. Consider this example, similar to the one in the blog post:

warn(X, Y) -> X+Y;
warn(6, 6) -> six. 

Here an early optimization pass in the compiler will see that the first clause will always match because all patterns are (distinct) variables and there is no guard. Therefore, the compiler will remove all clauses that follow it, and at the same emit a warning for each clause that is discarded.

I call warnings that are emitted when the compiler optimizes or attempts to optimize the code opportunistic warnings. Here is another example that produces an opportunistic warning:

atom_arith() ->
    X = 42,
    Y = a,
    X + Y.

The compiler will substitute the values of X and Y into the expression, get 42 + a and try to evaluate it. When the evaluation fails, the following warning will be emitted:

t.erl:7:7: Warning: evaluation of operator '+'/2 will fail with a 'badarith' exception
%    7|     X + Y.
%     |       ^

It might seem that opportunistic warnings are a great way to get useful warnings for free with no downsides, and I also thought so when I first implemented them. I was wrong.

Because of the opportunistic nature of the warning, the compiler could remain silent for code that is very similar to code that produces a warning. For example, the compiler will not emit any warning for this code (but Dialyzer will):

atom_arith(X) ->
    X + a.

The reason is that the compiler never attempts to evaluate X + a since X is unknown.

Many such “missing warnings” have been reported as bugs to the OTP team over the years.

Another downside is that when we introduce new optimizations in the early optimization passes, the compiler could start warning about legitimate code. We would have to add extra code to the compiler to ensure that no false warnings were emitted.

Because of those downsides, we don’t plan to introduce any new opportunistic warnings in the compiler, but we will keep emitting most of the opportunistic warnings that are currently emitted.

14 Likes