[project @ 2002-04-26 12:48:16 by simonmar]
[haskell-directory.git] / GHC / Err.lhs
1 \begin{code}
2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module      :  GHC.Err
6 -- Copyright   :  (c) The University of Glasgow, 1994-2002
7 -- License     :  see libraries/base/LICENSE
8 -- 
9 -- Maintainer  :  cvs-ghc@haskell.org
10 -- Stability   :  internal
11 -- Portability :  non-portable (GHC extensions)
12 --
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\").
15 -- 
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.
19 -- 
20 -----------------------------------------------------------------------------
21
22 module GHC.Err 
23        (
24          irrefutPatError
25        , noMethodBindingError
26        , nonExhaustiveGuardsError
27        , patError
28        , recSelError
29        , recConError
30        , runtimeError              -- :: Addr#  -> a    -- Addr# points to UTF8 encoded C string
31
32        , absentErr, parError       -- :: a
33        , seqError                  -- :: a
34
35        , error                     -- :: String -> a
36        , assertError               -- :: String -> Bool -> a -> a
37        
38        , undefined                 -- :: a
39        ) where
40
41 import GHC.Base
42 import GHC.List     ( span )
43 import GHC.Exception
44 \end{code}
45
46 %*********************************************************
47 %*                                                      *
48 \subsection{Error-ish functions}
49 %*                                                      *
50 %*********************************************************
51
52 \begin{code}
53 -- error stops execution and displays an error message
54 error :: String -> a
55 error s = throw (ErrorCall s)
56
57 -- It is expected that compilers will recognize this and insert error
58 -- messages which are more appropriate to the context in which undefined 
59 -- appears. 
60
61 undefined :: a
62 undefined =  error "Prelude.undefined"
63 \end{code}
64
65 %*********************************************************
66 %*                                                       *
67 \subsection{Compiler generated errors + local utils}
68 %*                                                       *
69 %*********************************************************
70
71 Used for compiler-generated error message;
72 encoding saves bytes of string junk.
73
74 \begin{code}
75 absentErr, parError, seqError :: a
76
77 absentErr = error "Oops! The program has entered an `absent' argument!\n"
78 parError  = error "Oops! Entered GHCerr.parError (a GHC bug -- please report it!)\n"
79 seqError  = error "Oops! Entered seqError (a GHC bug -- please report it!)\n"
80 \end{code}
81
82 \begin{code}
83 recSelError, recConError, irrefutPatError, runtimeError,
84              nonExhaustiveGuardsError, patError, noMethodBindingError
85         :: Addr# -> a   -- All take a UTF8-encoded C string
86
87 recSelError              s = throw (RecSelError (unpackCStringUtf8# s)) -- No location info unfortunately
88 runtimeError             s = error (unpackCStringUtf8# s)               -- No location info unfortunately
89
90 nonExhaustiveGuardsError s = throw (PatternMatchFail (untangle s "Non-exhaustive guards in"))
91 irrefutPatError          s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
92 recConError              s = throw (RecConError      (untangle s "Missing field in record construction"))
93 noMethodBindingError     s = throw (NoMethodError    (untangle s "No instance nor default method for class operation"))
94 patError                 s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
95
96 assertError :: Addr# -> Bool -> a -> a
97 assertError str pred v 
98   | pred      = v
99   | otherwise = throw (AssertionFailed (untangle str "Assertion failed"))
100 \end{code}
101
102
103 (untangle coded message) expects "coded" to be of the form 
104
105         "location|details"
106
107 It prints
108
109         location message details
110
111 \begin{code}
112 untangle :: Addr# -> String -> String
113 untangle coded message
114   =  location
115   ++ ": " 
116   ++ message
117   ++ details
118   ++ "\n"
119   where
120     coded_str = unpackCStringUtf8# coded
121
122     (location, details)
123       = case (span not_bar coded_str) of { (loc, rest) ->
124         case rest of
125           ('|':det) -> (loc, ' ' : det)
126           _         -> (loc, "")
127         }
128     not_bar c = c /= '|'
129 \end{code}