Skip to content

Commit 21636db

Browse files
committed
defer merging prototype
Currently we have achieve the following: - Stop merging, the merging function "endo" is id - CondTree are completly retained in bigger types such as libarry and executable We will need to do the following: - Allow merging in the accessor We broke: - A bunch of Read and Ord instances
1 parent e253225 commit 21636db

File tree

16 files changed

+112
-90
lines changed

16 files changed

+112
-90
lines changed

Cabal-syntax/Cabal-syntax.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ library
160160
Distribution.Types.Library.Lens
161161
Distribution.Types.LibraryName
162162
Distribution.Types.LibraryVisibility
163-
Distribution.Types.Imports
164163
Distribution.Types.MissingDependency
165164
Distribution.Types.MissingDependencyReason
166165
Distribution.Types.Mixin

Cabal-syntax/src/Distribution/PackageDescription/FieldGrammar.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ import Distribution.Package
8484
import Distribution.PackageDescription
8585
import Distribution.Parsec
8686
import Distribution.Pretty (Pretty (..), prettyShow, showToken)
87-
import Distribution.Types.Imports
8887
import Distribution.Utils.Path
8988
import Distribution.Version (Version, VersionRange)
9089

@@ -300,7 +299,7 @@ executableFieldGrammar n =
300299
-- | An intermediate type just used for parsing the test-suite stanza.
301300
-- After validation it is converted into the proper 'TestSuite' type.
302301
data TestSuiteStanza = TestSuiteStanza
303-
{ _testStanzaImports :: [ImportName]
302+
{ _testStanzaImports :: [(ImportName, CondTree ConfVar [Dependency] BuildInfo)]
304303
, _testStanzaTestType :: Maybe TestType
305304
-- ^ Retained imports for exact printing
306305
, _testStanzaMainIs :: Maybe (RelativePath Source File)
@@ -462,7 +461,7 @@ unvalidateTestSuite t =
462461
-- | An intermediate type just used for parsing the benchmark stanza.
463462
-- After validation it is converted into the proper 'Benchmark' type.
464463
data BenchmarkStanza = BenchmarkStanza
465-
{ _benchmarkStanzaImports :: [ImportName]
464+
{ _benchmarkStanzaImports :: [(ImportName, CondTree ConfVar [Dependency] BuildInfo)]
466465
-- ^ retained imports
467466
, _benchmarkStanzaBenchmarkType :: Maybe BenchmarkType
468467
, _benchmarkStanzaMainIs :: Maybe (RelativePath Source File)

Cabal-syntax/src/Distribution/PackageDescription/Parsec.hs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE Rank2Types #-}
44
{-# LANGUAGE ScopedTypeVariables #-}
5+
{-# LANGUAGE ViewPatterns #-}
56

67
-- |
78
-- Module : Distribution.PackageDescription.Parsec
@@ -66,7 +67,6 @@ import qualified Distribution.Types.BuildInfo.Lens as L
6667
import qualified Distribution.Types.Executable.Lens as L
6768
import qualified Distribution.Types.ForeignLib.Lens as L
6869
import qualified Distribution.Types.GenericPackageDescription.Lens as L
69-
import Distribution.Types.Imports
7070
import qualified Distribution.Types.PackageDescription.Lens as L
7171
import qualified Distribution.Types.SetupBuildInfo.Lens as L
7272
import qualified Text.Parsec as P
@@ -656,16 +656,16 @@ parseCondTreeWithCommonStanzas v grammar fromBuildInfo commonStanzas fields = do
656656
where
657657
hasElif = specHasElif v
658658

659-
-- | only attach import annotation on root
660-
attachImportsOnRoot
661-
:: [ImportName]
662-
-> CondTree v c a
663-
-> CondTree v c (WithImports a)
664-
attachImportsOnRoot imports = mapTreeData' (WithImports imports) noImports
659+
-- -- | only attach import annotation on root
660+
-- attachImportsOnRoot
661+
-- :: [ImportName]
662+
-- -> CondTree v c a
663+
-- -> CondTree v c (WithImports a)
664+
-- attachImportsOnRoot imports = mapTreeData' (WithImports imports) noImports
665665

666666
-- | only prepend import annotation on root
667667
prependImportsOnRoot
668-
:: [ImportName]
668+
:: [(ImportName, CondTreeBuildInfo)]
669669
-> CondTree v c (WithImports a)
670670
-> CondTree v c (WithImports a)
671671
prependImportsOnRoot imports = mapTreeData' (mapImports (imports <>)) id
@@ -682,8 +682,9 @@ processImports
682682
-> ParseResult
683683
src
684684
( [Field Position]
685-
, [ImportName]
686-
, CondTree ConfVar [Dependency] (WithImports a) -> CondTree ConfVar [Dependency] (WithImports a)
685+
, [(ImportName, CondTree ConfVar [Dependency] BuildInfo)]
686+
, -- NOTE(leana8959): as we know it's a build info we can insert in this function!
687+
CondTree ConfVar [Dependency] (WithImports a) -> CondTree ConfVar [Dependency] (WithImports a)
687688
)
688689
processImports v fromBuildInfo commonStanzas = go []
689690
where
@@ -693,12 +694,12 @@ processImports v fromBuildInfo commonStanzas = go []
693694
getList' = Newtype.unpack
694695

695696
go
696-
:: [(ImportName, CondTree ConfVar [Dependency] (WithImports BuildInfo))]
697+
:: [(ImportName, CondTree ConfVar [Dependency] BuildInfo)]
697698
-> [Field Position]
698699
-> ParseResult
699700
src
700701
( [Field Position]
701-
, [ImportName]
702+
, [(ImportName, CondTree ConfVar [Dependency] BuildInfo)]
702703
, CondTree ConfVar [Dependency] (WithImports a) -> CondTree ConfVar [Dependency] (WithImports a)
703704
)
704705
go acc (Field (Name pos name) _ : fields)
@@ -717,7 +718,7 @@ processImports v fromBuildInfo commonStanzas = go []
717718
parseFailure pos $ "Undefined common stanza imported: " ++ commonName
718719
pure Nothing
719720
Just commonTree ->
720-
pure (Just (commonName, mapTreeData noImports commonTree))
721+
pure (Just (commonName, commonTree))
721722

722723
go (acc ++ catMaybes namedTrees) fields
723724

@@ -726,12 +727,12 @@ processImports v fromBuildInfo commonStanzas = go []
726727
fields' <- catMaybes <$> traverse (warnImport v) fields
727728
let
728729
importNames :: [ImportName]
729-
importTrees :: [CondTree ConfVar [Dependency] (WithImports BuildInfo)]
730+
importTrees :: [CondTree ConfVar [Dependency] BuildInfo]
730731
(importNames, importTrees) = unzip acc
731732
pure
732733
( fields'
733-
, importNames
734-
, \x -> foldr (mergeCommonStanza fromBuildInfo) x importTrees
734+
, acc
735+
, id -- TODO(leana8959): defer merging
735736
)
736737

737738
-- | Warn on "import" fields, also map to Maybe, so erroneous fields can be filtered
@@ -743,26 +744,27 @@ warnImport v (Field (Name pos name) _) | name == "import" = do
743744
return Nothing
744745
warnImport _ f = pure (Just f)
745746

746-
mergeCommonStanza
747-
:: forall a
748-
. L.HasBuildInfo a
749-
=> (BuildInfo -> a)
750-
-> CondTree ConfVar [Dependency] (WithImports BuildInfo)
751-
-> CondTree ConfVar [Dependency] (WithImports a)
752-
-> CondTree ConfVar [Dependency] (WithImports a)
753-
mergeCommonStanza fromBuildInfo (CondNode bi _ bis) (CondNode x _ cs) =
754-
CondNode x' (unImportNames x' ^. L.targetBuildDepends) cs'
755-
where
756-
-- new value is old value with buildInfo field _prepended_.
757-
x' :: WithImports a
758-
x' =
759-
WithImports
760-
(getImportNames bi <> getImportNames x)
761-
(unImportNames x & L.buildInfo %~ (unImportNames bi <>))
762-
763-
-- tree components are appended together.
764-
cs' :: [CondBranch ConfVar [Dependency] (WithImports a)]
765-
cs' = map (fmap fromBuildInfo <$>) bis ++ cs
747+
-- -- TODO(leana8959): we defer merging
748+
-- mergeCommonStanza
749+
-- :: forall a
750+
-- . L.HasBuildInfo a
751+
-- => (BuildInfo -> a)
752+
-- -> CondTree ConfVar [Dependency] (WithImports BuildInfo)
753+
-- -> CondTree ConfVar [Dependency] (WithImports a)
754+
-- -> CondTree ConfVar [Dependency] (WithImports a)
755+
-- mergeCommonStanza fromBuildInfo (CondNode bi _ bis) (CondNode x _ cs) =
756+
-- CondNode x' (unImportNames x' ^. L.targetBuildDepends) cs'
757+
-- where
758+
-- -- new value is old value with buildInfo field _prepended_.
759+
-- x' :: WithImports a
760+
-- x' =
761+
-- WithImports
762+
-- (getImportNames bi <> getImportNames x)
763+
-- (unImportNames x & L.buildInfo %~ (unImportNames bi <>))
764+
--
765+
-- -- tree components are appended together.
766+
-- cs' :: [CondBranch ConfVar [Dependency] (WithImports a)]
767+
-- cs' = map (fmap fromBuildInfo <$>) bis ++ cs
766768

767769
-------------------------------------------------------------------------------
768770
-- Branches

Cabal-syntax/src/Distribution/Types/Benchmark.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import Prelude ()
1515
import Distribution.Types.BenchmarkInterface
1616
import Distribution.Types.BenchmarkType
1717
import Distribution.Types.BuildInfo
18-
import Distribution.Types.Imports
18+
import Distribution.Types.CondTree
19+
import Distribution.Types.ConfVar
20+
import Distribution.Types.Dependency
1921
import Distribution.Types.UnqualComponentName
2022

2123
import Distribution.ModuleName
@@ -25,11 +27,13 @@ import qualified Distribution.Types.BuildInfo.Lens as L
2527
-- | A \"benchmark\" stanza in a cabal file.
2628
data Benchmark = Benchmark
2729
{ benchmarkName :: UnqualComponentName
28-
, benchmarkImports :: [ImportName]
30+
, benchmarkImports :: [(ImportName, CondTree ConfVar [Dependency] BuildInfo)]
31+
-- ^ Retained condTree imports, not merged
2932
, benchmarkInterface :: BenchmarkInterface
3033
, benchmarkBuildInfo :: BuildInfo
34+
-- ^ the BuildInfo defined locally, unmerged with imports
3135
}
32-
deriving (Generic, Show, Read, Eq, Ord, Data)
36+
deriving (Generic, Show {- Read, -}, Eq {- Ord, -}, Data)
3337

3438
instance Binary Benchmark
3539
instance Structured Benchmark

Cabal-syntax/src/Distribution/Types/Benchmark/Lens.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import Prelude ()
99

1010
import Distribution.Types.Benchmark (Benchmark)
1111
import Distribution.Types.BenchmarkInterface (BenchmarkInterface)
12-
import Distribution.Types.BuildInfo (BuildInfo)
13-
import Distribution.Types.Imports (ImportName)
12+
import Distribution.Types.BuildInfo (BuildInfo, ImportName)
13+
import Distribution.Types.CondTree
14+
import Distribution.Types.ConfVar
15+
import Distribution.Types.Dependency
1416
import Distribution.Types.UnqualComponentName (UnqualComponentName)
1517

1618
import qualified Distribution.Types.Benchmark as T
1719

18-
benchmarkImports :: Lens' Benchmark [ImportName]
20+
benchmarkImports :: Lens' Benchmark [(ImportName, CondTree ConfVar [Dependency] BuildInfo)]
1921
benchmarkImports f s = fmap (\x -> s{T.benchmarkImports = x}) (f (T.benchmarkImports s))
2022
{-# INLINE benchmarkImports #-}
2123

Cabal-syntax/src/Distribution/Types/BuildInfo.hs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE DeriveDataTypeable #-}
3+
{-# LANGUAGE DeriveFunctor #-}
34
{-# LANGUAGE DeriveGeneric #-}
45

56
module Distribution.Types.BuildInfo
@@ -15,6 +16,12 @@ module Distribution.Types.BuildInfo
1516
, hcProfSharedOptions
1617
, hcStaticOptions
1718
, insertBuildInfoImports
19+
20+
-- * Imports
21+
, WithImports (..)
22+
, ImportName
23+
, noImports
24+
, mapImports
1825
) where
1926

2027
import Distribution.Compat.Prelude
@@ -24,7 +31,8 @@ import Distribution.Types.CondTree
2431
import Distribution.Types.ConfVar
2532
import Distribution.Types.Dependency
2633
import Distribution.Types.ExeDependency
27-
import Distribution.Types.Imports
34+
35+
-- import Distribution.Types.Imports
2836
import Distribution.Types.LegacyExeDependency
2937
import Distribution.Types.Mixin
3038
import Distribution.Types.PkgconfigDependency
@@ -36,7 +44,7 @@ import Language.Haskell.Extension
3644

3745
-- Consider refactoring into executable and library versions.
3846
data BuildInfo = BuildInfo
39-
{ buildInfoImports :: [String]
47+
{ buildInfoImports :: ![(ImportName, CondTreeBuildInfo)]
4048
, buildable :: Bool
4149
-- ^ component is buildable here
4250
, buildTools :: [LegacyExeDependency]
@@ -151,7 +159,8 @@ data BuildInfo = BuildInfo
151159
-- ^ Dependencies specific to a library or executable target
152160
, mixins :: [Mixin]
153161
}
154-
deriving (Generic, Show, Read, Eq, Ord, Data)
162+
-- TODO(leana8959): instances
163+
deriving (Generic, Show {- Read, -}, Eq {- Ord, -}, Data)
155164

156165
instance Binary BuildInfo
157166
instance Structured BuildInfo
@@ -334,3 +343,19 @@ lookupHcOptions f hc bi = case f bi of
334343
| hc == GHC -> ghc
335344
| hc == GHCJS -> ghcjs
336345
| otherwise -> mempty
346+
347+
-- TODO(leana8959): where do we put this to avoid cyclical import
348+
type ImportName = String
349+
type CondTreeBuildInfo = CondTree ConfVar [Dependency] BuildInfo
350+
351+
data WithImports a = WithImports
352+
{ getImportNames :: ![(ImportName, CondTreeBuildInfo)]
353+
, unImportNames :: !a
354+
}
355+
deriving (Show, Functor)
356+
357+
mapImports :: ([(ImportName, CondTreeBuildInfo)] -> [(ImportName, CondTreeBuildInfo)]) -> WithImports a -> WithImports a
358+
mapImports f (WithImports imports x) = WithImports (f imports) x
359+
360+
noImports :: a -> WithImports a
361+
noImports = WithImports mempty

Cabal-syntax/src/Distribution/Types/Component.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ data Component
2929
| CExe Executable
3030
| CTest TestSuite
3131
| CBench Benchmark
32-
deriving (Generic, Show, Eq, Read)
32+
deriving (Generic, Show, Eq {- , Read -})
3333

3434
instance Binary Component
3535
instance Structured Component

Cabal-syntax/src/Distribution/Types/Executable.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ import Distribution.Types.CondTree
1919
import Distribution.Types.ConfVar
2020
import Distribution.Types.Dependency
2121
import Distribution.Types.ExecutableScope
22-
import Distribution.Types.Imports
2322
import Distribution.Types.UnqualComponentName
2423
import Distribution.Utils.Path
2524

2625
import qualified Distribution.Types.BuildInfo.Lens as L
2726

2827
data Executable = Executable
2928
{ exeName :: UnqualComponentName
30-
, exeImports :: [ImportName]
31-
-- ^ Retained for exact print
29+
, exeImports :: [(ImportName, CondTree ConfVar [Dependency] BuildInfo)]
30+
-- ^ Retained condTree imports, not merged
3231
, modulePath :: RelativePath Source File
3332
, exeScope :: ExecutableScope
3433
, buildInfo :: BuildInfo
34+
-- ^ the BuildInfo defined locally, unmerged with imports
3535
}
36-
deriving (Generic, Show, Read, Eq, Ord, Data)
36+
-- TODO(leana8959): instances
37+
deriving (Generic, Show {- Read, -}, Eq {- Ord, -}, Data)
3738

3839
insertExeImports
3940
:: CondTree ConfVar [Dependency] (WithImports Executable)

Cabal-syntax/src/Distribution/Types/ForeignLib.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import Distribution.Types.ConfVar
3030
import Distribution.Types.Dependency
3131
import Distribution.Types.ForeignLibOption
3232
import Distribution.Types.ForeignLibType
33-
import Distribution.Types.Imports
3433
import Distribution.Types.UnqualComponentName
3534
import Distribution.Utils.Path
3635
import Distribution.Version
@@ -47,15 +46,15 @@ import qualified Distribution.Types.BuildInfo.Lens as L
4746
data ForeignLib = ForeignLib
4847
{ foreignLibName :: UnqualComponentName
4948
-- ^ Name of the foreign library
50-
, foreignLibImports :: [ImportName]
51-
-- ^ Retained imports for exact printing
49+
, foreignLibImports :: [(ImportName, CondTree ConfVar [Dependency] BuildInfo)]
50+
-- ^ Retained condTree imports, not merged
5251
, foreignLibType :: ForeignLibType
5352
-- ^ What kind of foreign library is this (static or dynamic).
5453
, foreignLibOptions :: [ForeignLibOption]
5554
-- ^ What options apply to this foreign library (e.g., are we
5655
-- merging in all foreign dependencies.)
5756
, foreignLibBuildInfo :: BuildInfo
58-
-- ^ Build information for this foreign library.
57+
-- ^ the BuildInfo for this foreign library. Defined locally, unmerged with imports
5958
, foreignLibVersionInfo :: Maybe LibVersionInfo
6059
-- ^ Libtool-style version-info data to compute library version.
6160
-- Refer to the libtool documentation on the
@@ -68,7 +67,7 @@ data ForeignLib = ForeignLib
6867
-- This is a list rather than a maybe field so that we can flatten
6968
-- the condition trees (for instance, when creating an sdist)
7069
}
71-
deriving (Generic, Show, Read, Eq, Ord, Data)
70+
deriving (Generic, Show {- Read, -}, Eq {- Ord, -}, Data)
7271

7372
data LibVersionInfo = LibVersionInfo Int Int Int deriving (Data, Eq, Generic)
7473

Cabal-syntax/src/Distribution/Types/Imports.hs

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)