From 52f5e8fb2e2401f459f07c0c7e833a068acae0cd Mon Sep 17 00:00:00 2001
From: cydparser <cydparser@gmail.com>
Date: Sat, 5 Aug 2023 21:26:46 -0700
Subject: [PATCH] Fix -ddump-to-file and -ddump-timings interaction (#20316)

---
 compiler/GHC/Utils/Error.hs          | 15 ++++++++++++---
 testsuite/tests/driver/T20316.stderr |  6 ------
 2 files changed, 12 insertions(+), 9 deletions(-)
 delete mode 100644 testsuite/tests/driver/T20316.stderr

diff --git a/compiler/GHC/Utils/Error.hs b/compiler/GHC/Utils/Error.hs
index 999bc49ac2bc..1597a7b8c34a 100644
--- a/compiler/GHC/Utils/Error.hs
+++ b/compiler/GHC/Utils/Error.hs
@@ -414,7 +414,7 @@ withTiming' :: MonadIO m
             -> m a
 withTiming' logger what force_result prtimings action
   = if logVerbAtLeast logger 2 || logHasDumpFlag logger Opt_D_dump_timings
-    then do whenPrintTimings $
+    then do when printTimingsNotDumpToFile $ liftIO $
               logInfo logger $ withPprStyle defaultUserStyle $
                 text "***" <+> what <> colon
             let ctx = log_default_user_context (logFlags logger)
@@ -432,7 +432,7 @@ withTiming' logger what force_result prtimings action
             let alloc = alloc0 - alloc1
                 time = realToFrac (end - start) * 1e-9
 
-            when (logVerbAtLeast logger 2 && prtimings == PrintTimings)
+            when (logVerbAtLeast logger 2 && printTimingsNotDumpToFile)
                 $ liftIO $ logInfo logger $ withPprStyle defaultUserStyle
                     (text "!!!" <+> what <> colon <+> text "finished in"
                      <+> doublePrec 2 time
@@ -452,7 +452,16 @@ withTiming' logger what force_result prtimings action
             pure r
      else action
 
-    where whenPrintTimings = liftIO . when (prtimings == PrintTimings)
+    where whenPrintTimings =
+            liftIO . when printTimings
+
+          printTimings =
+            prtimings == PrintTimings
+
+          -- Avoid both printing to console and dumping to a file (#20316).
+          printTimingsNotDumpToFile =
+            printTimings
+            && not (log_dump_to_file (logFlags logger))
 
           recordAllocs alloc =
             liftIO $ traceMarkerIO $ "GHC:allocs:" ++ show alloc
diff --git a/testsuite/tests/driver/T20316.stderr b/testsuite/tests/driver/T20316.stderr
deleted file mode 100644
index ef979dec18ab..000000000000
--- a/testsuite/tests/driver/T20316.stderr
+++ /dev/null
@@ -1,6 +0,0 @@
-*** initializing unit database:
-*** Chasing dependencies:
-*** Parser [Main]:
-*** Renamer/typechecker [Main]:
-*** Desugar [Main]:
-*** CoreTidy [Main]:
-- 
GitLab