Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#6005 closed bug (fixed)

Template Haskell disallows use of promoted data constructor in same splice

Reported by: goldfire Owned by:
Priority: normal Milestone:
Component: Template Haskell Version: 7.5
Keywords: DataKinds PolyKinds Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Difficulty: Unknown
Test Case: th/T6005, T6005a Blocked By:
Blocking: Related Tickets:

Description

The following code fails to compile under GHC 7.5.20120413:

{-# LANGUAGE TemplateHaskell, DataKinds, PolyKinds #-}

$( [d|
  data Nat = Zero | Succ Nat
  data Proxy a = Proxy
  foo :: Proxy Zero
  foo = Proxy
  |])

The error is:

The exact Name `Zero_a3N3' is not in scope
  Probable cause: you used a unique name (NameU), perhaps via newName,
  in Template Haskell, but did not bind it
  If that's it, then -ddump-splices might be useful

Using -ddump-splices shows that the same internal name is used for the data constructor at declaration and usage.

The problem seems to be that a data constructor declared within a splice cannot be used as a promoted type in that same splice. Separating the splices works.

Change History (5)

comment:1 Changed 2 years ago by simonpj@…

commit 2a1190431218f838797be3c09084e22dc131c58a

Author: Simon Peyton Jones <simonpj@microsoft.com>
Date:   Wed Apr 25 12:55:41 2012 +0100

    Fix looking up of Exact RdrNames, fixes Trac #6005
    
    See Note [Splicing Exact names] in RnEnv.

 compiler/rename/RnEnv.lhs |   43 ++++++++++++++++++++++++++++++++++---------
 1 files changed, 34 insertions(+), 9 deletions(-)

comment:2 Changed 2 years ago by simonpj

  • Difficulty set to Unknown
  • Resolution set to fixed
  • Status changed from new to closed
  • Test Case set to th/T6005

Thanks for pointing me to this. Fixed.

comment:3 Changed 2 years ago by goldfire

  • Resolution fixed deleted
  • Status changed from closed to new

Thanks for applying this fix. Unfortunately, the following case still fails:

$( [d|
  data Nat = Zero | Succ Nat deriving Show
  data Proxy a = Proxy
  foo :: Proxy Zero
  foo = Proxy
  |])

The only difference between this case and the original is the deriving Show.

comment:4 Changed 2 years ago by simonpj

  • Resolution set to fixed
  • Status changed from new to closed
  • Test Case changed from th/T6005 to th/T6005, T6005a

This turns out to be unrelated to data kinds etc. Instead it is simply to do with the difficulty of type-checking Template Haskell brackets, something that led me to propose a complete overhaul (not yet done).

In this case we are generating the derived code for the bracket when we typecheck the bracket. This is silly because it's sure to type check; and it goes wrong too for reasons that are not intersting.

I have brutally simply suppressed all 'deriving' processing inside TH brackets. It'll all become irrelevant with the overhaul.

comment:5 Changed 2 years ago by simonpj@…

commit d50cb3eda3912df0d75a8177f04783689b21dc9b

Author: Simon Peyton Jones <simonpj@microsoft.com>
Date:   Thu Apr 26 13:47:54 2012 +0100

    Do not generate derived instances in TH brackets
    
    See Note [Deriving inside TH brackets] in TcInstDcls
    Fixes Trac #6005 (again)

 compiler/typecheck/TcInstDcls.lhs |   23 ++++++++++++++++++++++-
 1 files changed, 22 insertions(+), 1 deletions(-)
Note: See TracTickets for help on using tickets.