Skip to content

`$` converts ambiguous record update error to a warning

Summary

Inserting $ between a function call and a record update can change ambiguous record update errors, into warnings. This behavior is specific to $ since using Data.Function.& or aliasing $ still produce error. As far as I can tell this seems to be a regression since before GHC 9.2.5, only type applications were able to change this error to a warning.

Steps to reproduce

{-# LANGUAGE DuplicateRecordFields #-}

data Foo1 = Foo1
  { foo :: Int
  , bar :: Int
  }

data Foo2 = Foo2
  { foo :: Int
  , baz :: Int
  }
 
setFoo :: Int -> Foo1 -> Foo1 
setFoo x f = id f {foo = x}

GHC gives the following error:

Example.hs:14:17: error: [GHC-99339]
    • Record update is ambiguous, and requires a type signature
    • In the first argument of ‘id’, namely ‘f {foo = x}’
      In the expression: id f {foo = x}
      In an equation for ‘setFoo’: setFoo x f = id f {foo = x}
   |
14 | setFoo x f = id f {foo = x}
   |     

If we insert a $ into setFoo as so: setFoo x f = id $ f {foo = x}, we get a warning instead:

Example.hs:14:22: warning: [GHC-02256] [-Wambiguous-fields]
    The record update f {foo = x} with type Foo1 is ambiguous.
    This will not be supported by -XDuplicateRecordFields in future releases of GHC.
   |
14 | setFoo x f = id $ f {foo = x}
   |

Expected behavior

Return the same error as if there were no $.

Environment

  • GHC version used: 9.6.1
Edited by Emeka Nkurumeh
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information