Skip to content

Better error for unimported constructor (GHC-31891)

Summary

When we use an un-imported constructor when its type is in-scope and has the same name, we get an error that warns us that we are using a type instead of a term. Whenever I get this error it's simply because I've forgotten to import the constructor. That's fine for me, but I think this would likely trip up a beginner.

It would be better if GHC suggested that we import the constructor.

Steps to reproduce

module A where

data A = A
module B where

import A (A)

test = A

Then compile it using ghc A.hs B.hs.

And you will get this error:

[1 of 2] Compiling A                ( A.hs, A.o )
[2 of 2] Compiling B                ( B.hs, B.o )

B.hs:5:8: error: [GHC-31891]
    • Illegal term-level use of the type constructor or class ‘A’
    • imported from ‘A’ at B.hs:3:11
      (and originally defined at A.hs:3:1-10)
    • Add ‘A’ to the import list in the import of ‘A’ (at B.hs:3:1-12).
    • In the expression: A
      In an equation for ‘test’: test = A
  |
5 | test = A
  |

Expected behavior

Suggest that the user imports this type, compare if we don't import the type at all:

[2 of 2] Compiling B                ( B.hs, B.o )

B.hs:5:8: error: [GHC-88464]
    Data constructor not in scope: A
    Suggested fix:
      Add ‘A’ to the import list in the import of ‘A’ (at B.hs:3:1-11).
  |
5 | test = A
  |

Environment

  • GHC version used: GHC-9.4.7, I think also happens with HEAD
Edited by sheaf
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information