Skip to content

NoFieldSelectors should prevent importing fields at top level

From #21625 (comment 450472):

It seems that the current implementation of NoFieldSelectors is inconsistent between export lists and import lists. Consider the following:

{-# LANGUAGE NoFieldSelectors #-}
module M where
  data T = MkT { foo :: Int, bar :: Int }
  foo = ()

If I instead write an export list module M (foo) the foo always refers to the value binding, not the field. I have to write T(foo) to export the field. (This is documented in the user's guide: https://downloads.haskell.org/ghc/9.4.1/docs/users_guide/exts/field_selectors.html#import-and-export-of-selector-functions). This means the module can choose to export the field or the value binding independently.

On the other hand, currently if I write a restricted import list import M (foo) the foo refers to both the field and the value (so it generates an ambiguity error, or with a variant of !8858 (closed) would import both), while import M (bar) is accepted and imports the field. The user's guide doesn't appear to document what should happen in this case. I think we should be consistent with exports, so that import M (foo) would import only the value binding, import M (T(foo) would import the field, and import M (bar) would be an error.

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