Skip to content

Improve Error message when field is missing on actual Haskell record

Motivation

When having -XOverloadedRecordDots on and writing the following code

data R = MkR 
  { ri :: Int 
  , rs :: String
  }

x :: R -> Bool 
x r = r.rb

I get the following error message

    • No instance for (GHC.Records.HasField "rb" R Bool)
        arising from selecting the field ‘rb’
    • In the expression: r.rb
      In an equation for ‘x’: x r = r.rb
   |
97 | x r = r.rb
   |       ^^^^

which is arguably too confusing if you just want to use this extension for allowing the record dot syntax.

Proposal

As this is a Haskell record the error message could just have been "The Haskell record R does not have the field rb" which is in my opinion easier to decipher for users who don't want to use the actual overloading of the record dot. As it's possible to also declare HasField instances for some specific field name (HasField "rb" R Bool) it would probably be good to nevertheless include a NB that this error results from a missing HasField instance.

I would like to implement this my own, if this was accepted, but I'm new so guidance would be appreciated 😄

Thanks in advance!

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