Skip to content

Commit

Permalink
(#1040) Utils: add & use {isPresent, handlePresence}
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-Latukha authored Jan 21, 2022
2 parents 9c971ed + 4899bc8 commit 33dbeff
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 70 deletions.
2 changes: 1 addition & 1 deletion data/nix
Submodule nix updated 49 files
+1 −0 doc/manual/src/release-notes/rl-next.md
+3 −3 src/libexpr/eval.hh
+22 −0 src/libexpr/flake/flakeref.cc
+22 −19 src/libexpr/lexer.l
+1 −1 src/libexpr/nixexpr.cc
+31 −17 src/libexpr/parser.y
+6 −3 src/libexpr/primops.cc
+14 −7 src/libexpr/symbol-table.hh
+5 −5 src/libfetchers/tarball.cc
+12 −12 src/libstore/binary-cache-store.cc
+5 −4 src/libstore/binary-cache-store.hh
+1 −1 src/libstore/build/derivation-goal.cc
+3 −3 src/libstore/build/local-derivation-goal.cc
+7 −7 src/libstore/daemon.cc
+3 −3 src/libstore/export-import.cc
+10 −10 src/libstore/filetransfer.cc
+4 −4 src/libstore/filetransfer.hh
+4 −4 src/libstore/http-binary-cache-store.cc
+1 −1 src/libstore/legacy-ssh-store.cc
+7 −9 src/libstore/local-fs-store.cc
+2 −1 src/libstore/local-fs-store.hh
+2 −2 src/libstore/local-store.cc
+4 −4 src/libstore/nar-accessor.cc
+1 −1 src/libstore/nar-accessor.hh
+16 −14 src/libstore/remote-fs-accessor.cc
+1 −2 src/libstore/remote-fs-accessor.hh
+1 −1 src/libstore/remote-store.cc
+1 −1 src/libstore/s3-binary-cache-store.cc
+3 −1 src/libstore/s3.hh
+2 −2 src/libstore/store-api.hh
+4 −4 src/libutil/compression.cc
+2 −2 src/libutil/compression.hh
+1 −1 src/libutil/error.hh
+4 −4 src/libutil/serialise.cc
+10 −9 src/libutil/serialise.hh
+14 −14 src/libutil/tests/compression.cc
+5 −3 src/libutil/util.cc
+1 −1 src/nix-build/nix-build.cc
+3 −3 src/nix/add-to-store.cc
+1 −1 src/nix/cat.cc
+0 −9 src/nix/flake.md
+1 −1 src/nix/ls.cc
+4 −4 src/nix/make-content-addressable.cc
+41 −3 src/nix/nix.md
+3 −3 src/nix/profile.cc
+1 −1 src/nix/repl.cc
+1 −1 src/nix/upgrade-nix.cc
+45 −0 tests/flake-searching.sh
+1 −0 tests/local.mk
4 changes: 2 additions & 2 deletions main/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ quit _ = liftIO Exit.exitSuccess
-- | @:set@ command
setConfig :: (MonadNix e t f m, MonadIO m) => Text -> Repl e t f m ()
setConfig args =
list
handlePresence
(liftIO $ Text.putStrLn "No option to set specified")
(\ (x:_xs) ->
case filter ((==x) . helpSetOptionName) helpSetOptions of
Expand Down Expand Up @@ -393,7 +393,7 @@ completeFunc reversedPrev word
listFiles word

-- Attributes of sets in REPL context
| var : subFields <- Text.split (== '.') (fromString word) , not $ null subFields =
| var : subFields <- Text.split (== '.') (fromString word) , isPresent subFields =
do
state <- get
maybe
Expand Down
2 changes: 1 addition & 1 deletion src/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ processResult h val =
where
processKeys :: [VarName] -> NValue t f m -> m a
processKeys kys v =
list
handlePresence
(h v)
(\ ((k : ks) :: [VarName]) ->
do
Expand Down
6 changes: 3 additions & 3 deletions src/Nix/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ splitDrvName s =
-- list
breakAfterFirstItem :: (a -> Bool) -> [a] -> ([a], [a])
breakAfterFirstItem f =
list
handlePresence
mempty
(\ (h : t) -> let (a, b) = break f t in (h : a, b))
(namePieces, versionPieces) =
Expand Down Expand Up @@ -963,7 +963,7 @@ elemNix x = inHaskM (anyMNix $ valueEqM x)
where
anyMNix :: Monad m => (a -> m Bool) -> [a] -> m Bool
anyMNix p =
list
handlePresence
(pure False)
(\ (x : xss) ->
bool
Expand Down Expand Up @@ -1028,7 +1028,7 @@ genericClosureNix c =
bool
(do
checkComparable k $
list
handlePresence
k
(\ (WValue j:_) -> j)
(S.toList ks)
Expand Down
2 changes: 1 addition & 1 deletion src/Nix/Cited/Basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ displayProvenance
-> m a
-> m a
displayProvenance =
list
handlePresence
id
(\ (Provenance scope e@(AnnF s _) : _) ->
withFrame Info $ ForcingExpr scope $ wrapExprLoc s e
Expand Down
2 changes: 1 addition & 1 deletion src/Nix/Convert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ instance Convertible e t f m
outputs = mkNixStringWithoutContext <$> nlcvOutputs nlcv

ts :: [NValue t f m] <- traverseToValue outputs
list
handlePresence
(pure Nothing)
(fmap pure . toValue)
ts
Expand Down
6 changes: 3 additions & 3 deletions src/Nix/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ attrSetAlter ks' pos m' p' val =
)
((`M.lookup` m) k)
)
(not $ null ks)
(isPresent ks)
where
insertVal :: m v -> (PositionSet, AttrSet (m v))
insertVal v =
Expand Down Expand Up @@ -369,7 +369,7 @@ evalBinds isRecursive binds =
-- Empty attrset - return a stub.
(pure (mempty, nullPos, toValue @(AttrSet v, PositionSet) mempty) )
(\ k ->
list
handlePresence
-- No more keys in the attrset - return the result
(pure ( one k, pos, finalValue ) )
-- There are unprocessed keys in attrset - recurse appending the results
Expand Down Expand Up @@ -431,7 +431,7 @@ evalSelect aset attr =
left
(maybe
left
(list
(handlePresence
(pure . pure)
(\ (y : ys) -> (extract (y :| ys) =<<))
ks
Expand Down
4 changes: 2 additions & 2 deletions src/Nix/Expr/Strings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ stripIndent xs =
where
pairWithLast ys =
zip
(list
(handlePresence
Nothing
(pure . Unsafe.last)
<$> inits ys
Expand All @@ -83,7 +83,7 @@ stripIndent xs =
ls' = dropSpaces minIndent <$> ls

minIndent =
list
handlePresence
0
(minimum . (countSpaces . mergePlain <$>))
(stripEmptyLines ls)
Expand Down
2 changes: 1 addition & 1 deletion src/Nix/Expr/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ ekey keys pos f e@(Fix x)
do
let keys' = NE.toList keys
(ks, rest) <- zip (inits keys') (tails keys')
list
handlePresence
mempty
(\ (j : js) ->
do
Expand Down
65 changes: 34 additions & 31 deletions src/Nix/Lint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ renderSymbolic =
) <=< unpackSymbolic
where
between a b c = a <> b <> c
parens = between "(" ")"
parens = between "(" ")"
brackets = between "[" "]"
braces = between "{" "}"
braces = between "{" "}"

-- This function is order and uniqueness preserving (of types).
merge
Expand Down Expand Up @@ -204,7 +204,7 @@ merge context = go
(TSet x , TSet Nothing ) -> (one (TSet x) <>) <$> rest
(TSet Nothing , TSet x ) -> (one (TSet x) <>) <$> rest
(TSet (Just l), TSet (Just r)) -> do
m <- sequenceA $ M.intersectionWith
hm <- sequenceA $ M.intersectionWith
(\ i j ->
do
i'' <- demand =<< i
Expand All @@ -213,10 +213,10 @@ merge context = go
)
(pure <$> l)
(pure <$> r)
bool
handlePresence
id
((one (TSet $ pure m) <>) <$>)
(not $ M.null m)
(const ((one (TSet $ pure hm) <>) <$>))
hm
rest

(TClosure{}, TClosure{}) ->
Expand Down Expand Up @@ -261,30 +261,32 @@ unify context (SV x) (SV y) = do
x' <- readRef x
y' <- readRef y
case (x', y') of
(NAny, _) -> do
writeRef x y'
pure $ SV y
(_, NAny) -> do
writeRef y x'
pure $ SV x
(NMany xs, NMany ys) -> do
m <- merge context xs ys
bool
(do
let
nm = NMany m
writeRef x nm
writeRef y nm
packSymbolic nm
)
(NAny, _) ->
do
writeRef x y'
pure $ SV y
(_, NAny) ->
do
writeRef y x'
pure $ SV x
(NMany xs, NMany ys) ->
handlePresence
(
-- x' <- renderSymbolic (Symbolic x)
-- y' <- renderSymbolic (Symbolic y)
-- x' <- renderSymbolic (Symbolic x)
-- y' <- renderSymbolic (Symbolic y)
throwError $ ErrorCall "Cannot unify "
-- <> show x' <> " with " <> show y'
-- <> " in context: " <> show context
)
(null m)
(\ m ->
do
let
nm = NMany m
writeRef x nm
writeRef y nm
packSymbolic nm
)
=<< merge context xs ys
unify _ _ _ = error "The unexpected hath transpired!"

-- These aren't worth defining yet, because once we move to Hindley-Milner,
Expand Down Expand Up @@ -323,19 +325,20 @@ instance MonadLint e m => MonadEval (Symbolic m) m where
freeVariable var = symerr $ "Undefined variable '" <> coerce var <> "'"

attrMissing ks ms =
evalError @(Symbolic m) $ ErrorCall $ toString $
evalError @(Symbolic m) . ErrorCall . toString $
maybe
("Inheriting unknown attribute: " <> attr)
(\ s -> "Could not look up attribute " <> attr <> " in " <> show s)
ms
where
attr = Text.intercalate "." $ NE.toList $ coerce ks

evalCurPos = do
f <- mkSymbolic $ one TPath
l <- mkSymbolic $ one $ TConstant $ one TInt
c <- mkSymbolic $ one $ TConstant $ one TInt
mkSymbolic $ one $ TSet (pure (M.fromList [("file", f), ("line", l), ("col", c)]))
evalCurPos =
do
f <- mkSymbolic $ one TPath
l <- mkSymbolic . one . TConstant $ one TInt
c <- mkSymbolic . one . TConstant $ one TInt
mkSymbolic . one . TSet . pure $ M.fromList [("file", f), ("line", l), ("col", c)]

evalConstant c = mkSymbolic $ one $ fun c
where
Expand Down
4 changes: 2 additions & 2 deletions src/Nix/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ exprFNixDoc = \case
NSynHole name -> simpleExpr $ pretty @Text ("^" <> coerce name)
where
prettyContainer h f t c =
list
handlePresence
(simpleExpr (h <> t))
(const $ simpleExpr $ group $ nest 2 $ vsep $ one h <> (f <$> c) <> one t)
c
Expand Down Expand Up @@ -380,7 +380,7 @@ prettyProv
-> NValue t f m
-> Doc ann
prettyProv wasThunk v =
list
handlePresence
id
(\ ps pv ->
fillSep
Expand Down
2 changes: 1 addition & 1 deletion src/Nix/Reduce.hs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ pruneTree opts =

NLet binds (Just body@(Ann _ x)) ->
pure $
list
handlePresence
x
(`NLet` body)
(mapMaybe pruneBinding binds)
Expand Down
6 changes: 5 additions & 1 deletion src/Nix/Render/Frame.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ renderFrames xss@(x : xs) =
-- 2021-10-22: NOTE: List reverse is completely conterproductive. `reverse` of list famously neest to traverse the whole list to take the last element
| verbosity <= Informational -> (foldMap renderPosition (reverse xs) <>) <$> render1 x
| otherwise -> foldMapM render1 (reverse xss)
pure $ list mempty vsep renderedFrames
pure $
handlePresence
mempty
vsep
renderedFrames
where
render1 :: NixFrame -> m [Doc ann1]
render1 = renderFrame @v @t @f
Expand Down
4 changes: 2 additions & 2 deletions src/Nix/String.hs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ mkNixString = NixString

-- | Returns True if the NixString has an associated context
hasContext :: NixString -> Bool
hasContext (NixString c _) = not $ null c
hasContext (NixString c _) = isPresent c


-- ** Getters
Expand Down Expand Up @@ -183,7 +183,7 @@ toStringContexts path = go
mkLstCtxFor DirectPath cv { nlcvPath = False }
NixLikeContextValue _ True _ ->
mkLstCtxFor AllOutputs cv { nlcvAllOutputs = False }
NixLikeContextValue _ _ ls | not (null ls) ->
NixLikeContextValue _ _ ls | isPresent ls ->
mkCtxFor . DerivationOutput <$> ls
_ -> mempty
where
Expand Down
13 changes: 7 additions & 6 deletions src/Nix/Type/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,8 @@ inferType env ex =
(Set.difference `on` Set.fromList)
(Assumption.keys as )
( Env.keys env)
unless
(Set.null unbounds)
when
(isPresent unbounds)
$ typeError $ UnboundVariables $ ordNub $ Set.toList unbounds

inferState <- get
Expand Down Expand Up @@ -765,9 +765,10 @@ runSolver (Solver s) =
where
report :: [a] -> [TypeError] -> Either [TypeError] [a]
report xs e =
if null xs
then Left (ordNub e)
else pure xs
handlePresence
(Left $ ordNub e)
pure
xs

-- ** Instances

Expand Down Expand Up @@ -829,7 +830,7 @@ nextSolvable = fromJust . find solvable . pickFirstOne
solvable (EqConst{} , _) = True
solvable (ExpInstConst{}, _) = True
solvable (ImpInstConst _t1 ms t2, cs) =
Set.null $ (ms `Set.difference` ftv t2) `Set.intersection` atv cs
null $ (ms `Set.difference` ftv t2) `Set.intersection` atv cs

solve :: forall m . MonadState InferState m => [Constraint] -> Solver m Subst
solve [] = stub
Expand Down
26 changes: 14 additions & 12 deletions src/Nix/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ module Nix.Utils
, whenTrue
, whenFalse
, whenJust
, isPresent
, handlePresence
, whenText
, list
, free

, Path(..)
Expand Down Expand Up @@ -207,24 +208,25 @@ whenJust =
mempty
{-# inline whenJust #-}

-- | Analog for @bool@ or @maybe@, for list-like cons structures.
list
:: Foldable t
=> b -> (t a -> b) -> t a -> b
list e f l =
isPresent :: Foldable t => t a -> Bool
isPresent = not . null


-- | 'maybe'-like eliminator, for foldable empty/inhabited structures.
handlePresence :: Foldable t => b -> (t a -> b) -> t a -> b
handlePresence d f t =
bool
(f l)
e
(null l)
{-# inline list #-}
d
(f t)
(isPresent t)

whenText
:: a -> (Text -> a) -> Text -> a
whenText e f t =
bool
(f t)
e
(Text.null t)
(f t)
(not $ Text.null t)

-- | Lambda analog of @maybe@ or @either@ for Free monad.
free :: (a -> b) -> (f (Free f a) -> b) -> Free f a -> b
Expand Down

0 comments on commit 33dbeff

Please sign in to comment.