diff --git a/Cabal/Distribution/Compat/NonEmptySet.hs b/Cabal/Distribution/Compat/NonEmptySet.hs index be48f0c4397..09516d6256f 100644 --- a/Cabal/Distribution/Compat/NonEmptySet.hs +++ b/Cabal/Distribution/Compat/NonEmptySet.hs @@ -4,6 +4,10 @@ module Distribution.Compat.NonEmptySet ( NonEmptySet, -- * Construction singleton, + -- * Insertion + insert, + -- * Deletion + delete, -- * Conversions toNonEmpty, fromNonEmpty, @@ -15,7 +19,7 @@ module Distribution.Compat.NonEmptySet ( map, ) where -import Prelude (Bool (..), Eq, Ord (..), Read, Show (..), String, error, return, showParen, showString, ($), (++), (.)) +import Prelude (Bool (..), Eq, Maybe (..), Ord (..), Read, Show (..), String, error, otherwise, return, showParen, showString, ($), (++), (.)) import Control.DeepSeq (NFData (..)) import Data.Data (Data) @@ -26,7 +30,7 @@ import Data.Typeable (Typeable) import qualified Data.Foldable as F import qualified Data.Set as Set -import Distribution.Compat.Binary (Binary (..)) +import Distribution.Compat.Binary (Binary (..)) import Distribution.Utils.Structured #if MIN_VERSION_binary(0,6,0) @@ -86,6 +90,24 @@ instance F.Foldable NonEmptySet where singleton :: a -> NonEmptySet a singleton = NES . Set.singleton +------------------------------------------------------------------------------- +-- Insertion +------------------------------------------------------------------------------- + +insert :: Ord a => a -> NonEmptySet a -> NonEmptySet a +insert x (NES xs) = NES (Set.insert x xs) + +------------------------------------------------------------------------------- +-- Deletion +------------------------------------------------------------------------------- + +delete :: Ord a => a -> NonEmptySet a -> Maybe (NonEmptySet a) +delete x (NES xs) + | Set.null res = Nothing + | otherwise = Just (NES xs) + where + res = Set.delete x xs + ------------------------------------------------------------------------------- -- Conversions ------------------------------------------------------------------------------- diff --git a/Cabal/Distribution/Types/Dependency.hs b/Cabal/Distribution/Types/Dependency.hs index 8170f2b3db2..e8a3b53853a 100644 --- a/Cabal/Distribution/Types/Dependency.hs +++ b/Cabal/Distribution/Types/Dependency.hs @@ -25,7 +25,7 @@ import Distribution.Types.LibraryName import Distribution.Types.PackageName import Distribution.Types.UnqualComponentName -import qualified Distribution.Compat.NonEmptySet as NonEmptySet +import qualified Distribution.Compat.NonEmptySet as NES import qualified Text.PrettyPrint as PP -- | Describes a dependency on a source package (API) @@ -59,7 +59,7 @@ depLibraries (Dependency _ _ cs) = cs -- @since 3.4.0.0 -- mkDependency :: PackageName -> VersionRange -> NonEmptySet LibraryName -> Dependency -mkDependency pn vr lb = Dependency pn vr (NonEmptySet.map conv lb) +mkDependency pn vr lb = Dependency pn vr (NES.map conv lb) where pn' = packageNameToUnqualComponentName pn @@ -71,6 +71,20 @@ instance Binary Dependency instance Structured Dependency instance NFData Dependency where rnf = genericRnf +-- | +-- +-- >>> prettyShow $ Dependency "pkg" anyVersion mainLibSet +-- "pkg" +-- +-- >>> prettyShow $ Dependency "pkg" anyVersion $ NES.insert (LSubLibName "sublib") mainLibSet +-- "pkg:{pkg, sublib}" +-- +-- >>> prettyShow $ Dependency "pkg" anyVersion $ NES.singleton (LSubLibName "sublib") +-- "pkg:sublib" +-- +-- >>> prettyShow $ Dependency "pkg" anyVersion $ NES.insert (LSubLibName "sublib-b") $ NES.singleton (LSubLibName "sublib-a") +-- "pkg:{sublib-a, sublib-b}" +-- instance Pretty Dependency where pretty (Dependency name ver sublibs) = withSubLibs (pretty name) <+> pver where @@ -78,11 +92,12 @@ instance Pretty Dependency where pver | isAnyVersionLight ver = PP.empty | otherwise = pretty ver - withSubLibs doc - | sublibs == mainLibSet = doc - | otherwise = doc <<>> PP.colon <<>> PP.braces prettySublibs + withSubLibs doc = case NES.toList sublibs of + [LMainLibName] -> doc + [LSubLibName uq] -> doc <<>> PP.colon <<>> pretty uq + _ -> doc <<>> PP.colon <<>> PP.braces prettySublibs - prettySublibs = PP.hsep $ PP.punctuate PP.comma $ prettySublib <$> NonEmptySet.toList sublibs + prettySublibs = PP.hsep $ PP.punctuate PP.comma $ prettySublib <$> NES.toList sublibs prettySublib LMainLibName = PP.text $ unPackageName name prettySublib (LSubLibName un) = PP.text $ unUnqualComponentName un @@ -127,7 +142,7 @@ instance Parsec Dependency where _ <- char ':' versionGuardMultilibs parsecWarning PWTExperimental "colon specifier is experimental feature (issue #5660)" - NonEmptySet.singleton <$> parseLib <|> parseMultipleLibs + NES.singleton <$> parseLib <|> parseMultipleLibs spaces -- https://github.com/haskell/cabal/issues/5846 @@ -138,7 +153,7 @@ instance Parsec Dependency where parseMultipleLibs = between (char '{' *> spaces) (spaces *> char '}') - (NonEmptySet.fromNonEmpty <$> parsecCommaNonEmpty parseLib) + (NES.fromNonEmpty <$> parsecCommaNonEmpty parseLib) versionGuardMultilibs :: CabalParsing m => m () versionGuardMultilibs = do @@ -152,7 +167,7 @@ versionGuardMultilibs = do -- | Library set with main library. mainLibSet :: NonEmptySet LibraryName -mainLibSet = NonEmptySet.singleton LMainLibName +mainLibSet = NES.singleton LMainLibName -- | Simplify the 'VersionRange' expression in a 'Dependency'. -- See 'simplifyVersionRange'. diff --git a/Cabal/tests/ParserTests/regressions/issue-5846.format b/Cabal/tests/ParserTests/regressions/issue-5846.format index f0e119da0c0..42194892150 100644 --- a/Cabal/tests/ParserTests/regressions/issue-5846.format +++ b/Cabal/tests/ParserTests/regressions/issue-5846.format @@ -10,6 +10,6 @@ library default-language: Haskell2010 build-depends: lib1:{a, b}, - lib2:{c}, - lib3:{d} >=1, + lib2:c, + lib3:d >=1, lib4:{a, b} >=1 diff --git a/cabal-testsuite/PackageTests/MultipleLibraries/T6894/cabal.out b/cabal-testsuite/PackageTests/MultipleLibraries/T6894/cabal.out index b3e8aed99bd..f8a13f7425f 100644 --- a/cabal-testsuite/PackageTests/MultipleLibraries/T6894/cabal.out +++ b/cabal-testsuite/PackageTests/MultipleLibraries/T6894/cabal.out @@ -10,6 +10,6 @@ Preprocessing library 'sublib' for issue-6894.. Building library 'sublib' for issue-6894.. Warning: issue.cabal:7:30: colon specifier is experimental feature (issue #5660) Configuring library for issue-6894.. -Warning: The package has an extraneous version range for a dependency on an internal library: issue:{sublib} >=0 && ==6894. This version range includes the current package but isn't needed as the current package's library will always be used. +Warning: The package has an extraneous version range for a dependency on an internal library: issue:sublib >=0 && ==6894. This version range includes the current package but isn't needed as the current package's library will always be used. Preprocessing library for issue-6894.. Building library for issue-6894..