Skip to content

Cannot disambiguate duplicate pattern synonym record fields in exports

Consider the following module:

{-# LANGUAGE GHC2021 #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE PatternSynonyms #-}

module RecPatSyn (B(B, x)) where

data A = MkA {x_ :: Int}

data B = MkB {x_ :: Int}

pattern A :: Int -> A
pattern A{x} <- MkA x

pattern B :: Int -> B
pattern B{x} <- MkB x

There is no way to express the fact that in the export list we mean the x originating from pattern synonym B. Hence this is rejected with an ambiguous occurrence error:

RecPatSyn.hs:5:19: error: [GHC-66025]
    • Pattern synonyms can only be bundled with matching type constructors
      Couldn't match expected type of ‘B’ with actual type of ‘A’
    • In the pattern synonym record selector: x
      In the export: B(B, x)
  |
5 | module RecPatSyn (B(B, x)) where
  |                   ^^^^^^^

RecPatSyn.hs:5:19: error: [GHC-87543]
    • Ambiguous occurrence ‘x’.
      It could refer to
         either the field ‘x’ of pattern synonym ‘A’,
                defined at RecPatSyn.hs:12:11,
             or the field ‘x’ of pattern synonym ‘B’,
                defined at RecPatSyn.hs:15:11.
    • In the export: B(B, x)
  |
5 | module RecPatSyn (B(B, x)) where
  |                   ^^^^^^^

The "Pattern synonyms can only be bundled with matching type constructors" bit of the error seems to be bogus. Presumably the ambiguous identifier is "resolved" to the first option to allow type-checking to continue. But it leads to the awkward situation where the renamer considers it ambiguous which x is meant, yet the type-checker apparently "knows" that x cannot come from A.

It's not obvious to me how we could do better here, but it's an awkward corner of the PatternSynonyms/DuplicateRecordFields interaction that I wanted to record.

This was originally reported at https://discourse.haskell.org/t/how-to-export-record-pattern-synonym-duplicate-field-names-in-the-same-module/7594.

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