Torsten Schmits pushed to branch wip/torsten.schmits/unit-index-debug at Glasgow Haskell Compiler / GHC

Commits:

22 changed files:

Changes:

  • compiler/GHC.hs
    ... ... @@ -341,7 +341,7 @@ import GHC.Builtin.Types.Prim ( alphaTyVars )
    341 341
     import GHC.Data.StringBuffer
    
    342 342
     import GHC.Data.FastString
    
    343 343
     import qualified GHC.LanguageExtensions as LangExt
    
    344
    -import GHC.Rename.Names (renamePkgQual, renameRawPkgQual, gresFromAvails)
    
    344
    +import GHC.Rename.Names (gresFromAvails, hscRenamePkgQual, hscRenameRawPkgQual)
    
    345 345
     
    
    346 346
     import GHC.Tc.Utils.Monad    ( finalSafeMode, fixSafeInstances, initIfaceTcRn )
    
    347 347
     import GHC.Tc.Types
    
    ... ... @@ -625,7 +625,8 @@ setUnitDynFlagsNoCheck uid dflags1 = do
    625 625
     
    
    626 626
       let old_hue = ue_findHomeUnitEnv uid (hsc_unit_env hsc_env)
    
    627 627
       let cached_unit_dbs = homeUnitEnv_unit_dbs old_hue
    
    628
    -  (dbs,unit_state,home_unit,mconstants) <- liftIO $ initUnits logger dflags1 cached_unit_dbs (hsc_all_home_unit_ids hsc_env)
    
    628
    +  index <- hscUnitIndex <$> getSession
    
    629
    +  (dbs,unit_state,home_unit,mconstants) <- liftIO $ initUnits logger dflags1 index cached_unit_dbs (hsc_all_home_unit_ids hsc_env)
    
    629 630
       updated_dflags <- liftIO $ updatePlatformConstants dflags1 mconstants
    
    630 631
     
    
    631 632
       let upd hue =
    
    ... ... @@ -760,6 +761,7 @@ setProgramDynFlags_ invalidate_needed dflags = do
    760 761
         then do
    
    761 762
             -- additionally, set checked dflags so we don't lose fixes
    
    762 763
             old_unit_env <- ue_setFlags dflags0 . hsc_unit_env <$> getSession
    
    764
    +        ue_index <- hscUnitIndex <$> getSession
    
    763 765
     
    
    764 766
             home_unit_graph <- forM (ue_home_unit_graph old_unit_env) $ \homeUnitEnv -> do
    
    765 767
               let cached_unit_dbs = homeUnitEnv_unit_dbs homeUnitEnv
    
    ... ... @@ -767,7 +769,7 @@ setProgramDynFlags_ invalidate_needed dflags = do
    767 769
                   old_hpt = homeUnitEnv_hpt homeUnitEnv
    
    768 770
                   home_units = unitEnv_keys (ue_home_unit_graph old_unit_env)
    
    769 771
     
    
    770
    -          (dbs,unit_state,home_unit,mconstants) <- liftIO $ initUnits logger dflags cached_unit_dbs home_units
    
    772
    +          (dbs,unit_state,home_unit,mconstants) <- liftIO $ initUnits logger dflags ue_index cached_unit_dbs home_units
    
    771 773
     
    
    772 774
               updated_dflags <- liftIO $ updatePlatformConstants dflags0 mconstants
    
    773 775
               pure HomeUnitEnv
    
    ... ... @@ -785,6 +787,7 @@ setProgramDynFlags_ invalidate_needed dflags = do
    785 787
                   , ue_home_unit_graph = home_unit_graph
    
    786 788
                   , ue_current_unit    = ue_currentUnit old_unit_env
    
    787 789
                   , ue_eps             = ue_eps old_unit_env
    
    790
    +              , ue_index
    
    788 791
                   }
    
    789 792
             modifySession $ \h -> hscSetFlags dflags1 h{ hsc_unit_env = unit_env }
    
    790 793
         else modifySession (hscSetFlags dflags0)
    
    ... ... @@ -1379,7 +1382,8 @@ getInsts = withSession $ \hsc_env ->
    1379 1382
     
    
    1380 1383
     getNamePprCtx :: GhcMonad m => m NamePprCtx
    
    1381 1384
     getNamePprCtx = withSession $ \hsc_env -> do
    
    1382
    -  return $ icNamePprCtx (hsc_unit_env hsc_env) (hsc_IC hsc_env)
    
    1385
    +  query <- liftIO $ hscUnitIndexQuery hsc_env
    
    1386
    +  return $ icNamePprCtx (hsc_unit_env hsc_env) query (hsc_IC hsc_env)
    
    1383 1387
     
    
    1384 1388
     -- | Container for information about a 'Module'.
    
    1385 1389
     data ModuleInfo = ModuleInfo {
    
    ... ... @@ -1474,7 +1478,8 @@ mkNamePprCtxForModule ::
    1474 1478
       ModuleInfo ->
    
    1475 1479
       m NamePprCtx
    
    1476 1480
     mkNamePprCtxForModule mod minf = withSession $ \hsc_env -> do
    
    1477
    -  let name_ppr_ctx = mkNamePprCtx ptc (hsc_unit_env hsc_env) (availsToGlobalRdrEnv hsc_env mod (minf_exports minf))
    
    1481
    +  query <- liftIO $ hscUnitIndexQuery hsc_env
    
    1482
    +  let name_ppr_ctx = mkNamePprCtx ptc (hsc_unit_env hsc_env) query (availsToGlobalRdrEnv hsc_env mod (minf_exports minf))
    
    1478 1483
           ptc = initPromotionTickContext (hsc_dflags hsc_env)
    
    1479 1484
       return name_ppr_ctx
    
    1480 1485
     
    
    ... ... @@ -1711,10 +1716,10 @@ modNotLoadedError dflags m loc = throwGhcExceptionIO $ CmdLineError $ showSDoc d
    1711 1716
        parens (text (expectJust "modNotLoadedError" (ml_hs_file loc)))
    
    1712 1717
     
    
    1713 1718
     renamePkgQualM :: GhcMonad m => ModuleName -> Maybe FastString -> m PkgQual
    
    1714
    -renamePkgQualM mn p = withSession $ \hsc_env -> pure (renamePkgQual (hsc_unit_env hsc_env) mn p)
    
    1719
    +renamePkgQualM mn p = withSession $ \hsc_env -> hscRenamePkgQual hsc_env mn p
    
    1715 1720
     
    
    1716 1721
     renameRawPkgQualM :: GhcMonad m => ModuleName -> RawPkgQual -> m PkgQual
    
    1717
    -renameRawPkgQualM mn p = withSession $ \hsc_env -> pure (renameRawPkgQual (hsc_unit_env hsc_env) mn p)
    
    1722
    +renameRawPkgQualM mn p = withSession $ \hsc_env -> hscRenameRawPkgQual hsc_env mn p
    
    1718 1723
     
    
    1719 1724
     -- | Like 'findModule', but differs slightly when the module refers to
    
    1720 1725
     -- a source file, and the file has not been loaded via 'load'.  In
    
    ... ... @@ -1738,7 +1743,8 @@ lookupQualifiedModule NoPkgQual mod_name = withSession $ \hsc_env -> do
    1738 1743
           let units  = hsc_units hsc_env
    
    1739 1744
           let dflags = hsc_dflags hsc_env
    
    1740 1745
           let fopts  = initFinderOpts dflags
    
    1741
    -      res <- findExposedPackageModule fc fopts units mod_name NoPkgQual
    
    1746
    +      query <- hscUnitIndexQuery hsc_env
    
    1747
    +      res <- findExposedPackageModule fc fopts units query mod_name NoPkgQual
    
    1742 1748
           case res of
    
    1743 1749
             Found _ m -> return m
    
    1744 1750
             err       -> throwOneError $ noModError hsc_env noSrcSpan mod_name err
    

  • compiler/GHC/Core/Opt/Pipeline.hs
    ... ... @@ -78,6 +78,8 @@ core2core hsc_env guts@(ModGuts { mg_module = mod
    78 78
                                     , mg_rdr_env = rdr_env })
    
    79 79
       = do { let builtin_passes = getCoreToDo dflags hpt_rule_base extra_vars
    
    80 80
                  uniq_tag = 's'
    
    81
    +       ; query <- hscUnitIndexQuery hsc_env
    
    82
    +       ; let name_ppr_ctx = mkNamePprCtx ptc (hsc_unit_env hsc_env) query rdr_env
    
    81 83
     
    
    82 84
            ; (guts2, stats) <- runCoreM hsc_env hpt_rule_base uniq_tag mod
    
    83 85
                                         name_ppr_ctx loc $
    
    ... ... @@ -100,7 +102,6 @@ core2core hsc_env guts@(ModGuts { mg_module = mod
    100 102
         home_pkg_rules = hptRules hsc_env (moduleUnitId mod) (GWIB { gwib_mod = moduleName mod
    
    101 103
                                                                    , gwib_isBoot = NotBoot })
    
    102 104
         hpt_rule_base  = mkRuleBase home_pkg_rules
    
    103
    -    name_ppr_ctx   = mkNamePprCtx ptc (hsc_unit_env hsc_env) rdr_env
    
    104 105
         ptc            = initPromotionTickContext dflags
    
    105 106
         -- mod: get the module out of the current HscEnv so we can retrieve it from the monad.
    
    106 107
         -- This is very convienent for the users of the monad (e.g. plugins do not have to
    
    ... ... @@ -459,6 +460,7 @@ doCorePass pass guts = do
    459 460
       dflags    <- getDynFlags
    
    460 461
       us        <- getUniqueSupplyM
    
    461 462
       p_fam_env <- getPackageFamInstEnv
    
    463
    +  query <- liftIO $ hscUnitIndexQuery hsc_env
    
    462 464
       let platform = targetPlatform dflags
    
    463 465
       let fam_envs = (p_fam_env, mg_fam_inst_env guts)
    
    464 466
       let updateBinds  f = return $ guts { mg_binds = f (mg_binds guts) }
    
    ... ... @@ -471,6 +473,7 @@ doCorePass pass guts = do
    471 473
             mkNamePprCtx
    
    472 474
               (initPromotionTickContext dflags)
    
    473 475
               (hsc_unit_env hsc_env)
    
    476
    +          query
    
    474 477
               rdr_env
    
    475 478
     
    
    476 479
     
    

  • compiler/GHC/Driver/Backpack.hs
    ... ... @@ -429,6 +429,7 @@ addUnit u = do
    429 429
         logger <- getLogger
    
    430 430
         let dflags0 = hsc_dflags hsc_env
    
    431 431
         let old_unit_env = hsc_unit_env hsc_env
    
    432
    +        ue_index = hscUnitIndex hsc_env
    
    432 433
         newdbs <- case ue_unit_dbs old_unit_env of
    
    433 434
             Nothing  -> panic "addUnit: called too early"
    
    434 435
             Just dbs ->
    
    ... ... @@ -437,7 +438,7 @@ addUnit u = do
    437 438
                    , unitDatabaseUnits = [u]
    
    438 439
                    }
    
    439 440
              in return (dbs ++ [newdb]) -- added at the end because ordering matters
    
    440
    -    (dbs,unit_state,home_unit,mconstants) <- liftIO $ initUnits logger dflags0 (Just newdbs) (hsc_all_home_unit_ids hsc_env)
    
    441
    +    (dbs,unit_state,home_unit,mconstants) <- liftIO $ initUnits logger dflags0 ue_index (Just newdbs) (hsc_all_home_unit_ids hsc_env)
    
    441 442
     
    
    442 443
         -- update platform constants
    
    443 444
         dflags <- liftIO $ updatePlatformConstants dflags0 mconstants
    
    ... ... @@ -452,6 +453,7 @@ addUnit u = do
    452 453
                         (homeUnitId home_unit)
    
    453 454
                         (mkHomeUnitEnv dflags (ue_hpt old_unit_env) (Just home_unit))
    
    454 455
               , ue_eps       = ue_eps old_unit_env
    
    456
    +          , ue_index
    
    455 457
               }
    
    456 458
         setSession $ hscSetFlags dflags $ hsc_env { hsc_unit_env = unit_env }
    
    457 459
     
    
    ... ... @@ -870,6 +872,8 @@ hsModuleToModSummary home_keys pn hsc_src modname
    870 872
         hi_timestamp <- liftIO $ modificationTimeIfExists (ml_hi_file location)
    
    871 873
         hie_timestamp <- liftIO $ modificationTimeIfExists (ml_hie_file location)
    
    872 874
     
    
    875
    +    query <- liftIO $ hscUnitIndexQuery hsc_env
    
    876
    +
    
    873 877
         -- Also copied from 'getImports'
    
    874 878
         let (src_idecls, ord_idecls) = partition ((== IsBoot) . ideclSource . unLoc) imps
    
    875 879
     
    
    ... ... @@ -882,7 +886,7 @@ hsModuleToModSummary home_keys pn hsc_src modname
    882 886
             implicit_imports = mkPrelImports modname loc
    
    883 887
                                              implicit_prelude imps
    
    884 888
     
    
    885
    -        rn_pkg_qual = renameRawPkgQual (hsc_unit_env hsc_env) modname
    
    889
    +        rn_pkg_qual = renameRawPkgQual (hsc_unit_env hsc_env) query modname
    
    886 890
             convImport (L _ i) = (rn_pkg_qual (ideclPkgQual i), reLoc $ ideclName i)
    
    887 891
     
    
    888 892
         extra_sig_imports <- liftIO $ findExtraSigImports hsc_env hsc_src modname
    

  • compiler/GHC/Driver/Env.hs
    ... ... @@ -7,6 +7,8 @@ module GHC.Driver.Env
    7 7
        , hsc_home_unit
    
    8 8
        , hsc_home_unit_maybe
    
    9 9
        , hsc_units
    
    10
    +   , hscUnitIndex
    
    11
    +   , hscUnitIndexQuery
    
    10 12
        , hsc_HPT
    
    11 13
        , hsc_HUE
    
    12 14
        , hsc_HUG
    
    ... ... @@ -58,6 +60,7 @@ import GHC.Unit.Module.ModDetails
    58 60
     import GHC.Unit.Home.ModInfo
    
    59 61
     import GHC.Unit.Env
    
    60 62
     import GHC.Unit.External
    
    63
    +import GHC.Unit.State (UnitIndex, UnitIndexQuery, unitIndexQuery)
    
    61 64
     
    
    62 65
     import GHC.Core         ( CoreRule )
    
    63 66
     import GHC.Core.FamInstEnv
    
    ... ... @@ -118,6 +121,12 @@ hsc_home_unit_maybe = ue_homeUnit . hsc_unit_env
    118 121
     hsc_units :: HasDebugCallStack => HscEnv -> UnitState
    
    119 122
     hsc_units = ue_units . hsc_unit_env
    
    120 123
     
    
    124
    +hscUnitIndex :: HscEnv -> UnitIndex
    
    125
    +hscUnitIndex = ue_index . hsc_unit_env
    
    126
    +
    
    127
    +hscUnitIndexQuery :: HscEnv -> IO UnitIndexQuery
    
    128
    +hscUnitIndexQuery = unitIndexQuery . hscUnitIndex
    
    129
    +
    
    121 130
     hsc_HPT :: HscEnv -> HomePackageTable
    
    122 131
     hsc_HPT = ue_hpt . hsc_unit_env
    
    123 132
     
    

  • compiler/GHC/Driver/Main.hs
    ... ... @@ -2665,9 +2665,10 @@ hscTidy hsc_env guts = do
    2665 2665
         $! {-# SCC "CoreTidy" #-} tidyProgram opts guts
    
    2666 2666
     
    
    2667 2667
       -- post tidy pretty-printing and linting...
    
    2668
    +  query <- hscUnitIndexQuery hsc_env
    
    2668 2669
       let tidy_rules     = md_rules details
    
    2669 2670
       let all_tidy_binds = cg_binds cgguts
    
    2670
    -  let name_ppr_ctx   = mkNamePprCtx ptc (hsc_unit_env hsc_env) (mg_rdr_env guts)
    
    2671
    +  let name_ppr_ctx   = mkNamePprCtx ptc (hsc_unit_env hsc_env) query (mg_rdr_env guts)
    
    2671 2672
           ptc            = initPromotionTickContext (hsc_dflags hsc_env)
    
    2672 2673
     
    
    2673 2674
       endPassHscEnvIO hsc_env name_ppr_ctx CoreTidy all_tidy_binds tidy_rules
    

  • compiler/GHC/Driver/Make.hs
    ... ... @@ -146,6 +146,7 @@ import GHC.Utils.Constants
    146 146
     import GHC.Types.Unique.DFM (udfmRestrictKeysSet)
    
    147 147
     import GHC.Types.Unique
    
    148 148
     import GHC.Iface.Errors.Types
    
    149
    +import GHC.Unit.State (UnitIndexQuery)
    
    149 150
     
    
    150 151
     import qualified GHC.Data.Word64Set as W
    
    151 152
     import GHC.Data.Graph.Directed.Reachability
    
    ... ... @@ -188,12 +189,13 @@ depanalE excluded_mods allow_dup_roots = do
    188 189
         if isEmptyMessages errs
    
    189 190
           then do
    
    190 191
             hsc_env <- getSession
    
    192
    +        query <- liftIO $ hscUnitIndexQuery hsc_env
    
    191 193
             let one_unit_messages get_mod_errs k hue = do
    
    192 194
                   errs <- get_mod_errs
    
    193 195
                   unknown_module_err <- warnUnknownModules (hscSetActiveUnitId k hsc_env) (homeUnitEnv_dflags hue) mod_graph
    
    194 196
     
    
    195 197
                   let unused_home_mod_err = warnMissingHomeModules (homeUnitEnv_dflags hue) (hsc_targets hsc_env) mod_graph
    
    196
    -                  unused_pkg_err = warnUnusedPackages (homeUnitEnv_units hue) (homeUnitEnv_dflags hue) mod_graph
    
    198
    +                  unused_pkg_err = warnUnusedPackages (homeUnitEnv_units hue) query (homeUnitEnv_dflags hue) mod_graph
    
    197 199
     
    
    198 200
     
    
    199 201
                   return $ errs `unionMessages` unused_home_mod_err
    
    ... ... @@ -511,15 +513,15 @@ loadWithCache cache diag_wrapper how_much = do
    511 513
     -- actually loaded packages. All the packages, specified on command line,
    
    512 514
     -- but never loaded, are probably unused dependencies.
    
    513 515
     
    
    514
    -warnUnusedPackages :: UnitState -> DynFlags -> ModuleGraph -> DriverMessages
    
    515
    -warnUnusedPackages us dflags mod_graph =
    
    516
    +warnUnusedPackages :: UnitState -> UnitIndexQuery -> DynFlags -> ModuleGraph -> DriverMessages
    
    517
    +warnUnusedPackages us query dflags mod_graph =
    
    516 518
         let diag_opts = initDiagOpts dflags
    
    517 519
     
    
    518 520
             home_mod_sum = filter (\ms -> homeUnitId_ dflags == ms_unitid ms) (mgModSummaries mod_graph)
    
    519 521
     
    
    520 522
         -- Only need non-source imports here because SOURCE imports are always HPT
    
    521 523
             loadedPackages = concat $
    
    522
    -          mapMaybe (\(fs, mn) -> lookupModulePackage us (unLoc mn) fs)
    
    524
    +          mapMaybe (\(fs, mn) -> lookupModulePackage us query (unLoc mn) fs)
    
    523 525
                 $ concatMap ms_imps home_mod_sum
    
    524 526
     
    
    525 527
             any_import_ghc_prim = any ms_ghc_prim_import home_mod_sum
    
    ... ... @@ -2386,7 +2388,8 @@ getPreprocessedImports hsc_env src_fn mb_phase maybe_buf = do
    2386 2388
               mimps <- getImports popts imp_prelude pi_hspp_buf pi_hspp_fn src_fn
    
    2387 2389
               let mopts = map unLoc $ snd $ getOptions popts pi_hspp_buf src_fn
    
    2388 2390
               pure $ ((, mopts) <$>) $ first (mkMessages . fmap mkDriverPsHeaderMessage . getMessages) mimps
    
    2389
    -  let rn_pkg_qual = renameRawPkgQual (hsc_unit_env hsc_env)
    
    2391
    +  query <- liftIO $ hscUnitIndexQuery hsc_env
    
    2392
    +  let rn_pkg_qual = renameRawPkgQual (hsc_unit_env hsc_env) query
    
    2390 2393
       let rn_imps = fmap (\(pk, lmn@(L _ mn)) -> (rn_pkg_qual mn pk, lmn))
    
    2391 2394
       let pi_srcimps = rn_imps pi_srcimps'
    
    2392 2395
       let pi_theimps = rn_imps pi_theimps'
    

  • compiler/GHC/Driver/Pipeline/Execute.hs
    ... ... @@ -692,9 +692,10 @@ runHscPhase pipe_env hsc_env0 input_fn src_flavour = do
    692 692
       -- gather the imports and module name
    
    693 693
       (hspp_buf,mod_name,imps,src_imps, ghc_prim_imp) <- do
    
    694 694
         buf <- hGetStringBuffer input_fn
    
    695
    +    query <- hscUnitIndexQuery hsc_env
    
    695 696
         let imp_prelude = xopt LangExt.ImplicitPrelude dflags
    
    696 697
             popts = initParserOpts dflags
    
    697
    -        rn_pkg_qual = renameRawPkgQual (hsc_unit_env hsc_env)
    
    698
    +        rn_pkg_qual = renameRawPkgQual (hsc_unit_env hsc_env) query
    
    698 699
             rn_imps = fmap (\(rpk, lmn@(L _ mn)) -> (rn_pkg_qual mn rpk, lmn))
    
    699 700
         eimps <- getImports popts imp_prelude buf input_fn (basename <.> suff)
    
    700 701
         case eimps of
    

  • compiler/GHC/HsToCore.hs
    ... ... @@ -149,7 +149,8 @@ deSugar hsc_env
    149 149
       = do { let dflags = hsc_dflags hsc_env
    
    150 150
                  logger = hsc_logger hsc_env
    
    151 151
                  ptc = initPromotionTickContext (hsc_dflags hsc_env)
    
    152
    -             name_ppr_ctx = mkNamePprCtx ptc (hsc_unit_env hsc_env) rdr_env
    
    152
    +        ; query <- hscUnitIndexQuery hsc_env
    
    153
    +        ; let name_ppr_ctx = mkNamePprCtx ptc (hsc_unit_env hsc_env) query rdr_env
    
    153 154
             ; withTiming logger
    
    154 155
                          (text "Desugar"<+>brackets (ppr mod))
    
    155 156
                          (const ()) $
    

  • compiler/GHC/HsToCore/Monad.hs
    ... ... @@ -89,6 +89,7 @@ import GHC.Data.FastString
    89 89
     
    
    90 90
     import GHC.Unit.Env
    
    91 91
     import GHC.Unit.External
    
    92
    +import GHC.Unit.State (UnitIndexQuery)
    
    92 93
     import GHC.Unit.Module
    
    93 94
     import GHC.Unit.Module.ModGuts
    
    94 95
     
    
    ... ... @@ -264,7 +265,8 @@ mkDsEnvsFromTcGbl hsc_env msg_var tcg_env
    264 265
                                     ++ eps_complete_matches eps     -- from imports
    
    265 266
                  -- re-use existing next_wrapper_num to ensure uniqueness
    
    266 267
                  next_wrapper_num_var = tcg_next_wrapper_num tcg_env
    
    267
    -       ; return $ mkDsEnvs unit_env this_mod rdr_env type_env fam_inst_env ptc
    
    268
    +       ; query <- liftIO $ hscUnitIndexQuery hsc_env
    
    269
    +       ; return $ mkDsEnvs unit_env query this_mod rdr_env type_env fam_inst_env ptc
    
    268 270
                                msg_var cc_st_var next_wrapper_num_var complete_matches
    
    269 271
            }
    
    270 272
     
    
    ... ... @@ -292,6 +294,7 @@ initDsWithModGuts hsc_env (ModGuts { mg_module = this_mod, mg_binds = binds
    292 294
            ; next_wrapper_num <- newIORef emptyModuleEnv
    
    293 295
            ; msg_var <- newIORef emptyMessages
    
    294 296
            ; eps <- liftIO $ hscEPS hsc_env
    
    297
    +       ; query <- liftIO $ hscUnitIndexQuery hsc_env
    
    295 298
            ; let unit_env = hsc_unit_env hsc_env
    
    296 299
                  type_env = typeEnvFromEntities ids tycons patsyns fam_insts
    
    297 300
                  ptc = initPromotionTickContext (hsc_dflags hsc_env)
    
    ... ... @@ -303,7 +306,7 @@ initDsWithModGuts hsc_env (ModGuts { mg_module = this_mod, mg_binds = binds
    303 306
                  bindsToIds (Rec    binds) = map fst binds
    
    304 307
                  ids = concatMap bindsToIds binds
    
    305 308
     
    
    306
    -             envs  = mkDsEnvs unit_env this_mod rdr_env type_env
    
    309
    +             envs  = mkDsEnvs unit_env query this_mod rdr_env type_env
    
    307 310
                                   fam_inst_env ptc msg_var cc_st_var
    
    308 311
                                   next_wrapper_num complete_matches
    
    309 312
            ; runDs hsc_env envs thing_inside
    
    ... ... @@ -342,12 +345,12 @@ initTcDsForSolver thing_inside
    342 345
                Just ret -> pure ret
    
    343 346
                Nothing  -> pprPanic "initTcDsForSolver" (vcat $ pprMsgEnvelopeBagWithLocDefault (getErrorMessages msgs)) }
    
    344 347
     
    
    345
    -mkDsEnvs :: UnitEnv -> Module -> GlobalRdrEnv -> TypeEnv -> FamInstEnv
    
    348
    +mkDsEnvs :: UnitEnv -> UnitIndexQuery -> Module -> GlobalRdrEnv -> TypeEnv -> FamInstEnv
    
    346 349
              -> PromotionTickContext
    
    347 350
              -> IORef (Messages DsMessage) -> IORef CostCentreState
    
    348 351
              -> IORef (ModuleEnv Int) -> CompleteMatches
    
    349 352
              -> (DsGblEnv, DsLclEnv)
    
    350
    -mkDsEnvs unit_env mod rdr_env type_env fam_inst_env ptc msg_var cc_st_var
    
    353
    +mkDsEnvs unit_env query mod rdr_env type_env fam_inst_env ptc msg_var cc_st_var
    
    351 354
              next_wrapper_num complete_matches
    
    352 355
       = let if_genv = IfGblEnv { if_doc       = text "mkDsEnvs"
    
    353 356
       -- Failing tests here are `ghci` and `T11985` if you get this wrong.
    
    ... ... @@ -364,7 +367,7 @@ mkDsEnvs unit_env mod rdr_env type_env fam_inst_env ptc msg_var cc_st_var
    364 367
                                , ds_fam_inst_env = fam_inst_env
    
    365 368
                                , ds_gbl_rdr_env  = rdr_env
    
    366 369
                                , ds_if_env  = (if_genv, if_lenv)
    
    367
    -                           , ds_name_ppr_ctx = mkNamePprCtx ptc unit_env rdr_env
    
    370
    +                           , ds_name_ppr_ctx = mkNamePprCtx ptc unit_env query rdr_env
    
    368 371
                                , ds_msgs    = msg_var
    
    369 372
                                , ds_complete_matches = complete_matches
    
    370 373
                                , ds_cc_st   = cc_st_var
    

  • compiler/GHC/Iface/Recomp.hs
    ... ... @@ -588,7 +588,8 @@ checkDependencies :: HscEnv -> ModSummary -> ModIface -> IfG RecompileRequired
    588 588
     checkDependencies hsc_env summary iface
    
    589 589
      = do
    
    590 590
         res_normal <- classify_import (findImportedModule hsc_env) (ms_textual_imps summary ++ ms_srcimps summary)
    
    591
    -    res_plugin <- classify_import (\mod _ -> findPluginModule fc fopts units mhome_unit mod) (ms_plugin_imps summary)
    
    591
    +    query <- liftIO $ hscUnitIndexQuery hsc_env
    
    592
    +    res_plugin <- classify_import (\mod _ -> findPluginModule fc fopts units query mhome_unit mod) (ms_plugin_imps summary)
    
    592 593
         case sequence (res_normal ++ res_plugin ++ [Right (fake_ghc_prim_import)| ms_ghc_prim_import summary]) of
    
    593 594
           Left recomp -> return $ NeedsRecompile recomp
    
    594 595
           Right es -> do
    

  • compiler/GHC/Rename/Names.hs
    ... ... @@ -27,6 +27,7 @@ module GHC.Rename.Names (
    27 27
             getMinimalImports,
    
    28 28
             printMinimalImports,
    
    29 29
             renamePkgQual, renameRawPkgQual,
    
    30
    +        hscRenamePkgQual, hscRenameRawPkgQual,
    
    30 31
             classifyGREs,
    
    31 32
             ImportDeclUsage,
    
    32 33
         ) where
    
    ... ... @@ -87,6 +88,7 @@ import GHC.Unit.Module.ModIface
    87 88
     import GHC.Unit.Module.Imported
    
    88 89
     import GHC.Unit.Module.Deps
    
    89 90
     import GHC.Unit.Env
    
    91
    +import GHC.Unit.State (UnitIndexQuery, unitIndexQuery)
    
    90 92
     
    
    91 93
     import GHC.Data.Bag
    
    92 94
     import GHC.Data.FastString
    
    ... ... @@ -337,7 +339,8 @@ rnImportDecl this_mod
    337 339
     
    
    338 340
         hsc_env <- getTopEnv
    
    339 341
         unit_env <- hsc_unit_env <$> getTopEnv
    
    340
    -    let pkg_qual = renameRawPkgQual unit_env imp_mod_name raw_pkg_qual
    
    342
    +    query <- unitIndexQuery (ue_index unit_env)
    
    343
    +    let pkg_qual = renameRawPkgQual unit_env query imp_mod_name raw_pkg_qual
    
    341 344
     
    
    342 345
         -- Check for self-import, which confuses the typechecker (#9032)
    
    343 346
         -- ghc --make rejects self-import cycles already, but batch-mode may not
    
    ... ... @@ -447,14 +450,14 @@ rnImportDecl this_mod
    447 450
     
    
    448 451
     
    
    449 452
     -- | Rename raw package imports
    
    450
    -renameRawPkgQual :: UnitEnv -> ModuleName -> RawPkgQual -> PkgQual
    
    451
    -renameRawPkgQual unit_env mn = \case
    
    453
    +renameRawPkgQual :: UnitEnv -> UnitIndexQuery -> ModuleName -> RawPkgQual -> PkgQual
    
    454
    +renameRawPkgQual unit_env query mn = \case
    
    452 455
       NoRawPkgQual -> NoPkgQual
    
    453
    -  RawPkgQual p -> renamePkgQual unit_env mn (Just (sl_fs p))
    
    456
    +  RawPkgQual p -> renamePkgQual unit_env query mn (Just (sl_fs p))
    
    454 457
     
    
    455 458
     -- | Rename raw package imports
    
    456
    -renamePkgQual :: UnitEnv -> ModuleName -> Maybe FastString -> PkgQual
    
    457
    -renamePkgQual unit_env mn mb_pkg = case mb_pkg of
    
    459
    +renamePkgQual :: UnitEnv -> UnitIndexQuery -> ModuleName -> Maybe FastString -> PkgQual
    
    460
    +renamePkgQual unit_env query mn mb_pkg = case mb_pkg of
    
    458 461
       Nothing -> NoPkgQual
    
    459 462
       Just pkg_fs
    
    460 463
         | Just uid <- homeUnitId <$> ue_homeUnit unit_env
    
    ... ... @@ -464,7 +467,7 @@ renamePkgQual unit_env mn mb_pkg = case mb_pkg of
    464 467
         | Just (uid, _) <- find (fromMaybe False . fmap (== pkg_fs) . snd) home_names
    
    465 468
         -> ThisPkg uid
    
    466 469
     
    
    467
    -    | Just uid <- resolvePackageImport (ue_units unit_env) mn (PackageName pkg_fs)
    
    470
    +    | Just uid <- resolvePackageImport (ue_units unit_env) query mn (PackageName pkg_fs)
    
    468 471
         -> OtherPkg uid
    
    469 472
     
    
    470 473
         | otherwise
    
    ... ... @@ -479,6 +482,25 @@ renamePkgQual unit_env mn mb_pkg = case mb_pkg of
    479 482
         hpt_deps :: [UnitId]
    
    480 483
         hpt_deps  = homeUnitDepends units
    
    481 484
     
    
    485
    +hscRenameRawPkgQual ::
    
    486
    +  MonadIO m =>
    
    487
    +  HscEnv ->
    
    488
    +  ModuleName ->
    
    489
    +  RawPkgQual ->
    
    490
    +  m PkgQual
    
    491
    +hscRenameRawPkgQual hsc_env name raw = do
    
    492
    +  query <- liftIO $ hscUnitIndexQuery hsc_env
    
    493
    +  pure (renameRawPkgQual (hsc_unit_env hsc_env) query name raw)
    
    494
    +
    
    495
    +hscRenamePkgQual ::
    
    496
    +  MonadIO m =>
    
    497
    +  HscEnv ->
    
    498
    +  ModuleName ->
    
    499
    +  Maybe FastString ->
    
    500
    +  m PkgQual
    
    501
    +hscRenamePkgQual hsc_env name package = do
    
    502
    +  query <- liftIO $ hscUnitIndexQuery hsc_env
    
    503
    +  pure (renamePkgQual (hsc_unit_env hsc_env) query name package)
    
    482 504
     
    
    483 505
     -- | Calculate the 'ImportAvails' induced by an import of a particular
    
    484 506
     -- interface, but without 'imp_mods'.
    

  • compiler/GHC/Runtime/Context.hs
    ... ... @@ -26,6 +26,7 @@ import GHC.Runtime.Eval.Types ( IcGlobalRdrEnv(..), Resume )
    26 26
     
    
    27 27
     import GHC.Unit
    
    28 28
     import GHC.Unit.Env
    
    29
    +import GHC.Unit.State (UnitIndexQuery)
    
    29 30
     
    
    30 31
     import GHC.Core.FamInstEnv
    
    31 32
     import GHC.Core.InstEnv
    
    ... ... @@ -351,8 +352,8 @@ icInScopeTTs ictxt = filter in_scope_unqualified (ic_tythings ictxt)
    351 352
             ]
    
    352 353
     
    
    353 354
     -- | Get the NamePprCtx function based on the flags and this InteractiveContext
    
    354
    -icNamePprCtx :: UnitEnv -> InteractiveContext -> NamePprCtx
    
    355
    -icNamePprCtx unit_env ictxt = mkNamePprCtx ptc unit_env (icReaderEnv ictxt)
    
    355
    +icNamePprCtx :: UnitEnv -> UnitIndexQuery -> InteractiveContext -> NamePprCtx
    
    356
    +icNamePprCtx unit_env query ictxt = mkNamePprCtx ptc unit_env query (icReaderEnv ictxt)
    
    356 357
       where ptc = initPromotionTickContext (ic_dflags ictxt)
    
    357 358
     
    
    358 359
     -- | extendInteractiveContext is called with new TyThings recently defined to update the
    

  • compiler/GHC/Runtime/Loader.hs
    ... ... @@ -348,7 +348,8 @@ lookupRdrNameInModuleForPlugins hsc_env mod_name rdr_name = do
    348 348
         let unit_state = ue_units unit_env
    
    349 349
         let mhome_unit = hsc_home_unit_maybe hsc_env
    
    350 350
         -- First find the unit the module resides in by searching exposed units and home modules
    
    351
    -    found_module <- findPluginModule fc fopts unit_state mhome_unit mod_name
    
    351
    +    query <- hscUnitIndexQuery hsc_env
    
    352
    +    found_module <- findPluginModule fc fopts unit_state query mhome_unit mod_name
    
    352 353
         case found_module of
    
    353 354
             Found _ mod -> do
    
    354 355
                 -- Find the exports of the module
    

  • compiler/GHC/Tc/Module.hs
    ... ... @@ -266,9 +266,11 @@ tcRnModuleTcRnM hsc_env mod_sum
    266 266
             ; when (notNull prel_imports) $ do
    
    267 267
                 addDiagnostic TcRnImplicitImportOfPrelude
    
    268 268
     
    
    269
    +        ; query <- liftIO $ hscUnitIndexQuery hsc_env
    
    270
    +
    
    269 271
             ; -- TODO This is a little skeevy; maybe handle a bit more directly
    
    270 272
               let { simplifyImport (L _ idecl) =
    
    271
    -                  ( renameRawPkgQual (hsc_unit_env hsc_env) (unLoc $ ideclName idecl) (ideclPkgQual idecl)
    
    273
    +                  ( renameRawPkgQual (hsc_unit_env hsc_env) query (unLoc $ ideclName idecl) (ideclPkgQual idecl)
    
    272 274
                       , reLoc $ ideclName idecl)
    
    273 275
                   }
    
    274 276
             ; raw_sig_imports <- liftIO
    
    ... ... @@ -1996,11 +1998,13 @@ runTcInteractive hsc_env thing_inside
    1996 1998
                                      (loadSrcInterface (text "runTcInteractive") m
    
    1997 1999
                                                        NotBoot mb_pkg)
    
    1998 2000
     
    
    2001
    +
    
    1999 2002
            ; !orphs <- fmap (force . concat) . forM (ic_imports icxt) $ \i ->
    
    2000 2003
                 case i of                   -- force above: see #15111
    
    2001 2004
                     IIModule n -> getOrphans n NoPkgQual
    
    2002
    -                IIDecl i   -> getOrphans (unLoc (ideclName i))
    
    2003
    -                                         (renameRawPkgQual (hsc_unit_env hsc_env) (unLoc $ ideclName i) (ideclPkgQual i))
    
    2005
    +                IIDecl i -> do
    
    2006
    +                  qual <- hscRenameRawPkgQual hsc_env (unLoc $ ideclName i) (ideclPkgQual i)
    
    2007
    +                  getOrphans (unLoc (ideclName i)) qual
    
    2004 2008
     
    
    2005 2009
            ; let imports = emptyImportAvails { imp_orphs = orphs }
    
    2006 2010
     
    

  • compiler/GHC/Tc/Utils/Monad.hs
    ... ... @@ -869,7 +869,8 @@ getNamePprCtx
    869 869
       = do { ptc <- initPromotionTickContext <$> getDynFlags
    
    870 870
            ; rdr_env <- getGlobalRdrEnv
    
    871 871
            ; hsc_env <- getTopEnv
    
    872
    -       ; return $ mkNamePprCtx ptc (hsc_unit_env hsc_env) rdr_env }
    
    872
    +       ; query <- liftIO $ hscUnitIndexQuery hsc_env
    
    873
    +       ; return $ mkNamePprCtx ptc (hsc_unit_env hsc_env) query rdr_env }
    
    873 874
     
    
    874 875
     -- | Like logInfoTcRn, but for user consumption
    
    875 876
     printForUserTcRn :: SDoc -> TcRn ()
    

  • compiler/GHC/Types/Name/Ppr.hs
    ... ... @@ -13,6 +13,7 @@ import GHC.Data.FastString
    13 13
     
    
    14 14
     import GHC.Unit
    
    15 15
     import GHC.Unit.Env
    
    16
    +import GHC.Unit.State (UnitIndexQuery)
    
    16 17
     
    
    17 18
     import GHC.Types.Name
    
    18 19
     import GHC.Types.Name.Reader
    
    ... ... @@ -68,11 +69,11 @@ with some holes, we should try to give the user some more useful information.
    68 69
     
    
    69 70
     -- | Creates some functions that work out the best ways to format
    
    70 71
     -- names for the user according to a set of heuristics.
    
    71
    -mkNamePprCtx :: Outputable info => PromotionTickContext -> UnitEnv -> GlobalRdrEnvX info -> NamePprCtx
    
    72
    -mkNamePprCtx ptc unit_env env
    
    72
    +mkNamePprCtx :: Outputable info => PromotionTickContext -> UnitEnv -> UnitIndexQuery -> GlobalRdrEnvX info -> NamePprCtx
    
    73
    +mkNamePprCtx ptc unit_env index env
    
    73 74
      = QueryQualify
    
    74 75
           (mkQualName env)
    
    75
    -      (mkQualModule unit_state home_unit)
    
    76
    +      (mkQualModule unit_state index home_unit)
    
    76 77
           (mkQualPackage unit_state)
    
    77 78
           (mkPromTick ptc env)
    
    78 79
       where
    
    ... ... @@ -206,8 +207,8 @@ Side note (int-index):
    206 207
     -- | Creates a function for formatting modules based on two heuristics:
    
    207 208
     -- (1) if the module is the current module, don't qualify, and (2) if there
    
    208 209
     -- is only one exposed package which exports this module, don't qualify.
    
    209
    -mkQualModule :: UnitState -> Maybe HomeUnit -> QueryQualifyModule
    
    210
    -mkQualModule unit_state mhome_unit mod
    
    210
    +mkQualModule :: UnitState -> UnitIndexQuery -> Maybe HomeUnit -> QueryQualifyModule
    
    211
    +mkQualModule unit_state index mhome_unit mod
    
    211 212
          | Just home_unit <- mhome_unit
    
    212 213
          , isHomeModule home_unit mod = False
    
    213 214
     
    
    ... ... @@ -218,7 +219,7 @@ mkQualModule unit_state mhome_unit mod
    218 219
          = False
    
    219 220
     
    
    220 221
          | otherwise = True
    
    221
    -     where lookup = lookupModuleInAllUnits unit_state (moduleName mod)
    
    222
    +     where lookup = lookupModuleInAllUnits unit_state index (moduleName mod)
    
    222 223
     
    
    223 224
     -- | Creates a function for formatting packages based on two heuristics:
    
    224 225
     -- (1) don't qualify if the package in question is "main", and (2) only qualify
    

  • compiler/GHC/Unit/Env.hs
    ... ... @@ -100,6 +100,8 @@ data UnitEnv = UnitEnv
    100 100
     
    
    101 101
         , ue_namever   :: !GhcNameVersion
    
    102 102
             -- ^ GHC name/version (used for dynamic library suffix)
    
    103
    +
    
    104
    +    , ue_index :: !UnitIndex
    
    103 105
         }
    
    104 106
     
    
    105 107
     ueEPS :: UnitEnv -> IO ExternalPackageState
    
    ... ... @@ -108,12 +110,14 @@ ueEPS = eucEPS . ue_eps
    108 110
     initUnitEnv :: UnitId -> HomeUnitGraph -> GhcNameVersion -> Platform -> IO UnitEnv
    
    109 111
     initUnitEnv cur_unit hug namever platform = do
    
    110 112
       eps <- initExternalUnitCache
    
    113
    +  ue_index <- newUnitIndex
    
    111 114
       return $ UnitEnv
    
    112 115
         { ue_eps             = eps
    
    113 116
         , ue_home_unit_graph = hug
    
    114 117
         , ue_current_unit    = cur_unit
    
    115 118
         , ue_platform        = platform
    
    116 119
         , ue_namever         = namever
    
    120
    +    , ue_index
    
    117 121
         }
    
    118 122
     
    
    119 123
     -- | Get home-unit
    

  • compiler/GHC/Unit/Finder.hs
    ... ... @@ -48,6 +48,7 @@ import GHC.Unit.Module
    48 48
     import GHC.Unit.Home
    
    49 49
     import GHC.Unit.State
    
    50 50
     import GHC.Unit.Finder.Types
    
    51
    +import GHC.Unit.State (UnitIndexQuery)
    
    51 52
     
    
    52 53
     import qualified GHC.Data.ShortText as ST
    
    53 54
     
    
    ... ... @@ -67,7 +68,7 @@ import Control.Monad
    67 68
     import Data.Time
    
    68 69
     import qualified Data.Map as M
    
    69 70
     import GHC.Driver.Env
    
    70
    -    ( hsc_home_unit_maybe, HscEnv(hsc_FC, hsc_dflags, hsc_unit_env) )
    
    71
    +    ( hsc_home_unit_maybe, HscEnv(hsc_FC, hsc_dflags, hsc_unit_env), hscUnitIndexQuery )
    
    71 72
     import GHC.Driver.Config.Finder
    
    72 73
     import qualified Data.Set as Set
    
    73 74
     import qualified Data.List.NonEmpty as NE
    
    ... ... @@ -162,17 +163,19 @@ findImportedModule hsc_env mod pkg_qual =
    162 163
           dflags    = hsc_dflags hsc_env
    
    163 164
           fopts     = initFinderOpts dflags
    
    164 165
       in do
    
    165
    -    findImportedModuleNoHsc fc fopts (hsc_unit_env hsc_env) mhome_unit mod pkg_qual
    
    166
    +    query <- hscUnitIndexQuery hsc_env
    
    167
    +    findImportedModuleNoHsc fc fopts (hsc_unit_env hsc_env) query mhome_unit mod pkg_qual
    
    166 168
     
    
    167 169
     findImportedModuleNoHsc
    
    168 170
       :: FinderCache
    
    169 171
       -> FinderOpts
    
    170 172
       -> UnitEnv
    
    173
    +  -> UnitIndexQuery
    
    171 174
       -> Maybe HomeUnit
    
    172 175
       -> ModuleName
    
    173 176
       -> PkgQual
    
    174 177
       -> IO FindResult
    
    175
    -findImportedModuleNoHsc fc fopts ue mhome_unit mod_name mb_pkg =
    
    178
    +findImportedModuleNoHsc fc fopts ue query mhome_unit mod_name mb_pkg =
    
    176 179
       case mb_pkg of
    
    177 180
         NoPkgQual  -> unqual_import
    
    178 181
         ThisPkg uid | (homeUnitId <$> mhome_unit) == Just uid -> home_import
    
    ... ... @@ -194,7 +197,7 @@ findImportedModuleNoHsc fc fopts ue mhome_unit mod_name mb_pkg =
    194 197
           -- If the module is reexported, then look for it as if it was from the perspective
    
    195 198
           -- of that package which reexports it.
    
    196 199
           | mod_name `Set.member` finder_reexportedModules opts =
    
    197
    -        findImportedModuleNoHsc fc opts ue (Just $ DefiniteHomeUnit uid Nothing) mod_name NoPkgQual
    
    200
    +        findImportedModuleNoHsc fc opts ue query (Just $ DefiniteHomeUnit uid Nothing) mod_name NoPkgQual
    
    198 201
           | mod_name `Set.member` finder_hiddenModules opts =
    
    199 202
             return (mkHomeHidden uid)
    
    200 203
           | otherwise =
    
    ... ... @@ -205,11 +208,11 @@ findImportedModuleNoHsc fc fopts ue mhome_unit mod_name mb_pkg =
    205 208
         -- first before looking at the packages in order.
    
    206 209
         any_home_import = foldr1 orIfNotFound (home_import: map home_pkg_import other_fopts)
    
    207 210
     
    
    208
    -    pkg_import    = findExposedPackageModule fc fopts units  mod_name mb_pkg
    
    211
    +    pkg_import    = findExposedPackageModule fc fopts units query mod_name mb_pkg
    
    209 212
     
    
    210 213
         unqual_import = any_home_import
    
    211 214
                         `orIfNotFound`
    
    212
    -                    findExposedPackageModule fc fopts units mod_name NoPkgQual
    
    215
    +                    findExposedPackageModule fc fopts units query mod_name NoPkgQual
    
    213 216
     
    
    214 217
         units     = case mhome_unit of
    
    215 218
                       Nothing -> ue_units ue
    
    ... ... @@ -222,13 +225,13 @@ findImportedModuleNoHsc fc fopts ue mhome_unit mod_name mb_pkg =
    222 225
     -- plugin.  This consults the same set of exposed packages as
    
    223 226
     -- 'findImportedModule', unless @-hide-all-plugin-packages@ or
    
    224 227
     -- @-plugin-package@ are specified.
    
    225
    -findPluginModule :: FinderCache -> FinderOpts -> UnitState -> Maybe HomeUnit -> ModuleName -> IO FindResult
    
    226
    -findPluginModule fc fopts units (Just home_unit) mod_name =
    
    228
    +findPluginModule :: FinderCache -> FinderOpts -> UnitState -> UnitIndexQuery -> Maybe HomeUnit -> ModuleName -> IO FindResult
    
    229
    +findPluginModule fc fopts units query (Just home_unit) mod_name =
    
    227 230
       findHomeModule fc fopts home_unit mod_name
    
    228 231
       `orIfNotFound`
    
    229
    -  findExposedPluginPackageModule fc fopts units mod_name
    
    230
    -findPluginModule fc fopts units Nothing mod_name =
    
    231
    -  findExposedPluginPackageModule fc fopts units mod_name
    
    232
    +  findExposedPluginPackageModule fc fopts units query mod_name
    
    233
    +findPluginModule fc fopts units query Nothing mod_name =
    
    234
    +  findExposedPluginPackageModule fc fopts units query mod_name
    
    232 235
     
    
    233 236
     -- | Locate a specific 'Module'.  The purpose of this function is to
    
    234 237
     -- create a 'ModLocation' for a given 'Module', that is to find out
    
    ... ... @@ -284,15 +287,15 @@ homeSearchCache fc home_unit mod_name do_this = do
    284 287
       let mod = mkModule home_unit mod_name
    
    285 288
       modLocationCache fc mod do_this
    
    286 289
     
    
    287
    -findExposedPackageModule :: FinderCache -> FinderOpts -> UnitState -> ModuleName -> PkgQual -> IO FindResult
    
    288
    -findExposedPackageModule fc fopts units mod_name mb_pkg =
    
    290
    +findExposedPackageModule :: FinderCache -> FinderOpts -> UnitState -> UnitIndexQuery -> ModuleName -> PkgQual -> IO FindResult
    
    291
    +findExposedPackageModule fc fopts units query mod_name mb_pkg =
    
    289 292
       findLookupResult fc fopts
    
    290
    -    $ lookupModuleWithSuggestions units mod_name mb_pkg
    
    293
    +    $ lookupModuleWithSuggestions units query mod_name mb_pkg
    
    291 294
     
    
    292
    -findExposedPluginPackageModule :: FinderCache -> FinderOpts -> UnitState -> ModuleName -> IO FindResult
    
    293
    -findExposedPluginPackageModule fc fopts units mod_name =
    
    295
    +findExposedPluginPackageModule :: FinderCache -> FinderOpts -> UnitState -> UnitIndexQuery -> ModuleName -> IO FindResult
    
    296
    +findExposedPluginPackageModule fc fopts units query mod_name =
    
    294 297
       findLookupResult fc fopts
    
    295
    -    $ lookupPluginModuleWithSuggestions units mod_name NoPkgQual
    
    298
    +    $ lookupPluginModuleWithSuggestions units query mod_name NoPkgQual
    
    296 299
     
    
    297 300
     findLookupResult :: FinderCache -> FinderOpts -> LookupResult -> IO FindResult
    
    298 301
     findLookupResult fc fopts r = case r of
    

  • compiler/GHC/Unit/State.hs
    1 1
     -- (c) The University of Glasgow, 2006
    
    2 2
     
    
    3
    -{-# LANGUAGE LambdaCase #-}
    
    3
    +{-# LANGUAGE LambdaCase, OverloadedRecordDot, RecordWildCards #-}
    
    4 4
     
    
    5 5
     -- | Unit manipulation
    
    6 6
     module GHC.Unit.State (
    
    ... ... @@ -49,6 +49,15 @@ module GHC.Unit.State (
    49 49
             closeUnitDeps',
    
    50 50
             mayThrowUnitErr,
    
    51 51
     
    
    52
    +        UnitConfig (..),
    
    53
    +        UnitIndex (..),
    
    54
    +        UnitIndexQuery (..),
    
    55
    +        UnitVisibility (..),
    
    56
    +        VisibilityMap,
    
    57
    +        ModuleNameProvidersMap,
    
    58
    +        newUnitIndex,
    
    59
    +        unitIndexQuery,
    
    60
    +
    
    52 61
             -- * Module hole substitution
    
    53 62
             ShHoleSubst,
    
    54 63
             renameHoleUnit,
    
    ... ... @@ -121,6 +130,8 @@ import qualified Data.Semigroup as Semigroup
    121 130
     import qualified Data.Set as Set
    
    122 131
     import GHC.LanguageExtensions
    
    123 132
     import Control.Applicative
    
    133
    +import Control.Monad.IO.Class (MonadIO (..))
    
    134
    +import Data.IORef (IORef, atomicModifyIORef', newIORef, readIORef)
    
    124 135
     
    
    125 136
     -- ---------------------------------------------------------------------------
    
    126 137
     -- The Unit state
    
    ... ... @@ -577,10 +588,10 @@ searchPackageId pkgstate pid = filter ((pid ==) . unitPackageId)
    577 588
     -- | Find the UnitId which an import qualified by a package import comes from.
    
    578 589
     -- Compared to 'lookupPackageName', this function correctly accounts for visibility,
    
    579 590
     -- renaming and thinning.
    
    580
    -resolvePackageImport :: UnitState -> ModuleName -> PackageName -> Maybe UnitId
    
    581
    -resolvePackageImport unit_st mn pn = do
    
    591
    +resolvePackageImport :: UnitState -> UnitIndexQuery -> ModuleName -> PackageName -> Maybe UnitId
    
    592
    +resolvePackageImport unit_st query mn pn = do
    
    582 593
       -- 1. Find all modules providing the ModuleName (this accounts for visibility/thinning etc)
    
    583
    -  providers <- filterUniqMap originVisible <$> lookupUniqMap (moduleNameProvidersMap unit_st) mn
    
    594
    +  providers <- filterUniqMap originVisible <$> query.findOrigin mn False
    
    584 595
       -- 2. Get the UnitIds of the candidates
    
    585 596
       let candidates_uid = concatMap to_uid $ sortOn fst $ nonDetUniqMapToList providers
    
    586 597
       -- 3. Get the package names of the candidates
    
    ... ... @@ -638,14 +649,14 @@ listUnitInfo state = nonDetEltsUniqMap (unitInfoMap state)
    638 649
     -- 'initUnits' can be called again subsequently after updating the
    
    639 650
     -- 'packageFlags' field of the 'DynFlags', and it will update the
    
    640 651
     -- 'unitState' in 'DynFlags'.
    
    641
    -initUnits :: Logger -> DynFlags -> Maybe [UnitDatabase UnitId] -> Set.Set UnitId -> IO ([UnitDatabase UnitId], UnitState, HomeUnit, Maybe PlatformConstants)
    
    642
    -initUnits logger dflags cached_dbs home_units = do
    
    652
    +initUnits :: Logger -> DynFlags -> UnitIndex -> Maybe [UnitDatabase UnitId] -> Set.Set UnitId -> IO ([UnitDatabase UnitId], UnitState, HomeUnit, Maybe PlatformConstants)
    
    653
    +initUnits logger dflags index cached_dbs home_units = do
    
    643 654
     
    
    644 655
       let forceUnitInfoMap (state, _) = unitInfoMap state `seq` ()
    
    645 656
     
    
    646 657
       (unit_state,dbs) <- withTiming logger (text "initializing unit database")
    
    647 658
                        forceUnitInfoMap
    
    648
    -                 $ mkUnitState logger (initUnitConfig dflags cached_dbs home_units)
    
    659
    +                 $ mkUnitState logger (initUnitConfig dflags cached_dbs home_units) index
    
    649 660
     
    
    650 661
       putDumpFileMaybe logger Opt_D_dump_mod_map "Module Map"
    
    651 662
         FormatText (updSDocContext (\ctx -> ctx {sdocLineLength = 200})
    
    ... ... @@ -1021,7 +1032,7 @@ selectPackages prec_map arg pkgs unusable
    1021 1032
       = let matches = matching arg
    
    1022 1033
             (ps,rest) = partition matches pkgs
    
    1023 1034
         in if null ps
    
    1024
    -        then Left (filter (matches.fst) (nonDetEltsUniqMap unusable))
    
    1035
    +        then Left (filter (matches . fst) (nonDetEltsUniqMap unusable))
    
    1025 1036
             else Right (sortByPreference prec_map ps, rest)
    
    1026 1037
     
    
    1027 1038
     -- | Rename a 'UnitInfo' according to some module instantiation.
    
    ... ... @@ -1485,8 +1496,9 @@ validateDatabase cfg pkg_map1 =
    1485 1496
     mkUnitState
    
    1486 1497
         :: Logger
    
    1487 1498
         -> UnitConfig
    
    1499
    +    -> UnitIndex
    
    1488 1500
         -> IO (UnitState,[UnitDatabase UnitId])
    
    1489
    -mkUnitState logger cfg = do
    
    1501
    +mkUnitState logger cfg index = do
    
    1490 1502
     {-
    
    1491 1503
        Plan.
    
    1492 1504
     
    
    ... ... @@ -1542,15 +1554,9 @@ mkUnitState logger cfg = do
    1542 1554
     
    
    1543 1555
       -- if databases have not been provided, read the database flags
    
    1544 1556
       raw_dbs <- case unitConfigDBCache cfg of
    
    1545
    -               Nothing  -> readUnitDatabases logger cfg
    
    1557
    +               Nothing  -> index.readDatabases logger cfg
    
    1546 1558
                    Just dbs -> return dbs
    
    1547 1559
     
    
    1548
    -  -- distrust all units if the flag is set
    
    1549
    -  let distrust_all db = db { unitDatabaseUnits = distrustAllUnits (unitDatabaseUnits db) }
    
    1550
    -      dbs | unitConfigDistrustAll cfg = map distrust_all raw_dbs
    
    1551
    -          | otherwise                 = raw_dbs
    
    1552
    -
    
    1553
    -
    
    1554 1560
       -- This, and the other reverse's that you will see, are due to the fact that
    
    1555 1561
       -- packageFlags, pluginPackageFlags, etc. are all specified in *reverse* order
    
    1556 1562
       -- than they are on the command line.
    
    ... ... @@ -1561,159 +1567,9 @@ mkUnitState logger cfg = do
    1561 1567
     
    
    1562 1568
       let home_unit_deps = selectHomeUnits (unitConfigHomeUnits cfg) hpt_flags
    
    1563 1569
     
    
    1564
    -  -- Merge databases together, without checking validity
    
    1565
    -  (pkg_map1, prec_map) <- mergeDatabases logger dbs
    
    1566
    -
    
    1567
    -  -- Now that we've merged everything together, prune out unusable
    
    1568
    -  -- packages.
    
    1569
    -  let (pkg_map2, unusable, sccs) = validateDatabase cfg pkg_map1
    
    1570
    -
    
    1571
    -  reportCycles   logger sccs
    
    1572
    -  reportUnusable logger unusable
    
    1573
    -
    
    1574
    -  -- Apply trust flags (these flags apply regardless of whether
    
    1575
    -  -- or not packages are visible or not)
    
    1576
    -  pkgs1 <- mayThrowUnitErr
    
    1577
    -            $ foldM (applyTrustFlag prec_map unusable)
    
    1578
    -                 (nonDetEltsUniqMap pkg_map2) (reverse (unitConfigFlagsTrusted cfg))
    
    1579
    -  let prelim_pkg_db = mkUnitInfoMap pkgs1
    
    1580
    -
    
    1581
    -  --
    
    1582
    -  -- Calculate the initial set of units from package databases, prior to any package flags.
    
    1583
    -  --
    
    1584
    -  -- Conceptually, we select the latest versions of all valid (not unusable) *packages*
    
    1585
    -  -- (not units). This is empty if we have -hide-all-packages.
    
    1586
    -  --
    
    1587
    -  -- Then we create an initial visibility map with default visibilities for all
    
    1588
    -  -- exposed, definite units which belong to the latest valid packages.
    
    1589
    -  --
    
    1590
    -  let preferLater unit unit' =
    
    1591
    -        case compareByPreference prec_map unit unit' of
    
    1592
    -            GT -> unit
    
    1593
    -            _  -> unit'
    
    1594
    -      addIfMorePreferable m unit = addToUDFM_C preferLater m (fsPackageName unit) unit
    
    1595
    -      -- This is the set of maximally preferable packages. In fact, it is a set of
    
    1596
    -      -- most preferable *units* keyed by package name, which act as stand-ins in
    
    1597
    -      -- for "a package in a database". We use units here because we don't have
    
    1598
    -      -- "a package in a database" as a type currently.
    
    1599
    -      mostPreferablePackageReps = if unitConfigHideAll cfg
    
    1600
    -                    then emptyUDFM
    
    1601
    -                    else foldl' addIfMorePreferable emptyUDFM pkgs1
    
    1602
    -      -- When exposing units, we want to consider all of those in the most preferable
    
    1603
    -      -- packages. We can implement that by looking for units that are equi-preferable
    
    1604
    -      -- with the most preferable unit for package. Being equi-preferable means that
    
    1605
    -      -- they must be in the same database, with the same version, and the same package name.
    
    1606
    -      --
    
    1607
    -      -- We must take care to consider all these units and not just the most
    
    1608
    -      -- preferable one, otherwise we can end up with problems like #16228.
    
    1609
    -      mostPreferable u =
    
    1610
    -        case lookupUDFM mostPreferablePackageReps (fsPackageName u) of
    
    1611
    -          Nothing -> False
    
    1612
    -          Just u' -> compareByPreference prec_map u u' == EQ
    
    1613
    -      vis_map1 = foldl' (\vm p ->
    
    1614
    -                            -- Note: we NEVER expose indefinite packages by
    
    1615
    -                            -- default, because it's almost assuredly not
    
    1616
    -                            -- what you want (no mix-in linking has occurred).
    
    1617
    -                            if unitIsExposed p && unitIsDefinite (mkUnit p) && mostPreferable p
    
    1618
    -                               then addToUniqMap vm (mkUnit p)
    
    1619
    -                                               UnitVisibility {
    
    1620
    -                                                 uv_expose_all = True,
    
    1621
    -                                                 uv_renamings = [],
    
    1622
    -                                                 uv_package_name = First (Just (fsPackageName p)),
    
    1623
    -                                                 uv_requirements = emptyUniqMap,
    
    1624
    -                                                 uv_explicit = Nothing
    
    1625
    -                                               }
    
    1626
    -                               else vm)
    
    1627
    -                         emptyUniqMap pkgs1
    
    1628
    -
    
    1629
    -  --
    
    1630
    -  -- Compute a visibility map according to the command-line flags (-package,
    
    1631
    -  -- -hide-package).  This needs to know about the unusable packages, since if a
    
    1632
    -  -- user tries to enable an unusable package, we should let them know.
    
    1633
    -  --
    
    1634
    -  vis_map2 <- mayThrowUnitErr
    
    1635
    -                $ foldM (applyPackageFlag prec_map prelim_pkg_db emptyUniqSet unusable
    
    1636
    -                        (unitConfigHideAll cfg) pkgs1)
    
    1637
    -                            vis_map1 other_flags
    
    1638
    -
    
    1639
    -  --
    
    1640
    -  -- Sort out which packages are wired in. This has to be done last, since
    
    1641
    -  -- it modifies the unit ids of wired in packages, but when we process
    
    1642
    -  -- package arguments we need to key against the old versions.
    
    1643
    -  --
    
    1644
    -  (pkgs2, wired_map) <- findWiredInUnits logger prec_map pkgs1 vis_map2
    
    1645
    -  let pkg_db = mkUnitInfoMap pkgs2
    
    1646
    -
    
    1647
    -  -- Update the visibility map, so we treat wired packages as visible.
    
    1648
    -  let vis_map = updateVisibilityMap wired_map vis_map2
    
    1649
    -
    
    1650
    -  let hide_plugin_pkgs = unitConfigHideAllPlugins cfg
    
    1651
    -  plugin_vis_map <-
    
    1652
    -    case unitConfigFlagsPlugins cfg of
    
    1653
    -        -- common case; try to share the old vis_map
    
    1654
    -        [] | not hide_plugin_pkgs -> return vis_map
    
    1655
    -           | otherwise -> return emptyUniqMap
    
    1656
    -        _ -> do let plugin_vis_map1
    
    1657
    -                        | hide_plugin_pkgs = emptyUniqMap
    
    1658
    -                        -- Use the vis_map PRIOR to wired in,
    
    1659
    -                        -- because otherwise applyPackageFlag
    
    1660
    -                        -- won't work.
    
    1661
    -                        | otherwise = vis_map2
    
    1662
    -                plugin_vis_map2
    
    1663
    -                    <- mayThrowUnitErr
    
    1664
    -                        $ foldM (applyPackageFlag prec_map prelim_pkg_db emptyUniqSet unusable
    
    1665
    -                                hide_plugin_pkgs pkgs1)
    
    1666
    -                             plugin_vis_map1
    
    1667
    -                             (reverse (unitConfigFlagsPlugins cfg))
    
    1668
    -                -- Updating based on wired in packages is mostly
    
    1669
    -                -- good hygiene, because it won't matter: no wired in
    
    1670
    -                -- package has a compiler plugin.
    
    1671
    -                -- TODO: If a wired in package had a compiler plugin,
    
    1672
    -                -- and you tried to pick different wired in packages
    
    1673
    -                -- with the plugin flags and the normal flags... what
    
    1674
    -                -- would happen?  I don't know!  But this doesn't seem
    
    1675
    -                -- likely to actually happen.
    
    1676
    -                return (updateVisibilityMap wired_map plugin_vis_map2)
    
    1677
    -
    
    1678
    -  let pkgname_map = listToUFM [ (unitPackageName p, unitInstanceOf p)
    
    1679
    -                              | p <- pkgs2
    
    1680
    -                              ]
    
    1681
    -  -- The explicitUnits accurately reflects the set of units we have turned
    
    1682
    -  -- on; as such, it also is the only way one can come up with requirements.
    
    1683
    -  -- The requirement context is directly based off of this: we simply
    
    1684
    -  -- look for nested unit IDs that are directly fed holes: the requirements
    
    1685
    -  -- of those units are precisely the ones we need to track
    
    1686
    -  let explicit_pkgs = [(k, uv_explicit v) | (k, v) <- nonDetUniqMapToList vis_map]
    
    1687
    -      req_ctx = mapUniqMap (Set.toList)
    
    1688
    -              $ plusUniqMapListWith Set.union (map uv_requirements (nonDetEltsUniqMap vis_map))
    
    1689
    -
    
    1690
    -
    
    1691
    -  --
    
    1692
    -  -- Here we build up a set of the packages mentioned in -package
    
    1693
    -  -- flags on the command line; these are called the "preload"
    
    1694
    -  -- packages.  we link these packages in eagerly.  The preload set
    
    1695
    -  -- should contain at least rts & base, which is why we pretend that
    
    1696
    -  -- the command line contains -package rts & -package base.
    
    1697
    -  --
    
    1698
    -  -- NB: preload IS important even for type-checking, because we
    
    1699
    -  -- need the correct include path to be set.
    
    1700
    -  --
    
    1701
    -  let preload1 = nonDetKeysUniqMap (filterUniqMap (isJust . uv_explicit) vis_map)
    
    1702
    -
    
    1703
    -      -- add default preload units if they can be found in the db
    
    1704
    -      basicLinkedUnits = fmap (RealUnit . Definite)
    
    1705
    -                         $ filter (flip elemUniqMap pkg_db)
    
    1706
    -                         $ unitConfigAutoLink cfg
    
    1707
    -      preload3 = ordNub $ (basicLinkedUnits ++ preload1)
    
    1708
    -
    
    1709
    -  -- Close the preload packages with their dependencies
    
    1710
    -  dep_preload <- mayThrowUnitErr
    
    1711
    -                    $ closeUnitDeps pkg_db
    
    1712
    -                    $ zip (map toUnitId preload3) (repeat Nothing)
    
    1570
    +  (pkg_db, explicit_pkgs, dep_preload, req_ctx, pkgname_map, wired_map) <- index.update logger cfg raw_dbs other_flags
    
    1713 1571
     
    
    1714
    -  let mod_map1 = mkModuleNameProvidersMap logger cfg pkg_db emptyUniqSet vis_map
    
    1715
    -      mod_map2 = mkUnusableModuleNameProvidersMap unusable
    
    1716
    -      mod_map = mod_map2 `plusUniqMap` mod_map1
    
    1572
    +  -- pluginModuleNameProvidersMap = mkModuleNameProvidersMap logger cfg pkg_db emptyUniqSet plugin_vis_map
    
    1717 1573
     
    
    1718 1574
       -- Force the result to avoid leaking input parameters
    
    1719 1575
       let !state = UnitState
    
    ... ... @@ -1722,8 +1578,8 @@ mkUnitState logger cfg = do
    1722 1578
              , homeUnitDepends              = Set.toList home_unit_deps
    
    1723 1579
              , unitInfoMap                  = pkg_db
    
    1724 1580
              , preloadClosure               = emptyUniqSet
    
    1725
    -         , moduleNameProvidersMap       = mod_map
    
    1726
    -         , pluginModuleNameProvidersMap = mkModuleNameProvidersMap logger cfg pkg_db emptyUniqSet plugin_vis_map
    
    1581
    +         , moduleNameProvidersMap       = emptyUniqMap
    
    1582
    +         , pluginModuleNameProvidersMap = emptyUniqMap
    
    1727 1583
              , packageNameMap               = pkgname_map
    
    1728 1584
              , wireMap                      = wired_map
    
    1729 1585
              , unwireMap                    = listToUniqMap [ (v,k) | (k,v) <- nonDetUniqMapToList wired_map ]
    
    ... ... @@ -1896,6 +1752,260 @@ addListTo = foldl' merge
    1896 1752
     mkModMap :: Unit -> ModuleName -> ModuleOrigin -> UniqMap Module ModuleOrigin
    
    1897 1753
     mkModMap pkg mod = unitUniqMap (mkModule pkg mod)
    
    1898 1754
     
    
    1755
    +-- -----------------------------------------------------------------------------
    
    1756
    +-- Index
    
    1757
    +
    
    1758
    +data UnitIndexQuery =
    
    1759
    +  UnitIndexQuery {
    
    1760
    +    findOrigin :: ModuleName -> Bool -> Maybe (UniqMap Module ModuleOrigin),
    
    1761
    +    index_all :: ModuleNameProvidersMap
    
    1762
    +  }
    
    1763
    +
    
    1764
    +data UnitIndex =
    
    1765
    +  UnitIndex {
    
    1766
    +    query :: IO UnitIndexQuery,
    
    1767
    +    readDatabases :: Logger -> UnitConfig -> IO [UnitDatabase UnitId],
    
    1768
    +    update ::
    
    1769
    +      Logger ->
    
    1770
    +      UnitConfig ->
    
    1771
    +      [UnitDatabase UnitId] ->
    
    1772
    +      [PackageFlag] ->
    
    1773
    +      IO (
    
    1774
    +        UnitInfoMap,
    
    1775
    +        [(Unit, Maybe PackageArg)],
    
    1776
    +        [UnitId],
    
    1777
    +        UniqMap ModuleName [InstantiatedModule],
    
    1778
    +        UniqFM PackageName UnitId,
    
    1779
    +        WiringMap
    
    1780
    +      )
    
    1781
    +  }
    
    1782
    +
    
    1783
    +unitIndexQuery ::
    
    1784
    +  MonadIO m =>
    
    1785
    +  UnitIndex ->
    
    1786
    +  m UnitIndexQuery
    
    1787
    +unitIndexQuery index = liftIO index.query
    
    1788
    +
    
    1789
    +data UnitIndexBackend =
    
    1790
    +  UnitIndexBackend {
    
    1791
    +    moduleNameProviders :: !ModuleNameProvidersMap,
    
    1792
    +    pluginModuleNameProviders :: !ModuleNameProvidersMap
    
    1793
    +  }
    
    1794
    +
    
    1795
    +newUnitIndexBackend :: UnitIndexBackend
    
    1796
    +newUnitIndexBackend =
    
    1797
    +  UnitIndexBackend {
    
    1798
    +    moduleNameProviders = mempty,
    
    1799
    +    pluginModuleNameProviders = mempty
    
    1800
    +  }
    
    1801
    +
    
    1802
    +queryFindOrigin ::
    
    1803
    +  UnitIndexBackend ->
    
    1804
    +  ModuleName ->
    
    1805
    +  Bool ->
    
    1806
    +  Maybe (UniqMap Module ModuleOrigin)
    
    1807
    +queryFindOrigin UnitIndexBackend {moduleNameProviders} name _plugins =
    
    1808
    +  lookupUniqMap moduleNameProviders name
    
    1809
    +
    
    1810
    +newUnitIndexQuery ::
    
    1811
    +  MonadIO m =>
    
    1812
    +  IORef UnitIndexBackend ->
    
    1813
    +  m UnitIndexQuery
    
    1814
    +newUnitIndexQuery ref = do
    
    1815
    +  state <- liftIO $ readIORef ref
    
    1816
    +  pure UnitIndexQuery {
    
    1817
    +    findOrigin = queryFindOrigin state,
    
    1818
    +    index_all = state.moduleNameProviders
    
    1819
    +  }
    
    1820
    +
    
    1821
    +updateIndexDefault ::
    
    1822
    +  IORef UnitIndexBackend ->
    
    1823
    +  Logger ->
    
    1824
    +  UnitConfig ->
    
    1825
    +  [UnitDatabase UnitId] ->
    
    1826
    +  [PackageFlag] ->
    
    1827
    +  IO (UnitInfoMap, [(Unit, Maybe PackageArg)], [UnitId], UniqMap ModuleName [InstantiatedModule], UniqFM PackageName UnitId, WiringMap)
    
    1828
    +updateIndexDefault ref logger cfg raw_dbs other_flags = do
    
    1829
    +
    
    1830
    +  -- distrust all units if the flag is set
    
    1831
    +  let distrust_all db = db { unitDatabaseUnits = distrustAllUnits (unitDatabaseUnits db) }
    
    1832
    +      dbs | unitConfigDistrustAll cfg = map distrust_all raw_dbs
    
    1833
    +          | otherwise                 = raw_dbs
    
    1834
    +
    
    1835
    +
    
    1836
    +  -- Merge databases together, without checking validity
    
    1837
    +  (pkg_map1, prec_map) <- mergeDatabases logger dbs
    
    1838
    +
    
    1839
    +  -- Now that we've merged everything together, prune out unusable
    
    1840
    +  -- packages.
    
    1841
    +  let (pkg_map2, unusable, sccs) = validateDatabase cfg pkg_map1
    
    1842
    +
    
    1843
    +  reportCycles   logger sccs
    
    1844
    +  reportUnusable logger unusable
    
    1845
    +
    
    1846
    +  -- Apply trust flags (these flags apply regardless of whether
    
    1847
    +  -- or not packages are visible or not)
    
    1848
    +  pkgs1 <- mayThrowUnitErr
    
    1849
    +            $ foldM (applyTrustFlag prec_map unusable)
    
    1850
    +                 (nonDetEltsUniqMap pkg_map2) (reverse (unitConfigFlagsTrusted cfg))
    
    1851
    +  let prelim_pkg_db = mkUnitInfoMap pkgs1
    
    1852
    +
    
    1853
    +  --
    
    1854
    +  -- Calculate the initial set of units from package databases, prior to any package flags.
    
    1855
    +  --
    
    1856
    +  -- Conceptually, we select the latest versions of all valid (not unusable) *packages*
    
    1857
    +  -- (not units). This is empty if we have -hide-all-packages.
    
    1858
    +  --
    
    1859
    +  -- Then we create an initial visibility map with default visibilities for all
    
    1860
    +  -- exposed, definite units which belong to the latest valid packages.
    
    1861
    +  --
    
    1862
    +  let preferLater unit unit' =
    
    1863
    +        case compareByPreference prec_map unit unit' of
    
    1864
    +            GT -> unit
    
    1865
    +            _  -> unit'
    
    1866
    +      addIfMorePreferable m unit = addToUDFM_C preferLater m (fsPackageName unit) unit
    
    1867
    +      -- This is the set of maximally preferable packages. In fact, it is a set of
    
    1868
    +      -- most preferable *units* keyed by package name, which act as stand-ins in
    
    1869
    +      -- for "a package in a database". We use units here because we don't have
    
    1870
    +      -- "a package in a database" as a type currently.
    
    1871
    +      mostPreferablePackageReps = if unitConfigHideAll cfg
    
    1872
    +                    then emptyUDFM
    
    1873
    +                    else foldl' addIfMorePreferable emptyUDFM pkgs1
    
    1874
    +      -- When exposing units, we want to consider all of those in the most preferable
    
    1875
    +      -- packages. We can implement that by looking for units that are equi-preferable
    
    1876
    +      -- with the most preferable unit for package. Being equi-preferable means that
    
    1877
    +      -- they must be in the same database, with the same version, and the same package name.
    
    1878
    +      --
    
    1879
    +      -- We must take care to consider all these units and not just the most
    
    1880
    +      -- preferable one, otherwise we can end up with problems like #16228.
    
    1881
    +      mostPreferable u =
    
    1882
    +        case lookupUDFM mostPreferablePackageReps (fsPackageName u) of
    
    1883
    +          Nothing -> False
    
    1884
    +          Just u' -> compareByPreference prec_map u u' == EQ
    
    1885
    +      vis_map1 = foldl' (\vm p ->
    
    1886
    +                            -- Note: we NEVER expose indefinite packages by
    
    1887
    +                            -- default, because it's almost assuredly not
    
    1888
    +                            -- what you want (no mix-in linking has occurred).
    
    1889
    +                            if unitIsExposed p && unitIsDefinite (mkUnit p) && mostPreferable p
    
    1890
    +                               then addToUniqMap vm (mkUnit p)
    
    1891
    +                                               UnitVisibility {
    
    1892
    +                                                 uv_expose_all = True,
    
    1893
    +                                                 uv_renamings = [],
    
    1894
    +                                                 uv_package_name = First (Just (fsPackageName p)),
    
    1895
    +                                                 uv_requirements = emptyUniqMap,
    
    1896
    +                                                 uv_explicit = Nothing
    
    1897
    +                                               }
    
    1898
    +                               else vm)
    
    1899
    +                         emptyUniqMap pkgs1
    
    1900
    +
    
    1901
    +  --
    
    1902
    +  -- Compute a visibility map according to the command-line flags (-package,
    
    1903
    +  -- -hide-package).  This needs to know about the unusable packages, since if a
    
    1904
    +  -- user tries to enable an unusable package, we should let them know.
    
    1905
    +  --
    
    1906
    +  vis_map2 <- mayThrowUnitErr
    
    1907
    +                $ foldM (applyPackageFlag prec_map prelim_pkg_db emptyUniqSet unusable
    
    1908
    +                        (unitConfigHideAll cfg) pkgs1)
    
    1909
    +                            vis_map1 other_flags
    
    1910
    +
    
    1911
    +  --
    
    1912
    +  -- Sort out which packages are wired in. This has to be done last, since
    
    1913
    +  -- it modifies the unit ids of wired in packages, but when we process
    
    1914
    +  -- package arguments we need to key against the old versions.
    
    1915
    +  --
    
    1916
    +  (pkgs2, wired_map) <- findWiredInUnits logger prec_map pkgs1 vis_map2
    
    1917
    +  let pkg_db = mkUnitInfoMap pkgs2
    
    1918
    +
    
    1919
    +  -- Update the visibility map, so we treat wired packages as visible.
    
    1920
    +  let vis_map = updateVisibilityMap wired_map vis_map2
    
    1921
    +
    
    1922
    +  let hide_plugin_pkgs = unitConfigHideAllPlugins cfg
    
    1923
    +  plugin_vis_map <-
    
    1924
    +    case unitConfigFlagsPlugins cfg of
    
    1925
    +        -- common case; try to share the old vis_map
    
    1926
    +        [] | not hide_plugin_pkgs -> return vis_map
    
    1927
    +           | otherwise -> return emptyUniqMap
    
    1928
    +        _ -> do let plugin_vis_map1
    
    1929
    +                        | hide_plugin_pkgs = emptyUniqMap
    
    1930
    +                        -- Use the vis_map PRIOR to wired in,
    
    1931
    +                        -- because otherwise applyPackageFlag
    
    1932
    +                        -- won't work.
    
    1933
    +                        | otherwise = vis_map2
    
    1934
    +                plugin_vis_map2
    
    1935
    +                    <- mayThrowUnitErr
    
    1936
    +                        $ foldM (applyPackageFlag prec_map prelim_pkg_db emptyUniqSet unusable
    
    1937
    +                                hide_plugin_pkgs pkgs1)
    
    1938
    +                             plugin_vis_map1
    
    1939
    +                             (reverse (unitConfigFlagsPlugins cfg))
    
    1940
    +                -- Updating based on wired in packages is mostly
    
    1941
    +                -- good hygiene, because it won't matter: no wired in
    
    1942
    +                -- package has a compiler plugin.
    
    1943
    +                -- TODO: If a wired in package had a compiler plugin,
    
    1944
    +                -- and you tried to pick different wired in packages
    
    1945
    +                -- with the plugin flags and the normal flags... what
    
    1946
    +                -- would happen?  I don't know!  But this doesn't seem
    
    1947
    +                -- likely to actually happen.
    
    1948
    +                return (updateVisibilityMap wired_map plugin_vis_map2)
    
    1949
    +
    
    1950
    +  let pkgname_map = listToUFM [ (unitPackageName p, unitInstanceOf p)
    
    1951
    +                              | p <- pkgs2
    
    1952
    +                              ]
    
    1953
    +  -- The explicitUnits accurately reflects the set of units we have turned
    
    1954
    +  -- on; as such, it also is the only way one can come up with requirements.
    
    1955
    +  -- The requirement context is directly based off of this: we simply
    
    1956
    +  -- look for nested unit IDs that are directly fed holes: the requirements
    
    1957
    +  -- of those units are precisely the ones we need to track
    
    1958
    +  let explicit_pkgs = [(k, uv_explicit v) | (k, v) <- nonDetUniqMapToList vis_map]
    
    1959
    +      req_ctx = mapUniqMap (Set.toList)
    
    1960
    +              $ plusUniqMapListWith Set.union (map uv_requirements (nonDetEltsUniqMap vis_map))
    
    1961
    +
    
    1962
    +
    
    1963
    +  --
    
    1964
    +  -- Here we build up a set of the packages mentioned in -package
    
    1965
    +  -- flags on the command line; these are called the "preload"
    
    1966
    +  -- packages.  we link these packages in eagerly.  The preload set
    
    1967
    +  -- should contain at least rts & base, which is why we pretend that
    
    1968
    +  -- the command line contains -package rts & -package base.
    
    1969
    +  --
    
    1970
    +  -- NB: preload IS important even for type-checking, because we
    
    1971
    +  -- need the correct include path to be set.
    
    1972
    +  --
    
    1973
    +  let preload1 = nonDetKeysUniqMap (filterUniqMap (isJust . uv_explicit) vis_map)
    
    1974
    +
    
    1975
    +      -- add default preload units if they can be found in the db
    
    1976
    +      basicLinkedUnits = fmap (RealUnit . Definite)
    
    1977
    +                         $ filter (flip elemUniqMap pkg_db)
    
    1978
    +                         $ unitConfigAutoLink cfg
    
    1979
    +      preload3 = ordNub $ (basicLinkedUnits ++ preload1)
    
    1980
    +
    
    1981
    +  -- Close the preload packages with their dependencies
    
    1982
    +  dep_preload <- mayThrowUnitErr
    
    1983
    +                    $ closeUnitDeps pkg_db
    
    1984
    +                    $ zip (map toUnitId preload3) (repeat Nothing)
    
    1985
    +
    
    1986
    +  let mod_map1 = mkModuleNameProvidersMap logger cfg pkg_db emptyUniqSet vis_map
    
    1987
    +      mod_map2 = mkUnusableModuleNameProvidersMap unusable
    
    1988
    +      mod_map = mod_map2 `plusUniqMap` mod_map1
    
    1989
    +  atomicModifyIORef' ref $ \ UnitIndexBackend {..} -> let
    
    1990
    +      updated = UnitIndexBackend {
    
    1991
    +        moduleNameProviders = moduleNameProviders Semigroup.<> mod_map,
    
    1992
    +        pluginModuleNameProviders = mkModuleNameProvidersMap logger cfg pkg_db emptyUniqSet plugin_vis_map Semigroup.<> pluginModuleNameProviders,
    
    1993
    +        ..
    
    1994
    +      }
    
    1995
    +    in (updated, (pkg_db, explicit_pkgs, dep_preload, req_ctx, pkgname_map, wired_map))
    
    1996
    +
    
    1997
    +readDatabasesDefault :: Logger -> UnitConfig -> IO [UnitDatabase UnitId]
    
    1998
    +readDatabasesDefault logger cfg =
    
    1999
    +  readUnitDatabases logger cfg
    
    2000
    +
    
    2001
    +newUnitIndex :: MonadIO m => m UnitIndex
    
    2002
    +newUnitIndex = do
    
    2003
    +  ref <- liftIO $ newIORef newUnitIndexBackend
    
    2004
    +  pure UnitIndex {
    
    2005
    +    query = newUnitIndexQuery ref,
    
    2006
    +    readDatabases = readDatabasesDefault,
    
    2007
    +    update = updateIndexDefault ref
    
    2008
    +  }
    
    1899 2009
     
    
    1900 2010
     -- -----------------------------------------------------------------------------
    
    1901 2011
     -- Package Utils
    
    ... ... @@ -1903,10 +2013,11 @@ mkModMap pkg mod = unitUniqMap (mkModule pkg mod)
    1903 2013
     -- | Takes a 'ModuleName', and if the module is in any package returns
    
    1904 2014
     -- list of modules which take that name.
    
    1905 2015
     lookupModuleInAllUnits :: UnitState
    
    2016
    +                          -> UnitIndexQuery
    
    1906 2017
                               -> ModuleName
    
    1907 2018
                               -> [(Module, UnitInfo)]
    
    1908
    -lookupModuleInAllUnits pkgs m
    
    1909
    -  = case lookupModuleWithSuggestions pkgs m NoPkgQual of
    
    2019
    +lookupModuleInAllUnits pkgs query m
    
    2020
    +  = case lookupModuleWithSuggestions pkgs query m NoPkgQual of
    
    1910 2021
           LookupFound a b -> [(a,fst b)]
    
    1911 2022
           LookupMultiple rs -> map f rs
    
    1912 2023
             where f (m,_) = (m, expectJust "lookupModule" (lookupUnit pkgs
    
    ... ... @@ -1933,18 +2044,24 @@ data ModuleSuggestion = SuggestVisible ModuleName Module ModuleOrigin
    1933 2044
                           | SuggestHidden ModuleName Module ModuleOrigin
    
    1934 2045
     
    
    1935 2046
     lookupModuleWithSuggestions :: UnitState
    
    2047
    +                            -> UnitIndexQuery
    
    1936 2048
                                 -> ModuleName
    
    1937 2049
                                 -> PkgQual
    
    1938 2050
                                 -> LookupResult
    
    1939
    -lookupModuleWithSuggestions pkgs
    
    1940
    -  = lookupModuleWithSuggestions' pkgs (moduleNameProvidersMap pkgs)
    
    2051
    +lookupModuleWithSuggestions pkgs query name
    
    2052
    +  = lookupModuleWithSuggestions' pkgs query name False
    
    1941 2053
     
    
    1942 2054
     -- | The package which the module **appears** to come from, this could be
    
    1943 2055
     -- the one which reexports the module from it's original package. This function
    
    1944 2056
     -- is currently only used for -Wunused-packages
    
    1945
    -lookupModulePackage :: UnitState -> ModuleName -> PkgQual -> Maybe [UnitInfo]
    
    1946
    -lookupModulePackage pkgs mn mfs =
    
    1947
    -    case lookupModuleWithSuggestions' pkgs (moduleNameProvidersMap pkgs) mn mfs of
    
    2057
    +lookupModulePackage ::
    
    2058
    +  UnitState ->
    
    2059
    +  UnitIndexQuery ->
    
    2060
    +  ModuleName ->
    
    2061
    +  PkgQual ->
    
    2062
    +  Maybe [UnitInfo]
    
    2063
    +lookupModulePackage pkgs query mn mfs =
    
    2064
    +    case lookupModuleWithSuggestions' pkgs query mn False mfs of
    
    1948 2065
           LookupFound _ (orig_unit, origin) ->
    
    1949 2066
             case origin of
    
    1950 2067
               ModOrigin {fromOrigUnit, fromExposedReexport} ->
    
    ... ... @@ -1960,19 +2077,21 @@ lookupModulePackage pkgs mn mfs =
    1960 2077
           _ -> Nothing
    
    1961 2078
     
    
    1962 2079
     lookupPluginModuleWithSuggestions :: UnitState
    
    2080
    +                                  -> UnitIndexQuery
    
    1963 2081
                                       -> ModuleName
    
    1964 2082
                                       -> PkgQual
    
    1965 2083
                                       -> LookupResult
    
    1966
    -lookupPluginModuleWithSuggestions pkgs
    
    1967
    -  = lookupModuleWithSuggestions' pkgs (pluginModuleNameProvidersMap pkgs)
    
    2084
    +lookupPluginModuleWithSuggestions pkgs query name
    
    2085
    +  = lookupModuleWithSuggestions' pkgs query name True
    
    1968 2086
     
    
    1969 2087
     lookupModuleWithSuggestions' :: UnitState
    
    1970
    -                            -> ModuleNameProvidersMap
    
    2088
    +                            -> UnitIndexQuery
    
    1971 2089
                                 -> ModuleName
    
    2090
    +                            -> Bool
    
    1972 2091
                                 -> PkgQual
    
    1973 2092
                                 -> LookupResult
    
    1974
    -lookupModuleWithSuggestions' pkgs mod_map m mb_pn
    
    1975
    -  = case lookupUniqMap mod_map m of
    
    2093
    +lookupModuleWithSuggestions' pkgs query m onlyPlugins mb_pn
    
    2094
    +  = case query.findOrigin m onlyPlugins of
    
    1976 2095
             Nothing -> LookupNotFound suggestions
    
    1977 2096
             Just xs ->
    
    1978 2097
               case foldl' classify ([],[],[], []) (sortOn fst $ nonDetUniqMapToList xs) of
    
    ... ... @@ -2033,16 +2152,16 @@ lookupModuleWithSuggestions' pkgs mod_map m mb_pn
    2033 2152
         all_mods :: [(String, ModuleSuggestion)]     -- All modules
    
    2034 2153
         all_mods = sortBy (comparing fst) $
    
    2035 2154
             [ (moduleNameString m, suggestion)
    
    2036
    -        | (m, e) <- nonDetUniqMapToList (moduleNameProvidersMap pkgs)
    
    2155
    +        | (m, e) <- nonDetUniqMapToList query.index_all
    
    2037 2156
             , suggestion <- map (getSuggestion m) (nonDetUniqMapToList e)
    
    2038 2157
             ]
    
    2039 2158
         getSuggestion name (mod, origin) =
    
    2040 2159
             (if originVisible origin then SuggestVisible else SuggestHidden)
    
    2041 2160
                 name mod origin
    
    2042 2161
     
    
    2043
    -listVisibleModuleNames :: UnitState -> [ModuleName]
    
    2044
    -listVisibleModuleNames state =
    
    2045
    -    map fst (filter visible (nonDetUniqMapToList (moduleNameProvidersMap state)))
    
    2162
    +listVisibleModuleNames :: UnitIndexQuery -> [ModuleName]
    
    2163
    +listVisibleModuleNames query =
    
    2164
    +    map fst (filter visible (nonDetUniqMapToList query.index_all))
    
    2046 2165
       where visible (_, ms) = anyUniqMap originVisible ms
    
    2047 2166
     
    
    2048 2167
     -- | Takes a list of UnitIds (and their "parent" dependency, used for error
    

  • ghc/GHCi/UI.hs
    ... ... @@ -3695,19 +3695,21 @@ completeBreakpoint = wrapCompleter spaces $ \w -> do -- #3000
    3695 3695
     
    
    3696 3696
     completeModule = wrapIdentCompleterMod $ \w -> do
    
    3697 3697
       hsc_env <- GHC.getSession
    
    3698
    -  let pkg_mods = allVisibleModules (hsc_units hsc_env)
    
    3698
    +  query <- liftIO $ hscUnitIndexQuery hsc_env
    
    3699
    +  let pkg_mods = allVisibleModules query
    
    3699 3700
       loaded_mods <- liftM (map GHC.ms_mod_name) getLoadedModules
    
    3700 3701
       return $ filter (w `isPrefixOf`)
    
    3701 3702
             $ map (showPpr (hsc_dflags hsc_env)) $ loaded_mods ++ pkg_mods
    
    3702 3703
     
    
    3703 3704
     completeSetModule = wrapIdentCompleterWithModifier "+-" $ \m w -> do
    
    3704 3705
       hsc_env <- GHC.getSession
    
    3706
    +  query <- liftIO $ hscUnitIndexQuery hsc_env
    
    3705 3707
       modules <- case m of
    
    3706 3708
         Just '-' -> do
    
    3707 3709
           imports <- GHC.getContext
    
    3708 3710
           return $ map iiModuleName imports
    
    3709 3711
         _ -> do
    
    3710
    -      let pkg_mods = allVisibleModules (hsc_units hsc_env)
    
    3712
    +      let pkg_mods = allVisibleModules query
    
    3711 3713
           loaded_mods <- liftM (map GHC.ms_mod_name) getLoadedModules
    
    3712 3714
           return $ loaded_mods ++ pkg_mods
    
    3713 3715
       return $ filter (w `isPrefixOf`) $ map (showPpr (hsc_dflags hsc_env)) modules
    
    ... ... @@ -3775,8 +3777,8 @@ wrapIdentCompleterWithModifier modifChars fun = completeWordWithPrev Nothing wor
    3775 3777
     
    
    3776 3778
     -- | Return a list of visible module names for autocompletion.
    
    3777 3779
     -- (NB: exposed != visible)
    
    3778
    -allVisibleModules :: UnitState -> [ModuleName]
    
    3779
    -allVisibleModules unit_state = listVisibleModuleNames unit_state
    
    3780
    +allVisibleModules :: UnitIndexQuery -> [ModuleName]
    
    3781
    +allVisibleModules query = listVisibleModuleNames query
    
    3780 3782
     
    
    3781 3783
     completeExpression = completeQuotedWord (Just '\\') "\"" listFiles
    
    3782 3784
                             completeIdentifier
    

  • ghc/GHCi/UI/Monad.hs
    ... ... @@ -374,10 +374,11 @@ printForUserGlobalRdrEnv mb_rdr_env doc = do
    374 374
         where
    
    375 375
           mkNamePprCtxFromGlobalRdrEnv _ Nothing = GHC.getNamePprCtx
    
    376 376
           mkNamePprCtxFromGlobalRdrEnv dflags (Just rdr_env) =
    
    377
    -        withSession $ \ hsc_env ->
    
    377
    +        withSession $ \ hsc_env -> do
    
    378
    +        query <- liftIO $ hscUnitIndexQuery hsc_env
    
    378 379
             let unit_env = hsc_unit_env hsc_env
    
    379 380
                 ptc = initPromotionTickContext dflags
    
    380
    -        in  return $ Ppr.mkNamePprCtx ptc unit_env rdr_env
    
    381
    +        return $ Ppr.mkNamePprCtx ptc unit_env query rdr_env
    
    381 382
     
    
    382 383
     printForUser :: GhcMonad m => SDoc -> m ()
    
    383 384
     printForUser doc = do
    

  • ghc/Main.hs
    ... ... @@ -844,7 +844,8 @@ initMulti unitArgsFiles = do
    844 844
         let cached_unit_dbs = homeUnitEnv_unit_dbs homeUnitEnv
    
    845 845
             hue_flags = homeUnitEnv_dflags homeUnitEnv
    
    846 846
             dflags = homeUnitEnv_dflags homeUnitEnv
    
    847
    -    (dbs,unit_state,home_unit,mconstants) <- liftIO $ State.initUnits logger hue_flags cached_unit_dbs home_units
    
    847
    +        index = hscUnitIndex hsc_env
    
    848
    +    (dbs,unit_state,home_unit,mconstants) <- liftIO $ State.initUnits logger hue_flags index cached_unit_dbs home_units
    
    848 849
     
    
    849 850
         updated_dflags <- liftIO $ updatePlatformConstants dflags mconstants
    
    850 851
         pure $ HomeUnitEnv
    
    ... ... @@ -859,7 +860,7 @@ initMulti unitArgsFiles = do
    859 860
     
    
    860 861
       let dflags = homeUnitEnv_dflags $ unitEnv_lookup mainUnitId home_unit_graph
    
    861 862
       unitEnv <- assertUnitEnvInvariant <$> (liftIO $ initUnitEnv mainUnitId home_unit_graph (ghcNameVersion dflags) (targetPlatform dflags))
    
    862
    -  let final_hsc_env = hsc_env { hsc_unit_env = unitEnv }
    
    863
    +  let final_hsc_env = hsc_env { hsc_unit_env = unitEnv {ue_index = hscUnitIndex hsc_env} }
    
    863 864
     
    
    864 865
       GHC.setSession final_hsc_env
    
    865 866