API Annotations : ExprWithTySig processing discards annotated spans
authorAlan Zimmerman <alan.zimm@gmail.com>
Tue, 14 Apr 2015 12:32:40 +0000 (07:32 -0500)
committerAustin Seipp <austin@well-typed.com>
Tue, 14 Apr 2015 12:33:25 +0000 (07:33 -0500)
In RdrHsSyn.checkAPat the processing for ExprWithTySig is defined as

   ExprWithTySig e t _ -> do e <- checkLPat msg e
                             -- Pattern signatures are parsed as sigtypes,
                             -- but they aren't explicit forall points.  Hence
                             -- we have to remove the implicit forall here.
                             let t' = case t of
                                        L _ (HsForAllTy Implicit _ _
                                             (L _ []) ty) -> ty
                                        other -> other
                             return (SigPatIn e (mkHsWithBndrs t'))

The t' variable ends up losing its original SrcSpan in the first case
branch. This results in annotations becoming detached from the AST.

Reviewed By: austin

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

GHC Trac Issues: #10255

compiler/parser/RdrHsSyn.hs
testsuite/tests/ghc-api/annotations/.gitignore
testsuite/tests/ghc-api/annotations/Makefile
testsuite/tests/ghc-api/annotations/T10255.stderr [new file with mode: 0644]
testsuite/tests/ghc-api/annotations/T10255.stdout [new file with mode: 0644]
testsuite/tests/ghc-api/annotations/Test10255.hs [new file with mode: 0644]
testsuite/tests/ghc-api/annotations/all.T
testsuite/tests/ghc-api/annotations/t10255.hs [new file with mode: 0644]

index 6bf8bc3..06c6564 100644 (file)
@@ -847,7 +847,8 @@ checkAPat msg loc e0 = do
                                         L _ (HsForAllTy Implicit _ _
                                              (L _ []) ty) -> ty
                                         other -> other
-                             return (SigPatIn e (mkHsWithBndrs t'))
+                             return (SigPatIn e (mkHsWithBndrs
+                                                   (L (getLoc t) (HsParTy t'))))
 
    -- n+k patterns
    OpApp (L nloc (HsVar n)) (L _ (HsVar plus)) _
index 383fb26..08a6d49 100644 (file)
@@ -31,4 +31,9 @@ listcomps:
        '$(TEST_HC)' $(TEST_HC_OPTS) --make -v0 -package ghc listcomps
        ./listcomps "`'$(TEST_HC)' $(TEST_HC_OPTS) --print-libdir | tr -d '\r'`"
 
-.PHONY: clean annotations parseTree comments exampleTest listcomps
+t10255:
+       rm -f t10255.o t10255.hi
+       '$(TEST_HC)' $(TEST_HC_OPTS) --make -v0 -package ghc t10255
+       ./t10255 "`'$(TEST_HC)' $(TEST_HC_OPTS) --print-libdir | tr -d '\r'`"
+
+.PHONY: clean annotations parseTree comments exampleTest listcomps t10255
diff --git a/testsuite/tests/ghc-api/annotations/T10255.stderr b/testsuite/tests/ghc-api/annotations/T10255.stderr
new file mode 100644 (file)
index 0000000..be1a915
--- /dev/null
@@ -0,0 +1,3 @@
+
+Test10255.hs:1:14: Warning:
+    -XPatternSignatures is deprecated: use -XScopedTypeVariables or pragma {-# LANGUAGE ScopedTypeVariables #-} instead
diff --git a/testsuite/tests/ghc-api/annotations/T10255.stdout b/testsuite/tests/ghc-api/annotations/T10255.stdout
new file mode 100644 (file)
index 0000000..099ef54
--- /dev/null
@@ -0,0 +1,36 @@
+---Problems---------------------
+[
+(AK <no location info> AnnEofPos = [Test10255.hs:8:1])
+]
+
+--------------------------------
+[
+(AK Test10255.hs:1:1 AnnModule = [Test10255.hs:2:1-6])
+
+(AK Test10255.hs:1:1 AnnWhere = [Test10255.hs:2:18-22])
+
+(AK Test10255.hs:4:1-17 AnnImport = [Test10255.hs:4:1-6])
+
+(AK Test10255.hs:4:1-17 AnnSemi = [Test10255.hs:6:1])
+
+(AK Test10255.hs:(6,1)-(7,11) AnnEqual = [Test10255.hs:6:29])
+
+(AK Test10255.hs:(6,1)-(7,11) AnnFunId = [Test10255.hs:6:1-3])
+
+(AK Test10255.hs:(6,1)-(7,11) AnnSemi = [Test10255.hs:8:1])
+
+(AK Test10255.hs:6:5-27 AnnCloseP = [Test10255.hs:6:27])
+
+(AK Test10255.hs:6:5-27 AnnOpenP = [Test10255.hs:6:5])
+
+(AK Test10255.hs:6:6-26 AnnDcolon = [Test10255.hs:6:8-9])
+
+(AK Test10255.hs:6:11-26 AnnCloseP = [Test10255.hs:6:26])
+
+(AK Test10255.hs:6:11-26 AnnOpenP = [Test10255.hs:6:11])
+
+(AK Test10255.hs:6:12-25 AnnRarrow = [Test10255.hs:6:20-21])
+
+(AK <no location info> AnnEofPos = [Test10255.hs:8:1])
+]
+
diff --git a/testsuite/tests/ghc-api/annotations/Test10255.hs b/testsuite/tests/ghc-api/annotations/Test10255.hs
new file mode 100644 (file)
index 0000000..386452d
--- /dev/null
@@ -0,0 +1,7 @@
+{-# LANGUAGE PatternSignatures #-}
+module Test10255 where
+
+import Data.Maybe
+
+fob (f :: (Maybe t -> Int)) =
+  undefined
index 9dadf7a..ed888a3 100644 (file)
@@ -3,3 +3,4 @@ test('parseTree',   normal, run_command, ['$MAKE -s --no-print-directory parseTr
 test('comments',    normal, run_command, ['$MAKE -s --no-print-directory comments'])
 test('exampleTest', normal, run_command, ['$MAKE -s --no-print-directory exampleTest'])
 test('listcomps',   normal, run_command, ['$MAKE -s --no-print-directory listcomps'])
+test('T10255',      normal, run_command, ['$MAKE -s --no-print-directory t10255'])
diff --git a/testsuite/tests/ghc-api/annotations/t10255.hs b/testsuite/tests/ghc-api/annotations/t10255.hs
new file mode 100644 (file)
index 0000000..49c68e2
--- /dev/null
@@ -0,0 +1,106 @@
+{-# LANGUAGE RankNTypes #-}
+
+-- This program must be called with GHC's libdir as the single command line
+-- argument.
+module Main where
+
+-- import Data.Generics
+import Data.Data
+import Data.List
+import System.IO
+import GHC
+import BasicTypes
+import DynFlags
+import MonadUtils
+import Outputable
+import ApiAnnotation
+import Bag (filterBag,isEmptyBag)
+import System.Directory (removeFile)
+import System.Environment( getArgs )
+import qualified Data.Map as Map
+import qualified Data.Set as Set
+import Data.Dynamic ( fromDynamic,Dynamic )
+
+main::IO()
+main = do
+        [libdir] <- getArgs
+        testOneFile libdir "Test10255"
+
+testOneFile libdir fileName = do
+       ((anns,cs),p) <- runGhc (Just libdir) $ do
+                        dflags <- getSessionDynFlags
+                        setSessionDynFlags dflags
+                        let mn =mkModuleName fileName
+                        addTarget Target { targetId = TargetModule mn
+                                         , targetAllowObjCode = True
+                                         , targetContents = Nothing }
+                        load LoadAllTargets
+                        modSum <- getModSummary mn
+                        p <- parseModule modSum
+                        return (pm_annotations p,p)
+
+       let spans = Set.fromList $ getAllSrcSpans (pm_parsed_source p)
+
+           problems = filter (\(s,a) -> not (Set.member s spans))
+                             $ getAnnSrcSpans (anns,cs)
+       putStrLn "---Problems---------------------"
+       putStrLn (intercalate "\n" [showAnns $ Map.fromList $ map snd problems])
+       putStrLn "--------------------------------"
+       putStrLn (intercalate "\n" [showAnns anns])
+
+    where
+      getAnnSrcSpans :: ApiAnns -> [(SrcSpan,(ApiAnnKey,[SrcSpan]))]
+      getAnnSrcSpans (anns,_) = map (\a@((ss,_),_) -> (ss,a)) $ Map.toList anns
+
+      getAllSrcSpans :: (Data t) => t -> [SrcSpan]
+      getAllSrcSpans ast = everything (++) ([] `mkQ` getSrcSpan) ast
+        where
+          getSrcSpan :: SrcSpan -> [SrcSpan]
+          getSrcSpan ss = [ss]
+
+
+showAnns anns = "[\n" ++ (intercalate "\n"
+   $ map (\((s,k),v)
+              -> ("(AK " ++ pp s ++ " " ++ show k ++" = " ++ pp v ++ ")\n"))
+   $ Map.toList anns)
+    ++ "]\n"
+
+pp a = showPpr unsafeGlobalDynFlags a
+
+
+-- ---------------------------------------------------------------------
+
+-- Copied from syb for the test
+
+
+-- | Generic queries of type \"r\",
+--   i.e., take any \"a\" and return an \"r\"
+--
+type GenericQ r = forall a. Data a => a -> r
+
+
+-- | Make a generic query;
+--   start from a type-specific case;
+--   return a constant otherwise
+--
+mkQ :: ( Typeable a
+       , Typeable b
+       )
+    => r
+    -> (b -> r)
+    -> a
+    -> r
+(r `mkQ` br) a = case cast a of
+                        Just b  -> br b
+                        Nothing -> r
+
+
+
+-- | Summarise all nodes in top-down, left-to-right order
+everything :: (r -> r -> r) -> GenericQ r -> GenericQ r
+
+-- Apply f to x to summarise top-level node;
+-- use gmapQ to recurse into immediate subterms;
+-- use ordinary foldl to reduce list of intermediate results
+
+everything k f x = foldl k (f x) (gmapQ (everything k f) x)