Panic.lhs 2.25 KB
Newer Older
1
%
2
% (c) The GRASP Project, Glasgow University, 1992-2000
3
4
5
6
7
8
9
10
11
%
\section{Panic error messages}

Defines basic funtions for printing error messages.

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

\begin{code}
12
13
14
15
16
module Panic  
   ( 
     GhcException(..), ghcError, progName, 
     panic, panic#, assertPanic, trace 
   ) where
17

18
import Config
19
import FastTypes
20

21
22
23
24
25
import Dynamic
import IOExts
import Exception

import System
26
27
28
#include "HsVersions.h"
\end{code}

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
GHC's own exception type.

\begin{code}
ghcError :: GhcException -> a
ghcError e = throwDyn e

data GhcException
  = PhaseFailed String ExitCode
  | Interrupted
  | UsageError String		-- prints the short usage msg after the error
  | Panic String		-- the `impossible' happened
  | OtherError String		-- just prints the error message
  deriving Eq

progName = unsafePerformIO (getProgName)
{-# NOINLINE progName #-}

short_usage = "Usage: For basic information, try the `--help' option."
   
instance Show GhcException where
  showsPrec _ e = showString progName . showString ": " . showBarf e

showBarf (UsageError str)
   = showString str . showChar '\n' . showString short_usage
showBarf (OtherError str)
   = showString str
showBarf (PhaseFailed phase code)
   = showString phase . showString " failed, code = " . shows code
showBarf (Interrupted)
   = showString "interrupted"
showBarf (Panic s)
60
61
   = showString ("panic! (the `impossible' happened, GHC version "
		 ++ cProjectVersion ++ "):\n\t"
62
63
64
65
66
67
68
69
70
71
72
73
	         ++ s ++ "\n\n"
	         ++ "Please report it as a compiler bug "
	         ++ "to glasgow-haskell-bugs@haskell.org.\n\n")

ghcExceptionTc = mkTyCon "GhcException"
{-# NOINLINE ghcExceptionTc #-}
instance Typeable GhcException where
  typeOf _ = mkAppTy ghcExceptionTc []
\end{code}

Panics and asserts.

74
75
\begin{code}
panic :: String -> a
76
panic x = throwDyn (Panic x)
77
78
79
80
81

-- #-versions because panic can't return an unboxed int, and that's
-- what TAG_ is with GHC at the moment.  Ugh. (Simon)
-- No, man -- Too Beautiful! (Will)

82
83
panic# :: String -> FastInt
panic# s = case (panic s) of () -> _ILIT 0
84
85

assertPanic :: String -> Int -> a
86
87
88
assertPanic file line = 
  throw (AssertionFailed 
           ("ASSERT failed! file " ++ file ++ ", line " ++ show line))
89
\end{code}