Err.hs 3.3 KB
Newer Older
1
{-# LANGUAGE Trustworthy #-}
2
{-# LANGUAGE NoImplicitPrelude, MagicHash, ImplicitParams #-}
3
{-# LANGUAGE RankNTypes, TypeInType #-}
4
{-# OPTIONS_HADDOCK hide #-}
5

6
7
8
9
10
-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.Err
-- Copyright   :  (c) The University of Glasgow, 1994-2002
-- License     :  see libraries/base/LICENSE
11
--
12
13
14
15
16
17
-- Maintainer  :  cvs-ghc@haskell.org
-- Stability   :  internal
-- Portability :  non-portable (GHC extensions)
--
-- The "GHC.Err" module defines the code for the wired-in error functions,
-- which have a special type in the compiler (with \"open tyvars\").
18
--
19
20
21
-- We cannot define these functions in a module where they might be used
-- (e.g., "GHC.Base"), because the magical wired-in type will get confused
-- with what the typechecker figures out.
22
--
23
24
-----------------------------------------------------------------------------

Eric Seidel's avatar
Eric Seidel committed
25
module GHC.Err( absentErr, error, errorWithoutStackTrace, undefined ) where
26
import GHC.CString ()
27
import GHC.Types (Char, Levity)
Ben Gamari's avatar
Ben Gamari committed
28
import GHC.Stack.Types
Simon Peyton Jones's avatar
Simon Peyton Jones committed
29
import GHC.Prim
30
31
32
import GHC.Integer ()   -- Make sure Integer is compiled first
                        -- because GHC depends on it in a wired-in way
                        -- so the build system doesn't see the dependency
33
import {-# SOURCE #-} GHC.Exception( errorCallWithCallStackException )
34

ross's avatar
ross committed
35
-- | 'error' stops execution and displays an error message.
36
37
error :: forall (v :: Levity). forall (a :: TYPE v).
         (?callStack :: CallStack) => [Char] -> a
38
error s = raise# (errorCallWithCallStackException s ?callStack)
39

Eric Seidel's avatar
Eric Seidel committed
40
41
42
-- | A variant of 'error' that does not produce a stack trace.
--
-- @since 4.9.0.0
43
44
errorWithoutStackTrace :: forall (v :: Levity). forall (a :: TYPE v).
                          [Char] -> a
Eric Seidel's avatar
Eric Seidel committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
errorWithoutStackTrace s
  = let ?callStack = freezeCallStack ?callStack
    in error s
{-# NOINLINE errorWithoutStackTrace #-}

-- Note [Errors in base]
-- ~~~~~~~~~~~~~~~~~~~~~
-- As of base-4.9.0.0, `error` produces a stack trace alongside the
-- error message using the Implicit CallStack machinery. This provides
-- a partial stack trace, containing the call-site of each function
-- with a (?callStack :: CallStack) implicit parameter constraint.
--
-- In base, however, the only functions that have such constraints are
-- error and undefined, so the stack traces from partial functions in
-- base will never contain a call-site in user code. Instead we'll
-- usually just get the actual call to error. Base functions already
-- have a good habit of providing detailed error messages, including the
-- name of the offending partial function, so the partial stack-trace
-- does not provide any extra information, just noise. Thus, we export
-- the callstack-aware error, but within base we use the
65
-- errorWithoutStackTrace variant for more hygienic error messages.
Eric Seidel's avatar
Eric Seidel committed
66
67


ross's avatar
ross committed
68
-- | A special case of 'error'.
69
-- It is expected that compilers will recognize this and insert error
ross's avatar
ross committed
70
-- messages which are more appropriate to the context in which 'undefined'
71
-- appears.
72
73
undefined :: forall (v :: Levity). forall (a :: TYPE v).
             (?callStack :: CallStack) => a
74
75
undefined =  error "Prelude.undefined"

76
77
-- | Used for compiler-generated error message;
-- encoding saves bytes of string junk.
78
absentErr :: a
Eric Seidel's avatar
Eric Seidel committed
79
absentErr = errorWithoutStackTrace "Oops! The program has entered an `absent' argument!\n"