Commit 614028e3 authored by Zhou Fangyi's avatar Zhou Fangyi Committed by Ben Gamari

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
parent a78e23b8
......@@ -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'
......
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"
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
......@@ -4,7 +4,7 @@
-- layout rule instead of explicit braces and semicolons works too
(1,2,3)
Data.Maybe.catMaybes :: [Maybe a] -> [a]
Data.Maybe.fromJust :: Maybe a -> a
Data.Maybe.fromJust :: GHC.Stack.Types.HasCallStack => Maybe a -> a
Data.Maybe.fromMaybe :: a -> Maybe a -> a
Data.Maybe.isJust :: Maybe a -> Bool
Data.Maybe.isNothing :: Maybe a -> Bool
......
......@@ -25,7 +25,7 @@ class GHC.Base.Applicative m => Monad (m :: * -> *)
...
-- imported via Data.Maybe
catMaybes :: [Maybe a] -> [a]
fromJust :: Maybe a -> a
fromJust :: GHC.Stack.Types.HasCallStack => Maybe a -> a
fromMaybe :: a -> Maybe a -> a
isJust :: Maybe a -> GHC.Types.Bool
isNothing :: Maybe a -> GHC.Types.Bool
......
catMaybes :: [Maybe a] -> [a]
fromJust :: Maybe a -> a
fromJust :: GHC.Stack.Types.HasCallStack => Maybe a -> a
fromMaybe :: a -> Maybe a -> a
isJust :: Maybe a -> Bool
isNothing :: Maybe a -> Bool
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment