Added HasCallStack constraint for trace functions (#55)

* added HasCallStack constraint for trace functions, for issue #39

* unpack string on 7.6
This commit is contained in:
Stephen Diehl
2017-05-31 10:13:08 +01:00
committed by GitHub
parent c2570f77e6
commit 46220a1a4a
7 changed files with 75 additions and 14 deletions
+2
View File
@@ -50,6 +50,8 @@ library
Functor Functor
Semiring Semiring
Bifunctor Bifunctor
CallStack
Error
Panic Panic
default-extensions: default-extensions:
-4
View File
@@ -32,10 +32,6 @@ import GHC.Float as X (
, showFloat , showFloat
, showSignedFloat , showSignedFloat
) )
import GHC.Err as X (
undefined
, error
)
import GHC.Show as X ( import GHC.Show as X (
Show(..) Show(..)
) )
+18
View File
@@ -0,0 +1,18 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE ConstraintKinds #-}
module CallStack
( HasCallStack
) where
#if MIN_VERSION_base(4,9,0)
import GHC.Stack (HasCallStack)
#elif MIN_VERSION_base(4,8,1)
import qualified GHC.Stack
type HasCallStack = (?callStack :: GHC.Stack.CallStack)
#else
import GHC.Exts (Constraint)
type HasCallStack = (() :: Constraint)
#endif
+3 -7
View File
@@ -3,7 +3,6 @@
module Debug ( module Debug (
undefined, undefined,
error,
trace, trace,
traceM, traceM,
traceId, traceId,
@@ -18,6 +17,7 @@ import Data.Text (Text, unpack)
import Control.Monad (Monad, return) import Control.Monad (Monad, return)
import qualified Base as P import qualified Base as P
import Error (error)
import Show (Print, putStrLn) import Show (Print, putStrLn)
import System.IO.Unsafe (unsafePerformIO) import System.IO.Unsafe (unsafePerformIO)
@@ -34,10 +34,6 @@ traceIO string expr = do
putStrLn string putStrLn string
return expr return expr
{-# WARNING error "'error' remains in code" #-}
error :: Text -> a
error s = P.error (unpack s)
{-# WARNING traceShow "'traceShow' remains in code" #-} {-# WARNING traceShow "'traceShow' remains in code" #-}
traceShow :: P.Show a => a -> b -> b traceShow :: P.Show a => a -> b -> b
traceShow a b = trace (P.show a) b traceShow a b = trace (P.show a) b
@@ -59,7 +55,7 @@ traceId :: Text -> Text
traceId s = trace s s traceId s = trace s s
notImplemented :: a notImplemented :: a
notImplemented = P.error "Not implemented" notImplemented = error "Not implemented"
undefined :: a undefined :: a
undefined = P.undefined undefined = error "Prelude.undefined"
+45
View File
@@ -0,0 +1,45 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE ExistentialQuantification #-}
#if MIN_VERSION_base(4,9,0)
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
#endif
module Error
( error
) where
import GHC.Prim
import Data.Text (Text, unpack)
#if MIN_VERSION_base(4,9,0)
-- Full stack trace.
import GHC.Types (RuntimeRep)
import CallStack (HasCallStack)
import GHC.Exception (errorCallWithCallStackException)
error :: forall (r :: RuntimeRep) . forall (a :: TYPE r) . HasCallStack => Text -> a
error s = raise# (errorCallWithCallStackException (unpack s) ?callstack)
#elif MIN_VERSION_base(4,7,0)
-- Basic Call Stack with callsite.
import GHC.Exception (errorCallException)
error :: Text -> a
error s = raise# (errorCallException (unpack s))
#else
-- No exception tracing.
import GHC.Types
import GHC.Exception
error :: Text -> a
error s = throw (ErrorCall (unpack s))
#endif
+7 -1
View File
@@ -1,5 +1,10 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE Trustworthy #-} {-# LANGUAGE Trustworthy #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveDataTypeable #-}
#if MIN_VERSION_base(4,9,0)
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
#endif
module Panic ( module Panic (
FatalError(..), FatalError(..),
@@ -9,6 +14,7 @@ module Panic (
import Base (Show) import Base (Show)
import Data.Text (Text) import Data.Text (Text)
import Data.Typeable (Typeable) import Data.Typeable (Typeable)
import CallStack (HasCallStack)
import Control.Exception as X import Control.Exception as X
-- | Uncatchable exceptions thrown and never caught. -- | Uncatchable exceptions thrown and never caught.
@@ -17,5 +23,5 @@ data FatalError = FatalError { fatalErrorMessage :: Text }
instance Exception FatalError instance Exception FatalError
panic :: Text -> a panic :: HasCallStack => Text -> a
panic a = throw (FatalError a) panic a = throw (FatalError a)
-2
View File
@@ -49,8 +49,6 @@ import Base as Base hiding (
putStr -- Overriden by Show.putStr putStr -- Overriden by Show.putStr
, putStrLn -- Overriden by Show.putStrLn , putStrLn -- Overriden by Show.putStrLn
, print -- Overriden by Protolude.print , print -- Overriden by Protolude.print
, error -- Overriden by Debug.error
, undefined -- Overriden by Debug.undefined
, show -- Overriden by Protolude.show , show -- Overriden by Protolude.show
, showFloat -- Custom Show instances deprecated. , showFloat -- Custom Show instances deprecated.
, showList -- Custom Show instances deprecated. , showList -- Custom Show instances deprecated.