Skip to content

Typed holes in Template Haskell splices produce bewildering error messages

If you compile this program with GHC 8.4 or later:

{-# LANGUAGE TemplateHaskell #-}
module Bug where

foo :: String
foo = test

bar :: String
bar = $(_ "baz")

You'll be greeted with a rather strange error message:

$ /opt/ghc/8.4.3/bin/ghc Bug.hs
[1 of 1] Compiling Bug              ( Bug.hs, Bug.o )

Bug.hs:8:7: error:
    • GHC stage restriction:
        ‘foo’ is used in a top-level splice, quasi-quote, or annotation,
        and must be imported, not defined locally
    • In the untyped splice: $(_ "baz")
  |
8 | bar = $(_ "baz")
  |       ^^^^^^^^^^

foo has nothing do with how bar's RHS should be typechecked, so why is it being mentioned in the error message?

In contrast, GHC 8.2 and earlier gives you quite a nice error message:

$ /opt/ghc/8.2.2/bin/ghc Bug.hs
[1 of 1] Compiling Bug              ( Bug.hs, Bug.o )

Bug.hs:8:9: error:
    • Found hole: _ :: [Char] -> Language.Haskell.TH.Lib.ExpQ
    • In the expression: _
      In the expression: _ "baz"
      In the untyped splice: $(_ "baz")
  |
8 | bar = $(_ "baz")
  |         ^

Tritlo, my hunch is that the valid hole fits stuff is the culprit here. Do you think that perhaps when building the subsumption graph, we are trying to check the hole's type against that of foo, which causes the stage restriction error? If so, do you think it is possible to work around this?

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