diff options
author | Sylvain Henry <sylvain@haskus.fr> | 2021-09-22 18:28:35 +0200 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2021-10-22 19:20:44 -0400 |
commit | 806e49ae36a058dbe4494055a6b936dd153c6194 (patch) | |
tree | 982c304986bf925187aae36997d4602e3419c8e9 /compiler/GHC/Tc | |
parent | 6fd7da745a518a93f6685171701a27283cfe2d4e (diff) | |
download | haskell-806e49ae36a058dbe4494055a6b936dd153c6194.tar.gz |
Refactor package imports
Use an (Raw)PkgQual datatype instead of `Maybe FastString` to represent
package imports. Factorize the code that renames RawPkgQual into PkgQual
in function `rnPkgQual`. Renaming consists in checking if the FastString
is the magic "this" keyword, the home-unit unit-id or something else.
Bump haddock submodule
Diffstat (limited to 'compiler/GHC/Tc')
-rw-r--r-- | compiler/GHC/Tc/Module.hs | 17 | ||||
-rw-r--r-- | compiler/GHC/Tc/Plugin.hs | 4 | ||||
-rw-r--r-- | compiler/GHC/Tc/Utils/Backpack.hs | 60 |
3 files changed, 31 insertions, 50 deletions
diff --git a/compiler/GHC/Tc/Module.hs b/compiler/GHC/Tc/Module.hs index 53ee34ed9e..2c425e6eda 100644 --- a/compiler/GHC/Tc/Module.hs +++ b/compiler/GHC/Tc/Module.hs @@ -153,9 +153,9 @@ import GHC.Types.Avail import GHC.Types.Basic hiding( SuccessFlag(..) ) import GHC.Types.Annotations import GHC.Types.SrcLoc -import GHC.Types.SourceText import GHC.Types.SourceFile import GHC.Types.TyThing.Ppr ( pprTyThingInContext ) +import GHC.Types.PkgQual import qualified GHC.LanguageExtensions as LangExt import GHC.Unit.External @@ -270,7 +270,8 @@ tcRnModuleTcRnM hsc_env mod_sum ; -- TODO This is a little skeevy; maybe handle a bit more directly let { simplifyImport (L _ idecl) = - ( fmap sl_fs (ideclPkgQual idecl) , ideclName idecl) + ( renameRawPkgQual (hsc_unit_env hsc_env) (ideclPkgQual idecl) + , ideclName idecl) } ; raw_sig_imports <- liftIO $ findExtraSigImports hsc_env hsc_src @@ -279,10 +280,9 @@ tcRnModuleTcRnM hsc_env mod_sum $ implicitRequirements hsc_env (map simplifyImport (prel_imports ++ import_decls)) - ; let { mkImport (Nothing, L _ mod_name) = noLocA + ; let { mkImport mod_name = noLocA $ (simpleImportDecl mod_name) - { ideclHiding = Just (False, noLocA [])} - ; mkImport _ = panic "mkImport" } + { ideclHiding = Just (False, noLocA [])}} ; let { withReason t imps = map (,text t) imps } ; let { all_imports = withReason "is implicitly imported" prel_imports ++ withReason "is directly imported" import_decls @@ -2052,10 +2052,9 @@ runTcInteractive hsc_env thing_inside ; !orphs <- fmap (force . concat) . forM (ic_imports icxt) $ \i -> case i of -- force above: see #15111 - IIModule n -> getOrphans n Nothing - IIDecl i -> - let mb_pkg = sl_fs <$> ideclPkgQual i in - getOrphans (unLoc (ideclName i)) mb_pkg + IIModule n -> getOrphans n NoPkgQual + IIDecl i -> getOrphans (unLoc (ideclName i)) + (renameRawPkgQual (hsc_unit_env hsc_env) (ideclPkgQual i)) ; let imports = emptyImportAvails { imp_orphs = orphs diff --git a/compiler/GHC/Tc/Plugin.hs b/compiler/GHC/Tc/Plugin.hs index f65b30db27..f2d9521a8c 100644 --- a/compiler/GHC/Tc/Plugin.hs +++ b/compiler/GHC/Tc/Plugin.hs @@ -86,8 +86,8 @@ import GHC.Utils.Outputable ( SDoc ) import GHC.Core.Type ( Kind, Type, PredType ) import GHC.Types.Id ( Id ) import GHC.Core.InstEnv ( InstEnvs ) -import GHC.Data.FastString ( FastString ) import GHC.Types.Unique ( Unique ) +import GHC.Types.PkgQual ( PkgQual ) -- | Perform some IO, typically to interact with an external tool. @@ -99,7 +99,7 @@ tcPluginTrace :: String -> SDoc -> TcPluginM () tcPluginTrace a b = unsafeTcPluginTcM (traceTc a b) -findImportedModule :: ModuleName -> Maybe FastString -> TcPluginM Finder.FindResult +findImportedModule :: ModuleName -> PkgQual -> TcPluginM Finder.FindResult findImportedModule mod_name mb_pkg = do hsc_env <- getTopEnv let fc = hsc_FC hsc_env diff --git a/compiler/GHC/Tc/Utils/Backpack.hs b/compiler/GHC/Tc/Utils/Backpack.hs index 5948f5a931..5594622100 100644 --- a/compiler/GHC/Tc/Utils/Backpack.hs +++ b/compiler/GHC/Tc/Utils/Backpack.hs @@ -4,9 +4,7 @@ {-# LANGUAGE TypeFamilies #-} module GHC.Tc.Utils.Backpack ( - findExtraSigImports', findExtraSigImports, - implicitRequirements', implicitRequirements, implicitRequirementsShallow, checkUnit, @@ -40,6 +38,7 @@ import GHC.Types.SourceFile import GHC.Types.Var import GHC.Types.Unique.DSet import GHC.Types.Name.Shape +import GHC.Types.PkgQual import GHC.Unit import GHC.Unit.Finder @@ -278,50 +277,33 @@ check_inst sig_inst = do -- process A first, because the merging process will cause B to indirectly -- import A. This function finds the TRANSITIVE closure of all such imports -- we need to make. -findExtraSigImports' :: HscEnv - -> HscSource - -> ModuleName - -> IO (UniqDSet ModuleName) -findExtraSigImports' hsc_env HsigFile modname = - fmap unionManyUniqDSets (forM reqs $ \(Module iuid mod_name) -> - (initIfaceLoad hsc_env +findExtraSigImports :: HscEnv + -> HscSource + -> ModuleName + -> IO [ModuleName] +findExtraSigImports hsc_env HsigFile modname = do + let + dflags = hsc_dflags hsc_env + ctx = initSDocContext dflags defaultUserStyle + unit_state = hsc_units hsc_env + reqs = requirementMerges unit_state modname + holes <- forM reqs $ \(Module iuid mod_name) -> do + initIfaceLoad hsc_env . withException ctx $ moduleFreeHolesPrecise (text "findExtraSigImports") - (mkModule (VirtUnit iuid) mod_name))) - where - dflags = hsc_dflags hsc_env - ctx = initSDocContext dflags defaultUserStyle - unit_state = hsc_units hsc_env - reqs = requirementMerges unit_state modname - -findExtraSigImports' _ _ _ = return emptyUniqDSet - --- | 'findExtraSigImports', but in a convenient form for "GHC.Driver.Make" and --- "GHC.Tc.Module". -findExtraSigImports :: HscEnv -> HscSource -> ModuleName - -> IO [(Maybe FastString, Located ModuleName)] -findExtraSigImports hsc_env hsc_src modname = do - extra_requirements <- findExtraSigImports' hsc_env hsc_src modname - return [ (Nothing, noLoc mod_name) - | mod_name <- uniqDSetToList extra_requirements ] - --- A version of 'implicitRequirements'' which is more friendly --- for "GHC.Tc.Module". -implicitRequirements :: HscEnv - -> [(Maybe FastString, Located ModuleName)] - -> IO [(Maybe FastString, Located ModuleName)] -implicitRequirements hsc_env normal_imports - = do mns <- implicitRequirements' hsc_env normal_imports - return [ (Nothing, noLoc mn) | mn <- mns ] + (mkModule (VirtUnit iuid) mod_name) + return (uniqDSetToList (unionManyUniqDSets holes)) + +findExtraSigImports _ _ _ = return [] -- Given a list of 'import M' statements in a module, figure out -- any extra implicit requirement imports they may have. For -- example, if they 'import M' and M resolves to p[A=<B>,C=D], then -- they actually also import the local requirement B. -implicitRequirements' :: HscEnv - -> [(Maybe FastString, Located ModuleName)] +implicitRequirements :: HscEnv + -> [(PkgQual, Located ModuleName)] -> IO [ModuleName] -implicitRequirements' hsc_env normal_imports +implicitRequirements hsc_env normal_imports = fmap concat $ forM normal_imports $ \(mb_pkg, L _ imp) -> do found <- findImportedModule fc fopts units home_unit imp mb_pkg @@ -342,7 +324,7 @@ implicitRequirements' hsc_env normal_imports -- than a transitive closure done here) all the free holes are still reachable. implicitRequirementsShallow :: HscEnv - -> [(Maybe FastString, Located ModuleName)] + -> [(PkgQual, Located ModuleName)] -> IO ([ModuleName], [InstantiatedUnit]) implicitRequirementsShallow hsc_env normal_imports = go ([], []) normal_imports where |