summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorFangyi Zhou <fangyi.zhou@yuriko.moe>2018-11-01 18:21:23 -0400
committerBen Gamari <ben@smart-cactus.org>2018-11-01 18:36:08 -0400
commit614028e3b02a5b71a9fbf9c7028f270760ccdab2 (patch)
tree53d4e3e23825dadb8fdd2482d638f96eaf54e600 /libraries
parenta78e23b8bb614ded2ff842e3a5c2dc51db1fa790 (diff)
downloadhaskell-614028e3b02a5b71a9fbf9c7028f270760ccdab2.tar.gz
Data.Maybe: add callstack for fromJust (Trac #15559)
Per feature request, add `HasCallStack` to `fromJust` in `Data.Maybe` and use `error` instead of `errorWithoutStackTrace`. This allows `fromJust` to print call stacks when throwing the error. Also add a new test case for the behaviour, modify existing test cases for new signature Test Plan: New test cases Reviewers: hvr, bgamari Reviewed By: bgamari Subscribers: ulysses4ever, rwbarton, carter GHC Trac Issues: #15559 Differential Revision: https://phabricator.haskell.org/D5256
Diffstat (limited to 'libraries')
-rw-r--r--libraries/base/Data/Maybe.hs5
-rw-r--r--libraries/base/tests/fromJust.hs10
-rw-r--r--libraries/base/tests/fromJust.stderr4
3 files changed, 17 insertions, 2 deletions
diff --git a/libraries/base/Data/Maybe.hs b/libraries/base/Data/Maybe.hs
index d41ae92672..2a3e0efe7f 100644
--- a/libraries/base/Data/Maybe.hs
+++ b/libraries/base/Data/Maybe.hs
@@ -32,6 +32,7 @@ module Data.Maybe
) where
import GHC.Base
+import GHC.Stack.Types ( HasCallStack )
-- $setup
-- Allow the use of some Prelude functions in doctests.
@@ -143,8 +144,8 @@ isNothing _ = False
-- >>> 2 * (fromJust Nothing)
-- *** Exception: Maybe.fromJust: Nothing
--
-fromJust :: Maybe a -> a
-fromJust Nothing = errorWithoutStackTrace "Maybe.fromJust: Nothing" -- yuck
+fromJust :: HasCallStack => Maybe a -> a
+fromJust Nothing = error "Maybe.fromJust: Nothing" -- yuck
fromJust (Just x) = x
-- | The 'fromMaybe' function takes a default value and and 'Maybe'
diff --git a/libraries/base/tests/fromJust.hs b/libraries/base/tests/fromJust.hs
new file mode 100644
index 0000000000..2da524ffed
--- /dev/null
+++ b/libraries/base/tests/fromJust.hs
@@ -0,0 +1,10 @@
+module Main where
+
+-- Trac #15559: Add HasCallStack to fromJust
+
+import Data.Maybe ( fromJust )
+
+main :: IO ()
+main = do
+ _ <- fromJust Nothing `seq` return ()
+ putStrLn "Should see a stacktrace instead of this"
diff --git a/libraries/base/tests/fromJust.stderr b/libraries/base/tests/fromJust.stderr
new file mode 100644
index 0000000000..9b3a63885f
--- /dev/null
+++ b/libraries/base/tests/fromJust.stderr
@@ -0,0 +1,4 @@
+fromJust.hs: Maybe.fromJust: Nothing
+CallStack (from HasCallStack):
+ error, called at libraries/base/Data/Maybe.hs:148:21 in base:Data.Maybe
+ fromJust, called at fromJust.hs:9:8 in main:Main