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

#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

#9871: Template Haskell declaration splice causes subsequent declarations to fall
out of scope
-------------------------------------+-------------------------------------
Reporter: | Owner:
RyanGlScott | Status: new
Type: bug | Milestone:
Priority: normal | Version: 7.8.3
Component: Template | Keywords:
Haskell | Architecture: Unknown/Multiple
Resolution: | Difficulty: Unknown
Operating System: | Blocked By:
Unknown/Multiple | Related Tickets:
Type of failure: GHC |
rejects valid program |
Test Case: |
Blocking: |
Differential Revisions: |
-------------------------------------+-------------------------------------
Comment (by Richard Eisenberg

#9871: Template Haskell declaration splice causes subsequent declarations to fall out of scope -------------------------------------+------------------------------------- Reporter: | Owner: RyanGlScott | Status: closed Type: bug | Milestone: Priority: normal | Version: 7.8.3 Component: | Keywords: Documentation | Architecture: Unknown/Multiple Resolution: fixed | Difficulty: Unknown Operating System: | Blocked By: Unknown/Multiple | Related Tickets: Type of failure: GHC | rejects valid program | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Changes (by goldfire): * status: new => closed * resolution: => fixed * component: Template Haskell => Documentation Comment: This is by design, but I've tweaked the TH documentation to make this more apparent. A top-level declaration splice (your `$(deriveJSON ... )`) breaks up the mutual recursion among all top-level declarations. Code above the splice can't see code below it. This allows code within the splice to see some definitions in your file, and code after the splice to see the definitions in the splice itself. Thanks for reporting! -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9871#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9871: Template Haskell declaration splice causes subsequent declarations to fall out of scope -------------------------------------+------------------------------------- Reporter: | Owner: RyanGlScott | Status: closed Type: bug | Milestone: Priority: normal | Version: 7.8.3 Component: | Keywords: Documentation | Architecture: Unknown/Multiple Resolution: fixed | Difficulty: Unknown Operating System: | Blocked By: Unknown/Multiple | Related Tickets: Type of failure: GHC | rejects valid program | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Comment (by RyanGlScott): Just to clarify, which of the above workarounds would you recommend? Should I move all TH declaration splices to the top or bottom of the module? Something else? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9871#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9871: Template Haskell declaration splice causes subsequent declarations to fall out of scope -------------------------------------+------------------------------------- Reporter: | Owner: RyanGlScott | Status: closed Type: bug | Milestone: Priority: normal | Version: 7.8.3 Component: | Keywords: Documentation | Architecture: Unknown/Multiple Resolution: fixed | Difficulty: Unknown Operating System: | Blocked By: Unknown/Multiple | Related Tickets: Type of failure: GHC | rejects valid program | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Comment (by goldfire): It's up to you and what works with your coding style. I can't really recommend one ordering over another. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9871#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC