2 {-# OPTIONS_GHC -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The University of Glasgow, 1994-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC extensions)
13 -- The "GHC.Err" module defines the code for the wired-in error functions,
14 -- which have a special type in the compiler (with \"open tyvars\").
16 -- We cannot define these functions in a module where they might be used
17 -- (e.g., "GHC.Base"), because the magical wired-in type will get confused
18 -- with what the typechecker figures out.
20 -----------------------------------------------------------------------------
26 , noMethodBindingError
27 , nonExhaustiveGuardsError
31 , runtimeError -- :: Addr# -> a -- Addr# points to UTF8 encoded C string
34 , divZeroError -- :: a
35 , overflowError -- :: a
37 , error -- :: String -> a
38 , assertError -- :: String -> Bool -> a -> a
45 import GHC.List ( span )
50 %*********************************************************
52 \subsection{Error-ish functions}
54 %*********************************************************
57 -- | 'error' stops execution and displays an error message.
59 error s = throw (ErrorCall s)
61 -- | A special case of 'error'.
62 -- It is expected that compilers will recognize this and insert error
63 -- messages which are more appropriate to the context in which 'undefined'
67 undefined = error "Prelude.undefined"
70 %*********************************************************
72 \subsection{Compiler generated errors + local utils}
74 %*********************************************************
76 Used for compiler-generated error message;
77 encoding saves bytes of string junk.
82 absentErr = error "Oops! The program has entered an `absent' argument!\n"
86 recSelError, recConError, irrefutPatError, runtimeError,
87 nonExhaustiveGuardsError, patError, noMethodBindingError
88 :: Addr# -> a -- All take a UTF8-encoded C string
90 recSelError s = throw (RecSelError (unpackCStringUtf8# s)) -- No location info unfortunately
91 runtimeError s = error (unpackCStringUtf8# s) -- No location info unfortunately
93 nonExhaustiveGuardsError s = throw (PatternMatchFail (untangle s "Non-exhaustive guards in"))
94 irrefutPatError s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
95 recConError s = throw (RecConError (untangle s "Missing field in record construction"))
96 noMethodBindingError s = throw (NoMethodError (untangle s "No instance nor default method for class operation"))
97 patError s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
99 assertError :: Addr# -> Bool -> a -> a
100 assertError str pred v
102 | otherwise = throw (AssertionFailed (untangle str "Assertion failed"))
106 (untangle coded message) expects "coded" to be of the form
112 location message details
115 untangle :: Addr# -> String -> String
116 untangle coded message
123 coded_str = unpackCStringUtf8# coded
126 = case (span not_bar coded_str) of { (loc, rest) ->
128 ('|':det) -> (loc, ' ' : det)
134 Divide by zero and arithmetic overflow.
135 We put them here because they are needed relatively early
136 in the libraries before the Exception type has been defined yet.
139 {-# NOINLINE divZeroError #-}
141 divZeroError = throw (ArithException DivideByZero)
143 {-# NOINLINE overflowError #-}
145 overflowError = throw (ArithException Overflow)