2 {-# OPTIONS_GHC -fno-implicit-prelude #-}
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
7 -- Copyright : (c) The University of Glasgow, 1994-2002
8 -- License : see libraries/base/LICENSE
10 -- Maintainer : cvs-ghc@haskell.org
11 -- Stability : internal
12 -- Portability : non-portable (GHC extensions)
14 -- The "GHC.Err" module defines the code for the wired-in error functions,
15 -- which have a special type in the compiler (with \"open tyvars\").
17 -- We cannot define these functions in a module where they might be used
18 -- (e.g., "GHC.Base"), because the magical wired-in type will get confused
19 -- with what the typechecker figures out.
21 -----------------------------------------------------------------------------
27 , noMethodBindingError
28 , nonExhaustiveGuardsError
32 , runtimeError -- :: Addr# -> a -- Addr# points to UTF8 encoded C string
35 , divZeroError -- :: a
36 , overflowError -- :: a
38 , error -- :: String -> a
39 , assertError -- :: String -> Bool -> a -> a
46 import GHC.List ( span )
51 %*********************************************************
53 \subsection{Error-ish functions}
55 %*********************************************************
58 -- | 'error' stops execution and displays an error message.
60 error s = throw (ErrorCall s)
62 -- | A special case of 'error'.
63 -- It is expected that compilers will recognize this and insert error
64 -- messages which are more appropriate to the context in which 'undefined'
68 undefined = error "Prelude.undefined"
71 %*********************************************************
73 \subsection{Compiler generated errors + local utils}
75 %*********************************************************
77 Used for compiler-generated error message;
78 encoding saves bytes of string junk.
83 absentErr = error "Oops! The program has entered an `absent' argument!\n"
87 recSelError, recConError, irrefutPatError, runtimeError,
88 nonExhaustiveGuardsError, patError, noMethodBindingError
89 :: Addr# -> a -- All take a UTF8-encoded C string
91 recSelError s = throw (RecSelError (unpackCStringUtf8# s)) -- No location info unfortunately
92 runtimeError s = error (unpackCStringUtf8# s) -- No location info unfortunately
94 nonExhaustiveGuardsError s = throw (PatternMatchFail (untangle s "Non-exhaustive guards in"))
95 irrefutPatError s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
96 recConError s = throw (RecConError (untangle s "Missing field in record construction"))
97 noMethodBindingError s = throw (NoMethodError (untangle s "No instance nor default method for class operation"))
98 patError s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
100 assertError :: Addr# -> Bool -> a -> a
101 assertError str pred v
103 | otherwise = throw (AssertionFailed (untangle str "Assertion failed"))
107 (untangle coded message) expects "coded" to be of the form
113 location message details
116 untangle :: Addr# -> String -> String
117 untangle coded message
124 coded_str = unpackCStringUtf8# coded
127 = case (span not_bar coded_str) of { (loc, rest) ->
129 ('|':det) -> (loc, ' ' : det)
135 Divide by zero and arithmetic overflow.
136 We put them here because they are needed relatively early
137 in the libraries before the Exception type has been defined yet.
140 {-# NOINLINE divZeroError #-}
142 divZeroError = throw (ArithException DivideByZero)
144 {-# NOINLINE overflowError #-}
146 overflowError = throw (ArithException Overflow)