Skip to content

Commit 95a2cdb

Browse files
committed
Control expensive assertions in cabal-install with a build flag.
I added a build flag, 'debug-assertions', and a function, 'debugAssert'. 'debugAssert' only calls 'assert' when the flag is enabled. I only replaced one call to 'assert' so far (in Distribution.Solver.Modular.Linking) in order to resolve haskell#4258.
1 parent adb3f8c commit 95a2cdb

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{-# LANGUAGE CPP #-}
2+
module Distribution.Client.Utils.Assertion (debugAssert) where
3+
4+
#ifdef DEBUG_ASSERTIONS
5+
import Control.Exception (assert)
6+
#endif
7+
8+
-- | Like 'assert', but only enabled with -fdebug-assertions. This function can
9+
-- be used for expensive assertions that should only be turned on during testing
10+
-- or debugging.
11+
debugAssert :: Bool -> a -> a
12+
#ifdef DEBUG_ASSERTIONS
13+
debugAssert = assert
14+
#else
15+
debugAssert _ = id
16+
#endif

cabal-install/Distribution/Solver/Modular/Linking.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import qualified Data.Map as M
1616
import qualified Data.Set as S
1717
import qualified Data.Traversable as T
1818

19+
import Distribution.Client.Utils.Assertion
1920
import Distribution.Solver.Modular.Assignment
2021
import Distribution.Solver.Modular.Dependency
2122
import Distribution.Solver.Modular.Flag
@@ -136,7 +137,7 @@ newtype UpdateState a = UpdateState {
136137
instance MonadState ValidateState UpdateState where
137138
get = UpdateState $ get
138139
put st = UpdateState $ do
139-
assert (lgInvariant $ vsLinks st) $ return ()
140+
debugAssert (lgInvariant $ vsLinks st) $ return ()
140141
put st
141142

142143
lift' :: Either Conflict a -> UpdateState a

cabal-install/cabal-install.cabal

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ Flag network-uri
189189
description: Get Network.URI from the network-uri package
190190
default: True
191191

192+
Flag debug-assertions
193+
description: Enable expensive assertions for testing or debugging
194+
default: False
195+
manual: True
196+
192197
Flag debug-conflict-sets
193198
description: Add additional information to ConflictSets
194199
default: False
@@ -295,6 +300,7 @@ library
295300
Distribution.Client.Update
296301
Distribution.Client.Upload
297302
Distribution.Client.Utils
303+
Distribution.Client.Utils.Assertion
298304
Distribution.Client.Utils.Json
299305
Distribution.Client.World
300306
Distribution.Client.Win32SelfUpgrade
@@ -408,6 +414,9 @@ library
408414
else
409415
build-depends: unix >= 2.5 && < 2.8
410416

417+
if flag(debug-assertions)
418+
cpp-options: -DDEBUG_ASSERTIONS
419+
411420
if flag(debug-conflict-sets)
412421
cpp-options: -DDEBUG_CONFLICT_SETS
413422
build-depends: base >= 4.8
@@ -497,6 +506,9 @@ executable cabal
497506
else
498507
build-depends: unix >= 2.5 && < 2.8
499508

509+
if flag(debug-assertions)
510+
cpp-options: -DDEBUG_ASSERTIONS
511+
500512
if flag(debug-conflict-sets)
501513
cpp-options: -DDEBUG_CONFLICT_SETS
502514
build-depends: base >= 4.8

0 commit comments

Comments
 (0)