Decoding Dialyzer - Part 1
Dialyzer can be hard to interpret but is useful for finding bugs and incorrect typespecs. The text walks through two of the most common and important warnings using a small Elixir example with a simple bug that triggers multiple Dialyzer messages. It contrasts the clearer dialyxir 1.0.0-style formatting with the older Erlang-style output.
The example shows a function that calls concat/2 with arguments in the wrong order. This leads Dialyzer to report that a function never returns and that a particular function call will always fail. The explanation covers why literals let Dialyzer prove the failure, how no_return and call warnings are related, and how success typings differ from user-written contracts. The fix is to call the function with arguments that match its spec.
Reading notes#
- Dialyzer is described as tricky to understand but effective at spotting bugs and inaccurate typespecs.
- The goal is to explain several common Dialyzer warnings, their causes, and ways to resolve them.
- The author uses
dialyxir1.0.0-rc4 and shows both new Elixir-formatted and old Erlang-formatted warnings. - The new
dialyxir1.0.0 formatting is said to be much more helpful than the original. - A small Elixir module
Testis introduced withconcat/2andcall_concat/0and a typespec for each. - In the example,
concat/2expects anatom()and aString.t(), butcall_concat/0calls it asconcat("string", :atom). - That code contains one obvious bug (argument order/type mismatch), yet Dialyzer emits two warnings.
- The author notes that one bug frequently causes multiple Dialyzer warnings.
- Grouping warnings by related cause is suggested as more effective than tackling each in isolation.
- The first warning discussed is
no_return, presented with its new-style formatted message. - The message says
Function call_concat/0 has no local return, meaning the function never returns successfully. - The explanation states this implies any call to
call_concat/0will always raise an exception. - Two situations that cause this are listed: functions that always raise on purpose, and ones that always raise unintentionally.
- If a function intentionally always raises, the warning can be resolved by annotating its spec with
:: no_return(). - The second situation is where Dialyzer is most helpful because it indicates a serious bug.
- Dialyzer can be certain about this bug in the initial example because it knows the values are a string and an atom literal.
- A second version of the code changes
call_concat/0tocall_concat/2with typed arguments instead of literals. - In that second version, Dialyzer no longer emits a
no_returnwarning. - Dialyzer instead produces a different warning for that variation but cannot prove a guaranteed exception.
- The text explains that Dialyzer only issues
no_returnwhen it can determine argument types with full confidence, often via literals. - The
no_returnwarning alone does not clearly indicate where the underlying bug is located. - The next warning,
call, helps locate the problematic function invocation. - The
callwarning is shown for the original code on the line callingTest.concat("string", :atom). - The message states this call will never return, and displays the success typing and the contract for
concat/2. - New formatting is presented as much clearer than the old, more cryptic Erlang-style representation.
- The warning explains that calling
concat/2with the given arguments will always fail. - It identifies the call as the likely source of the bug, even though earlier
no_returnwarned about a different function. - The text notes there is no explicit link between the
no_returnandcallwarnings in the output. - The
no_returnwarning referencescall_concat, while thecallwarning referencesconcat. - At a glance the two warnings appear separate, yet they arise from the same underlying issue.
- A practical tip is given: Dialyzer warnings are grouped by file and ordered by line number.
- If a
no_returnwarning appears and a nearbycallwarning follows in the same file, they are likely related. - The
callwarning states the attempted use ofconcat/2“is never going to work”. - The text explains the difference between success typing and contract for a function.
- Success typing is described as the minimum requirements Dialyzer has inferred to avoid failure.
- A contract is described as a programmer-written requirement for using the function.
- The author notes humans often make mistakes in contracts, and Dialyzer’s inference may be less specific.
- The suggestion is to choose whichever of the two (success typing or contract) is more strict when deciding correct usage.
- In the example,
String.t()is more strict thanbinary, soconcatshould be called with strings, not arbitrary binaries. - To resolve the warning, the function must be used according to its spec.
- The fix in this case is to change
concat("string", :atom)toconcat(:atom, "string")so types and order match. - After changing the call, rerunning Dialyzer results in no more warnings.
- The resolution confirms that the bug has been found and fixed through interpreting the two warning types together.
