ghc-9.6.3: The GHC API

Safe Haskell | Safe-Inferred
Language | Haskell2010

GHC.Utils.Panic

Contents

- GHC exception type
- Command error throwing patterns
  - Assertions
- Call stacks
- Exception utilities

Description

Defines basic functions for printing error messages.

It's hard to put these functions anywhere else without causing some unnecessary loops in the module dependency graph.

Synopsis

- data GhcException
  - = Signal Int
  - | UsageError String
  - | CmdLineError String
  - | Panic String
  - | PprPanic String SDoc
  - | Sorry String
  - | PprSorry String SDoc
  - | InstallationError String
  - | ProgramError String
  - | PprProgramError String SDoc
- showGhcException :: SDocContext -> GhcException -> ShowS
- showGhcExceptionUnsafe :: GhcException -> ShowS
- throwGhcException :: GhcException -> a
- throwGhcExceptionIO :: GhcException -> IO a
- handleGhcException :: ExceptionMonad m => (GhcException -> m a) -> m a -> m a
- pgmError :: HasCallStack => String -> a
- panic :: HasCallStack => String -> a
- pprPanic :: HasCallStack => String -> SDoc -> a
- sorry :: HasCallStack => String -> a
- panicDoc :: String -> SDoc -> a
- sorryDoc :: String -> SDoc -> a
- pgmErrorDoc :: String -> SDoc -> a
- cmdLineError :: String -> a
- cmdLineErrorIO :: String -> IO a
- assertPanic :: String -> Int -> a
- assertPprPanic :: HasCallStack => SDoc -> a
- assertPpr :: HasCallStack => Bool -> SDoc -> a -> a
- assertPprMaybe :: HasCallStack => Maybe SDoc -> a -> a
- assertPprM :: (HasCallStack, Monad m) => m Bool -> SDoc -> m ()
- massertPpr :: (HasCallStack, Applicative m) => Bool -> SDoc -> m ()
- callStackDoc :: HasCallStack => SDoc
- prettyCallStackDoc :: CallStack -> SDoc
- class (Typeable e, Show e) => Exception e where
  - toException :: e -> SomeException
  - fromException :: SomeException -> Maybe e
  - displayException :: e -> String
- showException :: Exception e => e -> String
- safeShowException :: Exception e => e -> IO String
- try :: Exception e => IO a -> IO (Either e a)
- tryMost :: IO a -> IO (Either SomeException a)
- throwTo :: Exception e => ThreadId -> e -> IO ()
- withSignalHandlers :: ExceptionMonad m => m a -> m a

# GHC exception type

data GhcException Source #

GHC's own exception type error messages all take the form:

         <location>: <error>

If the location is on the command line, or in GHC itself, then <location>="ghc". All of the error types below correspond to a <location> of "ghc", except for ProgramError (where the string is assumed to contain a location already, so we don't print one).

Constructors

Signal Int | Some other fatal signal (SIGHUP,SIGTERM)
UsageError String | Prints the short usage msg after the error
CmdLineError String | A problem with the command line arguments, but don't print usage.
Panic String | The `impossible` happened.
PprPanic String SDoc |
Sorry String | The user tickled something that's known not to work yet, but we're not counting it as a bug.
PprSorry String SDoc |
InstallationError String | An installation problem.
ProgramError String | An error in the user's code, probably.
PprProgramError String SDoc |

#### Instances

Instances details

Exception GhcException Source # |
Instance details Defined in GHC.Utils.Panic Methods toException :: GhcException -> SomeException Source # fromException :: SomeException -> Maybe GhcException Source # displayException :: GhcException -> String Source #
Show GhcException Source # |
Instance details Defined in GHC.Utils.Panic Methods showsPrec :: Int -> GhcException -> ShowS Source # show :: GhcException -> String Source # showList :: [GhcException] -> ShowS Source #

showGhcException :: SDocContext -> GhcException -> ShowS Source #

Append a description of the given exception to this string.

showGhcExceptionUnsafe :: GhcException -> ShowS Source #

Append a description of the given exception to this string.

Note that this uses `defaultSDocContext`, which doesn't use the options set by the user via DynFlags.

throwGhcException :: GhcException -> a Source #

throwGhcExceptionIO :: GhcException -> IO a Source #

handleGhcException :: ExceptionMonad m => (GhcException -> m a) -> m a -> m a Source #

# Command error throwing patterns

pgmError :: HasCallStack => String -> a Source #

Panics and asserts.

panic :: HasCallStack => String -> a Source #

Panics and asserts.

pprPanic :: HasCallStack => String -> SDoc -> a Source #

Throw an exception saying "bug in GHC" with a callstack

sorry :: HasCallStack => String -> a Source #

Panics and asserts.

panicDoc :: String -> SDoc -> a Source #

Throw an exception saying "bug in GHC"

sorryDoc :: String -> SDoc -> a Source #

Throw an exception saying "this isn't finished yet"

pgmErrorDoc :: String -> SDoc -> a Source #

Throw an exception saying "bug in pgm being compiled" (used for unusual program errors)

cmdLineError :: String -> a Source #

cmdLineErrorIO :: String -> IO a Source #

## Assertions

assertPanic :: String -> Int -> a Source #

Throw a failed assertion exception for a given filename and line number.

assertPprPanic :: HasCallStack => SDoc -> a Source #

Panic with an assertion failure, recording the given file and line number. Should typically be accessed with the ASSERT family of macros

assertPpr :: HasCallStack => Bool -> SDoc -> a -> a Source #

assertPprMaybe :: HasCallStack => Maybe SDoc -> a -> a Source #

assertPprM :: (HasCallStack, Monad m) => m Bool -> SDoc -> m () Source #

massertPpr :: (HasCallStack, Applicative m) => Bool -> SDoc -> m () Source #

# Call stacks

callStackDoc :: HasCallStack => SDoc Source #

prettyCallStackDoc :: CallStack -> SDoc Source #

# Exception utilities

class (Typeable e, Show e) => Exception e where Source #

Any type that you wish to throw or catch as an exception must be an instance of the `Exception` class. The simplest case is a new exception type directly below the root:

    data MyException = ThisException | ThatException
        deriving Show

    instance Exception MyException

The default method definitions in the `Exception` class do what we need in this case. You can now throw and catch `ThisException` and `ThatException` as exceptions:

    *Main> throw ThisException `catch` \e -> putStrLn ("Caught " ++ show (e :: MyException))
    Caught ThisException

In more complicated examples, you may wish to define a whole hierarchy of exceptions:

    ---------------------------------------------------------------------
    -- Make the root exception type for all the exceptions in a compiler

    data SomeCompilerException = forall e . Exception e => SomeCompilerException e

    instance Show SomeCompilerException where
        show (SomeCompilerException e) = show e

    instance Exception SomeCompilerException

    compilerExceptionToException :: Exception e => e -> SomeException
    compilerExceptionToException = toException . SomeCompilerException

    compilerExceptionFromException :: Exception e => SomeException -> Maybe e
    compilerExceptionFromException x = do
        SomeCompilerException a <- fromException x
        cast a

    ---------------------------------------------------------------------
    -- Make a subhierarchy for exceptions in the frontend of the compiler

    data SomeFrontendException = forall e . Exception e => SomeFrontendException e

    instance Show SomeFrontendException where
        show (SomeFrontendException e) = show e

    instance Exception SomeFrontendException where
        toException = compilerExceptionToException
        fromException = compilerExceptionFromException

    frontendExceptionToException :: Exception e => e -> SomeException
    frontendExceptionToException = toException . SomeFrontendException

    frontendExceptionFromException :: Exception e => SomeException -> Maybe e
    frontendExceptionFromException x = do
        SomeFrontendException a <- fromException x
        cast a

    ---------------------------------------------------------------------
    -- Make an exception type for a particular frontend compiler exception

    data MismatchedParentheses = MismatchedParentheses
        deriving Show

    instance Exception MismatchedParentheses where
        toException   = frontendExceptionToException
        fromException = frontendExceptionFromException

We can now catch a `MismatchedParentheses` exception as `MismatchedParentheses`, `SomeFrontendException` or `SomeCompilerException`, but not other types, e.g. `IOException`:

    *Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: MismatchedParentheses))
    Caught MismatchedParentheses
    *Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: SomeFrontendException))
    Caught MismatchedParentheses
    *Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: SomeCompilerException))
    Caught MismatchedParentheses
    *Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: IOException))
    *** Exception: MismatchedParentheses

Minimal complete definition

Nothing

Methods

toException :: e -> SomeException Source #

fromException :: SomeException -> Maybe e Source #

displayException :: e -> String Source #

Render this exception value in a human-friendly manner.

Default implementation: ``show``.

Since: base-4.8.0.0

#### Instances

Instances details

Exception NestedAtomically | Since: base-4.0
Instance details Defined in Control.Exception.Base Methods toException :: NestedAtomically -> SomeException Source # fromException :: SomeException -> Maybe NestedAtomically Source # displayException :: NestedAtomically -> String Source #
Exception NoMatchingContinuationPrompt | Since: base-4.18
Instance details Defined in Control.Exception.Base Methods toException :: NoMatchingContinuationPrompt -> SomeException Source # fromException :: SomeException -> Maybe NoMatchingContinuationPrompt Source # displayException :: NoMatchingContinuationPrompt -> String Source #
Exception NoMethodError | Since: base-4.0
Instance details Defined in Control.Exception.Base Methods toException :: NoMethodError -> SomeException Source # fromException :: SomeException -> Maybe NoMethodError Source # displayException :: NoMethodError -> String Source #
Exception NonTermination | Since: base-4.0
Instance details Defined in Control.Exception.Base Methods toException :: NonTermination -> SomeException Source # fromException :: SomeException -> Maybe NonTermination Source # displayException :: NonTermination -> String Source #
Exception PatternMatchFail | Since: base-4.0
Instance details Defined in Control.Exception.Base Methods toException :: PatternMatchFail -> SomeException Source # fromException :: SomeException -> Maybe PatternMatchFail Source # displayException :: PatternMatchFail -> String Source #
Exception RecConError | Since: base-4.0
Instance details Defined in Control.Exception.Base Methods toException :: RecConError -> SomeException Source # fromException :: SomeException -> Maybe RecConError Source # displayException :: RecConError -> String Source #
Exception RecSelError | Since: base-4.0
Instance details Defined in Control.Exception.Base Methods toException :: RecSelError -> SomeException Source # fromException :: SomeException -> Maybe RecSelError Source # displayException :: RecSelError -> String Source #
Exception RecUpdError | Since: base-4.0
Instance details Defined in Control.Exception.Base Methods toException :: RecUpdError -> SomeException Source # fromException :: SomeException -> Maybe RecUpdError Source # displayException :: RecUpdError -> String Source #
Exception TypeError | Since: base-4.9.0.0
Instance details Defined in Control.Exception.Base Methods toException :: TypeError -> SomeException Source # fromException :: SomeException -> Maybe TypeError Source # displayException :: TypeError -> String Source #
Exception Dynamic | Since: base-4.0.0.0
Instance details Defined in Data.Dynamic Methods toException :: Dynamic -> SomeException Source # fromException :: SomeException -> Maybe Dynamic Source # displayException :: Dynamic -> String Source #
Exception Void | Since: base-4.8.0.0
Instance details Defined in GHC.Exception.Type Methods toException :: Void -> SomeException Source # fromException :: SomeException -> Maybe Void Source # displayException :: Void -> String Source #
Exception ErrorCall | Since: base-4.0.0.0
Instance details Defined in GHC.Exception Methods toException :: ErrorCall -> SomeException Source # fromException :: SomeException -> Maybe ErrorCall Source # displayException :: ErrorCall -> String Source #
Exception ArithException | Since: base-4.0.0.0
Instance details Defined in GHC.Exception.Type Methods toException :: ArithException -> SomeException Source # fromException :: SomeException -> Maybe ArithException Source # displayException :: ArithException -> String Source #
Exception SomeException | Since: base-3.0
Instance details Defined in GHC.Exception.Type Methods toException :: SomeException -> SomeException Source # fromException :: SomeException -> Maybe SomeException Source # displayException :: SomeException -> String Source #
Exception AllocationLimitExceeded | Since: base-4.8.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: AllocationLimitExceeded -> SomeException Source # fromException :: SomeException -> Maybe AllocationLimitExceeded Source # displayException :: AllocationLimitExceeded -> String Source #
Exception ArrayException | Since: base-4.1.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: ArrayException -> SomeException Source # fromException :: SomeException -> Maybe ArrayException Source # displayException :: ArrayException -> String Source #
Exception AssertionFailed | Since: base-4.1.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: AssertionFailed -> SomeException Source # fromException :: SomeException -> Maybe AssertionFailed Source # displayException :: AssertionFailed -> String Source #
Exception AsyncException | Since: base-4.7.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: AsyncException -> SomeException Source # fromException :: SomeException -> Maybe AsyncException Source # displayException :: AsyncException -> String Source #
Exception BlockedIndefinitelyOnMVar | Since: base-4.1.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: BlockedIndefinitelyOnMVar -> SomeException Source # fromException :: SomeException -> Maybe BlockedIndefinitelyOnMVar Source # displayException :: BlockedIndefinitelyOnMVar -> String Source #
Exception BlockedIndefinitelyOnSTM | Since: base-4.1.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: BlockedIndefinitelyOnSTM -> SomeException Source # fromException :: SomeException -> Maybe BlockedIndefinitelyOnSTM Source # displayException :: BlockedIndefinitelyOnSTM -> String Source #
Exception CompactionFailed | Since: base-4.10.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: CompactionFailed -> SomeException Source # fromException :: SomeException -> Maybe CompactionFailed Source # displayException :: CompactionFailed -> String Source #
Exception Deadlock | Since: base-4.1.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: Deadlock -> SomeException Source # fromException :: SomeException -> Maybe Deadlock Source # displayException :: Deadlock -> String Source #
Exception ExitCode | Since: base-4.1.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: ExitCode -> SomeException Source # fromException :: SomeException -> Maybe ExitCode Source # displayException :: ExitCode -> String Source #
Exception FixIOException | Since: base-4.11.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: FixIOException -> SomeException Source # fromException :: SomeException -> Maybe FixIOException Source # displayException :: FixIOException -> String Source #
Exception IOException | Since: base-4.1.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: IOException -> SomeException Source # fromException :: SomeException -> Maybe IOException Source # displayException :: IOException -> String Source #
Exception SomeAsyncException | Since: base-4.7.0.0
Instance details Defined in GHC.IO.Exception Methods toException :: SomeAsyncException -> SomeException Source # fromException :: SomeException -> Maybe SomeAsyncException Source # displayException :: SomeAsyncException -> String Source #
Exception GhcApiError Source # |
Instance details Defined in GHC Methods toException :: GhcApiError -> SomeException Source # fromException :: SomeException -> Maybe GhcApiError Source # displayException :: GhcApiError -> String Source #
Exception IOEnvFailure Source # |
Instance details Defined in GHC.Data.IOEnv Methods toException :: IOEnvFailure -> SomeException Source # fromException :: SomeException -> Maybe IOEnvFailure Source # displayException :: IOEnvFailure -> String Source #
Exception SourceError Source # |
Instance details Defined in GHC.Types.SourceError Methods toException :: SourceError -> SomeException Source # fromException :: SomeException -> Maybe SourceError Source # displayException :: SourceError -> String Source #
Exception GhcException Source # |
Instance details Defined in GHC.Utils.Panic Methods toException :: GhcException -> SomeException Source # fromException :: SomeException -> Maybe GhcException Source # displayException :: GhcException -> String Source #
Exception PlainGhcException Source # |
Instance details Defined in GHC.Utils.Panic.Plain Methods toException :: PlainGhcException -> SomeException Source # fromException :: SomeException -> Maybe PlainGhcException Source # displayException :: PlainGhcException -> String Source #

showException :: Exception e => e -> String Source #

Show an exception as a string.

safeShowException :: Exception e => e -> IO String Source #

Show an exception which can possibly throw other exceptions. Used when displaying exception thrown within TH code.

try :: Exception e => IO a -> IO (Either e a) Source #

Similar to `catch`, but returns an `Either` result which is `(`Right` a)` if no exception of type `e` was raised, or `(`Left` ex)` if an exception of type `e` was raised and its value is `ex`. If any other type of exception is raised then it will be propagated up to the next enclosing exception handler.

     try a = catch (Right `liftM` a) (return . Left)

tryMost :: IO a -> IO (Either SomeException a) Source #

Like try, but pass through UserInterrupt and Panic exceptions. Used when we want soft failures when reading interface files, for example. TODO: I'm not entirely sure if this is catching what we really want to catch

throwTo :: Exception e => ThreadId -> e -> IO () Source #

`throwTo` raises an arbitrary exception in the target thread (GHC only).

Exception delivery synchronizes between the source and target thread: `throwTo` does not return until the exception has been raised in the target thread. The calling thread can thus be certain that the target thread has received the exception. Exception delivery is also atomic with respect to other exceptions. Atomicity is a useful property to have when dealing with race conditions: e.g. if there are two threads that can kill each other, it is guaranteed that only one of the threads will get to kill the other.

Whatever work the target thread was doing when the exception was raised is not lost: the computation is suspended until required by another thread.

If the target thread is currently making a foreign call, then the exception will not be raised (and hence `throwTo` will not return) until the call has completed. This is the case regardless of whether the call is inside a `mask` or not. However, in GHC a foreign call can be annotated as `interruptible`, in which case a `throwTo` will cause the RTS to attempt to cause the call to return; see the GHC documentation for more details.

Important note: the behaviour of `throwTo` differs from that described in the paper "Asynchronous exceptions in Haskell" (http://research.microsoft.com/~simonpj/Papers/asynch-exns.htm). In the paper, `throwTo` is non-blocking; but the library implementation adopts a more synchronous design in which `throwTo` does not return until the exception is received by the target thread. The trade-off is discussed in Section 9 of the paper. Like any blocking operation, `throwTo` is therefore interruptible (see Section 5.3 of the paper). Unlike other interruptible operations, however, `throwTo` is always interruptible, even if it does not actually block.

There is no guarantee that the exception will be delivered promptly, although the runtime will endeavour to ensure that arbitrary delays don't occur. In GHC, an exception can only be raised when a thread reaches a safe point, where a safe point is where memory allocation occurs. Some loops do not perform any memory allocation inside the loop and therefore cannot be interrupted by a `throwTo`.

If the target of `throwTo` is the calling thread, then the behaviour is the same as `throwIO`, except that the exception is thrown as an asynchronous exception. This means that if there is an enclosing pure computation, which would be the case if the current IO operation is inside `unsafePerformIO` or `unsafeInterleaveIO`, that computation is not permanently replaced by the exception, but is suspended as if it had received an asynchronous exception.

Note that if `throwTo` is called with the current thread as the target, the exception will be thrown even if the thread is currently inside `mask` or `uninterruptibleMask`.

withSignalHandlers :: ExceptionMonad m => m a -> m a Source #

Temporarily install standard signal handlers for catching ^C, which just throw an exception in the current thread.