@@ -6,6 +6,20 @@
6{-# LANGUAGE ConstraintKinds #-}
6{-# LANGUAGE ConstraintKinds #-}
8-- | A Shake implementation of the compiler service.
8-- | A Shake implementation of the compiler service.
10+-- There are two primary locations where data lives, and both of
11+-- these contain much the same data:
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'.
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
9module Development.IDE.State.Shake(
23module Development.IDE.State.Shake(
11 IdeRule, IdeResult,
25 IdeRule, IdeResult,
@@ -73,9 +87,8 @@ getShakeExtras = do
74getShakeExtrasRules :: Rules ShakeExtras
88getShakeExtrasRules :: Rules ShakeExtras
75getShakeExtrasRules = do
89getShakeExtrasRules = do
76− -- We'd like to use binding, but no MonadFail Rules https://github.com/ndmitchell/shake/issues/643
77− x <- getShakeExtraRules @ShakeExtras
90+ Just x <- getShakeExtraRules @ShakeExtras
78− return $ fromMaybe (error "Can't find ShakeExtras, serious error") x
91+ return x
@@ -135,7 +148,7 @@ instance Hashable Key where
135type IdeResult v = ([Diagnostic], Maybe v)
148type IdeResult v = ([Diagnostic], Maybe v)
137type IdeRule k v =
150type IdeRule k v =
138− ( Shake.RuleResult k ~ IdeResult v
151+ ( Shake.RuleResult k ~ v
139 , Shake.ShakeValue k
152 , Shake.ShakeValue k
141 , Typeable v
154 , Typeable v
@@ -184,15 +197,17 @@ setValues :: IdeRule k v
184 -> IO (Maybe [Diagnostic], [Diagnostic]) -- ^ (before, after)
197 -> IO (Maybe [Diagnostic], [Diagnostic]) -- ^ (before, after)
185setValues state key file val = modifyVar state $ \inVal -> do
198setValues state key file val = modifyVar state $ \inVal -> do
186 let k = Key key
199 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
188 f = concatMap fst . Map.elems
201 f = concatMap fst . Map.elems
189 return (outVal, (f <$> Map.lookup file inVal, f $ outVal Map.! file))
202 return (outVal, (f <$> Map.lookup file inVal, f $ outVal Map.! file))
191−getValues :: forall k v. IdeRule k v => Var Values -> k -> FilePath -> IO (Maybe (IdeResult v))
204+getValues :: forall k v. IdeRule k v => Var Values -> k -> FilePath -> IO (Maybe (Maybe v))
192−getValues state key file = flip fmap (readVar state) $ \vs -> do
205+getValues state key file = do
193− f <- Map.lookup file vs
208+ f <- Map.lookup file vs
194− k <- Map.lookup (Key key) f
209+ v <- Map.lookup (Key key) f
195− pure $ fmap (fromJust . fromDynamic) <$> k
210+ pure $ fmap (fromJust . fromDynamic @v) $ snd v
197-- | Open a 'IdeState', should be shut using 'shakeShut'.
212-- | Open a 'IdeState', should be shut using 'shakeShut'.
198shakeOpen :: (Event -> IO ()) -- ^ diagnostic handler
213shakeOpen :: (Event -> IO ()) -- ^ diagnostic handler
@@ -235,9 +250,8 @@ shakeRun IdeState{shakeExtras=ShakeExtras{..}, ..} acts = modifyVar shakeAbort $
236 :: IdeRule k v
251 :: IdeRule k v
237 => IdeState -> k -> FilePath -> IO (Maybe v)
252 => IdeState -> k -> FilePath -> IO (Maybe v)
238−useStale IdeState{shakeExtras=ShakeExtras{state}} k fp = do
253+useStale IdeState{shakeExtras=ShakeExtras{state}} k fp =
239− v <- getValues state k fp
254+ join <$> getValues state k fp
240− return $ maybe Nothing snd v
243getAllDiagnostics :: IdeState -> IO [Diagnostic]
257getAllDiagnostics :: IdeState -> IO [Diagnostic]
@@ -262,7 +276,7 @@ define
262define op = defineEarlyCutoff $ \k v -> (Nothing,) <$> op k v
276define op = defineEarlyCutoff $ \k v -> (Nothing,) <$> op k v
264use :: IdeRule k v
278use :: IdeRule k v
265− => k -> FilePath -> Action (IdeResult v)
279+ => k -> FilePath -> Action (Maybe v)
266use key file = head <$> uses key [file]
280use key file = head <$> uses key [file]
268use_ :: IdeRule k v => k -> FilePath -> Action v
282use_ :: IdeRule k v => k -> FilePath -> Action v
@@ -271,7 +285,7 @@ use_ key file = head <$> uses_ key [file]
271uses_ :: IdeRule k v => k -> [FilePath] -> Action [v]
285uses_ :: IdeRule k v => k -> [FilePath] -> Action [v]
272uses_ key files = do
286uses_ key files = do
273 res <- uses key files
287 res <- uses key files
274− case mapM snd res of
288+ case sequence res of
275 Nothing -> liftIO $ throwIO BadDependency
289 Nothing -> liftIO $ throwIO BadDependency
276 Just v -> return v
290 Just v -> return v
@@ -307,17 +321,19 @@ instance Show k => Show (Q k) where
308-- | Invariant: the 'v' must be in normal form (fully evaluated).
322-- | Invariant: the 'v' must be in normal form (fully evaluated).
309-- Otherwise we keep repeatedly 'rnf'ing values taken from the Shake database
323-- 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)
311 deriving Show
325 deriving Show
313instance NFData (A v) where rnf (A v x) = v `seq` rnf x
327instance NFData (A v) where rnf (A v x) = v `seq` rnf x
329+-- In the Shake database we only store one type of key/result pairs,
330+-- namely Q (question) / A (answer).
315type instance RuleResult (Q k) = A (RuleResult k)
331type instance RuleResult (Q k) = A (RuleResult k)
318-- | Compute the value
334-- | Compute the value
319uses :: IdeRule k v
335uses :: IdeRule k v
320− => k -> [FilePath] -> Action [IdeResult v]
336+ => k -> [FilePath] -> Action [Maybe v]
321uses key files = map (\(A value _) -> value) <$> apply (map (Q . (key,)) files)
337uses key files = map (\(A value _) -> value) <$> apply (map (Q . (key,)) files)
323defineEarlyCutoff
339defineEarlyCutoff
@@ -353,7 +369,7 @@ defineEarlyCutoff op = addBuiltinRule noLint noIdentity $ \(Q (key, file)) old m
353 return $ RunResult
369 return $ RunResult
354 (if eq then ChangedRecomputeSame else ChangedRecomputeDiff)
370 (if eq then ChangedRecomputeSame else ChangedRecomputeDiff)
355 (wrap bs)
371 (wrap bs)
356− $ A res bs
372+ $ A (snd res) bs
358 wrap = maybe BS.empty (BS.cons '_')
374 wrap = maybe BS.empty (BS.cons '_')
359 unwrap x = if BS.null x then Nothing else Just $ BS.tail x
375 unwrap x = if BS.null x then Nothing else Just $ BS.tail x