Warnings, errors and -Wall

What -Wall turns on

-Wall is the name of a group of warnings, not a single one. In GHC 9.10 the group includes -Wmissing-signatures (a top-level binding without a type), -Wunused-imports, -Wunused-top-binds, -Wtype-defaults and -Wincomplete-patterns, among others. Each of them can be turned back off with -Wno- and its name.

Every program in this course is built with ghc -Wall -O1, so a message on a page is one you get too. A warning is not an error: the binary is written, and the program runs. It is the compiler telling you something a reader would also notice.

Two warnings in one file

This file imports a function it never calls and defines a value nothing uses. Both are invisible at run time, and both are mistakes in waiting.

module Main (main) where import Data.List (sort) main :: IO () main = putStrLn "three lines follow" unusedHelper :: Int unusedHelper = 41

ghc -Wall -O1 -o unused Unused.hs
echo "status: $?"

[1 of 2] Compiling Main ( Unused.hs, Unused.o )
Unused.hs:3:1: warning: [GHC-66111] [-Wunused-imports]
The import of ‘Data.List’ is redundant
except perhaps to import instances from ‘Data.List’
To import instances alone, use: import Data.List()
|
3 | import Data.List (sort)
| ^^^^^^^^^^^^^^^^^^^^^^^

Unused.hs:9:1: warning: [GHC-40910] [-Wunused-top-binds]
Defined but not used: ‘unusedHelper’
|
9 | unusedHelper = 41
| ^^^^^^^^^^^^

[2 of 2] Linking unused
status: 0

Each message has the same five parts, and they are always in the same order:

The build still links and the status is 0. The warning is a hint, not a wall, unless you make it one.

-Werror and -fforce-recomp

-Werror turns every warning into an error: the build stops, no binary is written, and the status is 1. This is the same file without the unused binding, so the import is the only finding:

module Main (main) where import Data.List (sort) main :: IO () main = putStrLn "this file is not strict"

ghc -Wall -O1 -o werror Werror.hs
ghc -Wall -Werror -fforce-recomp -O1 -o werror Werror.hs
echo "status: $?"

[1 of 2] Compiling Main ( Werror.hs, Werror.o )
Werror.hs:3:1: warning: [GHC-66111] [-Wunused-imports]
The import of ‘Data.List’ is redundant
except perhaps to import instances from ‘Data.List’
To import instances alone, use: import Data.List()
|
3 | import Data.List (sort)
| ^^^^^^^^^^^^^^^^^^^^^^^

[2 of 2] Linking werror
[1 of 2] Compiling Main ( Werror.hs, Werror.o )
Werror.hs:3:1: error: [GHC-66111] [-Wunused-imports, Werror=unused-imports]
The import of ‘Data.List’ is redundant
except perhaps to import instances from ‘Data.List’
To import instances alone, use: import Data.List()
|
3 | import Data.List (sort)
| ^^^^^^^^^^^^^^^^^^^^^^^

status: 1

Two details in the second message. error: replaced warning:, and the flag list grew a second entry, Werror=unused-imports: the warning name is still there, so you know which one stopped the build.

-fforce-recomp is in the command because a build reuses Werror.o when the source is unchanged, and a reused object file hides the warning. Without the flag the second build prints nothing and reports success, which is the wrong answer. When you try a flag on a file that was built before, add -fforce-recomp or delete the .o and .hi files.

A type error, read line by line

A warning lets the program through; an error does not. This file adds a number to a string:

module Main (main) where main :: IO () main = print (1 + "two")

ghc -Wall -O1 -o bad Bad.hs
echo "status: $?"

[1 of 2] Compiling Main ( Bad.hs, Bad.o )
Bad.hs:4:15: error: [GHC-39999]
• No instance for ‘Num String’ arising from the literal ‘1’
• In the first argument of ‘(+)’, namely ‘1’
In the first argument of ‘print’, namely ‘(1 + "two")’
In the expression: print (1 + "two")
|
4 | main = print (1 + "two")
| ^

status: 1

The message does not say "you cannot add a number and a string", because the compiler does not know what a string is: (+) is the method of the Num class, and it works on any type that has an instance of it. String has none, so there is no Num String, and the caret points at the literal the compiler was trying to fit into that instance.

The lines after the caret, each starting with •, are a breadcrumb trail of where the expression came from: the argument of (+), then of print, then the whole equation. On a large file that trail is what tells you which of five additions is the wrong one.

Nothing is written: no .o, no binary, status 1. The type checker runs before code generation, so an error of this kind costs nothing to fix and nothing to run.

The GHC error code

GHC-39999 is a stable identifier for this message. Every diagnostic in GHC 9.10 has one, and searching for the code finds the page of the GHC user guide that explains the message, without the noise of every forum post that quotes an older wording. The index is at downloads.haskell.org.

The codes this course meets most often are worth knowing by sight:

A code is also a handle for the flag system: -Wno-unused-imports silences that one warning, -Werror=unused-imports promotes only that one to an error. The lesson on GHC flags collects the rest.

Exercises

  1. Fix Unused.hs in two ways: use the import and use the helper, and then delete both. Which fix is the right one for a file you are about to commit?
  2. Build Werror.hs with -Wall -Werror twice, without -fforce-recomp. What does the second build print, and why is that misleading?
  3. Change Bad.hs to print (1 + 2 :: Int) and build it. Which of the five parts of the earlier message disappears?
  4. Run hlint Unused.hs. Does hlint report the same two problems as the compiler?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does -Wall do?

    Answer

    Turns on a group of warnings, including missing signatures and unused imports

    -Wall selects a group of warnings; -Werror is the flag that turns them into errors.

  2. A build prints a warning and a binary. What is the exit status of that build?

    Answer

    0

    A warning does not stop the build, so ghc links the binary and returns 0.

  3. What does -Werror change about a warning?

    Answer

    It makes the build stop with status 1

    -Werror promotes warnings to errors, so the build fails and no binary is produced.

  4. Why does a second build of an unchanged file print no warnings?

    Answer

    Because the object file is up to date and the compiler does no work

    An up-to-date .o file is reused, so the module is not compiled again and nothing is reported; -fforce-recomp or deleting the .o forces the work.

  5. What does the flag part [-Wunused-imports, Werror=unused-imports] tell you?

    Answer

    The warning is unused-imports, and -Werror promoted that one to an error

    The first entry names the warning, the second says which promotion turned it into an error.

  6. What does the GHC code in a message, such as GHC-39999, identify?

    Answer

    A stable identifier for that diagnostic

    Each diagnostic has a stable code that can be searched in the GHC user guide, whatever the wording of the message.

  7. What does this program print? module Main (main) where main :: IO () main = putStrLn "three lines follow"

    Answer

    three lines follow

    The program prints its one string once; the import is missing, and since nothing needs it there is no error.

  8. What is the first line of the compiler output for this program? module Main (main) where main :: IO () main = print (1 + "two")

    Answer

    error: [GHC-39999]

    There is no Num instance for String, and GHC reports that as GHC-39999.

  9. What is the first line of the compiler output for this program? module Main (main) where main :: IO () main = putStrLn (1 + 2)

    Answer

    error: [GHC-39999]

    putStrLn needs a String, the sum is a number, and no Num instance makes the number a String, so GHC reports GHC-39999.

  10. Which command checks a file and writes nothing at all?

    Answer

    ghc -fno-code -Wall File.hs

    -fno-code stops after type checking, while a full build writes a .hi, a .o and a binary.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image