Opened 4 years ago

Closed 4 years ago

#10596 closed bug (fixed)

Template Haskell : getQ and putQ doesn't work

Reported by: kiripon Owned by:
Priority: normal Milestone: 7.10.3
Component: Template Haskell Version: 7.10.1
Keywords: getQ putQ Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case: th/T10596
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D1026
Wiki Page:

Description

Functions getQ and putQ in the module Language.Haskell.TH.Syntax do not work.

Following code is an example of this problem. The variable x should be (Just B), but x is Nothing.

{-# LANGUAGE TemplateHaskell #-}
module X where
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
do
  putQ (100 :: Int)
  x <- getQ :: Q (Maybe Int)

  -- It should print "Just 100" but "Nothing"
  runIO $ print x
  return []

As a result, I get following output on compile.

$ ghc -fforce-recomp X.hs
[1 of 1] Compiling X                ( X.hs, X.o )
Nothing

Change History (10)

comment:1 Changed 4 years ago by ezyang

Bug is quite simple (wrong type is used for lookup), fix on the way...

comment:2 Changed 4 years ago by kiripon

I fixed this bug and checked work correctly.

diff --git a/compiler/typecheck/TcSplice.hs b/compiler/typecheck/TcSplice.hs
index a7363d8..2bcb78c 100644
--- a/compiler/typecheck/TcSplice.hs
+++ b/compiler/typecheck/TcSplice.hs
@@ -836,7 +836,7 @@ instance TH.Quasi (IOEnv (Env TcGblEnv TcLclEnv)) where
   qGetQ = do
       th_state_var <- fmap tcg_th_state getGblEnv
       th_state <- readTcRef th_state_var
-      let x = Map.lookup (typeOf x) th_state >>= fromDynamic
+      let x = Map.lookup (typeOf $ fromJust x) th_state >>= fromDynamic
       return x
 
   qPutQ x = do

diff --git a/compiler/typecheck/TcSplice.hs b/compiler/typecheck/TcSplice.hs
kiripon% ghc -fforce-recomp Y.hs
[1 of 1] Compiling Y                ( Y.hs, Y.o )
Nothing
Nothing
kiripon% $HOME/opt/local/ghc-head/bin/ghc -fforce-recomp Y.hs                                                                       
[1 of 1] Compiling Y                ( Y.hs, Y.o )
Just 100
Just 100
kiripon% cat Y.hs
{-# LANGUAGE TemplateHaskell #-}
module Y where
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
-- splice for testing getQ and putQ
do
  putQ (100 :: Int) :: Q ()
  x <- getQ :: Q (Maybe Int)
  runIO $ print x -- prints Nothing
  return []
do
  x <- getQ :: Q (Maybe Int)
  runIO $ print x -- prints Nothing
  return []
kiripon%  

comment:3 Changed 4 years ago by ezyang

Differential Rev(s): Phab:D1026

comment:4 Changed 4 years ago by Ben Gamari <ben@…>

In 00c8d4d551472940303437be1496bf23b882273b/ghc:

Fix #10596 by looking up 'Int' not 'Maybe Int' in the map.


Test Plan: validate

Reviewers: goldfire, austin, simonpj, bgamari

Reviewed By: bgamari

Subscribers: simonpj, rwbarton, thomie, bgamari

Differential Revision: https://phabricator.haskell.org/D1026

GHC Trac Issues: #10596

comment:5 Changed 4 years ago by bgamari

Status: newmerge

comment:6 Changed 4 years ago by bgamari

Milestone: 7.10.3

comment:7 Changed 4 years ago by thomie

Status: mergenew
Test Case: th/T10596

This test is failing on Windows, and also on Travis (https://s3.amazonaws.com/archive.travis-ci.org/jobs/70789702/log.txt).

 Actual stderr output differs from expected:
    --- ./th/T10596.stderr.normalised               2015-07-13 19:47:37.667428464 +0000
    +++ ./th/T10596.comp.stderr.normalised          2015-07-13 19:47:37.667428464 +0000
    @@ -1 +0,0 @@
    -Just 100
    \ No newline at end of file
    *** unexpected failure for T10596(normal)

Note that Windows and Travis don't build dynamic libraries.

comment:8 Changed 4 years ago by Joachim Breitner <mail@…>

In 0a402785c9c5ab4be9487518790d24af9d211a8b/ghc:

Flush stdout in test case for #10596

which might help, as it has helped with lots of other TH-related test
cases in the past.

comment:9 Changed 4 years ago by goldfire

Status: newmerge

According to Travis, this fix works. Thanks for thinking of this, Joachim!

comment:10 Changed 4 years ago by bgamari

Resolution: fixed
Status: mergeclosed

Merged to ghc-7.10.

Note: See TracTickets for help on using tickets.