Skip to main content

W0005 - Mutable Variable Bug

Error

-module(main).
-export([test/0]).
test() ->
Zero = 0,
One = 1,

Result = One = Zero,
%% ^^^^^^^^^^^^^^^^^^^ error: Possible mutable variable bug

io:format("~p ~p~n", [Zero, One]),
Result.

Explanation

The error message indicates that this specific pattern could trigger a known bug with certain OTP releases. Earlier (i.e. < OTP 26) Erlang releases were affected by a subtle bug which caused the pattern matching operator to incorrectly mutate variables.

By compiling the above snippet on one of the problematic releases you'd get:

1> test:test().
0 0
0

The code should have crashed with a pattern match error. Instead, the variable One mutated from the original value 1 to the value 0.