Skip to content

Commit 6ca7382

Browse files
committed
Add NoFlagValue type class
1 parent 32c06d4 commit 6ca7382

9 files changed

Lines changed: 48 additions & 46 deletions

File tree

Cabal-syntax/src/Distribution/Parsec.hs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ module Distribution.Parsec
6060

6161
import Data.ByteString (ByteString)
6262
import Data.Char (digitToInt, intToDigit)
63-
import Data.Functor (($>))
6463
import Data.List (transpose)
6564
import Distribution.CabalSpecVersion
6665
import Distribution.Compat.Prelude
@@ -253,15 +252,11 @@ instance Parsec Bool where
253252
parsec = P.munch1 isAlpha >>= postprocess
254253
where
255254
postprocess str
256-
| str == "True" = pure True
257-
| str == "False" = pure False
258-
| lstr == "true" = parsecWarning PWTBoolCase caseWarning $> True
259-
| lstr == "false" = parsecWarning PWTBoolCase caseWarning $> False
255+
| lstr == "true" = pure True
256+
| lstr == "false" = pure False
260257
| otherwise = fail $ "Not a boolean: " ++ str
261258
where
262259
lstr = map toLower str
263-
caseWarning =
264-
"Boolean values are case sensitive, use 'True' or 'False'."
265260

266261
instance Parsec a => Parsec (Last a) where
267262
parsec = parsecLast

Cabal/src/Distribution/Simple/Build.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ dumpBuildInfo verbosity distPref dumpBuildInfoFlag pkg_descr lbi flags = do
309309
removeFileForcibly buildInfoFile
310310
where
311311
buildInfoFile = interpretSymbolicPathLBI lbi $ buildInfoPref distPref
312-
shouldDumpBuildInfo = fromFlagOrDefault NoDumpBuildInfo dumpBuildInfoFlag == DumpBuildInfo
312+
shouldDumpBuildInfo = fromNoFlag dumpBuildInfoFlag == DumpBuildInfo
313313

314314
-- \| Given the flavor of the compiler, try to find out
315315
-- which program we need.

Cabal/src/Distribution/Simple/Flag.hs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ module Distribution.Simple.Flag
3030
, flagToList
3131
, maybeToFlag
3232
, mergeListFlag
33-
, fromFlagOrMonoid
3433
, BooleanFlag (..)
34+
, NoFlagValue (..)
35+
, fromNoFlag
3536
) where
3637

3738
import Data.Monoid (Last (..))
@@ -87,10 +88,6 @@ fromFlag NoFlag = error "fromFlag NoFlag. Use fromFlagOrDefault"
8788
fromFlagOrDefault :: a -> Flag a -> a
8889
fromFlagOrDefault def = fromMaybe def . getLast
8990

90-
-- | Extracts a value from a 'Flag', and returns the mempty value on 'NoFlag'.
91-
fromFlagOrMonoid :: Monoid a => Flag a -> a
92-
fromFlagOrMonoid = fromFlagOrDefault mempty
93-
9491
-- | Converts a 'Flag' value to a 'Maybe' value.
9592
flagToMaybe :: Flag a -> Maybe a
9693
flagToMaybe = getLast
@@ -128,3 +125,15 @@ class BooleanFlag a where
128125

129126
instance BooleanFlag Bool where
130127
asBool = id
128+
129+
-- | Flag is a Monoid, with 'NoFlag' as the identity element, and 'Flag' as the binary operation.
130+
--
131+
-- @since 3.18.0.0
132+
class NoFlagValue a where
133+
noFlagValue :: a
134+
135+
-- | Extracts a value from a 'Flag', and returns the 'noFlagValue' on 'NoFlag'.
136+
--
137+
-- @since 3.18.0.0
138+
fromNoFlag :: NoFlagValue a => Flag a -> a
139+
fromNoFlag = fromFlagOrDefault noFlagValue

Cabal/src/Distribution/Simple/Setup/Config.hs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,15 @@ configureOptions showOrParseArgs =
579579
(Flag . fromString)
580580
( \case
581581
NoFlag -> []
582-
flag -> [OptimisationLevel.toString $ fromFlagOrMonoid flag]
582+
Flag flag -> [OptimisationLevel.toString flag]
583583
)
584584
"O"
585585
["enable-optimization", "enable-optimisation"]
586-
"Build with optimization (n is 0--2, default is 1)"
586+
( let minLevel = OptimisationLevel.toString minBound
587+
maxLevel = OptimisationLevel.toString maxBound
588+
def = OptimisationLevel.toString noFlagValue
589+
in "Build with optimization (n is " ++ minLevel ++ "--" ++ maxLevel ++ ", default is " ++ def ++ ")"
590+
)
587591
, noArg
588592
(Flag NoOptimisation)
589593
[]
@@ -599,11 +603,15 @@ configureOptions showOrParseArgs =
599603
(Flag . fromString)
600604
( \case
601605
NoFlag -> []
602-
flag -> [DebugInfoLevel.toString $ fromFlagOrMonoid flag]
606+
Flag flag -> [DebugInfoLevel.toString flag]
603607
)
604608
"g"
605609
["enable-debug-info"]
606-
"Emit debug info (n is 0--3, default is 0)"
610+
( let minLevel = DebugInfoLevel.toString minBound
611+
maxLevel = DebugInfoLevel.toString maxBound
612+
def = DebugInfoLevel.toString noFlagValue
613+
in "Emit debug info (n is " ++ minLevel ++ "--" ++ maxLevel ++ ", default is " ++ def ++ ")"
614+
)
607615
, noArg
608616
(Flag NoDebugInfo)
609617
[]

Cabal/src/Distribution/Types/DebugInfoLevel.hs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Distribution.Parsec (CabalParsing, Parsec (..))
1515
import Prelude ()
1616

1717
import Data.Bool (bool)
18+
import Distribution.Simple.Flag (NoFlagValue (..))
1819

1920
-- ------------------------------------------------------------
2021

@@ -36,25 +37,17 @@ instance Binary DebugInfoLevel
3637
instance NFData DebugInfoLevel
3738
instance Structured DebugInfoLevel
3839

40+
instance NoFlagValue DebugInfoLevel where
41+
noFlagValue :: DebugInfoLevel
42+
noFlagValue = NoDebugInfo
43+
3944
instance Parsec DebugInfoLevel where
4045
parsec :: CabalParsing m => m DebugInfoLevel
4146
parsec = boolParser <|> intParser
4247
where
4348
boolParser = bool NoDebugInfo NormalDebugInfo <$> parsec
4449
intParser = intToDebugInfoLevel <$> integral
4550

46-
instance Ord DebugInfoLevel where
47-
compare :: DebugInfoLevel -> DebugInfoLevel -> Ordering
48-
compare = comparing fromEnum
49-
50-
instance Semigroup DebugInfoLevel where
51-
(<>) :: DebugInfoLevel -> DebugInfoLevel -> DebugInfoLevel
52-
(<>) = max
53-
54-
instance Monoid DebugInfoLevel where
55-
mempty :: DebugInfoLevel
56-
mempty = NoDebugInfo
57-
5851
instance IsString DebugInfoLevel where
5952
fromString :: String -> DebugInfoLevel
6053
fromString s = case reads s of

Cabal/src/Distribution/Types/DumpBuildInfo.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Distribution.Types.DumpBuildInfo
1010

1111
import Distribution.Compat.Prelude (Binary, Generic, NFData, Structured)
1212
import Distribution.Parsec (CabalParsing, Parsec (..))
13+
import Distribution.Simple.Flag (NoFlagValue (..))
1314

1415
data DumpBuildInfo
1516
= NoDumpBuildInfo
@@ -20,6 +21,10 @@ instance Binary DumpBuildInfo
2021
instance NFData DumpBuildInfo
2122
instance Structured DumpBuildInfo
2223

24+
instance NoFlagValue DumpBuildInfo where
25+
noFlagValue :: DumpBuildInfo
26+
noFlagValue = NoDumpBuildInfo
27+
2328
instance Parsec DumpBuildInfo where
2429
parsec :: CabalParsing m => m DumpBuildInfo
2530
parsec = boolToDumpBuildInfo <$> parsec

Cabal/src/Distribution/Types/OptimisationLevel.hs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Distribution.Parsec (CabalParsing, Parsec (..))
1515
import Prelude ()
1616

1717
import Data.Bool (bool)
18+
import Distribution.Simple.Flag (NoFlagValue (..))
1819

1920
-- ------------------------------------------------------------
2021

@@ -35,25 +36,17 @@ instance Binary OptimisationLevel
3536
instance NFData OptimisationLevel
3637
instance Structured OptimisationLevel
3738

39+
instance NoFlagValue OptimisationLevel where
40+
noFlagValue :: OptimisationLevel
41+
noFlagValue = NormalOptimisation
42+
3843
instance Parsec OptimisationLevel where
3944
parsec :: CabalParsing m => m OptimisationLevel
4045
parsec = boolParser <|> intParser
4146
where
4247
boolParser = bool NoOptimisation NormalOptimisation <$> parsec
4348
intParser = intToOptimisationLevel <$> integral
4449

45-
instance Ord OptimisationLevel where
46-
compare :: OptimisationLevel -> OptimisationLevel -> Ordering
47-
compare = comparing fromEnum
48-
49-
instance Semigroup OptimisationLevel where
50-
(<>) :: OptimisationLevel -> OptimisationLevel -> OptimisationLevel
51-
(<>) = max
52-
53-
instance Monoid OptimisationLevel where
54-
mempty :: OptimisationLevel
55-
mempty = NormalOptimisation
56-
5750
instance IsString OptimisationLevel where
5851
fromString :: String -> OptimisationLevel
5952
fromString s = case reads s of

cabal-install/src/Distribution/Client/ProjectOrchestration.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ import Distribution.Simple.Compiler
190190
, showCompilerId
191191
)
192192
import Distribution.Simple.Configure (computeEffectiveProfiling)
193-
import Distribution.Simple.Flag (flagToMaybe, fromFlagOrDefault, fromFlagOrMonoid)
193+
import Distribution.Simple.Flag (flagToMaybe, fromFlagOrDefault, fromNoFlag)
194194
import Distribution.Simple.LocalBuildInfo
195195
( ComponentName (..)
196196
, pkgComponents
@@ -1255,7 +1255,7 @@ printPlan
12551255
"Build profile: "
12561256
++ unwords
12571257
[ "-w " ++ (showCompilerId . pkgConfigCompiler) elaboratedShared
1258-
, "-O" ++ (OptimisationLevel.toString . fromFlagOrMonoid) (globalOptimization <> localOptimization)
1258+
, "-O" ++ (OptimisationLevel.toString . fromNoFlag) (globalOptimization <> localOptimization)
12591259
]
12601260
++ "\n"
12611261

cabal-install/src/Distribution/Client/ScriptUtils.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ import Distribution.Simple.Compiler
122122
)
123123
import Distribution.Simple.Flag
124124
( flagToMaybe
125-
, fromFlagOrDefault
125+
, fromNoFlag
126126
)
127127
import Distribution.Simple.PackageDescription
128128
( parseString
@@ -193,7 +193,6 @@ import qualified Data.ByteString.Char8 as BS
193193
import Data.ByteString.Lazy ()
194194
import qualified Data.Set as S
195195
import Distribution.Client.Errors
196-
import Distribution.Types.OptimisationLevel (OptimisationLevel (..))
197196
import Distribution.Utils.Path
198197
( unsafeMakeSymbolicPath
199198
)
@@ -434,7 +433,7 @@ scriptDistDirParams scriptPath ctx compiler platform =
434433
, distParamComponentName = Just $ CExeName cn
435434
, distParamCompilerId = compilerId compiler
436435
, distParamPlatform = platform
437-
, distParamOptimization = fromFlagOrDefault NormalOptimisation optimization
436+
, distParamOptimization = fromNoFlag optimization
438437
}
439438
where
440439
cn = scriptComponentName scriptPath

0 commit comments

Comments
 (0)