Skip to content

Commit e168bc9

Browse files
committed
treewide: Expr.Types: add PositionSet
1 parent a65d6bb commit e168bc9

File tree

8 files changed

+41
-40
lines changed

8 files changed

+41
-40
lines changed

src/Nix/Builtins.hs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ import Nix.Effects
5959
import Nix.Effects.Basic ( fetchTarball )
6060
import Nix.Exec
6161
import Nix.Expr.Types
62-
import Nix.Expr.Types.Annotated
6362
import qualified Nix.Eval as Eval
6463
import Nix.Frames
6564
import Nix.Json
@@ -441,7 +440,7 @@ hasAttrNix
441440
hasAttrNix x y =
442441
do
443442
(coerce -> key) <- fromStringNoContext =<< fromValue x
444-
(aset, _) <- fromValue @(AttrSet (NValue t f m), KeyMap SourcePos) y
443+
(aset, _) <- fromValue @(AttrSet (NValue t f m), PositionSet) y
445444

446445
toValue $ M.member key aset
447446

@@ -457,7 +456,7 @@ getAttrNix
457456
getAttrNix x y =
458457
do
459458
(coerce -> key) <- fromStringNoContext =<< fromValue x
460-
(aset, _) <- fromValue @(AttrSet (NValue t f m), KeyMap SourcePos) y
459+
(aset, _) <- fromValue @(AttrSet (NValue t f m), PositionSet) y
461460

462461
attrsetGet key aset
463462

@@ -477,7 +476,7 @@ unsafeGetAttrPosNix nvX nvY =
477476
maybe
478477
(pure nvNull)
479478
toValue
480-
(M.lookup (stringIgnoreContext ns) apos)
479+
(M.lookup @VarName (coerce $ stringIgnoreContext ns) apos)
481480
_xy -> throwError $ ErrorCall $ "Invalid types for builtins.unsafeGetAttrPosNix: " <> show _xy
482481

483482
-- This function is a bit special in that it doesn't care about the contents
@@ -1074,10 +1073,10 @@ removeAttrsNix
10741073
-> m (NValue t f m)
10751074
removeAttrsNix set v =
10761075
do
1077-
(m, p) <- fromValue @(AttrSet (NValue t f m), KeyMap SourcePos) set
1076+
(m, p) <- fromValue @(AttrSet (NValue t f m), PositionSet) set
10781077
(nsToRemove :: [NixString]) <- fromValue $ Deeper v
1079-
toRemove <- traverse (fmap (coerce @Text @VarName) . fromStringNoContext) nsToRemove
1080-
toValue (go @VarName m toRemove, go @Text p (coerce @VarName <$> toRemove))
1078+
(coerce -> toRemove) <- traverse fromStringNoContext nsToRemove
1079+
toValue (go m toRemove, go p toRemove)
10811080
where
10821081
go :: forall k v . (Eq k, Hashable k) => HashMap k v -> [k] -> HashMap k v
10831082
go = foldl' (flip M.delete)
@@ -1090,8 +1089,8 @@ intersectAttrsNix
10901089
-> m (NValue t f m)
10911090
intersectAttrsNix set1 set2 =
10921091
do
1093-
(s1, p1) <- fromValue @(AttrSet (NValue t f m), KeyMap SourcePos) set1
1094-
(s2, p2) <- fromValue @(AttrSet (NValue t f m), KeyMap SourcePos) set2
1092+
(s1, p1) <- fromValue @(AttrSet (NValue t f m), PositionSet) set1
1093+
(s2, p2) <- fromValue @(AttrSet (NValue t f m), PositionSet) set2
10951094

10961095
pure $ nvSet (p2 `M.intersection` p1) (s2 `M.intersection` s1)
10971096

src/Nix/Convert.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ instance ( Convertible e t f m
285285
fromValue = fromMayToDeeperValue TSet
286286

287287
instance Convertible e t f m
288-
=> FromValue (AttrSet (NValue t f m), KeyMap SourcePos) m
288+
=> FromValue (AttrSet (NValue t f m), PositionSet) m
289289
(NValue' t f m (NValue t f m)) where
290290

291291
fromValueMay =
@@ -299,7 +299,7 @@ instance Convertible e t f m
299299
instance ( Convertible e t f m
300300
, FromValue a m (NValue t f m)
301301
)
302-
=> FromValue (AttrSet a, KeyMap SourcePos) m
302+
=> FromValue (AttrSet a, PositionSet) m
303303
(Deeper (NValue' t f m (NValue t f m))) where
304304

305305
fromValueMay =
@@ -400,12 +400,12 @@ instance (Convertible e t f m, ToValue a m (NValue t f m))
400400
stub
401401

402402
instance Convertible e t f m
403-
=> ToValue (AttrSet (NValue t f m), KeyMap SourcePos) m
403+
=> ToValue (AttrSet (NValue t f m), PositionSet) m
404404
(NValue' t f m (NValue t f m)) where
405405
toValue (s, p) = pure $ nvSet' p s
406406

407407
instance (Convertible e t f m, ToValue a m (NValue t f m))
408-
=> ToValue (AttrSet a, KeyMap SourcePos) m
408+
=> ToValue (AttrSet a, PositionSet) m
409409
(Deeper (NValue' t f m (NValue t f m))) where
410410
toValue (s, p) =
411411
liftA2 (\ v s -> Deeper $ nvSet' s v)

src/Nix/Eval.hs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ class (Show v, Monad m) => MonadEval v m where
6060
evalListElem :: [m v] -> Int -> m v -> m v
6161
evalList :: [v] -> m v
6262
evalSetElem :: AttrSet (m v) -> Text -> m v -> m v
63-
evalSet :: AttrSet v -> KeyMap SourcePos -> m v
63+
evalSet :: AttrSet v -> PositionSet -> m v
6464
evalRecSetElem :: AttrSet (m v) -> Text -> m v -> m v
65-
evalRecSet :: AttrSet v -> KeyMap SourcePos -> m v
65+
evalRecSet :: AttrSet v -> PositionSet -> m v
6666
evalLetElem :: Text -> m v -> m v
6767
evalLet :: m v -> m v
6868
-}
@@ -76,8 +76,8 @@ type MonadNixEval v m
7676
, ToValue Bool m v
7777
, ToValue [v] m v
7878
, FromValue NixString m v
79-
, ToValue (AttrSet v, KeyMap SourcePos) m v
80-
, FromValue (AttrSet v, KeyMap SourcePos) m v
79+
, ToValue (AttrSet v, PositionSet) m v
80+
, FromValue (AttrSet v, PositionSet) m v
8181
)
8282

8383
data EvalFrame m v
@@ -209,7 +209,7 @@ evalWithAttrSet aset body = do
209209
-- sure the action it evaluates is to force a thunk, so its value is only
210210
-- computed once.
211211
deferredAset <- defer $ withScopes scope aset
212-
let attrSet = fst <$> (fromValue @(AttrSet v, KeyMap SourcePos) =<< demand deferredAset)
212+
let attrSet = fst <$> (fromValue @(AttrSet v, PositionSet) =<< demand deferredAset)
213213

214214
pushWeakScope attrSet body
215215

@@ -219,9 +219,9 @@ attrSetAlter
219219
=> [VarName]
220220
-> SourcePos
221221
-> AttrSet (m v)
222-
-> KeyMap SourcePos
222+
-> PositionSet
223223
-> m v
224-
-> m (AttrSet (m v), KeyMap SourcePos)
224+
-> m (AttrSet (m v), PositionSet)
225225
attrSetAlter [] _ _ _ _ = evalError @v $ ErrorCall "invalid selector with no components"
226226
attrSetAlter (k : ks) pos m p val =
227227
bool
@@ -230,7 +230,7 @@ attrSetAlter (k : ks) pos m p val =
230230
(recurse mempty mempty)
231231
(\x ->
232232
do
233-
(st, sp) <- fromValue @(AttrSet v, KeyMap SourcePos) =<< x
233+
(st, sp) <- fromValue @(AttrSet v, PositionSet) =<< x
234234
recurse (demand <$> st) sp
235235
)
236236
(M.lookup k m)
@@ -243,7 +243,7 @@ attrSetAlter (k : ks) pos m p val =
243243
(\(st', _) ->
244244
(M.insert
245245
k
246-
(toValue @(AttrSet v, KeyMap SourcePos) =<< (, mempty) <$> sequence st')
246+
(toValue @(AttrSet v, PositionSet) =<< (, mempty) <$> sequence st')
247247
m
248248
, M.insert (coerce k) pos p
249249
)
@@ -293,7 +293,7 @@ evalBinds
293293
. MonadNixEval v m
294294
=> Bool
295295
-> [Binding (m v)]
296-
-> m (AttrSet v, KeyMap SourcePos)
296+
-> m (AttrSet v, PositionSet)
297297
evalBinds recursive binds =
298298
do
299299
scope <- currentScopes :: m (Scopes m v)
@@ -304,7 +304,7 @@ evalBinds recursive binds =
304304
buildResult
305305
:: Scopes m v
306306
-> [([VarName], SourcePos, m v)]
307-
-> m (AttrSet v, KeyMap SourcePos)
307+
-> m (AttrSet v, PositionSet)
308308
buildResult scope bindings =
309309
do
310310
(s, p) <- foldM insert (mempty, mempty) bindings
@@ -331,7 +331,7 @@ evalBinds recursive binds =
331331
pure $
332332
(\ (k, v) ->
333333
( [k]
334-
, fromMaybe pos $ M.lookup @Text (coerce k) p'
334+
, fromMaybe pos $ M.lookup k p'
335335
, demand v
336336
)
337337
) <$> M.toList o'
@@ -349,7 +349,7 @@ evalBinds recursive binds =
349349
processAttrSetKeys (h :| t) =
350350
maybe
351351
-- Empty attrset - return a stub.
352-
(pure ( mempty, nullPos, toValue @(AttrSet v, KeyMap SourcePos) (mempty, mempty)) )
352+
(pure ( mempty, nullPos, toValue @(AttrSet v, PositionSet) (mempty, mempty)) )
353353
(\ k ->
354354
list
355355
-- No more keys in the attrset - return the result
@@ -387,7 +387,7 @@ evalBinds recursive binds =
387387
(withScopes scope $ lookupVar key)
388388
(\ s ->
389389
do
390-
(attrset, _) <- fromValue @(AttrSet v, KeyMap SourcePos) =<< s
390+
(attrset, _) <- fromValue @(AttrSet v, PositionSet) =<< s
391391

392392
clearScopes @v $ pushScope attrset $ lookupVar key
393393
)
@@ -423,7 +423,7 @@ evalSelect aset attr =
423423

424424
case x' of
425425
Nothing -> pure $ Left (x, path)
426-
Just (s :: AttrSet v, p :: KeyMap SourcePos)
426+
Just (s :: AttrSet v, p :: PositionSet)
427427
| Just t <- M.lookup k s ->
428428
do
429429
list
@@ -490,7 +490,7 @@ buildArgument params arg =
490490
Param name -> M.singleton name <$> argThunk
491491
ParamSet s isVariadic m ->
492492
do
493-
(args, _) <- fromValue @(AttrSet v, KeyMap SourcePos) =<< arg
493+
(args, _) <- fromValue @(AttrSet v, PositionSet) =<< arg
494494
let
495495
inject =
496496
maybe

src/Nix/Exec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ nvListP p l = addProvenance p $ nvList l
8686
nvSetP
8787
:: MonadCited t f m
8888
=> Provenance m (NValue t f m)
89-
-> KeyMap SourcePos
89+
-> PositionSet
9090
-> AttrSet (NValue t f m)
9191
-> NValue t f m
9292
nvSetP p x s = addProvenance p $ nvSet x s

src/Nix/Expr/Types.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ instance FromJSON Pos where
104104
parseJSON = fmap mkPos . parseJSON
105105
instance FromJSON SourcePos
106106

107+
-- | Holds file positionng information for abstrations.
108+
-- A type synonym for @HashMap VarName SourcePos@.
109+
type PositionSet = HashMap VarName SourcePos
110+
107111

108112
-- * Components of Nix expressions
109113

src/Nix/Lint.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ instance ToValue [Symbolic m] m (Symbolic m) where
273273

274274
instance FromValue NixString m (Symbolic m) where
275275

276-
instance FromValue (AttrSet (Symbolic m), KeyMap SourcePos) m (Symbolic m) where
276+
instance FromValue (AttrSet (Symbolic m), PositionSet) m (Symbolic m) where
277277

278-
instance ToValue (AttrSet (Symbolic m), KeyMap SourcePos) m (Symbolic m) where
278+
instance ToValue (AttrSet (Symbolic m), PositionSet) m (Symbolic m) where
279279

280280
instance (MonadThunkId m, MonadAtomicRef m, MonadCatch m)
281281
=> MonadValue (Symbolic m) m where

src/Nix/Type/Infer.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ import qualified Nix.Eval as Eval
5656
, evalWithAttrSet
5757
)
5858
import Nix.Expr.Types
59-
import Nix.Expr.Types.Annotated
6059
import Nix.Fresh
6160
import Nix.String
6261
import Nix.Scope
@@ -335,7 +334,7 @@ instance
335334
instance
336335
MonadInfer m
337336
=> FromValue ( AttrSet (Judgment s)
338-
, KeyMap SourcePos
337+
, PositionSet
339338
) (InferT s m) (Judgment s)
340339
where
341340
fromValueMay (Judgment _ _ (TSet _ xs)) =
@@ -350,7 +349,7 @@ instance
350349
<=< fromValueMay
351350

352351
instance MonadInfer m
353-
=> ToValue (AttrSet (Judgment s), KeyMap SourcePos)
352+
=> ToValue (AttrSet (Judgment s), PositionSet)
354353
(InferT s m) (Judgment s) where
355354
toValue (xs, _) =
356355
liftA3

src/Nix/Value.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import Lens.Family2.TH ( makeTraversals
4242
)
4343
import Nix.Atoms
4444
import Nix.Expr.Types
45-
import Nix.Expr.Types.Annotated
4645
import Nix.String
4746
import Nix.Thunk
4847

@@ -124,8 +123,8 @@ data NValueF p m r
124123
-- Quite frequently actions/processing happens with values
125124
-- (for example - forcing of values & recreation of the monad),
126125
-- but SourcePos does not change then
127-
-- That would be good to flip all 'AttrSet.* KeyMap SourcePos'
128-
| NVSetF (AttrSet r) (KeyMap SourcePos)
126+
-- That would be good to flip all 'AttrSet.* PositionSet'
127+
| NVSetF (AttrSet r) PositionSet
129128
| NVClosureF (Params ()) (p -> m r)
130129
-- ^ A function is a closed set of parameters representing the "call
131130
-- signature", used at application time to check the type of arguments
@@ -445,7 +444,7 @@ nvList' = NValue' . pure . NVListF
445444

446445
-- | Haskell key-value to the Nix key-value,
447446
nvSet' :: Applicative f
448-
=> KeyMap SourcePos
447+
=> PositionSet
449448
-> AttrSet r
450449
-> NValue' t f m r
451450
-- 2021-07-16: NOTE: that the arguments are flipped.
@@ -628,7 +627,7 @@ nvList = Free . nvList'
628627

629628

630629
nvSet :: Applicative f
631-
=> KeyMap SourcePos
630+
=> PositionSet
632631
-> AttrSet (NValue t f m)
633632
-> NValue t f m
634633
nvSet p s = Free $ nvSet' p s

0 commit comments

Comments
 (0)