Skip to content

Strange typechecking errors to do with Generics in a module with ANN and a type error

Summary

This is a reproducer extracted from something we encountered in the wild. Adding a new field to datatype led to a bunch of strange errors about Generics.

In this minimal reproducer, the generics error is quite small, but it becomes huge in any non-trivial example.

Steps to reproduce

Compile the following module:

{-# LANGUAGE DeriveAnyClass    #-}
module Repro where
import GHC.Generics

data IHaveNoFields = IHaveNoFields { } deriving (Generic)

getField
  (IHaveNoFields field1 )
  = field1

{-# ANN iHaveAnn ("" :: String) #-}
iHaveAnn = undefined

Here is the output:

Repro.hs:5:50: error:
    • Couldn't match type: Rep IHaveNoFields
                     with: M1 i0 c0 (M1 i1 c1 U1)
      Expected: Rep IHaveNoFields x
        Actual: M1 i0 c0 (M1 i1 c1 U1) x
    • In the expression: M1 (case x of IHaveNoFields -> M1 U1)
      In an equation for ‘from’:
          from x = M1 (case x of IHaveNoFields -> M1 U1)
      When typechecking the code for ‘from’
        in a derived instance for ‘Generic IHaveNoFields’:
        To see the code I am typechecking, use -ddump-deriv
      In the instance declaration for ‘Generic IHaveNoFields’
  |
5 | data IHaveNoFields = IHaveNoFields { } deriving (Generic)
  |                                                  ^^^^^^^

Repro.hs:5:50: error:
    • Couldn't match type: Rep IHaveNoFields
                     with: M1 i2 c2 (M1 i3 c3 U1)
      Expected: Rep IHaveNoFields x
        Actual: M1 i2 c2 (M1 i3 c3 U1) x
    • In the pattern: M1 x
      In an equation for ‘to’:
          to (M1 x) = case x of (M1 U1) -> IHaveNoFields
      When typechecking the code for ‘to’
        in a derived instance for ‘Generic IHaveNoFields’:
        To see the code I am typechecking, use -ddump-deriv
      In the instance declaration for ‘Generic IHaveNoFields’
  |
5 | data IHaveNoFields = IHaveNoFields { } deriving (Generic)
  |                                                  ^^^^^^^

Repro.hs:8:4: error:
    • The constructor ‘IHaveNoFields’ should have no arguments, but has been given 1
    • In the pattern: IHaveNoFields field1
      In an equation for ‘getField’:
          getField (IHaveNoFields field1) = field1
  |
8 |   (IHaveNoFields field1 )
  |

Expected behavior

This should error but should only output: And indeed this is what we get if we comment out the ANN line.

Repro.hs:8:4: error:
    • The constructor ‘IHaveNoFields’ should have no arguments, but has been given 1
    • In the pattern: IHaveNoFields field1
      In an equation for ‘getField’:
          getField (IHaveNoFields field1) = field1
  |
8 |   (IHaveNoFields field1 )
  |    ^^^^^^^^^^^^^^^^^^^^

Environment

  • GHC version used: 9.4.8, 9.8.1
Edited by Teo Camarasu
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information