{-# LANGUAGE CPP, ScopedTypeVariables, LambdaCase #-}
module PlainPanic
( PlainGhcException(..)
, showPlainGhcException
, panic, sorry, pgmError
, cmdLineError, cmdLineErrorIO
, assertPanic
, progName
) where
#include "HsVersions.h"
import Config
import Exception
import GHC.Stack
import GhcPrelude
import System.Environment
import System.IO.Unsafe
data PlainGhcException
= PlainSignal Int
| PlainUsageError String
| PlainCmdLineError String
| PlainPanic String
| PlainSorry String
| PlainInstallationError String
| PlainProgramError String
instance Exception PlainGhcException
instance Show PlainGhcException where
showsPrec :: Int -> PlainGhcException -> ShowS
showsPrec _ e :: PlainGhcException
e@(PlainProgramError _) = PlainGhcException -> ShowS
showPlainGhcException PlainGhcException
e
showsPrec _ e :: PlainGhcException
e@(PlainCmdLineError _) = String -> ShowS
showString "<command line>: " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PlainGhcException -> ShowS
showPlainGhcException PlainGhcException
e
showsPrec _ e :: PlainGhcException
e = String -> ShowS
showString String
progName ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString ": " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PlainGhcException -> ShowS
showPlainGhcException PlainGhcException
e
progName :: String
progName :: String
progName = IO String -> String
forall a. IO a -> a
unsafePerformIO (IO String
getProgName)
{-# NOINLINE progName #-}
short_usage :: String
short_usage :: String
short_usage = "Usage: For basic information, try the `--help' option."
showPlainGhcException :: PlainGhcException -> ShowS
showPlainGhcException :: PlainGhcException -> ShowS
showPlainGhcException =
\case
PlainSignal n :: Int
n -> String -> ShowS
showString "signal: " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Show a => a -> ShowS
shows Int
n
PlainUsageError str :: String
str -> String -> ShowS
showString String
str ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar '\n' ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
short_usage
PlainCmdLineError str :: String
str -> String -> ShowS
showString String
str
PlainPanic s :: String
s -> ShowS -> ShowS
panicMsg (String -> ShowS
showString String
s)
PlainSorry s :: String
s -> ShowS -> ShowS
sorryMsg (String -> ShowS
showString String
s)
PlainInstallationError str :: String
str -> String -> ShowS
showString String
str
PlainProgramError str :: String
str -> String -> ShowS
showString String
str
where
sorryMsg :: ShowS -> ShowS
sorryMsg :: ShowS -> ShowS
sorryMsg s :: ShowS
s =
String -> ShowS
showString "sorry! (unimplemented feature or known bug)\n"
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString (" (GHC version " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
cProjectVersion String -> ShowS
forall a. [a] -> [a] -> [a]
++ " for " String -> ShowS
forall a. [a] -> [a] -> [a]
++ TargetPlatform_NAME ++ "):\n\t")
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
s ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString "\n"
panicMsg :: ShowS -> ShowS
panicMsg :: ShowS -> ShowS
panicMsg s :: ShowS
s =
String -> ShowS
showString "panic! (the 'impossible' happened)\n"
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString (" (GHC version " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
cProjectVersion String -> ShowS
forall a. [a] -> [a] -> [a]
++ " for " String -> ShowS
forall a. [a] -> [a] -> [a]
++ TargetPlatform_NAME ++ "):\n\t")
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShowS
s ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString "\n\n"
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString "Please report this as a GHC bug: https://www.haskell.org/ghc/reportabug\n"
throwPlainGhcException :: PlainGhcException -> a
throwPlainGhcException :: PlainGhcException -> a
throwPlainGhcException = PlainGhcException -> a
forall a e. Exception e => e -> a
Exception.throw
panic, sorry, pgmError :: String -> a
panic :: String -> a
panic x :: String
x = IO a -> a
forall a. IO a -> a
unsafeDupablePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$ do
[String]
stack <- Ptr CostCentreStack -> IO [String]
ccsToStrings (Ptr CostCentreStack -> IO [String])
-> IO (Ptr CostCentreStack) -> IO [String]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String -> IO (Ptr CostCentreStack)
forall dummy. dummy -> IO (Ptr CostCentreStack)
getCurrentCCS String
x
if [String] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
stack
then PlainGhcException -> IO a
forall a. PlainGhcException -> a
throwPlainGhcException (String -> PlainGhcException
PlainPanic String
x)
else PlainGhcException -> IO a
forall a. PlainGhcException -> a
throwPlainGhcException (String -> PlainGhcException
PlainPanic (String
x String -> ShowS
forall a. [a] -> [a] -> [a]
++ '\n' Char -> ShowS
forall a. a -> [a] -> [a]
: [String] -> String
renderStack [String]
stack))
sorry :: String -> a
sorry x :: String
x = PlainGhcException -> a
forall a. PlainGhcException -> a
throwPlainGhcException (String -> PlainGhcException
PlainSorry String
x)
pgmError :: String -> a
pgmError x :: String
x = PlainGhcException -> a
forall a. PlainGhcException -> a
throwPlainGhcException (String -> PlainGhcException
PlainProgramError String
x)
cmdLineError :: String -> a
cmdLineError :: String -> a
cmdLineError = IO a -> a
forall a. IO a -> a
unsafeDupablePerformIO (IO a -> a) -> (String -> IO a) -> String -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO a
forall a. String -> IO a
cmdLineErrorIO
cmdLineErrorIO :: String -> IO a
cmdLineErrorIO :: String -> IO a
cmdLineErrorIO x :: String
x = do
[String]
stack <- Ptr CostCentreStack -> IO [String]
ccsToStrings (Ptr CostCentreStack -> IO [String])
-> IO (Ptr CostCentreStack) -> IO [String]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String -> IO (Ptr CostCentreStack)
forall dummy. dummy -> IO (Ptr CostCentreStack)
getCurrentCCS String
x
if [String] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
stack
then PlainGhcException -> IO a
forall a. PlainGhcException -> a
throwPlainGhcException (String -> PlainGhcException
PlainCmdLineError String
x)
else PlainGhcException -> IO a
forall a. PlainGhcException -> a
throwPlainGhcException (String -> PlainGhcException
PlainCmdLineError (String
x String -> ShowS
forall a. [a] -> [a] -> [a]
++ '\n' Char -> ShowS
forall a. a -> [a] -> [a]
: [String] -> String
renderStack [String]
stack))
assertPanic :: String -> Int -> a
assertPanic :: String -> Int -> a
assertPanic file :: String
file line :: Int
line =
AssertionFailed -> a
forall a e. Exception e => e -> a
Exception.throw (String -> AssertionFailed
Exception.AssertionFailed
("ASSERT failed! file " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
file String -> ShowS
forall a. [a] -> [a] -> [a]
++ ", line " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
line))