[Git][ghc/ghc][wip/T23942] 2 commits: testsuite changes from implicit GHC.Num.BigNat deps

Matthew Craven (@clyring) gitlab at gitlab.haskell.org
Tue Mar 5 17:31:45 UTC 2024



Matthew Craven pushed to branch wip/T23942 at Glasgow Haskell Compiler / GHC


Commits:
088b0243 by Matthew Craven at 2024-03-05T07:30:56-05:00
testsuite changes from implicit GHC.Num.BigNat deps

- - - - -
023bf72c by Matthew Craven at 2024-03-05T12:30:27-05:00
Attempt to properly track more implicit dependencies

See the new Note [Tracking implicit dependencies].

- - - - -


16 changed files:

- compiler/GHC/Driver/MakeFile.hs
- compiler/GHC/Iface/Type.hs-boot
- libraries/ghc-bignum/src/GHC/Num/Primitives.hs
- libraries/ghc-internal/src/GHC/Internal/Base.hs
- libraries/ghc-internal/src/GHC/Internal/Data/Tuple.hs
- libraries/ghc-internal/src/GHC/Internal/Exception/Type.hs-boot
- libraries/ghc-internal/src/GHC/Internal/IO/Handle/Types.hs-boot
- libraries/ghc-internal/src/GHC/Internal/Maybe.hs
- libraries/ghc-internal/src/GHC/Internal/Stack/Types.hs
- libraries/ghc-internal/src/GHC/Internal/TypeError.hs
- libraries/ghc-prim/GHC/Classes.hs
- libraries/ghc-prim/GHC/Tuple.hs
- testsuite/tests/plugins/plugins09.stdout
- testsuite/tests/plugins/plugins10.stdout
- testsuite/tests/plugins/plugins11.stdout
- testsuite/tests/plugins/static-plugins.stdout


Changes:

=====================================
compiler/GHC/Driver/MakeFile.hs
=====================================
@@ -19,6 +19,7 @@ import qualified GHC
 import GHC.Driver.Monad
 import GHC.Driver.DynFlags
 import GHC.Driver.Ppr
+import GHC.Driver.Config.Finder
 import GHC.Utils.Misc
 import GHC.Driver.Env
 import GHC.Driver.Errors.Types
@@ -34,20 +35,26 @@ import GHC.Utils.TmpFs
 
 import GHC.Iface.Load (cannotFindModule)
 
+import GHC.Builtin.Names
+
 import GHC.Unit.Module
 import GHC.Unit.Module.ModSummary
 import GHC.Unit.Module.Graph
 import GHC.Unit.Finder
+import GHC.Unit.Home
+import GHC.Unit.Env
 
 import GHC.Utils.Exception
 import GHC.Utils.Error
 import GHC.Utils.Logger
 
+import GHC.LanguageExtensions
+
 import System.Directory
 import System.FilePath
 import System.IO
 import System.IO.Error  ( isEOFError )
-import Control.Monad    ( when, forM_ )
+import Control.Monad
 import Data.Maybe       ( isJust )
 import Data.IORef
 import qualified Data.Set as Set
@@ -229,19 +236,22 @@ processDeps dflags hsc_env excl_mods root hdl (AcyclicSCC (ModuleNode _ node))
               obj_file  = msObjFilePath node
               obj_files = insertSuffixes obj_file extra_suffixes
 
-              do_imp loc is_boot pkg_qual imp_mod
+              handle_hi_file hi_file = do
+                { let hi_files = insertSuffixes hi_file extra_suffixes
+                      write_dep (obj,hi) = writeDependency root hdl [obj] hi
+
+                  -- Add one dependency for each suffix;
+                  -- e.g.         A.o   : B.hi
+                  --              A.x_o : B.x_hi
+                ; mapM_ write_dep (obj_files `zip` hi_files) }
+
+              do_import loc is_boot pkg_qual imp_mod
                 = do { mb_hi <- findDependency hsc_env loc pkg_qual imp_mod
                                                is_boot include_pkg_deps
                      ; case mb_hi of {
                            Nothing      -> return () ;
-                           Just hi_file -> do
-                     { let hi_files = insertSuffixes hi_file extra_suffixes
-                           write_dep (obj,hi) = writeDependency root hdl [obj] hi
-
-                        -- Add one dependency for each suffix;
-                        -- e.g.         A.o   : B.hi
-                        --              A.x_o : B.x_hi
-                     ; mapM_ write_dep (obj_files `zip` hi_files) }}}
+                           Just hi_file -> handle_hi_file hi_file
+                     }}
 
 
                 -- Emit std dependency of the object(s) on the source file
@@ -272,15 +282,91 @@ processDeps dflags hsc_env excl_mods root hdl (AcyclicSCC (ModuleNode _ node))
 
                 -- Emit a dependency for each import
 
-        ; let do_imps is_boot idecls = sequence_
-                    [ do_imp loc is_boot mb_pkg mod
+        ; let do_imports is_boot idecls = sequence_
+                    [ do_import loc is_boot mb_pkg mod
                     | (mb_pkg, L loc mod) <- idecls,
                       mod `notElem` excl_mods ]
 
-        ; do_imps IsBoot (ms_srcimps node)
-        ; do_imps NotBoot (ms_imps node)
+        ; do_imports IsBoot (ms_srcimps node)
+        ; do_imports NotBoot (ms_imps node)
+
+        -- Handle implicit built-in stuff:
+        -- see Note [Tracking implicit dependencies]
+        ; let
+            do_implicit_import :: Module -> IO ()
+            do_implicit_import mod = when include_pkg_deps $ do
+              -- hard part: get a ModuleLoc from a Module and the env
+              -- This is mostly copied from GHC.Iface.LoadfindAndReadIface.
+              -- Surely there is a better way to do this?
+              let im = fst (getModuleInstantiation mod)
+                  fc = hsc_FC hsc_env
+                  fopts = initFinderOpts (hsc_dflags hsc_env)
+                  other_fopts = initFinderOpts . homeUnitEnv_dflags <$> (hsc_HUG hsc_env)
+                  unit_state = hsc_units hsc_env
+                  mhome_unit  = hsc_home_unit_maybe hsc_env
+              mb_found <- findExactModule fc fopts other_fopts unit_state mhome_unit im
+              case mb_found of
+                InstalledFound ml _ -> handle_hi_file (ml_hi_file ml)
+                InstalledNoPackage _  -> panic "processDeps.do_implicit_import"
+                InstalledNotFound _ _ -> panic "processDeps.do_implicit_import"
+
+        -- every module implicitly depends on GHC.Types
+        ; unless (ms_mod node == gHC_TYPES) $
+            do_implicit_import gHC_TYPES
+
+        -- A module may implicitly depend on GHC.Tuple if ListTuplePuns is set
+        ; when (xopt ListTuplePuns dflags) $
+            -- see Note [Tracking implicit dependencies], wrinkle TID2
+            unless (isHomeUnitInstanceOf (hsc_home_unit hsc_env) primUnitId) $
+              do_implicit_import gHC_INTERNAL_TUPLE
         }
 
+{-
+Note [Tracking implicit dependencies]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+There are several wired-in things that ghc will read interface
+files to look up even if they are not imported.  They include
+
+ * Monad-related stuff in GHC.Internal.Base, if `do` notation is used
+ * Tuple-related stuff in GHC.Tuple, if the built-in tuple syntax is used
+ * TypeRep-related stuff in GHC.Types, unless `-dno-typeable-binds` is set
+ * deriving-related stuff mostly elsewhere in ghc-prim
+
+In order for GHC's build system to work reliably, we have to track the
+implicit dependencies introduced by GHC's habit of reading these
+interfaces.  So we include them in our -M output if -include-pkg-deps
+is set, with the following wrinkles:
+
+(TID1) We don't actually bother adding implicit dependencies for
+  Monad, Arrow, etc. because the program will presumably fail to
+  typecheck unless these are reachable via explicit imports anyway.
+
+(TID2) Users can opt out of implicitly depending on GHC.Tuple with the
+  NoListTuplePuns languge extension.  Ideally we would just turn off
+  ListTuplePuns in the bits of ghc-prim that GHC.Tuple depends on, but
+  when I tried, I got stupid errors like this:
+
+  libraries/ghc-prim/GHC/Types.hs:371:15: error: [GHC-46574]
+      Cannot parse data constructor in a data/newtype declaration: []
+      |
+  371 | data List a = [] | a : List a
+      |               ^^
+
+  So for now we don't emit this dependency in the `ghc-prim` package,
+  which must explicitly import GHC.Tuple for build-order reasons.
+  Yuck! But this doesn't make things in `ghc-prim` much worse, because
+
+(TID3) We don't even try to track dependencies involving `deriving`.
+  We try to prevent this from causing problems by ensuring that any
+  machinery `deriving` needs to reference related to a typeclass is
+  imported from the defining module for that class.  For example, the
+  class Eq is defined in GHC.Classes, and derived Eq instances can
+  reference GHC.Magic.dataToTag#.  So we make sure that GHC.Magic is
+  imported in GHC.Classes.
+
+  Failing to do this for the Lift class caused #22229, which is sadly
+  still open as of March 2024.
+-}
 
 findDependency  :: HscEnv
                 -> SrcSpan


=====================================
compiler/GHC/Iface/Type.hs-boot
=====================================
@@ -5,10 +5,6 @@ module GHC.Iface.Type
    )
 where
 
--- Empty import to influence the compilation ordering.
--- See Note [Depend on GHC.Num.Integer] in GHC.Base
-import GHC.Base ()
-
 data IfaceAppArgs
 
 data IfaceType


=====================================
libraries/ghc-bignum/src/GHC/Num/Primitives.hs
=====================================
@@ -97,7 +97,6 @@ import GHC.Prim.Exception
 
 import GHC.Prim
 import GHC.Types
-import GHC.Tuple () -- See Note [Depend on GHC.Tuple] in GHC.Base
 
 default ()
 


=====================================
libraries/ghc-internal/src/GHC/Internal/Base.hs
=====================================
@@ -326,8 +326,7 @@ import GHC.Internal.Err
 import GHC.Internal.Maybe
 import {-# SOURCE #-} GHC.Internal.IO (mkUserError, mplusIO)
 
-import GHC.Tuple (Solo (MkSolo)) -- Note [Depend on GHC.Tuple]
-import GHC.Num.Integer ()        -- Note [Depend on GHC.Num.Integer]
+import GHC.Tuple (Solo (MkSolo))
 
 -- See Note [Semigroup stimes cycle]
 import {-# SOURCE #-} GHC.Internal.Num (Num (..))
@@ -348,38 +347,6 @@ infixl 4 <*>, <*, *>, <**>
 default ()              -- Double isn't available yet
 
 {-
-Note [Depend on GHC.Num.Integer]
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The Integer type is special because GHC.CoreToStg.Prep.mkConvertNumLiteral
-lookups names in ghc-bignum interfaces to construct Integer literal values.
-Currently it reads the interface file whether or not the current module *has*
-any Integer literals, so it's important that GHC.Num.Integer is compiled before
-any other module.
-
-The danger is that if the build system doesn't know about the implicit
-dependency on Integer, it'll compile some base module before GHC.Num.Integer,
-resulting in:
-  Failed to load interface for ‘GHC.Num.Integer’
-    There are files missing in the ‘ghc-bignum’ package,
-
-To ensure that GHC.Num.Integer is there, we must ensure that there is a visible
-dependency on GHC.Num.Integer from every module in base.  We make GHC.Internal.Base
-depend on GHC.Num.Integer; and everything else either depends on GHC.Base,
-directly on GHC.Num.Integer, or does not have NoImplicitPrelude (and hence
-depends on Prelude).
-
-The lookup is only disabled for packages ghc-prim and ghc-bignum, which aren't
-allowed to contain any Integer literal.
-
-
-Note [Depend on GHC.Tuple]
-~~~~~~~~~~~~~~~~~~~~~~~~~~
-Similarly, tuple syntax (or ()) creates an implicit dependency on
-GHC.Tuple, so we use the same rule as for Integer --- see Note [Depend on
-GHC.Num.Integer] --- to explain this to the build system.  We make GHC.Internal.Base
-depend on GHC.Tuple, and everything else depends on GHC.Internal.Base or Prelude.
-
-
 Note [Semigroup stimes cycle]
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Semigroup is defined in this module, GHC.Base, with the method


=====================================
libraries/ghc-internal/src/GHC/Internal/Data/Tuple.hs
=====================================
@@ -25,7 +25,6 @@ module GHC.Internal.Data.Tuple
   , swap
   ) where
 
-import GHC.Internal.Base ()      -- Note [Depend on GHC.Tuple]
 import GHC.Tuple (Solo (..), getSolo)
 
 default ()              -- Double isn't available yet


=====================================
libraries/ghc-internal/src/GHC/Internal/Exception/Type.hs-boot
=====================================
@@ -9,8 +9,6 @@ module GHC.Internal.Exception.Type
   , underflowException
   ) where
 
-import GHC.Num.Integer ()   -- See Note [Depend on GHC.Num.Integer] in GHC.Internal.Base
-
 data SomeException
 divZeroException, overflowException,
   ratioZeroDenomException, underflowException :: SomeException


=====================================
libraries/ghc-internal/src/GHC/Internal/IO/Handle/Types.hs-boot
=====================================
@@ -2,7 +2,4 @@
 
 module GHC.Internal.IO.Handle.Types ( Handle ) where
 
--- See Note [Depend on GHC.Num.Integer] in GHC.Internal.Base
-import GHC.Types ()
-
 data Handle


=====================================
libraries/ghc-internal/src/GHC/Internal/Maybe.hs
=====================================
@@ -7,7 +7,6 @@ module GHC.Internal.Maybe
    )
 where
 
-import GHC.Num.Integer () -- See Note [Depend on GHC.Num.Integer] in GHC.Internal.Base
 import GHC.Classes
 
 default ()


=====================================
libraries/ghc-internal/src/GHC/Internal/Stack/Types.hs
=====================================
@@ -54,10 +54,6 @@ import cycle,
 import GHC.Classes (Eq)
 import GHC.Types (Char, Int)
 
--- Make implicit dependency known to build system
-import GHC.Tuple ()       -- See Note [Depend on GHC.Tuple] in GHC.Internal.Base
-import GHC.Num.Integer () -- See Note [Depend on GHC.Num.Integer] in GHC.Internal.Base
-
 -- $setup
 -- >>> import Prelude
 -- >>> import GHC.Internal.Stack (prettyCallStack, callStack)


=====================================
libraries/ghc-internal/src/GHC/Internal/TypeError.hs
=====================================
@@ -32,7 +32,6 @@ module GHC.Internal.TypeError
   ) where
 
 import GHC.Internal.Data.Bool
-import GHC.Num.Integer () -- See Note [Depend on GHC.Num.Integer] in GHC.Internal.Base
 import GHC.Types (TYPE, Constraint, Symbol)
 
 {- Note [Custom type errors]


=====================================
libraries/ghc-prim/GHC/Classes.hs
=====================================
@@ -137,7 +137,10 @@ module GHC.Classes(
  ) where
 
 -- GHC.Magic is used in some derived instances
+-- See Note [Tracking implicit dependencies]
+-- in compiler/GHC/Driver/MakeFile.hs, wrinkle TID3
 import GHC.Magic ()
+
 import GHC.Prim
 import GHC.Tuple
 import GHC.CString (unpackCString#)


=====================================
libraries/ghc-prim/GHC/Tuple.hs
=====================================
@@ -29,10 +29,6 @@ module GHC.Tuple (
   Tuple60(..), Tuple61(..), Tuple62(..), Tuple63(..), Tuple64(..),
 ) where
 
-import GHC.CString ()  -- Make sure we do it first, so that the
-                       -- implicit Typeable stuff can see GHC.Types.TyCon
-                       -- and unpackCString# etc
-
 default () -- Double and Integer aren't available yet
 
 -- | The unit datatype @Unit@ has one non-undefined member, the nullary


=====================================
testsuite/tests/plugins/plugins09.stdout
=====================================
@@ -5,4 +5,3 @@ interfacePlugin: GHC.Internal.Float
 interfacePlugin: GHC.Prim.Ext
 typeCheckPlugin (rn)
 typeCheckPlugin (tc)
-interfacePlugin: GHC.Num.BigNat


=====================================
testsuite/tests/plugins/plugins10.stdout
=====================================
@@ -8,7 +8,6 @@ interfacePlugin: GHC.Prim.Ext
 interfacePlugin: Language.Haskell.TH.Syntax
 typeCheckPlugin (rn)
 typeCheckPlugin (tc)
-interfacePlugin: GHC.Num.BigNat
 parsePlugin(a)
 typeCheckPlugin (rn)
 interfacePlugin: Language.Haskell.TH.Lib.Internal


=====================================
testsuite/tests/plugins/plugins11.stdout
=====================================
@@ -5,4 +5,3 @@ interfacePlugin: GHC.Internal.Float
 interfacePlugin: GHC.Prim.Ext
 typeCheckPlugin (rn)
 typeCheckPlugin (tc)
-interfacePlugin: GHC.Num.BigNat


=====================================
testsuite/tests/plugins/static-plugins.stdout
=====================================
@@ -12,7 +12,6 @@ interfacePlugin: GHC.Internal.TopHandler
 typeCheckPlugin (tc)
 interfacePlugin: GHC.CString
 interfacePlugin: GHC.Prim
-interfacePlugin: GHC.Num.BigNat
 ==pure.1
 ==fp0.0
 parsePlugin()



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/0a83c4255167dbdbc02dccaffda96d60f2ab7ee0...023bf72cf6cf394e6739faa3f51830cd2fc182d0

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/0a83c4255167dbdbc02dccaffda96d60f2ab7ee0...023bf72cf6cf394e6739faa3f51830cd2fc182d0
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/20240305/d0c87beb/attachment-0001.html>


More information about the ghc-commits mailing list