ApiAnnotations : BooleanFormula construction discards original
authorAlan Zimmerman <alan.zimm@gmail.com>
Mon, 11 May 2015 12:19:14 +0000 (14:19 +0200)
committerAlan Zimmerman <alan.zimm@gmail.com>
Mon, 11 May 2015 12:19:14 +0000 (14:19 +0200)
Summary:
The MINIMAL pragma is captured in the parser using a BooleanFormula.

The constructors (mkBool,mkAnd,mkOr) are smart and try to minimise the
boolean formula as it is constructed.

This discards the original information, making round tripping
impossible.

Note: there is another version which provides a more API Annotations
friendly version of the MINIMAL pragma, but this requires changes to
haddock, which will cause problems for 7.10.2.

See https://github.com/alanz/ghc/tree/wip/10287

Test Plan: ./validate

Reviewers: hvr, austin

Reviewed By: austin

Subscribers: bgamari, Fuuzetsu, thomie, mpickering

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

GHC Trac Issues: #10287

compiler/parser/Parser.y
testsuite/tests/ghc-api/annotations/.gitignore
testsuite/tests/ghc-api/annotations/Makefile
testsuite/tests/ghc-api/annotations/TestBoolFormula.hs [new file with mode: 0644]
testsuite/tests/ghc-api/annotations/all.T
testsuite/tests/ghc-api/annotations/boolFormula.hs [new file with mode: 0644]
testsuite/tests/ghc-api/annotations/boolFormula.stderr [new file with mode: 0644]
testsuite/tests/ghc-api/annotations/boolFormula.stdout [new file with mode: 0644]

index 3d9b2a3..5137e77 100644 (file)
@@ -50,7 +50,7 @@ import DynFlags
 
 -- compiler/utils
 import OrdList
-import BooleanFormula   ( BooleanFormula, mkAnd, mkOr, mkTrue, mkVar )
+import BooleanFormula   ( BooleanFormula(..), mkTrue )
 import FastString
 import Maybes           ( orElse )
 import Outputable
@@ -2750,16 +2750,16 @@ name_boolformula :: { ([AddAnn],BooleanFormula (Located RdrName)) }
         : name_boolformula_and                      { $1 }
         | name_boolformula_and '|' name_boolformula
                                              { ((mj AnnVbar $2:fst $1)++(fst $3)
-                                                ,mkOr [snd $1,snd $3]) }
+                                                ,Or [snd $1,snd $3]) }
 
 name_boolformula_and :: { ([AddAnn],BooleanFormula (Located RdrName)) }
         : name_boolformula_atom                             { $1 }
         | name_boolformula_atom ',' name_boolformula_and
-                  { ((mj AnnComma $2:fst $1)++(fst $3), mkAnd [snd $1,snd $3]) }
+                  { ((mj AnnComma $2:fst $1)++(fst $3), And [snd $1,snd $3]) }
 
 name_boolformula_atom :: { ([AddAnn],BooleanFormula (Located RdrName)) }
-        : '(' name_boolformula ')'  { ([mop $1,mcp $3],snd $2) }
-        | name_var                  { ([],mkVar $1) }
+        : '(' name_boolformula ')'  { ((mop $1:mcp $3:(fst $2)),snd $2) }
+        | name_var                  { ([],Var $1) }
 
 namelist :: { Located [Located RdrName] }
 namelist : name_var              { sL1 $1 [$1] }
index c7aa1e5..c181ef0 100644 (file)
@@ -9,6 +9,7 @@ clean:
        rm -f t10255 t10312
        rm -f t1037
        rm -f t10309
+       rm -f listcomps boolFormula
 
 annotations: 
        rm -f annotations.o annotations.hi
@@ -81,3 +82,10 @@ t10309:
        ./t10309 "`'$(TEST_HC)' $(TEST_HC_OPTS) --print-libdir | tr -d '\r'`"
 
 .PHONY: t10309
+
+boolFormula:
+       rm -f boolFormula.o boolFormula.hi
+       '$(TEST_HC)' $(TEST_HC_OPTS) --make -v0 -package ghc boolFormula
+       ./boolFormula "`'$(TEST_HC)' $(TEST_HC_OPTS) --print-libdir | tr -d '\r'`"
+
+.PHONY: clean annotations parseTree comments exampleTest listcomps boolFormula
diff --git a/testsuite/tests/ghc-api/annotations/TestBoolFormula.hs b/testsuite/tests/ghc-api/annotations/TestBoolFormula.hs
new file mode 100644 (file)
index 0000000..e945318
--- /dev/null
@@ -0,0 +1,26 @@
+module TestBoolFormula where
+
+class ManyOps a where
+    aOp :: a -> a -> Bool
+    bOp :: a -> a -> Bool
+    cOp :: a -> a -> Bool
+    dOp :: a -> a -> Bool
+    eOp :: a -> a -> Bool
+    fOp :: a -> a -> Bool
+    {-# MINIMAL  ( aOp)
+               | ( bOp   , cOp)
+               | ((dOp  |  eOp) , fOp)
+      #-}
+
+class Foo a where
+    bar :: a -> a -> Bool
+    foo :: a -> a -> Bool
+    baq :: a -> a -> Bool
+    baz :: a -> a -> Bool
+    quux :: a -> a -> Bool
+    {-# MINIMAL bar, (foo, baq | foo, quux) #-}
+
+instance Foo Int where
+    bar = undefined
+    baz = undefined
+    quux = undefined
index 81aec52..4709c2a 100644 (file)
@@ -10,3 +10,4 @@ test('T10280',      normal, run_command, ['$MAKE -s --no-print-directory T10280'
 test('T10312',      normal, run_command, ['$MAKE -s --no-print-directory t10312'])
 test('T10307',      normal, run_command, ['$MAKE -s --no-print-directory t10307'])
 test('T10309',      normal, run_command, ['$MAKE -s --no-print-directory t10309'])
+test('boolFormula', normal, run_command, ['$MAKE -s --no-print-directory boolFormula'])
diff --git a/testsuite/tests/ghc-api/annotations/boolFormula.hs b/testsuite/tests/ghc-api/annotations/boolFormula.hs
new file mode 100644 (file)
index 0000000..be23f8b
--- /dev/null
@@ -0,0 +1,107 @@
+{-# 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 "TestBoolFormula"
+
+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)
+
+       -- putStrLn (pp spans)
+           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)
diff --git a/testsuite/tests/ghc-api/annotations/boolFormula.stderr b/testsuite/tests/ghc-api/annotations/boolFormula.stderr
new file mode 100644 (file)
index 0000000..65cbf26
--- /dev/null
@@ -0,0 +1,17 @@
+
+TestBoolFormula.hs:3:1: Warning:
+    The MINIMAL pragma does not require:
+      ‘aOp’, ‘bOp’, ‘cOp’, ‘dOp’, ‘eOp’, and ‘fOp’
+    but there is no default implementation.
+    In the class declaration for ‘ManyOps’
+
+TestBoolFormula.hs:15:1: Warning:
+    The MINIMAL pragma does not require:
+      ‘baq’, ‘baz’, and ‘quux’
+    but there is no default implementation.
+    In the class declaration for ‘Foo’
+
+TestBoolFormula.hs:23:10: Warning:
+    No explicit implementation for
+      either (‘foo’ and ‘baq’) or ‘foo’
+    In the instance declaration for ‘Foo Int’
diff --git a/testsuite/tests/ghc-api/annotations/boolFormula.stdout b/testsuite/tests/ghc-api/annotations/boolFormula.stdout
new file mode 100644 (file)
index 0000000..1c7a943
--- /dev/null
@@ -0,0 +1,163 @@
+---Problems---------------------
+[
+]
+
+--------------------------------
+[
+(AK TestBoolFormula.hs:1:1 AnnModule = [TestBoolFormula.hs:1:1-6])
+
+(AK TestBoolFormula.hs:1:1 AnnWhere = [TestBoolFormula.hs:1:24-28])
+
+(AK TestBoolFormula.hs:(3,1)-(13,9) AnnClass = [TestBoolFormula.hs:3:1-5])
+
+(AK TestBoolFormula.hs:(3,1)-(13,9) AnnSemi = [TestBoolFormula.hs:15:1])
+
+(AK TestBoolFormula.hs:(3,1)-(13,9) AnnWhere = [TestBoolFormula.hs:3:17-21])
+
+(AK TestBoolFormula.hs:4:5-25 AnnDcolon = [TestBoolFormula.hs:4:9-10])
+
+(AK TestBoolFormula.hs:4:5-25 AnnSemi = [TestBoolFormula.hs:5:5])
+
+(AK TestBoolFormula.hs:4:12-25 AnnRarrow = [TestBoolFormula.hs:4:14-15])
+
+(AK TestBoolFormula.hs:4:17-25 AnnRarrow = [TestBoolFormula.hs:4:19-20])
+
+(AK TestBoolFormula.hs:5:5-25 AnnDcolon = [TestBoolFormula.hs:5:9-10])
+
+(AK TestBoolFormula.hs:5:5-25 AnnSemi = [TestBoolFormula.hs:6:5])
+
+(AK TestBoolFormula.hs:5:12-25 AnnRarrow = [TestBoolFormula.hs:5:14-15])
+
+(AK TestBoolFormula.hs:5:17-25 AnnRarrow = [TestBoolFormula.hs:5:19-20])
+
+(AK TestBoolFormula.hs:6:5-25 AnnDcolon = [TestBoolFormula.hs:6:9-10])
+
+(AK TestBoolFormula.hs:6:5-25 AnnSemi = [TestBoolFormula.hs:7:5])
+
+(AK TestBoolFormula.hs:6:12-25 AnnRarrow = [TestBoolFormula.hs:6:14-15])
+
+(AK TestBoolFormula.hs:6:17-25 AnnRarrow = [TestBoolFormula.hs:6:19-20])
+
+(AK TestBoolFormula.hs:7:5-25 AnnDcolon = [TestBoolFormula.hs:7:9-10])
+
+(AK TestBoolFormula.hs:7:5-25 AnnSemi = [TestBoolFormula.hs:8:5])
+
+(AK TestBoolFormula.hs:7:12-25 AnnRarrow = [TestBoolFormula.hs:7:14-15])
+
+(AK TestBoolFormula.hs:7:17-25 AnnRarrow = [TestBoolFormula.hs:7:19-20])
+
+(AK TestBoolFormula.hs:8:5-25 AnnDcolon = [TestBoolFormula.hs:8:9-10])
+
+(AK TestBoolFormula.hs:8:5-25 AnnSemi = [TestBoolFormula.hs:9:5])
+
+(AK TestBoolFormula.hs:8:12-25 AnnRarrow = [TestBoolFormula.hs:8:14-15])
+
+(AK TestBoolFormula.hs:8:17-25 AnnRarrow = [TestBoolFormula.hs:8:19-20])
+
+(AK TestBoolFormula.hs:9:5-25 AnnDcolon = [TestBoolFormula.hs:9:9-10])
+
+(AK TestBoolFormula.hs:9:5-25 AnnSemi = [TestBoolFormula.hs:10:5])
+
+(AK TestBoolFormula.hs:9:12-25 AnnRarrow = [TestBoolFormula.hs:9:14-15])
+
+(AK TestBoolFormula.hs:9:17-25 AnnRarrow = [TestBoolFormula.hs:9:19-20])
+
+(AK TestBoolFormula.hs:(10,5)-(13,9) AnnClose = [TestBoolFormula.hs:13:7-9])
+
+(AK TestBoolFormula.hs:(10,5)-(13,9) AnnCloseP = [TestBoolFormula.hs:10:23, TestBoolFormula.hs:11:31,
+ TestBoolFormula.hs:12:38, TestBoolFormula.hs:12:31])
+
+(AK TestBoolFormula.hs:(10,5)-(13,9) AnnComma = [TestBoolFormula.hs:11:26, TestBoolFormula.hs:12:33])
+
+(AK TestBoolFormula.hs:(10,5)-(13,9) AnnOpen = [TestBoolFormula.hs:10:5-15])
+
+(AK TestBoolFormula.hs:(10,5)-(13,9) AnnOpenP = [TestBoolFormula.hs:10:18, TestBoolFormula.hs:11:18,
+ TestBoolFormula.hs:12:18, TestBoolFormula.hs:12:19])
+
+(AK TestBoolFormula.hs:(10,5)-(13,9) AnnVbar = [TestBoolFormula.hs:11:16, TestBoolFormula.hs:12:16,
+ TestBoolFormula.hs:12:25])
+
+(AK TestBoolFormula.hs:(15,1)-(21,47) AnnClass = [TestBoolFormula.hs:15:1-5])
+
+(AK TestBoolFormula.hs:(15,1)-(21,47) AnnSemi = [TestBoolFormula.hs:23:1])
+
+(AK TestBoolFormula.hs:(15,1)-(21,47) AnnWhere = [TestBoolFormula.hs:15:13-17])
+
+(AK TestBoolFormula.hs:16:5-25 AnnDcolon = [TestBoolFormula.hs:16:9-10])
+
+(AK TestBoolFormula.hs:16:5-25 AnnSemi = [TestBoolFormula.hs:17:5])
+
+(AK TestBoolFormula.hs:16:12-25 AnnRarrow = [TestBoolFormula.hs:16:14-15])
+
+(AK TestBoolFormula.hs:16:17-25 AnnRarrow = [TestBoolFormula.hs:16:19-20])
+
+(AK TestBoolFormula.hs:17:5-25 AnnDcolon = [TestBoolFormula.hs:17:9-10])
+
+(AK TestBoolFormula.hs:17:5-25 AnnSemi = [TestBoolFormula.hs:18:5])
+
+(AK TestBoolFormula.hs:17:12-25 AnnRarrow = [TestBoolFormula.hs:17:14-15])
+
+(AK TestBoolFormula.hs:17:17-25 AnnRarrow = [TestBoolFormula.hs:17:19-20])
+
+(AK TestBoolFormula.hs:18:5-25 AnnDcolon = [TestBoolFormula.hs:18:9-10])
+
+(AK TestBoolFormula.hs:18:5-25 AnnSemi = [TestBoolFormula.hs:19:5])
+
+(AK TestBoolFormula.hs:18:12-25 AnnRarrow = [TestBoolFormula.hs:18:14-15])
+
+(AK TestBoolFormula.hs:18:17-25 AnnRarrow = [TestBoolFormula.hs:18:19-20])
+
+(AK TestBoolFormula.hs:19:5-25 AnnDcolon = [TestBoolFormula.hs:19:9-10])
+
+(AK TestBoolFormula.hs:19:5-25 AnnSemi = [TestBoolFormula.hs:20:5])
+
+(AK TestBoolFormula.hs:19:12-25 AnnRarrow = [TestBoolFormula.hs:19:14-15])
+
+(AK TestBoolFormula.hs:19:17-25 AnnRarrow = [TestBoolFormula.hs:19:19-20])
+
+(AK TestBoolFormula.hs:20:5-26 AnnDcolon = [TestBoolFormula.hs:20:10-11])
+
+(AK TestBoolFormula.hs:20:5-26 AnnSemi = [TestBoolFormula.hs:21:5])
+
+(AK TestBoolFormula.hs:20:13-26 AnnRarrow = [TestBoolFormula.hs:20:15-16])
+
+(AK TestBoolFormula.hs:20:18-26 AnnRarrow = [TestBoolFormula.hs:20:20-21])
+
+(AK TestBoolFormula.hs:21:5-47 AnnClose = [TestBoolFormula.hs:21:45-47])
+
+(AK TestBoolFormula.hs:21:5-47 AnnCloseP = [TestBoolFormula.hs:21:43])
+
+(AK TestBoolFormula.hs:21:5-47 AnnComma = [TestBoolFormula.hs:21:20, TestBoolFormula.hs:21:26,
+ TestBoolFormula.hs:21:37])
+
+(AK TestBoolFormula.hs:21:5-47 AnnOpen = [TestBoolFormula.hs:21:5-15])
+
+(AK TestBoolFormula.hs:21:5-47 AnnOpenP = [TestBoolFormula.hs:21:22])
+
+(AK TestBoolFormula.hs:21:5-47 AnnVbar = [TestBoolFormula.hs:21:32])
+
+(AK TestBoolFormula.hs:(23,1)-(26,20) AnnInstance = [TestBoolFormula.hs:23:1-8])
+
+(AK TestBoolFormula.hs:(23,1)-(26,20) AnnSemi = [TestBoolFormula.hs:27:1])
+
+(AK TestBoolFormula.hs:(23,1)-(26,20) AnnWhere = [TestBoolFormula.hs:23:18-22])
+
+(AK TestBoolFormula.hs:24:5-19 AnnEqual = [TestBoolFormula.hs:24:9])
+
+(AK TestBoolFormula.hs:24:5-19 AnnFunId = [TestBoolFormula.hs:24:5-7])
+
+(AK TestBoolFormula.hs:24:5-19 AnnSemi = [TestBoolFormula.hs:25:5])
+
+(AK TestBoolFormula.hs:25:5-19 AnnEqual = [TestBoolFormula.hs:25:9])
+
+(AK TestBoolFormula.hs:25:5-19 AnnFunId = [TestBoolFormula.hs:25:5-7])
+
+(AK TestBoolFormula.hs:25:5-19 AnnSemi = [TestBoolFormula.hs:26:5])
+
+(AK TestBoolFormula.hs:26:5-20 AnnEqual = [TestBoolFormula.hs:26:10])
+
+(AK TestBoolFormula.hs:26:5-20 AnnFunId = [TestBoolFormula.hs:26:5-8])
+
+(AK <no location info> AnnEofPos = [TestBoolFormula.hs:27:1])
+]
+