Skip to content

Error messages: Improve Error messages for Data constructors in type signatures.

Jade requested to merge wip/T17879 into master

Currently the error messages for a malformed LHS of a type signature is very poor:

(:>) :: String -> String -> String
(:>) = (++)
Foo.hs:1:1: error: [GHC-94426]
    Invalid type signature: (:>) :: ...
    Suggested fix:
      A type signature should be of form <variables> :: <type>
  |
1 | (:>) :: String -> String -> String
  | ^^^^

There are multiple issues: It seems like :> should be a valid operator. Of course it is not, because this is reserved for data constructor operators, but it doesn't say here. Secondly, it mentions the source, but when it is also mentioned in the caret diagnostics in a way more elaborate way.

In this patch I suggest to instead mention a more specific error and suggest an appropriate fix. The old behavior (minus the repeated source) is kept for all cases where nothing more specific can be given.

Some examples for the new behavior:

Qualified name:

M.x :: ()
M.x = ()
Invalid qualified name in type signature
Suggested fix: Perhaps you meant to omit the qualifier

Illegal data constructor operator:

(:+) :: Int -> Int -> Int
(:+) = (+)
Invalid data constructor ‘(:+)’ in type signature:
You can only define data constructors in data type declarations.

Illegal data constructor:

Foo :: ()
Foo = ()
Invalid data constructor ‘Foo’ in type signature:
You can only define data constructors in data type declarations.

All other error conditions:

Invalid type signature:
A type signature should be of form <variables> :: <type>.
Edited by Jade

Merge request reports