Skip to content

Commit bcc18e8

Browse files
committed
Add input constraints in Shake.hs
1 parent 050586d commit bcc18e8

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Development.IDE.Core.InputPath where
22

33
import Development.IDE.Graph.Internal.RuleInput (Input)
4-
import Development.IDE (NormalizedFilePath)
4+
import Language.LSP.Protocol.Types (NormalizedFilePath)
55

66
newtype InputPath (i :: Input) =
7-
InputPath { unInputPath :: NormalizedFilePath }
7+
InputPath { unInputPath :: NormalizedFilePath } deriving Eq

ghcide/src/Development/IDE/Core/Shake.hs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ import System.FilePath hiding (makeRelative)
181181
import System.IO.Unsafe (unsafePerformIO)
182182
import System.Time.Extra
183183
import UnliftIO (MonadUnliftIO (withRunInIO))
184-
import Development.IDE.Graph.Internal.RuleInput (RuleInput, HasInput)
184+
import Development.IDE.Graph.Internal.RuleInput (RuleInput, HasInput, Input(NoFile))
185185

186186

187187
data Log
@@ -345,7 +345,7 @@ type WithProgressFunc = forall a.
345345
type WithIndefiniteProgressFunc = forall a.
346346
T.Text -> LSP.ProgressCancellable -> IO a -> IO a
347347

348-
type GetStalePersistent = InputPath i -> IdeAction (Maybe (Dynamic,PositionDelta,Maybe Int32))
348+
type GetStalePersistent = NormalizedFilePath -> IdeAction (Maybe (Dynamic,PositionDelta,Maybe Int32))
349349

350350
getShakeExtras :: Action ShakeExtras
351351
getShakeExtras = do
@@ -387,7 +387,7 @@ getPluginConfigAction plId = do
387387
-- This is called when we don't already have a result, or computing the rule failed.
388388
-- The result of this function will always be marked as 'stale', and a 'proper' rebuild of the rule will
389389
-- be queued if the rule hasn't run before.
390-
addPersistentRule :: IdeRule k i is v => k -> (InputPath i -> IdeAction (Maybe (v,PositionDelta,Maybe Int32))) -> Rules ()
390+
addPersistentRule :: IdeRule k i is v => k -> (NormalizedFilePath -> IdeAction (Maybe (v,PositionDelta,Maybe Int32))) -> Rules ()
391391
addPersistentRule k getVal = do
392392
ShakeExtras{persistentKeys} <- getShakeExtrasRules
393393
void $ liftIO $ atomically $ modifyTVar' persistentKeys $ insertKeyMap (newKey k) (fmap (fmap (first3 toDyn)) . getVal)
@@ -456,7 +456,7 @@ getIdeOptionsIO ide = do
456456
-- | Return the most recent, potentially stale, value and a PositionMapping
457457
-- for the version of that value.
458458
lastValueIO :: IdeRule k i is v => ShakeExtras -> k -> InputPath i -> IO (Maybe (v, PositionMapping))
459-
lastValueIO s@ShakeExtras{positionMapping,persistentKeys,state} k file = do
459+
lastValueIO s@ShakeExtras{positionMapping,persistentKeys,state} k (InputPath file) = do
460460

461461
let readPersistent
462462
| IdeTesting testing <- ideTesting s -- Don't read stale persistent values in tests
@@ -593,7 +593,7 @@ setValues :: IdeRule k i is v
593593
-> Value v
594594
-> Vector FileDiagnostic
595595
-> STM ()
596-
setValues state key file val diags =
596+
setValues state key (InputPath file) val diags =
597597
STM.insert (ValueWithDiagnostics (fmap toDyn val) diags) (toKey key file) state
598598

599599

@@ -618,7 +618,7 @@ getValues ::
618618
k ->
619619
InputPath i ->
620620
STM (Maybe (Value v, Vector FileDiagnostic))
621-
getValues state key file = do
621+
getValues state key (InputPath file) = do
622622
STM.lookup (toKey key file) state >>= \case
623623
Nothing -> pure Nothing
624624
Just (ValueWithDiagnostics v diagsV) -> do
@@ -1094,7 +1094,7 @@ useWithStaleFast' key file = do
10941094

10951095
-- Async trigger the key to be built anyway because we want to
10961096
-- keep updating the value in the key.
1097-
waitValue <- delayedAction $ mkDelayedAction ("C:" ++ show key ++ ":" ++ fromNormalizedFilePath file) Debug $ use key file
1097+
waitValue <- delayedAction $ mkDelayedAction ("C:" ++ show key ++ ":" ++ fromNormalizedFilePath (unInputPath file)) Debug $ use key file
10981098

10991099
s@ShakeExtras{state} <- askShake
11001100
r <- liftIO $ atomicallyNamed "useStateFast" $ getValues state key file
@@ -1113,8 +1113,8 @@ useWithStaleFast' key file = do
11131113
res <- lastValueIO s key file
11141114
pure $ FastResult res waitValue
11151115

1116-
useNoFile :: IdeRule k i is v => k -> Action (Maybe v)
1117-
useNoFile key = use key emptyFilePath
1116+
useNoFile :: forall k is v. IdeRule k NoFile is v => k -> Action (Maybe v)
1117+
useNoFile key = use key (InputPath @NoFile emptyFilePath)
11181118

11191119
-- Requests a rule if available.
11201120
--
@@ -1125,8 +1125,8 @@ useNoFile key = use key emptyFilePath
11251125
use_ :: IdeRule k i is v => k -> InputPath i -> Action v
11261126
use_ key file = runIdentity <$> uses_ key (Identity file)
11271127

1128-
useNoFile_ :: IdeRule k i is v => k -> Action v
1129-
useNoFile_ key = use_ key emptyFilePath
1128+
useNoFile_ :: forall k is v. IdeRule k NoFile is v => k -> Action v
1129+
useNoFile_ key = use_ key (InputPath @NoFile emptyFilePath)
11301130

11311131
-- |Plural version of `use_`
11321132
--
@@ -1144,21 +1144,21 @@ uses_ key files = do
11441144
-- | Plural version of 'use'
11451145
uses :: (Traversable f, IdeRule k i is v)
11461146
=> k -> f (InputPath i) -> Action (f (Maybe v))
1147-
uses key files = fmap (\(A value) -> currentValue value) <$> apply (fmap (Q . (key,)) files)
1147+
uses key files = fmap (\(A value) -> currentValue value) <$> apply (fmap (Q . (key,) . unInputPath) files)
11481148

11491149
-- | Return the last computed result which might be stale.
11501150
usesWithStale :: (Traversable f, IdeRule k i is v)
11511151
=> k -> f (InputPath i) -> Action (f (Maybe (v, PositionMapping)))
11521152
usesWithStale key files = do
1153-
_ <- apply (fmap (Q . (key,)) files)
1153+
_ <- apply (fmap (Q . (key,) . unInputPath) files)
11541154
-- We don't look at the result of the 'apply' since 'lastValue' will
11551155
-- return the most recent successfully computed value regardless of
11561156
-- whether the rule succeeded or not.
11571157
traverse (lastValue key) files
11581158

11591159
useWithoutDependency :: IdeRule k i is v
11601160
=> k -> InputPath i -> Action (Maybe v)
1161-
useWithoutDependency key file =
1161+
useWithoutDependency key (InputPath file) =
11621162
(\(Identity (A value)) -> currentValue value) <$> applyWithoutDependency (Identity (Q (key, file)))
11631163

11641164
data RuleBody k i v
@@ -1172,7 +1172,8 @@ data RuleBody k i v
11721172

11731173
-- | Define a new Rule with early cutoff
11741174
defineEarlyCutoff
1175-
:: IdeRule k i is v
1175+
:: forall k i is v
1176+
. IdeRule k i is v
11761177
=> Recorder (WithPriority Log)
11771178
-> RuleBody k i v
11781179
-> Rules ()
@@ -1181,35 +1182,35 @@ defineEarlyCutoff recorder (Rule op) = addRule $ \(Q (key, file)) (old :: Maybe
11811182
let diagnostics ver diags = do
11821183
traceDiagnostics diags
11831184
updateFileDiagnostics recorder file ver (newKey key) extras . map (\(_,y,z) -> (y,z)) $ diags
1184-
defineEarlyCutoff' diagnostics (==) key file old mode $ const $ op key file
1185+
defineEarlyCutoff' diagnostics (==) key (InputPath @i file) old mode $ const $ op key (InputPath @i file)
11851186
defineEarlyCutoff recorder (RuleNoDiagnostics op) = addRule $ \(Q (key, file)) (old :: Maybe BS.ByteString) mode -> otTracedAction key file mode traceA $ \traceDiagnostics -> do
11861187
let diagnostics _ver diags = do
11871188
traceDiagnostics diags
11881189
mapM_ (logWith recorder Warning . LogDefineEarlyCutoffRuleNoDiagHasDiag) diags
1189-
defineEarlyCutoff' diagnostics (==) key file old mode $ const $ second (mempty,) <$> op key file
1190+
defineEarlyCutoff' diagnostics (==) key (InputPath @i file) old mode $ const $ second (mempty,) <$> op key (InputPath @i file)
11901191
defineEarlyCutoff recorder RuleWithCustomNewnessCheck{..} =
11911192
addRule $ \(Q (key, file)) (old :: Maybe BS.ByteString) mode ->
11921193
otTracedAction key file mode traceA $ \ traceDiagnostics -> do
11931194
let diagnostics _ver diags = do
11941195
traceDiagnostics diags
11951196
mapM_ (logWith recorder Warning . LogDefineEarlyCutoffRuleCustomNewnessHasDiag) diags
1196-
defineEarlyCutoff' diagnostics newnessCheck key file old mode $
1197-
const $ second (mempty,) <$> build key file
1197+
defineEarlyCutoff' diagnostics newnessCheck key (InputPath @i file) old mode $
1198+
const $ second (mempty,) <$> build key (InputPath @i file)
11981199
defineEarlyCutoff recorder (RuleWithOldValue op) = addRule $ \(Q (key, file)) (old :: Maybe BS.ByteString) mode -> otTracedAction key file mode traceA $ \traceDiagnostics -> do
11991200
extras <- getShakeExtras
12001201
let diagnostics ver diags = do
12011202
traceDiagnostics diags
12021203
updateFileDiagnostics recorder file ver (newKey key) extras . map (\(_,y,z) -> (y,z)) $ diags
1203-
defineEarlyCutoff' diagnostics (==) key file old mode $ op key file
1204+
defineEarlyCutoff' diagnostics (==) key (InputPath @i file) old mode $ op key (InputPath @i file)
12041205

1205-
defineNoFile :: IdeRule k i is v => Recorder (WithPriority Log) -> (k -> Action v) -> Rules ()
1206+
defineNoFile :: forall k i is v. IdeRule k i is v => Recorder (WithPriority Log) -> (k -> Action v) -> Rules ()
12061207
defineNoFile recorder f = defineNoDiagnostics recorder $ \k file -> do
1207-
if file == emptyFilePath then do res <- f k; return (Just res) else
1208+
if file == (InputPath @i emptyFilePath) then do res <- f k; return (Just res) else
12081209
fail $ "Rule " ++ show k ++ " should always be called with the empty string for a file"
12091210

1210-
defineEarlyCutOffNoFile :: IdeRule k i is v => Recorder (WithPriority Log) -> (k -> Action (BS.ByteString, v)) -> Rules ()
1211-
defineEarlyCutOffNoFile recorder f = defineEarlyCutoff recorder $ RuleNoDiagnostics $ \k (InputPath file) -> do
1212-
if file == emptyFilePath then do (hashString, res) <- f k; return (Just hashString, Just res) else
1211+
defineEarlyCutOffNoFile :: forall k i is v. IdeRule k i is v => Recorder (WithPriority Log) -> (k -> Action (BS.ByteString, v)) -> Rules ()
1212+
defineEarlyCutOffNoFile recorder f = defineEarlyCutoff recorder $ RuleNoDiagnostics $ \k file -> do
1213+
if file == (InputPath @i emptyFilePath) then do (hashString, res) <- f k; return (Just hashString, Just res) else
12131214
fail $ "Rule " ++ show k ++ " should always be called with the empty string for a file"
12141215

12151216
defineEarlyCutoff'

hls-graph/src/Development/IDE/Graph/Internal/RuleInput.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type ValidInputs = [Input]
66
data Input
77
= ProjectHaskellFile
88
| DependencyHaskellFile
9+
| NoFile
910

1011
type family RuleInput k :: ValidInputs
1112

0 commit comments

Comments
 (0)