TemplateHaskell reify fails with fields from DuplicateRecordFields
Summary
Trying to reify
fields from a record defined with DuplicateRecordFields
extension leads to a confusing error message:
Exp.hs:11:1: error:
Ambiguous occurrence ‘Exp.foo’
It could refer to
either the field ‘foo’, defined at Exp.hs:9:18
or the field ‘foo’, defined at Exp.hs:8:18
Even if the name itself is not present in spliced code.
Steps to reproduce
Complete code snippet is here: https://gist.github.com/maksbotan/146b250ad295c0f5896a2313ce918cd0
Briefly:
- Write TemplateHaskell function of type
Name -> Q [Dec]
, whereName
is supposed to refer to a data type. -
reify
theName
to getRecC
info withName
s of the - call
reify
on thoseName
s to get their types
make :: Name -> Q [Dec]
make name = do
TyConI (DataD _ _ _ _ [RecC con fields'] _) <- reify name
let fields = [ field | (field, _, _) <- fields' ]
fieldInfos <- mapM reify fields
return []
When the record in question is defined in a file with DuplicateRecordFields
and really has duplicate fields, GHC will
emit a compilation error with confusing message.
Expected behavior
Since I reify
ed concrete record type, I expect to be able to extras its fields without any ambiguity.
Environment
- GHC version used: 8.6.3 (Stack LTS-13.6), 8.6.5 (nix), 8.8.1 (nix)
- Operating System: macOS mojave
This looks related to #14848 (closed) and #17381 (closed), but in my case I don't do any quasiquoting. Another difference is that in my case the error message is really misleading.