GHC 9.8 TemplateHaskell lookupValueName for field selector
Summary
Since GHC 9.8, the field selectors lives in a different namespace compared to other names. This gives TH the abilities to work with duplicated record fields.
Because of that, it is not possible anymore to check for the existence of a field selector in a template haskell session using lookupValueName
I observed it in PyF, see https://github.com/guibou/PyF/issues/133, where I'm parsing haskell expression in a quasi quote and turning them to template haskell, but before doing that, I'm doing a bit of sanity checking in order to detect invalid / incomplete haskell expression and give a better feedback to the user.
Steps to reproduce
{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.TH
data Cheval = Cheval { hibou :: Int }
name = $(do
n <- lookupValueName "hibou"
pure $ LitE $ StringL $ show n)
with GHC < 9.8, name
will contain "Just Main.Hibou"
, but it contains "Nothing"
with GHC 9.8:
$ ghci-9.4.8
GHCi, version 9.4.8: https://www.haskell.org/ghc/ :? for help
ghci> :l THRefiy.hs
[1 of 2] Compiling Main ( THRefiy.hs, interpreted )
Ok, one module loaded.
ghci> name
"Just Main.hibou"
$ ghci-9.8.1
GHCi, version 9.8.1: https://www.haskell.org/ghc/ :? for help
ghci> :l THRefiy.hs
[1 of 2] Compiling Main ( THRefiy.hs, interpreted )
Ok, one module loaded.
ghci> name
"Nothing"
Expected behavior
Either lookupValueName
should return something, or I would like to find another solution to look for the existence of the name.
Environment
- GHC version used: 9.8.1