[Git][ghc/ghc][wip/t22405] Add special case for :Main module in `GHC.IfaceToCore.mk_top_id`

Matthew Pickering (@mpickering) gitlab at gitlab.haskell.org
Wed Nov 9 12:32:18 UTC 2022



Matthew Pickering pushed to branch wip/t22405 at Glasgow Haskell Compiler / GHC


Commits:
7346a019 by Matthew Pickering at 2022-11-09T12:32:08+00:00
Add special case for :Main module in `GHC.IfaceToCore.mk_top_id`

See Note [Root-main Id]

The `:Main` special binding is actually defined in the current module
(hence don't go looking for it externally) but the module name is rOOT_MAIN
rather than the current module so we need this special case.

There was already some similar logic in `GHC.Rename.Env` for
External Core, but now the "External Core" is in interface files it
needs to be moved here instead.

Fixes #22405

- - - - -


9 changed files:

- compiler/GHC/IfaceToCore.hs
- compiler/GHC/Rename/Env.hs
- compiler/GHC/Tc/Module.hs
- + testsuite/tests/driver/fat-iface/T22405/Main.hs
- + testsuite/tests/driver/fat-iface/T22405/Main2.hs
- + testsuite/tests/driver/fat-iface/T22405/Makefile
- + testsuite/tests/driver/fat-iface/T22405/T22405.stdout
- + testsuite/tests/driver/fat-iface/T22405/T22405b.stdout
- + testsuite/tests/driver/fat-iface/T22405/all.T


Changes:

=====================================
compiler/GHC/IfaceToCore.hs
=====================================
@@ -123,6 +123,7 @@ import GHC.Driver.Env.KnotVars
 import GHC.Unit.Module.WholeCoreBindings
 import Data.IORef
 import Data.Foldable
+import GHC.Builtin.Names (ioTyConName, rOOT_MAIN)
 
 {-
 This module takes
@@ -930,7 +931,17 @@ tc_iface_binding i IfUseUnfoldingRhs = return (unfoldingTemplate $ realIdUnfoldi
 tc_iface_binding _ (IfRhs rhs) = tcIfaceExpr rhs
 
 mk_top_id :: IfaceTopBndrInfo -> IfL Id
-mk_top_id (IfGblTopBndr gbl_name) = tcIfaceExtId gbl_name
+mk_top_id (IfGblTopBndr gbl_name)
+  -- See Note [Root-main Id]
+  -- This special binding is actually defined in the current module
+  -- (hence don't go looking for it externally) but the module name is rOOT_MAIN
+  -- rather than the current module so we need this special case.
+  -- See some similar logic in `GHC.Rename.Env`.
+  | Just rOOT_MAIN == nameModule_maybe gbl_name
+    = do
+        ATyCon ioTyCon <- tcIfaceGlobal ioTyConName
+        return $ mkExportedVanillaId gbl_name (mkTyConApp ioTyCon [unitTy])
+  | otherwise = tcIfaceExtId gbl_name
 mk_top_id (IfLclTopBndr raw_name iface_type info details) = do
    name <- newIfaceName (mkVarOccFS raw_name)
    ty <- tcIfaceType iface_type


=====================================
compiler/GHC/Rename/Env.hs
=====================================
@@ -207,6 +207,11 @@ newTopSrcBinder (L loc rdr_name)
         -- the nice Exact name for the TyCon gets swizzled to an Orig name.
         -- Hence the badOrigBinding error message.
         --
+
+        -- MP 2022: I suspect this code path is never called for `rOOT_MAIN` anymore
+        -- because External Core has been removed but we instead have some similar logic for
+        -- serialising whole programs into interface files in GHC.IfaceToCore.mk_top_id.
+
         -- Except for the ":Main.main = ..." definition inserted into
         -- the Main module; ugh!
 


=====================================
compiler/GHC/Tc/Module.hs
=====================================
@@ -2042,6 +2042,14 @@ This is unusual: it's a LocalId whose Name has a Module from another
 module. Tiresomely, we must filter it out again in GHC.Iface.Make, less we
 get two defns for 'main' in the interface file!
 
+When using `-fwrite-if-simplified-core` the root_main_id can end up in an interface file.
+When the interface is read back in we have to add a special case when creating the
+Id because otherwise we would go looking for the :Main module which obviously doesn't
+exist. For this logic see GHC.IfaceToCore.mk_top_id.
+
+There is also some similar (probably dead) logic in GHC.Rename.Env which says it
+was added for External Core which faced a similar issue.
+
 
 *********************************************************
 *                                                       *


=====================================
testsuite/tests/driver/fat-iface/T22405/Main.hs
=====================================
@@ -0,0 +1,4 @@
+module Main where
+
+main :: IO ()
+main = return ()


=====================================
testsuite/tests/driver/fat-iface/T22405/Main2.hs
=====================================
@@ -0,0 +1,6 @@
+module Main2 where
+
+main :: IO ()
+main = return ()
+
+


=====================================
testsuite/tests/driver/fat-iface/T22405/Makefile
=====================================
@@ -0,0 +1,17 @@
+TOP=../../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
+
+TEST_HC_OPTS_NO_RTSOPTS = $(filter-out -rtsopts,$(TEST_HC_OPTS))
+
+clean:
+	rm -f *.hi *.hi-fat *.o
+
+T22405: clean
+	"$(TEST_HC)" $(TEST_HC_OPTS) -fbyte-code-and-object-code Main
+	"$(TEST_HC)" $(TEST_HC_OPTS) -fbyte-code-and-object-code Main
+
+T22405b: clean
+	"$(TEST_HC)" $(TEST_HC_OPTS) -fbyte-code-and-object-code Main2 -main-is Main2
+	"$(TEST_HC)" $(TEST_HC_OPTS) -fbyte-code-and-object-code Main2 -main-is Main2
+


=====================================
testsuite/tests/driver/fat-iface/T22405/T22405.stdout
=====================================
@@ -0,0 +1,2 @@
+[1 of 2] Compiling Main             ( Main.hs, Main.o, interpreted )
+[2 of 2] Linking Main


=====================================
testsuite/tests/driver/fat-iface/T22405/T22405b.stdout
=====================================
@@ -0,0 +1,2 @@
+[1 of 2] Compiling Main2            ( Main2.hs, Main2.o, interpreted )
+[2 of 2] Linking Main2


=====================================
testsuite/tests/driver/fat-iface/T22405/all.T
=====================================
@@ -0,0 +1,2 @@
+test('T22405', [extra_files(['Main.hs'])], makefile_test, ['T22405'])
+test('T22405b', [extra_files(['Main2.hs'])], makefile_test, ['T22405b'])



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/7346a019517822b3a1bf3f26535c7c1f83a12461

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/7346a019517822b3a1bf3f26535c7c1f83a12461
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20221109/401e365d/attachment-0001.html>


More information about the ghc-commits mailing list