on master help sign in
Simplify IDE rules (#708)

* Move the IdeResult term into the A data type * Nothing ever consults the errors stored in A, so stop storing them * Use the new Shake MonadFail Rules instance * Document the information in the Shake database * More documentation of the data in the Shake service * Change getValues to avoid getting the diagnostics * Avoid fmap over a pair, a bit weird

commit42461c44fee65b22cda89a44fc8bd35b133c87b4 authorNeil Mitchell <35463327+neil-da@users.noreply.github.com> dateThu 25 Apr 2019, 22:04 (8 years ago) parentbbdcbdd
4 files changed+51−36
src/Development/IDE/State/FileStore.hs+3−3 src/Development/IDE/State/RuleTypes.hs+11−12 src/Development/IDE/State/Rules.hs+3−3 src/Development/IDE/State/Shake.hs+34−18
src/Development/IDE/State/FileStore.hs+3−3
@@ -42,13 +42,13 @@ instance IsIdeGlobal GlobalDirtyFiles
4242
4343
4444-- | Get the modification time of a file.
45−type instance RuleResult GetModificationTime = IdeResult UTCTime
45+type instance RuleResult GetModificationTime = UTCTime
4646
4747-- | Get the contents of a file, either dirty (if the buffer is modified) or from disk.
48−type instance RuleResult GetFileContents = IdeResult (UTCTime, StringBuffer)
48+type instance RuleResult GetFileContents = (UTCTime, StringBuffer)
4949
5050-- | Does the file exist.
51−type instance RuleResult GetFileExists = IdeResult Bool
51+type instance RuleResult GetFileExists = Bool
5252
5353
5454data GetFileExists = GetFileExists
src/Development/IDE/State/RuleTypes.hs+11−12
@@ -27,7 +27,6 @@ import GHC.Generics (Generic)
2727import "ghc-lib" GHC
2828import "ghc-lib-parser" Module
2929
30−import Development.IDE.State.Shake
3130import Development.IDE.Types.SpanInfo
3231
3332
@@ -36,44 +35,44 @@ import Development.IDE.Types.SpanInfo
3635-- Foo* means Foo for me and Foo+
3736
3837-- | Kick off things
39−type instance RuleResult OfInterest = IdeResult ()
38+type instance RuleResult OfInterest = ()
4039
4140-- | The parse tree for the file using GetFileContents
42−type instance RuleResult GetParsedModule = IdeResult ParsedModule
41+type instance RuleResult GetParsedModule = ParsedModule
4342
4443-- | The dependency information produced by following the imports recursively.
4544-- This rule will succeed even if there is an error, e.g., a module could not be located,
4645-- a module could not be parsed or an import cycle.
47−type instance RuleResult GetDependencyInformation = IdeResult DependencyInformation
46+type instance RuleResult GetDependencyInformation = DependencyInformation
4847
4948-- | Transitive module and pkg dependencies based on the information produced by GetDependencyInformation.
5049-- This rule is also responsible for calling ReportImportCycles for each file in the transitive closure.
51−type instance RuleResult GetDependencies = IdeResult TransitiveDependencies
50+type instance RuleResult GetDependencies = TransitiveDependencies
5251
5352-- | The type checked version of this file, requires TypeCheck+
54−type instance RuleResult TypeCheck = IdeResult TcModuleResult
53+type instance RuleResult TypeCheck = TcModuleResult
5554
5655-- | The result of loading a module from a package.
57−type instance RuleResult LoadPackage = IdeResult LoadPackageResult
56+type instance RuleResult LoadPackage = LoadPackageResult
5857
5958-- | Information about what spans occur where, requires TypeCheck
60−type instance RuleResult GetSpanInfo = IdeResult [SpanInfo]
59+type instance RuleResult GetSpanInfo = [SpanInfo]
6160
6261-- | Convert to Core, requires TypeCheck*
63−type instance RuleResult GenerateCore = IdeResult GhcModule
62+type instance RuleResult GenerateCore = GhcModule
6463
6564-- | We capture the subset of `DynFlags` that is computed by package initialization in a rule to
6665-- make session initialization cheaper by reusing it.
67−type instance RuleResult GeneratePackageState = IdeResult Compile.PackageState
66+type instance RuleResult GeneratePackageState = Compile.PackageState
6867
6968-- | Resolve the imports in a module to the list of either external packages or absolute file paths
7069-- for modules in the same package.
71−type instance RuleResult GetLocatedImports = IdeResult [(Located ModuleName, Maybe Import)]
70+type instance RuleResult GetLocatedImports = [(Located ModuleName, Maybe Import)]
7271
7372-- | This rule is used to report import cycles. It depends on GetDependencyInformation.
7473-- We cannot report the cycles directly from GetDependencyInformation since
7574-- we can only report diagnostics for the current file.
76−type instance RuleResult ReportImportCycles = IdeResult ()
75+type instance RuleResult ReportImportCycles = ()
7776
7877
7978data OfInterest = OfInterest
src/Development/IDE/State/Rules.hs+3−3
@@ -58,8 +58,8 @@ toIdeResultNew :: Either [Diagnostic] v -> IdeResult v
5858toIdeResultNew = either (, Nothing) (([],) . Just)
5959
6060-- Convert to a legacy Ide result but dropping dependencies
61−toIdeResultSilent :: IdeResult v -> Either [Diagnostic] v
62−toIdeResultSilent (_, val) = maybe (Left []) Right val
61+toIdeResultSilent :: Maybe v -> Either [Diagnostic] v
62+toIdeResultSilent val = maybe (Left []) Right val
6363
6464
6565defineNoFile :: IdeRule k v => (k -> Action v) -> Rules ()
@@ -198,7 +198,7 @@ rawDependencyInformation f = go (Set.singleton f) Map.empty Map.empty
198198 Nothing -> pure (RawDependencyInformation modGraph pkgs)
199199 Just (f, fs) -> do
200200 importsOrErr <- lift $ use GetLocatedImports f
201− case snd importsOrErr of
201+ case importsOrErr of
202202 Nothing ->
203203 let modGraph' = Map.insert f (Left ModuleParseError) modGraph
204204 in go fs modGraph' pkgs
src/Development/IDE/State/Shake.hs+34−18
@@ -6,6 +6,20 @@
66{-# LANGUAGE ConstraintKinds #-}
77
88-- | A Shake implementation of the compiler service.
9+--
10+-- There are two primary locations where data lives, and both of
11+-- these contain much the same data:
12+--
13+-- * The Shake database (inside 'shakeDb') stores a map of shake keys
14+-- to shake values. In our case, these are all of type 'Q' to 'A'.
15+-- During a single run all the values in the Shake database are consistent
16+-- so are used in conjunction with each other, e.g. in 'uses'.
17+--
18+-- * The 'Values' type stores a map of keys to values. These values are
19+-- always stored as real Haskell values, whereas Shake serialises all 'A' values
20+-- between runs. To deserialise a Shake value, we just consult Values.
21+-- Additionally, Values can be used in an inconsistent way, for example
22+-- useStale.
923module Development.IDE.State.Shake(
1024 IdeState,
1125 IdeRule, IdeResult,
@@ -73,9 +87,8 @@ getShakeExtras = do
7387
7488getShakeExtrasRules :: Rules ShakeExtras
7589getShakeExtrasRules = do
76− -- We'd like to use binding, but no MonadFail Rules https://github.com/ndmitchell/shake/issues/643
77− x <- getShakeExtraRules @ShakeExtras
78− return $ fromMaybe (error "Can't find ShakeExtras, serious error") x
90+ Just x <- getShakeExtraRules @ShakeExtras
91+ return x
7992
8093
8194
@@ -135,7 +148,7 @@ instance Hashable Key where
135148type IdeResult v = ([Diagnostic], Maybe v)
136149
137150type IdeRule k v =
138− ( Shake.RuleResult k ~ IdeResult v
151+ ( Shake.RuleResult k ~ v
139152 , Shake.ShakeValue k
140153 , Show v
141154 , Typeable v
@@ -184,15 +197,17 @@ setValues :: IdeRule k v
184197 -> IO (Maybe [Diagnostic], [Diagnostic]) -- ^ (before, after)
185198setValues state key file val = modifyVar state $ \inVal -> do
186199 let k = Key key
187− outVal = Map.insertWith Map.union file (Map.singleton k $ fmap toDyn <$> val) inVal
200+ outVal = Map.insertWith Map.union file (Map.singleton k $ second (fmap toDyn) val) inVal
188201 f = concatMap fst . Map.elems
189202 return (outVal, (f <$> Map.lookup file inVal, f $ outVal Map.! file))
190203
191−getValues :: forall k v. IdeRule k v => Var Values -> k -> FilePath -> IO (Maybe (IdeResult v))
192−getValues state key file = flip fmap (readVar state) $ \vs -> do
193− f <- Map.lookup file vs
194− k <- Map.lookup (Key key) f
195− pure $ fmap (fromJust . fromDynamic) <$> k
204+getValues :: forall k v. IdeRule k v => Var Values -> k -> FilePath -> IO (Maybe (Maybe v))
205+getValues state key file = do
206+ vs <- readVar state
207+ return $ do
208+ f <- Map.lookup file vs
209+ v <- Map.lookup (Key key) f
210+ pure $ fmap (fromJust . fromDynamic @v) $ snd v
196211
197212-- | Open a 'IdeState', should be shut using 'shakeShut'.
198213shakeOpen :: (Event -> IO ()) -- ^ diagnostic handler
@@ -235,9 +250,8 @@ shakeRun IdeState{shakeExtras=ShakeExtras{..}, ..} acts = modifyVar shakeAbort $
235250useStale
236251 :: IdeRule k v
237252 => IdeState -> k -> FilePath -> IO (Maybe v)
238−useStale IdeState{shakeExtras=ShakeExtras{state}} k fp = do
239− v <- getValues state k fp
240− return $ maybe Nothing snd v
253+useStale IdeState{shakeExtras=ShakeExtras{state}} k fp =
254+ join <$> getValues state k fp
241255
242256
243257getAllDiagnostics :: IdeState -> IO [Diagnostic]
@@ -262,7 +276,7 @@ define
262276define op = defineEarlyCutoff $ \k v -> (Nothing,) <$> op k v
263277
264278use :: IdeRule k v
265− => k -> FilePath -> Action (IdeResult v)
279+ => k -> FilePath -> Action (Maybe v)
266280use key file = head <$> uses key [file]
267281
268282use_ :: IdeRule k v => k -> FilePath -> Action v
@@ -271,7 +285,7 @@ use_ key file = head <$> uses_ key [file]
271285uses_ :: IdeRule k v => k -> [FilePath] -> Action [v]
272286uses_ key files = do
273287 res <- uses key files
274− case mapM snd res of
288+ case sequence res of
275289 Nothing -> liftIO $ throwIO BadDependency
276290 Just v -> return v
277291
@@ -307,17 +321,19 @@ instance Show k => Show (Q k) where
307321
308322-- | Invariant: the 'v' must be in normal form (fully evaluated).
309323-- Otherwise we keep repeatedly 'rnf'ing values taken from the Shake database
310−data A v = A v (Maybe BS.ByteString)
324+data A v = A (Maybe v) (Maybe BS.ByteString)
311325 deriving Show
312326
313327instance NFData (A v) where rnf (A v x) = v `seq` rnf x
314328
329+-- In the Shake database we only store one type of key/result pairs,
330+-- namely Q (question) / A (answer).
315331type instance RuleResult (Q k) = A (RuleResult k)
316332
317333
318334-- | Compute the value
319335uses :: IdeRule k v
320− => k -> [FilePath] -> Action [IdeResult v]
336+ => k -> [FilePath] -> Action [Maybe v]
321337uses key files = map (\(A value _) -> value) <$> apply (map (Q . (key,)) files)
322338
323339defineEarlyCutoff
@@ -353,7 +369,7 @@ defineEarlyCutoff op = addBuiltinRule noLint noIdentity $ \(Q (key, file)) old m
353369 return $ RunResult
354370 (if eq then ChangedRecomputeSame else ChangedRecomputeDiff)
355371 (wrap bs)
356− $ A res bs
372+ $ A (snd res) bs
357373 where
358374 wrap = maybe BS.empty (BS.cons '_')
359375 unwrap x = if BS.null x then Nothing else Just $ BS.tail x