summaryrefslogtreecommitdiff
path: root/compiler/GHC/Unit/State.hs
diff options
context:
space:
mode:
authorSylvain Henry <sylvain@haskus.fr>2020-06-04 19:13:56 +0200
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-07-22 20:23:00 -0400
commit7f44df1ec6df2b02be83e41cec4dc3b5f7f540f0 (patch)
treecf061835b5622e41cd40f3b44a4d04019de51628 /compiler/GHC/Unit/State.hs
parent02f40b0da2eadbf8a0e2930b95d4cef686acd92f (diff)
downloadhaskell-7f44df1ec6df2b02be83e41cec4dc3b5f7f540f0.tar.gz
Minor refactoring of Unit display
* for consistency, try to always use UnitPprInfo to display units to users * remove some uses of `unitPackageIdString` as it doesn't show the component name and it uses String
Diffstat (limited to 'compiler/GHC/Unit/State.hs')
-rw-r--r--compiler/GHC/Unit/State.hs46
1 files changed, 28 insertions, 18 deletions
diff --git a/compiler/GHC/Unit/State.hs b/compiler/GHC/Unit/State.hs
index 2efd9626e6..6406e79e07 100644
--- a/compiler/GHC/Unit/State.hs
+++ b/compiler/GHC/Unit/State.hs
@@ -28,7 +28,6 @@ module GHC.Unit.State (
lookupPackageName,
improveUnit,
searchPackageId,
- displayUnitId,
listVisibleModuleNames,
lookupModuleInAllUnits,
lookupModuleWithSuggestions,
@@ -61,14 +60,18 @@ module GHC.Unit.State (
instUnitToUnit,
instModuleToModule,
- -- * Utils
- mkIndefUnitId,
- updateIndefUnitId,
- unwireUnit,
+ -- * Pretty-printing
pprFlag,
pprUnits,
pprUnitsSimple,
+ pprUnitIdForUser,
+ pprUnitInfoForUser,
pprModuleMap,
+
+ -- * Utils
+ mkIndefUnitId,
+ updateIndefUnitId,
+ unwireUnit,
homeUnitIsIndefinite,
homeUnitIsDefinite,
)
@@ -81,6 +84,7 @@ import GHC.Prelude
import GHC.Platform
import GHC.Unit.Database
import GHC.Unit.Info
+import GHC.Unit.Ppr
import GHC.Unit.Types
import GHC.Unit.Module
import GHC.Driver.Session
@@ -887,7 +891,7 @@ findPackages prec_map pkg_map closure arg pkgs unusable
else Right (sortByPreference prec_map ps)
where
finder (PackageArg str) p
- = if str == unitPackageIdString p || str == unitPackageNameString p
+ = if matchingStr str p
then Just p
else Nothing
finder (UnitIdArg uid) p
@@ -2100,6 +2104,8 @@ add_unit pkg_map ps (p, mb_parent)
-- -----------------------------------------------------------------------------
+-- | Pretty-print a UnitId for the user.
+--
-- Cabal packages may contain several components (programs, libraries, etc.).
-- As far as GHC is concerned, installed package components ("units") are
-- identified by an opaque IndefUnitId string provided by Cabal. As the string
@@ -2111,26 +2117,30 @@ add_unit pkg_map ps (p, mb_parent)
--
-- Component name is only displayed if it isn't the default library
--
--- To do this we need to query the database (cached in DynFlags). We cache
--- these details in the IndefUnitId itself because we don't want to query
--- DynFlags each time we pretty-print the IndefUnitId
---
+-- To do this we need to query a unit database.
+pprUnitIdForUser :: UnitState -> UnitId -> SDoc
+pprUnitIdForUser state uid@(UnitId fs) =
+ case lookupUnitPprInfo state uid of
+ Nothing -> ftext fs -- we didn't find the unit at all
+ Just i -> ppr i
+
+pprUnitInfoForUser :: UnitInfo -> SDoc
+pprUnitInfoForUser info = ppr (mkUnitPprInfo unitIdFS info)
+
+lookupUnitPprInfo :: UnitState -> UnitId -> Maybe UnitPprInfo
+lookupUnitPprInfo state uid = fmap (mkUnitPprInfo unitIdFS) (lookupUnitId state uid)
+
+-- | Create a IndefUnitId.
mkIndefUnitId :: UnitState -> FastString -> IndefUnitId
-mkIndefUnitId pkgstate raw =
+mkIndefUnitId state raw =
let uid = UnitId raw
- in case lookupUnitId pkgstate uid of
- Nothing -> Indefinite uid Nothing -- we didn't find the unit at all
- Just c -> Indefinite uid $ Just $ mkUnitPprInfo c
+ in Indefinite uid $! lookupUnitPprInfo state uid
-- | Update component ID details from the database
updateIndefUnitId :: UnitState -> IndefUnitId -> IndefUnitId
updateIndefUnitId pkgstate uid = mkIndefUnitId pkgstate (unitIdFS (indefUnit uid))
-displayUnitId :: UnitState -> UnitId -> Maybe String
-displayUnitId pkgstate uid =
- fmap unitPackageIdString (lookupUnitId pkgstate uid)
-
-- -----------------------------------------------------------------------------
-- Displaying packages