Fix handling of unbound constructor names in TH #14627
authorMichael Sloan <mgsloan@gmail.com>
Thu, 12 Jul 2018 14:07:28 +0000 (10:07 -0400)
committerBen Gamari <ben@smart-cactus.org>
Thu, 12 Jul 2018 19:04:05 +0000 (15:04 -0400)
Also adds a comment to UnboundVarE clarifying that it also is used for
unbound constructor identifiers, since that isn't very clear from the
name.

Test Plan: testsuite/tests/th/T14627.hs

Reviewers: goldfire, bgamari

Reviewed By: goldfire

Subscribers: rwbarton, thomie, carter

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

compiler/hsSyn/Convert.hs
libraries/template-haskell/Language/Haskell/TH/Syntax.hs
testsuite/tests/th/T14627.hs [new file with mode: 0644]
testsuite/tests/th/T14627.stderr [new file with mode: 0644]
testsuite/tests/th/all.T

index c64cb7c..84e4594 100644 (file)
@@ -913,7 +913,11 @@ cvtl e = wrapL (cvt e)
                                            flds
                               ; return $ mkRdrRecordUpd e' flds' }
     cvt (StaticE e)      = fmap (HsStatic noExt) $ cvtl e
-    cvt (UnboundVarE s)  = do { s' <- vName s; return $ HsVar noExt (noLoc s') }
+    cvt (UnboundVarE s)  = do -- Use of 'vcName' here instead of 'vName' is
+                              -- important, because UnboundVarE may contain
+                              -- constructor names - see #14627.
+                              { s' <- vcName s
+                              ; return $ HsVar noExt (noLoc s') }
     cvt (LabelE s)       = do { return $ HsOverLabel noExt Nothing (fsLit s) }
 
 {- Note [Dropping constructors]
index 9665c65..f5f60c3 100644 (file)
@@ -1620,7 +1620,12 @@ data Exp
   | RecConE Name [FieldExp]            -- ^ @{ T { x = y, z = w } }@
   | RecUpdE Exp [FieldExp]             -- ^ @{ (f x) { z = w } }@
   | StaticE Exp                        -- ^ @{ static e }@
-  | UnboundVarE Name                   -- ^ @{ _x }@ (hole)
+  | UnboundVarE Name                   -- ^ @{ _x }@
+                                       --
+                                       -- This is used for holes or unresolved
+                                       -- identifiers in AST quotes. Note that
+                                       -- it could either have a variable name
+                                       -- or constructor name.
   | LabelE String                      -- ^ @{ #x }@ ( Overloaded label )
   deriving( Show, Eq, Ord, Data, Generic )
 
diff --git a/testsuite/tests/th/T14627.hs b/testsuite/tests/th/T14627.hs
new file mode 100644 (file)
index 0000000..aebf6bd
--- /dev/null
@@ -0,0 +1,6 @@
+{-# LANGUAGE TemplateHaskell #-}
+import Language.Haskell.TH.Syntax
+
+[d| f = Bool |] >>= addTopDecls >> return []
+
+main = return ()
diff --git a/testsuite/tests/th/T14627.stderr b/testsuite/tests/th/T14627.stderr
new file mode 100644 (file)
index 0000000..1db6488
--- /dev/null
@@ -0,0 +1,2 @@
+
+T14627.hs:4:1: error: Data constructor not in scope: Bool
index d55d415..b3f72c8 100644 (file)
@@ -422,3 +422,4 @@ test('T15331', normal, compile, ['-v0 -ddump-splices -dsuppress-uniques'])
 test('T9693', expect_broken(9693), ghci_script, ['T9693.script'])
 test('T14471', normal, compile, [''])
 test('TH_rebindableAdo', normal, compile, [''])
+test('T14627', normal, compile_fail, [''])