From 4e17ebb1872f1b0ec7868abe0e92c11b9effd910 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Wed, 11 Jan 2023 22:42:13 -0500 Subject: compiler: Small optimisation of assertM In #22739 @AndreasK noticed that assertM performed the action to compute the asserted predicate regardless of whether DEBUG is enabled. This is inconsistent with the other assertion operations and general convention. Fix this. Closes #22739. --- compiler/GHC/Utils/Panic/Plain.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/compiler/GHC/Utils/Panic/Plain.hs b/compiler/GHC/Utils/Panic/Plain.hs index 10b963bf5d..a12ddb4050 100644 --- a/compiler/GHC/Utils/Panic/Plain.hs +++ b/compiler/GHC/Utils/Panic/Plain.hs @@ -29,6 +29,8 @@ import GHC.Utils.Constants import GHC.Utils.Exception as Exception import GHC.Stack import GHC.Prelude.Basic + +import Control.Monad (when) import System.IO.Unsafe -- | This type is very similar to 'GHC.Utils.Panic.GhcException', but it omits @@ -150,4 +152,8 @@ massert cond = withFrozenCallStack (assert cond (pure ())) assertM :: (HasCallStack, Monad m) => m Bool -> m () {-# INLINE assertM #-} -assertM mcond = withFrozenCallStack (mcond >>= massert) +assertM mcond + | debugIsOn = withFrozenCallStack $ do + res <- mcond + when (not res) assertPanic' + | otherwise = return () -- cgit v1.2.1