[GHC] #9871: Template Haskell declaration splice causes subsequent declarations to fall out of scope

GHC ghc-devs at haskell.org
Sun Dec 7 22:30:05 UTC 2014


#9871: Template Haskell declaration splice causes subsequent declarations to fall
out of scope
-------------------------------------+-------------------------------------
       Reporter:  RyanGlScott        |                   Owner:
           Type:  bug                |                  Status:  new
       Priority:  normal             |               Milestone:
      Component:  Template Haskell   |                 Version:  7.8.3
       Keywords:                     |        Operating System:
   Architecture:  Unknown/Multiple   |  Unknown/Multiple
     Difficulty:  Unknown            |         Type of failure:  GHC
     Blocked By:                     |  rejects valid program
Related Tickets:                     |               Test Case:
                                     |                Blocking:
                                     |  Differential Revisions:
-------------------------------------+-------------------------------------
 When using Template Haskell to splice in {{{instance}}} declarations, it
 is possible to cause identifiers following it to fall out of scope:

 {{{#!hs
 -- DeriveJSON.hs
 {-# LANGUAGE TemplateHaskell #-}
 module DeriveJSON where

 import Data.Aeson.TH

 data ADT = ADT

 instance Show ADT
     where show = adtFun

 $(deriveJSON defaultOptions ''ADT)

 adtFun :: ADT -> String
 adtFun = undefined
 }}}

 {{{ghc DeriveJSON.hs}}} gives the following error:

 {{{
 [1 of 1] Compiling DeriveJSON       ( DeriveJSON.hs, DeriveJSON.o )

 DeriveJSON.hs:10:18: Not in scope: ‘adtFun’
 }}}

 Since {{{deriveJSON}}} simply returns a {{{Q [Dec]}}}, I don't see why
 this code shouldn't compile as is.

 I can find two workarounds at the moment:
 * Move the {{{instance Show ADT}}} declaration after the {{{$(deriveJSON
 defaultOptions ''ADT)}}} line.
 * Move the {{{$(deriveJSON defaultOptions ''ADT)}}} line after the
 {{{adtFun}}} function declaration.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/9871>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list