Skip to content

Error when splicing code with DuplicateRecordFields

Summary

Splicing code that uses DuplicateRecordFields doesn't work.

Steps to reproduce

Three files Dup.hs, TH.hs and Use.hs lead to this error:

$ ghc --make Use.hs 
[1 of 3] Compiling Dup              ( Dup.hs, Dup.o, Dup.dyn_o )
[2 of 3] Compiling TH               ( TH.hs, TH.o, TH.dyn_o )
[3 of 3] Compiling Use              ( Use.hs, Use.o, Use.dyn_o )

Use.hs:8:17: error:
    • Illegal variable name: ‘$sel:field:Con1’
      When splicing a TH expression:
        case Dup.val1 of
    (Dup.Con1 {Dup.$sel:field:Con1 = field_0}) -> field_0
    • In the Template Haskell splice $$(process [|| val1 ||])
      In the first argument of ‘print’, namely ‘$$(process [|| val1 ||])’
      In the expression: print $$(process [|| val1 ||])
  |
8 | main = print $$(process [||val1||])
  |                 ^^^^^^^^^^^^^^^^^^

Code:

$ cat Dup.hs 
{-# language DuplicateRecordFields #-}
module Dup where

data Type1 = Con1 { field :: Int }

val1 = Con1 12

$ cat TH.hs 
{-# language DuplicateRecordFields #-}
{-# language TemplateHaskell #-}
{-# language NamedFieldPuns #-}
module TH where

import Language.Haskell.TH

import Dup

type Code a = Q (TExp a)

process
  :: Code Type1
  -> Code Int
process v1 = [|| case $$v1 of
    Con1{field} -> field
  ||]

$ cat Use.hs 
{-# language DuplicateRecordFields #-}
{-# language TemplateHaskell #-}
module Use where

import Dup
import TH

main = print $$(process [||val1||])

Expected behavior

Code works.

Environment

  • GHC version used: 8.10.5
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information