Skip to content

Commit 5c2c130

Browse files
committed
Resolve #6355: Fix most incomplete-uni-patterns
Or replace Just foo = rhs with foo = fromMaybe (error "...") rhs which there are plenty. I didn't tried to refactor these errors away, let cabal panic, if it hits them.
1 parent 009c42d commit 5c2c130

File tree

36 files changed

+166
-121
lines changed

36 files changed

+166
-121
lines changed

Cabal/Cabal.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ library
291291
else
292292
build-depends: unix >= 2.6.0.0 && < 2.8
293293

294-
ghc-options: -Wall -fno-ignore-asserts -fwarn-tabs
294+
ghc-options: -Wall -fno-ignore-asserts -fwarn-tabs -fwarn-incomplete-uni-patterns
295295
if impl(ghc >= 8.0)
296296
ghc-options: -Wcompat -Wnoncanonical-monad-instances
297297

Cabal/Distribution/Backpack/Configure.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ toComponentLocalBuildInfos
162162
. map Right
163163
$ graph
164164
combined_graph = Graph.unionRight external_graph internal_graph
165-
Just local_graph = Graph.closure combined_graph (map nodeKey graph)
165+
local_graph = fromMaybe (error "toComponentLocalBuildInfos: closure returned Nothing")
166+
$ Graph.closure combined_graph (map nodeKey graph)
166167
-- The database of transitively reachable installed packages that the
167168
-- external components the package (as a whole) depends on. This will be
168169
-- used in several ways:

Cabal/Distribution/Compat/Binary.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ module Distribution.Compat.Binary
1818
#endif
1919
) where
2020

21-
import Control.Exception (catch, evaluate)
22-
#if __GLASGOW_HASKELL__ >= 711
23-
import Control.Exception (pattern ErrorCall)
24-
#else
25-
import Control.Exception (ErrorCall(..))
26-
#endif
21+
import Control.Exception (ErrorCall (..), catch, evaluate)
2722
import Data.ByteString.Lazy (ByteString)
2823

2924
#if __GLASGOW_HASKELL__ >= 708 || MIN_VERSION_binary(0,7,0)
@@ -67,5 +62,10 @@ encodeFile f = BSL.writeFile f . encode
6762

6863
decodeOrFailIO :: Binary a => ByteString -> IO (Either String a)
6964
decodeOrFailIO bs =
70-
catch (evaluate (decode bs) >>= return . Right)
71-
$ \(ErrorCall str) -> return $ Left str
65+
catch (evaluate (decode bs) >>= return . Right) handler
66+
where
67+
#if MIN_VERSION_base(4,9,0)
68+
handler (ErrorCallWithLocation str _) = return $ Left str
69+
#else
70+
handler (ErrorCall str) = return $ Left str
71+
#endif

Cabal/Distribution/Parsec.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@ escapeCode = (charEsc <|> charNum <|> charAscii <|> charControl) P.<?> "escape c
379379
nomore :: m ()
380380
nomore = P.notFollowedBy anyd <|> toomuch
381381

382-
(low, ex : high) = splitAt bd dps
382+
(low, ex, high) = case splitAt bd dps of
383+
(low', ex' : high') -> (low', ex', high')
384+
(_, _) -> error "escapeCode: Logic error"
383385
in ((:) <$> P.choice low <*> atMost (length bds) anyd) <* nomore
384386
<|> ((:) <$> ex <*> ([] <$ nomore <|> bounded'' dps bds))
385387
<|> if not (null bds)

Cabal/Distribution/Simple/Build/Macros.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ generateToolVersionMacros progs = concat
109109
++ generateMacros "TOOL_" progname version
110110
| prog <- progs
111111
, isJust . programVersion $ prog
112-
, let progid = programId prog ++ "-" ++ prettyShow version
113-
progname = map fixchar (programId prog)
114-
Just version = programVersion prog
112+
, let progid = programId prog ++ "-" ++ prettyShow version
113+
progname = map fixchar (programId prog)
114+
version = fromMaybe version0 (programVersion prog)
115115
]
116116

117117
-- | Common implementation of 'generatePackageVersionMacros' and
@@ -131,7 +131,11 @@ generateMacros macro_prefix name version =
131131
]
132132
,"\n"]
133133
where
134-
(major1:major2:minor:_) = map show (versionNumbers version ++ repeat 0)
134+
(major1,major2,minor) = case map show (versionNumbers version) of
135+
[] -> ("0", "0", "0")
136+
[x] -> (x, "0", "0")
137+
[x,y] -> (x, y, "0")
138+
(x:y:z:_) -> (x, y, z)
135139

136140
-- | Generate the @CURRENT_COMPONENT_ID@ definition for the component ID
137141
-- of the current package.

Cabal/Distribution/Simple/BuildTarget.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import qualified Distribution.Compat.CharParsing as P
5858

5959
import Control.Monad ( msum )
6060
import Data.List ( stripPrefix, groupBy, partition )
61+
import qualified Data.List.NonEmpty as NE
6162
import Data.Either ( partitionEithers )
6263
import System.FilePath as FilePath
6364
( dropExtension, normalise, splitDirectories, joinPath, splitPath
@@ -318,8 +319,9 @@ resolveBuildTarget pkg userTarget fexists =
318319

319320
where
320321
classifyMatchErrors errs
321-
| not (null expected) = let (things, got:_) = unzip expected in
322-
BuildTargetExpected userTarget things got
322+
| Just expected' <- NE.nonEmpty expected
323+
= let (things, got:|_) = NE.unzip expected' in
324+
BuildTargetExpected userTarget (NE.toList things) got
323325
| not (null nosuch) = BuildTargetNoSuch userTarget nosuch
324326
| otherwise = error $ "resolveBuildTarget: internal error in matching"
325327
where

Cabal/Distribution/Simple/GHC.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ guessRunghcFromGhcPath = guessToolFromGhcPath runghcProgram
317317
getGhcInfo :: Verbosity -> ConfiguredProgram -> IO [(String, String)]
318318
getGhcInfo verbosity ghcProg = Internal.getGhcInfo verbosity implInfo ghcProg
319319
where
320-
Just version = programVersion ghcProg
320+
version = fromMaybe (error "GHC.getGhcInfo: no ghc version") $ programVersion ghcProg
321321
implInfo = ghcVersionImplInfo version
322322

323323
-- | Given a single package DB, return all installed packages.
@@ -363,7 +363,7 @@ toPackageIndex verbosity pkgss progdb = do
363363
return $! mconcat indices
364364

365365
where
366-
Just ghcProg = lookupProgram ghcProgram progdb
366+
ghcProg = fromMaybe (error "GHC.toPackageIndex: no ghc program") $ lookupProgram ghcProgram progdb
367367

368368
getLibDir :: Verbosity -> LocalBuildInfo -> IO FilePath
369369
getLibDir verbosity lbi =
@@ -396,7 +396,7 @@ getUserPackageDB _verbosity ghcProg platform = do
396396
platformAndVersion = Internal.ghcPlatformAndVersionString
397397
platform ghcVersion
398398
packageConfFileName = "package.conf.d"
399-
Just ghcVersion = programVersion ghcProg
399+
ghcVersion = fromMaybe (error "GHC.getUserPackageDB: no ghc version") $ programVersion ghcProg
400400

401401
checkPackageDbEnvVar :: Verbosity -> IO ()
402402
checkPackageDbEnvVar verbosity =
@@ -475,7 +475,7 @@ getInstalledPackagesMonitorFiles verbosity platform progdb =
475475
if isFileStyle then return path
476476
else return (path </> "package.cache")
477477

478-
Just ghcProg = lookupProgram ghcProgram progdb
478+
ghcProg = fromMaybe (error "GHC.toPackageIndex: no ghc program") $ lookupProgram ghcProgram progdb
479479

480480

481481
-- -----------------------------------------------------------------------------
@@ -2032,9 +2032,9 @@ hcPkgInfo progdb = HcPkg.HcPkgInfo
20322032
, HcPkg.suppressFilesCheck = v >= [6,6]
20332033
}
20342034
where
2035-
v = versionNumbers ver
2036-
Just ghcPkgProg = lookupProgram ghcPkgProgram progdb
2037-
Just ver = programVersion ghcPkgProg
2035+
v = versionNumbers ver
2036+
ghcPkgProg = fromMaybe (error "GHC.hcPkgInfo: no ghc program") $ lookupProgram ghcPkgProgram progdb
2037+
ver = fromMaybe (error "GHC.hcPkgInfo: no ghc version") $ programVersion ghcPkgProg
20382038

20392039
registerPackage
20402040
:: Verbosity
@@ -2051,7 +2051,7 @@ pkgRoot :: Verbosity -> LocalBuildInfo -> PackageDB -> IO FilePath
20512051
pkgRoot verbosity lbi = pkgRoot'
20522052
where
20532053
pkgRoot' GlobalPackageDB =
2054-
let Just ghcProg = lookupProgram ghcProgram (withPrograms lbi)
2054+
let ghcProg = fromMaybe (error "GHC.pkgRoot: no ghc program") $ lookupProgram ghcProgram (withPrograms lbi)
20552055
in fmap takeDirectory (getGlobalPackageDB verbosity ghcProg)
20562056
pkgRoot' UserPackageDB = do
20572057
appDir <- getAppUserDataDirectory "ghc"

Cabal/Distribution/Simple/GHCJS.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ guessToolFromGhcjsPath tool ghcjsProg verbosity searchpath
241241
getGhcInfo :: Verbosity -> ConfiguredProgram -> IO [(String, String)]
242242
getGhcInfo verbosity ghcjsProg = Internal.getGhcInfo verbosity implInfo ghcjsProg
243243
where
244-
Just version = programVersion ghcjsProg
244+
version = fromMaybe (error "GHCJS.getGhcInfo: no version") $ programVersion ghcjsProg
245245
implInfo = ghcVersionImplInfo version
246246

247247
-- | Given a single package DB, return all installed packages.
@@ -275,7 +275,7 @@ toPackageIndex verbosity pkgss progdb = do
275275
return $! (mconcat indices)
276276

277277
where
278-
Just ghcjsProg = lookupProgram ghcjsProgram progdb
278+
ghcjsProg = fromMaybe (error "GHCJS.toPackageIndex no ghcjs program") $ lookupProgram ghcjsProgram progdb
279279

280280
getLibDir :: Verbosity -> LocalBuildInfo -> IO FilePath
281281
getLibDir verbosity lbi =
@@ -307,7 +307,7 @@ getUserPackageDB _verbosity ghcjsProg platform = do
307307
platformAndVersion = Internal.ghcPlatformAndVersionString
308308
platform ghcjsVersion
309309
packageConfFileName = "package.conf.d"
310-
Just ghcjsVersion = programVersion ghcjsProg
310+
ghcjsVersion = fromMaybe (error "GHCJS.getUserPackageDB: no version") $ programVersion ghcjsProg
311311

312312
checkPackageDbEnvVar :: Verbosity -> IO ()
313313
checkPackageDbEnvVar verbosity =
@@ -360,7 +360,7 @@ getInstalledPackagesMonitorFiles verbosity platform progdb =
360360
if isFileStyle then return path
361361
else return (path </> "package.cache")
362362

363-
Just ghcjsProg = lookupProgram ghcjsProgram progdb
363+
ghcjsProg = fromMaybe (error "GHCJS.toPackageIndex no ghcjs program") $ lookupProgram ghcjsProgram progdb
364364

365365

366366
toJSLibName :: String -> String
@@ -1782,8 +1782,8 @@ hcPkgInfo progdb = HcPkg.HcPkgInfo { HcPkg.hcPkgProgram = ghcjsPkgProg
17821782
}
17831783
where
17841784
v7_10 = mkVersion [7,10]
1785-
Just ghcjsPkgProg = lookupProgram ghcjsPkgProgram progdb
1786-
Just ver = programVersion ghcjsPkgProg
1785+
ghcjsPkgProg = fromMaybe (error "GHCJS.hcPkgInfo no ghcjs program") $ lookupProgram ghcjsPkgProgram progdb
1786+
ver = fromMaybe (error "GHCJS.hcPkgInfo no ghcjs version") $ programVersion ghcjsPkgProg
17871787

17881788
registerPackage
17891789
:: Verbosity
@@ -1800,7 +1800,7 @@ pkgRoot :: Verbosity -> LocalBuildInfo -> PackageDB -> IO FilePath
18001800
pkgRoot verbosity lbi = pkgRoot'
18011801
where
18021802
pkgRoot' GlobalPackageDB =
1803-
let Just ghcjsProg = lookupProgram ghcjsProgram (withPrograms lbi)
1803+
let ghcjsProg = fromMaybe (error "GHCJS.pkgRoot: no ghcjs program") $ lookupProgram ghcjsProgram (withPrograms lbi)
18041804
in fmap takeDirectory (getGlobalPackageDB verbosity ghcjsProg)
18051805
pkgRoot' UserPackageDB = do
18061806
appDir <- getAppUserDataDirectory "ghcjs"
@@ -1830,4 +1830,4 @@ runCmd progdb exe =
18301830
)
18311831
where
18321832
script = exe <.> "jsexe" </> "all" <.> "js"
1833-
Just ghcjsProg = lookupProgram ghcjsProgram progdb
1833+
ghcjsProg = fromMaybe (error "GHCJS.runCmd: no ghcjs program") $ lookupProgram ghcjsProgram progdb

Cabal/Distribution/Simple/Haddock.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,11 @@ getGhcCppOpts haddockVersion bi =
525525
haddockVersionMacro = "-D__HADDOCK_VERSION__="
526526
++ show (v1 * 1000 + v2 * 10 + v3)
527527
where
528-
[v1, v2, v3] = take 3 $ versionNumbers haddockVersion ++ [0,0]
528+
(v1, v2, v3) = case versionNumbers haddockVersion of
529+
[] -> (0,0,0)
530+
[x] -> (x,0,0)
531+
[x,y] -> (x,y,0)
532+
(x:y:z:_) -> (x,y,z)
529533

530534
getGhcLibDir :: Verbosity -> LocalBuildInfo
531535
-> IO HaddockArgs

Cabal/Distribution/Simple/ShowBuildInfo.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656

5757
module Distribution.Simple.ShowBuildInfo (mkBuildInfo) where
5858

59+
import Distribution.Compat.Prelude
60+
import Prelude ()
61+
5962
import qualified Distribution.Simple.GHC as GHC
6063
import qualified Distribution.Simple.Program.GHC as GHC
6164

@@ -122,7 +125,7 @@ mkBuildInfo pkg_descr lbi _flags targetsToBuild = info
122125
]
123126
where
124127
bi = componentBuildInfo comp
125-
Just comp = lookupComponent pkg_descr name
128+
comp = fromMaybe (error $ "mkBuildInfo: no component " ++ prettyShow name) $ lookupComponent pkg_descr name
126129
compType = case comp of
127130
CLib _ -> "lib"
128131
CExe _ -> "exe"

Cabal/Distribution/Simple/Test/LibV09.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ writeSimpleTestStub :: PD.TestSuite -- ^ library 'TestSuite' for which a stub
207207
writeSimpleTestStub t dir = do
208208
createDirectoryIfMissing True dir
209209
let filename = dir </> stubFilePath t
210-
PD.TestSuiteLibV09 _ m = PD.testInterface t
210+
m = case PD.testInterface t of
211+
PD.TestSuiteLibV09 _ m' -> m'
212+
_ -> error "writeSimpleTestStub: invalid TestSuite passed"
211213
writeFile filename $ simpleTestStub m
212214

213215
-- | Source code for library test suite stub executable

Cabal/Distribution/Simple/UHC.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ getGlobalPackageDir :: Verbosity -> ProgramDb -> IO FilePath
116116
getGlobalPackageDir verbosity progdb = do
117117
output <- getDbProgramOutput verbosity
118118
uhcProgram progdb ["--meta-pkgdir-system"]
119-
-- call to "lines" necessary, because pkgdir contains an extra newline at the end
120-
let [pkgdir] = lines output
119+
-- we need to trim because pkgdir contains an extra newline at the end
120+
let pkgdir = trimEnd output
121121
return pkgdir
122+
where
123+
trimEnd = reverse . dropWhile isSpace . reverse
122124

123125
getUserPackageDir :: NoCallStackIO FilePath
124126
getUserPackageDir = do

Cabal/Distribution/Utils/Structured.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ import Data.Word (Word, Word16, Word32, Word64, Word8)
7979

8080
import qualified Control.Monad.Trans.State.Strict as State
8181

82-
import Control.Exception (catch, evaluate)
83-
#if __GLASGOW_HASKELL__ >= 711
84-
import Control.Exception (pattern ErrorCall)
85-
#else
86-
import Control.Exception (ErrorCall (..))
87-
#endif
82+
import Control.Exception (ErrorCall (..), catch, evaluate)
8883

8984
import GHC.Generics
9085

@@ -277,8 +272,13 @@ structuredDecode lbs = snd (Binary.decode lbs :: (Tag a, a))
277272

278273
structuredDecodeOrFailIO :: (Binary.Binary a, Structured a) => LBS.ByteString -> IO (Either String a)
279274
structuredDecodeOrFailIO bs =
280-
catch (evaluate (structuredDecode bs) >>= return . Right)
281-
$ \(ErrorCall str) -> return $ Left str
275+
catch (evaluate (structuredDecode bs) >>= return . Right) handler
276+
where
277+
#if MIN_VERSION_base(4,9,0)
278+
handler (ErrorCallWithLocation str _) = return $ Left str
279+
#else
280+
handler (ErrorCall str) = return $ Left str
281+
#endif
282282

283283
-------------------------------------------------------------------------------
284284
-- Helper data

cabal-install/Distribution/Client/BuildReports/Storage.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- TODO
2+
{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}
13
-----------------------------------------------------------------------------
24
-- |
35
-- Module : Distribution.Client.Reporting

cabal-install/Distribution/Client/CmdErrorMessages.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ module Distribution.Client.CmdErrorMessages (
77
module Distribution.Client.TargetSelector,
88
) where
99

10+
import Distribution.Client.Compat.Prelude
11+
import Prelude ()
12+
1013
import Distribution.Client.ProjectOrchestration
1114
import Distribution.Client.TargetSelector
1215
( ComponentKindFilter, componentKind, showTargetSelector )
@@ -22,8 +25,7 @@ import Distribution.Solver.Types.OptionalStanza
2225
import Distribution.Deprecated.Text
2326
( display )
2427

25-
import Data.Maybe (isNothing)
26-
import Data.List (sortBy, groupBy, nub)
28+
import qualified Data.List.NonEmpty as NE
2729
import Data.Function (on)
2830

2931

@@ -77,8 +79,8 @@ renderListSemiAnd (x:xs) = x ++ "; " ++ renderListSemiAnd xs
7779
-- > | (pkgname, components) <- sortGroupOn packageName allcomponents ]
7880
--
7981
sortGroupOn :: Ord b => (a -> b) -> [a] -> [(b, [a])]
80-
sortGroupOn key = map (\xs@(x:_) -> (key x, xs))
81-
. groupBy ((==) `on` key)
82+
sortGroupOn key = map (\(x:|xs) -> (key x, x:xs))
83+
. NE.groupBy ((==) `on` key)
8284
. sortBy (compare `on` key)
8385

8486

cabal-install/Distribution/Client/CmdInstall.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ import Distribution.Pretty
125125
import Control.Exception
126126
( catch )
127127
import Control.Monad
128-
( mapM, mapM_ )
128+
( mapM, forM_ )
129129
import qualified Data.ByteString.Lazy.Char8 as BS
130130
import Data.Either
131131
( partitionEithers )
@@ -371,7 +371,7 @@ installAction ( configFlags, configExFlags, installFlags
371371
gatherTargets :: UnitId -> TargetSelector
372372
gatherTargets targetId = TargetPackageNamed pkgName targetFilter
373373
where
374-
Just targetUnit = Map.lookup targetId planMap
374+
targetUnit = Map.findWithDefault (error "cannot find target unit") targetId planMap
375375
PackageIdentifier{..} = packageId targetUnit
376376

377377
targets' = fmap gatherTargets targetIds
@@ -385,12 +385,11 @@ installAction ( configFlags, configExFlags, installFlags
385385

386386
createDirectoryIfMissing True (distSdistDirectory localDistDirLayout)
387387

388-
unless (Map.null targets) $
389-
mapM_
390-
(\(SpecificSourcePackage pkg) -> packageToSdist verbosity
388+
unless (Map.null targets) $ forM_ (localPackages localBaseCtx) $ \lpkg -> case lpkg of
389+
SpecificSourcePackage pkg -> packageToSdist verbosity
391390
(distProjectRootDirectory localDistDirLayout) TarGzArchive
392391
(distSdistFile localDistDirLayout (packageId pkg)) pkg
393-
) (localPackages localBaseCtx)
392+
NamedPackage pkgName _ -> error $ "Got NamedPackage " ++ prettyShow pkgName
394393

395394
if null targets
396395
then return (hackagePkgs, hackageTargets)

cabal-install/Distribution/Client/CmdRepl.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,14 @@ replAction ( configFlags, configExFlags, installFlags
250250
-- help us resolve the targets, but that isn't ideal for performance,
251251
-- especially in the no-project case.
252252
withInstallPlan (lessVerbose verbosity) baseCtx $ \elaboratedPlan _ -> do
253+
-- targets should be non-empty map, but there's no NonEmptyMap yet.
253254
targets <- validatedTargets elaboratedPlan targetSelectors
254255

255256
let
256-
Just (unitId, _) = safeHead $ Map.toList targets
257+
(unitId, _) = fromMaybe (error "panic: targets should be non-empty") $ safeHead $ Map.toList targets
257258
originalDeps = installedUnitId <$> InstallPlan.directDeps elaboratedPlan unitId
258259
oci = OriginalComponentInfo unitId originalDeps
259-
Just pkgId = packageId <$> InstallPlan.lookup elaboratedPlan unitId
260+
pkgId = fromMaybe (error $ "cannot find " ++ prettyShow unitId) $ packageId <$> InstallPlan.lookup elaboratedPlan unitId
260261
baseCtx' = addDepsToProjectTarget (envPackages envFlags) pkgId baseCtx
261262

262263
return (Just oci, baseCtx')

0 commit comments

Comments
 (0)