Identifier is not a record selector [GHC-47535]

Within a record update expression r { selector1 = ..., ..., selectorN = ... }, the selectors selector1selectorN must be bound by a record constructor declaration.

Examples

Identifier is a top-level binding, not a record selector

The identifier foo is used as a record selector, but is never declared as such. Using bar fixes the error, since it is declared as a selector of the Bar record.

Error Message

NotARecordSelector.hs:6:13: error: [GHC-47535]
    • ‘foo’ is not a record selector
    • In the expression: x {foo = 1}
      In an equation for ‘foo’: foo x = x {foo = 1}
  |
6 | foo x = x { foo = 1 }
  |             ^^^
NotARecordSelector.hs
Before
module NotARecordSelector where

newtype Bar = Bar { bar :: Int }

foo :: Bar -> Bar
foo x = x { foo = 1 }
After
module NotARecordSelector where

newtype Bar = Bar { bar :: Int }

foo :: Bar -> Bar
foo x = x { bar = 1 }
Identifier is a class method, not a record selector

The identifier foo is used as a record selector, but it refers to a function of the Foo class instead. Using bar fixes the error, since it is a selector of the Bar record. This error can only occur when the type of the record is ambiguous, in the example below this is achieved by the use of undefined.

Error Message

NotARecordSelector.hs:9:20: error: [GHC-47535]
    • ‘foo’ is not a record selector
    • In the expression: undefined {foo = ()}
      In an equation for ‘test’: test = undefined {foo = ()}
  |
9 | test = undefined { foo = () }
  |                    ^^^
NotARecordSelector.hs
Before
module NotARecordSelector where

class Foo a where
  foo :: a -> ()

data Bar = Bar { bar :: () }

test :: Bar
test = undefined { foo = () }
After
module NotARecordSelector where

class Foo a where
  foo :: a -> ()

data Bar = Bar { bar :: () }

test :: Bar
test = undefined { bar = () }