Skip to content

Template Haskell allows building invalid record fields/names

This is not really a bug, more like a unintuitive behaviour.

Due to a bug in my definitions, I was passing a name like "opTestDelay " (note extra space) to a TH splice builder, which ended up with:

data OpCode
  = OpTestDelay {opDelayDuration  :: Double,
                 opDelayOnMaster :: Bool,
                 opDelayOnNodes :: [Ganeti.Types.NonEmptyString],
                 opDelayRepeat :: Ganeti.Types.NonNegative Int}

Note the double space around the first record field. This results in the actual accessor functions having the space in the name, which makes them unusable from normal code.

This seems to be allowed as well in other TH constructs:

λ> runQ $ return (ValD (VarP (mkName "a ") ) (NormalB (LitE (IntegerL 5))) [])
ValD (VarP a ) (NormalB (LitE (IntegerL 5))) []

I think that names should not be allowed to contain invalid identifiers (that would make them non-usable in normal Haskell code), but I'm not sure - maybe TH is designed to allow you to shoot yourself in the foot indeed. Anyway, opening this bug just in case.

Tested and behaves the same both on 6.12 and 7.6.

Trac metadata
Trac field Value
Version 7.6.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Template Haskell
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information