Simon Jakobi pushed to branch wip/sjakobi/T27847-ghci-rethrow-backtrace at Glasgow Haskell Compiler / GHC Commits: b5957144 by Simon Jakobi at 2026-09-25T01:11:21+02:00 ghc -e: Print uncaught exceptions without their context Since 2ab02c579a9, exceptions rethrown inside GHCi keep collecting backtraces and WhileHandling annotations, so a failing `ghc -e` or runghc script printed extra "HasCallStack backtrace" blocks pointing into GHC's own sources (#27847). By the time an exception reaches the top-level handler, its context only describes GHC-internal frames: the user program's own context is already part of the message text. So drop the context before printing. This also removes the WhileHandling noise from prettyPrintGhcErrors that predates 2ab02c579a9. Fixes #27847. Assisted-by: Claude Opus 5.5 - - - - - 5 changed files: - compiler/GHC/Utils/Exception.hs - ghc/GHCi/UI.hs - libraries/ghci/GHCi/Message.hs - testsuite/tests/ghc-e/should_fail/T18441fail7.stderr - testsuite/tests/ghc-e/should_run/ghc-e005.stderr Changes: ===================================== compiler/GHC/Utils/Exception.hs ===================================== @@ -11,6 +11,7 @@ import GHC.Prelude.Basic import GHC.IO (catchException) import Control.Exception as CE hiding (assert) +import Control.Exception.Context (emptyExceptionContext) import Control.Monad.IO.Class import Control.Monad.Catch @@ -24,4 +25,8 @@ handleIO = flip catchIO tryIO :: IO a -> IO (Either IOException a) tryIO = CE.try +-- | Remove the context, such as backtraces and annotations, from an exception. +dropExceptionContext :: SomeException -> SomeException +dropExceptionContext e = toException (ExceptionWithContext emptyExceptionContext e) + type ExceptionMonad m = (MonadCatch m, MonadThrow m, MonadMask m, MonadIO m) ===================================== ghc/GHCi/UI.hs ===================================== @@ -1079,8 +1079,12 @@ runGHCi paths maybe_exprs = do -- current progname in the exception text: -- <progname>: <exception> liftIO $ withProgName (progname st) - $ topHandler e -- this used to be topHandlerFastExit, see #2228 + $ topHandler + -- The program's own context is already part of the + -- message (see toSerializableException). The rest + -- comes from GHC itself (#27847). + $ dropExceptionContext e runInputTWithPrefs defaultPrefs defaultSettings $ do -- make `ghc -e` exit nonzero on failure, see #7962, #9916, #17560, #18441 _ <- runCommands' hdle ===================================== libraries/ghci/GHCi/Message.hs ===================================== @@ -466,8 +466,7 @@ data LoadedDLL -- * UserInterrupt has a special meaning -- * In ghc -e, exitWith should exit with the appropriate exit code -- --- So all we need to do is distinguish UserInterrupt and ExitCode, and --- all other exceptions can be represented by their 'show' string. +-- So all we need to do is distinguish UserInterrupt and ExitCode. -- data SerializableException = EUserInterrupt ===================================== testsuite/tests/ghc-e/should_fail/T18441fail7.stderr ===================================== @@ -1,16 +1,4 @@ <interactive>: Uncaught exception ghc-10.1-inplace:GHC.Utils.Panic.GhcException: IO error: "Abcde" does not exist - -While handling ghc-10.1-inplace:GHC.Utils.Panic.GhcException: - | - | IO error: "Abcde" does not exist - | - | HasCallStack backtrace: - | throw, called at compiler/GHC/Utils/Panic.hs:180:21 in ghc-10.1-inplace:GHC.Utils.Panic - | throwGhcException, called at ghc/GHCi/UI.hs:2851:21 in ghc-bin-10.1.20260801-inplace:GHCi.UI - -HasCallStack backtrace: - throwIO, called at compiler/GHC/Utils/Error.hs:513:19 in ghc-10.1-inplace:GHC.Utils.Error - 1 ===================================== testsuite/tests/ghc-e/should_run/ghc-e005.stderr ===================================== @@ -4,8 +4,3 @@ foo HasCallStack backtrace: error, called at ghc-e005.hs:12:10 in main:Main - - -HasCallStack backtrace: - throwIO, called at ghc\GHCi\UI.hs:1655:31 in ghc-bin-10.1.20260629-inplace:GHCi.UI - View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/b595714474d516f621143a1f8b4459e3... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/b595714474d516f621143a1f8b4459e3... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help