2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
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
47 import GHC.List ( span )
52 %*********************************************************
54 \subsection{Error-ish functions}
56 %*********************************************************
59 -- | 'error' stops execution and displays an error message.
61 error s = throw (ErrorCall s)
63 -- | A special case of 'error'.
64 -- It is expected that compilers will recognize this and insert error
65 -- messages which are more appropriate to the context in which 'undefined'
69 undefined = error "Prelude.undefined"
72 %*********************************************************
74 \subsection{Compiler generated errors + local utils}
76 %*********************************************************
78 Used for compiler-generated error message;
79 encoding saves bytes of string junk.
84 absentErr = error "Oops! The program has entered an `absent' argument!\n"
88 recSelError, recConError, irrefutPatError, runtimeError,
89 nonExhaustiveGuardsError, patError, noMethodBindingError
90 :: Addr# -> a -- All take a UTF8-encoded C string
92 recSelError s = throw (RecSelError (unpackCStringUtf8# s)) -- No location info unfortunately
93 runtimeError s = error (unpackCStringUtf8# s) -- No location info unfortunately
95 nonExhaustiveGuardsError s = throw (PatternMatchFail (untangle s "Non-exhaustive guards in"))
96 irrefutPatError s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
97 recConError s = throw (RecConError (untangle s "Missing field in record construction"))
98 noMethodBindingError s = throw (NoMethodError (untangle s "No instance nor default method for class operation"))
99 patError s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
101 assertError :: Addr# -> Bool -> a -> a
102 assertError str pred v
104 | otherwise = throw (AssertionFailed (untangle str "Assertion failed"))
108 (untangle coded message) expects "coded" to be of the form
114 location message details
117 untangle :: Addr# -> String -> String
118 untangle coded message
125 coded_str = unpackCStringUtf8# coded
128 = case (span not_bar coded_str) of { (loc, rest) ->
130 ('|':det) -> (loc, ' ' : det)
136 Divide by zero and arithmetic overflow.
137 We put them here because they are needed relatively early
138 in the libraries before the Exception type has been defined yet.
141 {-# NOINLINE divZeroError #-}
143 divZeroError = throw (ArithException DivideByZero)
145 {-# NOINLINE overflowError #-}
147 overflowError = throw (ArithException Overflow)