From 500d8cb8d27ee1b1ec2d9a523f04ca798aa7e497 Mon Sep 17 00:00:00 2001
From: Jade <Nils.Jadefalke@gmail.com>
Date: Fri, 26 Jan 2024 13:53:51 +0100
Subject: [PATCH] prevent GHCi (and runghc) from suggesting other symbols when
 not finding main

Fixes: #23996
---
 ghc/GHCi/UI.hs                                | 25 +++++++++++++------
 .../ghc-e/should_fail/T18441fail18.stderr     |  5 ++--
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/ghc/GHCi/UI.hs b/ghc/GHCi/UI.hs
index 9f12240d3482..d0e39e2cc78d 100644
--- a/ghc/GHCi/UI.hs
+++ b/ghc/GHCi/UI.hs
@@ -1622,13 +1622,24 @@ pprInfo (thing, fixity, cls_insts, fam_insts, docs)
 
 runMain :: GhciMonad m => String -> m ()
 runMain s = case toArgsNoLoc s of
-            Left err   -> liftIO (hPutStrLn stderr err)
-            Right args ->
-                do dflags <- getDynFlags
-                   let main = fromMaybe "main" (mainFunIs dflags)
-                   -- Wrap the main function in 'void' to discard its value instead
-                   -- of printing it (#9086). See Haskell 2010 report Chapter 5.
-                   doWithArgs args $ "Control.Monad.void (" ++ main ++ ")"
+              Left err   -> liftIO (hPutStrLn stderr err)
+              Right args -> doWithMain (doWithArgs args)
+  where
+    doWithMain fun = do
+      dflags  <- getDynFlags
+      let main = fromMaybe "main" (mainFunIs dflags)
+      handleSourceError printErrAndMaybeExit $ do
+        -- doing this will prevent the main to run when it is not in scope
+        -- this might seem useless, but it doesn't suggest other functions
+        -- to be used, which is exactly what we want here. See #23996.
+        _ <- GHC.parseName main
+
+        -- Wrap the main function in 'void' to discard its value instead
+        -- of printing it (#9086). See Haskell 2010 report Chapter 5.
+        fun $ "Control.Monad.void (" ++ main ++ ")"
+
+
+
 
 -----------------------------------------------------------------------------
 -- :run
diff --git a/testsuite/tests/ghc-e/should_fail/T18441fail18.stderr b/testsuite/tests/ghc-e/should_fail/T18441fail18.stderr
index 6a298acb5e7d..571bea4c98d5 100644
--- a/testsuite/tests/ghc-e/should_fail/T18441fail18.stderr
+++ b/testsuite/tests/ghc-e/should_fail/T18441fail18.stderr
@@ -1,5 +1,4 @@
 
-<interactive>:0:53: error: [GHC-88464]
-    Variable not in scope: main :: IO a0
-    Suggested fix: Perhaps use ‘min’ (imported from Prelude)
+<interactive>:1:1: error: [GHC-76037]
+    Not in scope: ‘main’
 1
-- 
GitLab