Skip to content

GHC should disambiguate fields from non-fields

I tried writing a program like this:

{-# LANGUAGE NoFieldSelectors #-}

module Bug where

data Rec = MkRec { field :: Int }

field :: Int -> Int
field = (+1)

foo (MkRec { field = n }) = MkRec { field = n + 1 }

But GHC said

Bug.hs:10:14: error:
    Ambiguous occurrence `field'
    It could refer to
       either the field `field', defined at Bug.hs:5:20
           or `Bug.field', defined at Bug.hs:8:1
   |
10 | foo (MkRec { field = n }) = MkRec { field = n + 1 }
   |              ^^^^^

Bug.hs:10:37: error:
    Ambiguous occurrence `field'
    It could refer to
       either the field `field', defined at Bug.hs:5:20
           or `Bug.field', defined at Bug.hs:8:1
   |
10 | foo (MkRec { field = n }) = MkRec { field = n + 1 }
   |                                     ^^^^^

I think GHC should be able to figure out what I mean here, given that field occurs in a place only a field name could occur.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information