Revert "Support for multiple signature files in scope."
[ghc.git] / compiler / main / Finder.hs
1 {-
2 (c) The University of Glasgow, 2000-2006
3
4 \section[Finder]{Module Finder}
5 -}
6
7 {-# LANGUAGE CPP #-}
8
9 module Finder (
10 flushFinderCaches,
11 FindResult(..),
12 findImportedModule,
13 findExactModule,
14 findHomeModule,
15 findExposedPackageModule,
16 mkHomeModLocation,
17 mkHomeModLocation2,
18 mkHiOnlyModLocation,
19 addHomeModuleToFinder,
20 uncacheModule,
21 mkStubPaths,
22
23 findObjectLinkableMaybe,
24 findObjectLinkable,
25
26 cannotFindModule,
27 cannotFindInterface,
28
29 ) where
30
31 #include "HsVersions.h"
32
33 import Module
34 import HscTypes
35 import Packages
36 import FastString
37 import Util
38 import PrelNames ( gHC_PRIM )
39 import DynFlags
40 import Outputable
41 import Maybes ( expectJust )
42
43 import Data.IORef ( IORef, readIORef, atomicModifyIORef' )
44 import System.Directory
45 import System.FilePath
46 import Control.Monad
47 import Data.Time
48 import Data.List ( foldl' )
49
50
51 type FileExt = String -- Filename extension
52 type BaseName = String -- Basename of file
53
54 -- -----------------------------------------------------------------------------
55 -- The Finder
56
57 -- The Finder provides a thin filesystem abstraction to the rest of
58 -- the compiler. For a given module, it can tell you where the
59 -- source, interface, and object files for that module live.
60
61 -- It does *not* know which particular package a module lives in. Use
62 -- Packages.lookupModuleInAllPackages for that.
63
64 -- -----------------------------------------------------------------------------
65 -- The finder's cache
66
67 -- remove all the home modules from the cache; package modules are
68 -- assumed to not move around during a session.
69 flushFinderCaches :: HscEnv -> IO ()
70 flushFinderCaches hsc_env =
71 atomicModifyIORef' fc_ref $ \fm -> (filterModuleEnv is_ext fm, ())
72 where
73 this_pkg = thisPackage (hsc_dflags hsc_env)
74 fc_ref = hsc_FC hsc_env
75 is_ext mod _ | modulePackageKey mod /= this_pkg = True
76 | otherwise = False
77
78 addToFinderCache :: IORef FinderCache -> Module -> FindResult -> IO ()
79 addToFinderCache ref key val =
80 atomicModifyIORef' ref $ \c -> (extendModuleEnv c key val, ())
81
82 removeFromFinderCache :: IORef FinderCache -> Module -> IO ()
83 removeFromFinderCache ref key =
84 atomicModifyIORef' ref $ \c -> (delModuleEnv c key, ())
85
86 lookupFinderCache :: IORef FinderCache -> Module -> IO (Maybe FindResult)
87 lookupFinderCache ref key = do
88 c <- readIORef ref
89 return $! lookupModuleEnv c key
90
91 -- -----------------------------------------------------------------------------
92 -- The two external entry points
93
94 -- | Locate a module that was imported by the user. We have the
95 -- module's name, and possibly a package name. Without a package
96 -- name, this function will use the search path and the known exposed
97 -- packages to find the module, if a package is specified then only
98 -- that package is searched for the module.
99
100 findImportedModule :: HscEnv -> ModuleName -> Maybe FastString -> IO FindResult
101 findImportedModule hsc_env mod_name mb_pkg =
102 case mb_pkg of
103 Nothing -> unqual_import
104 Just pkg | pkg == fsLit "this" -> home_import -- "this" is special
105 | otherwise -> pkg_import
106 where
107 home_import = findHomeModule hsc_env mod_name
108
109 pkg_import = findExposedPackageModule hsc_env mod_name mb_pkg
110
111 unqual_import = home_import
112 `orIfNotFound`
113 findExposedPackageModule hsc_env mod_name Nothing
114
115 -- | Locate a specific 'Module'. The purpose of this function is to
116 -- create a 'ModLocation' for a given 'Module', that is to find out
117 -- where the files associated with this module live. It is used when
118 -- reading the interface for a module mentioned by another interface,
119 -- for example (a "system import").
120
121 findExactModule :: HscEnv -> Module -> IO FindResult
122 findExactModule hsc_env mod =
123 let dflags = hsc_dflags hsc_env
124 in if modulePackageKey mod == thisPackage dflags
125 then findHomeModule hsc_env (moduleName mod)
126 else findPackageModule hsc_env mod
127
128 -- -----------------------------------------------------------------------------
129 -- Helpers
130
131 orIfNotFound :: IO FindResult -> IO FindResult -> IO FindResult
132 orIfNotFound this or_this = do
133 res <- this
134 case res of
135 NotFound { fr_paths = paths1, fr_mods_hidden = mh1
136 , fr_pkgs_hidden = ph1, fr_suggestions = s1 }
137 -> do res2 <- or_this
138 case res2 of
139 NotFound { fr_paths = paths2, fr_pkg = mb_pkg2, fr_mods_hidden = mh2
140 , fr_pkgs_hidden = ph2, fr_suggestions = s2 }
141 -> return (NotFound { fr_paths = paths1 ++ paths2
142 , fr_pkg = mb_pkg2 -- snd arg is the package search
143 , fr_mods_hidden = mh1 ++ mh2
144 , fr_pkgs_hidden = ph1 ++ ph2
145 , fr_suggestions = s1 ++ s2 })
146 _other -> return res2
147 _other -> return res
148
149 -- | Helper function for 'findHomeModule': this function wraps an IO action
150 -- which would look up @mod_name@ in the file system (the home package),
151 -- and first consults the 'hsc_FC' cache to see if the lookup has already
152 -- been done. Otherwise, do the lookup (with the IO action) and save
153 -- the result in the finder cache and the module location cache (if it
154 -- was successful.)
155 homeSearchCache :: HscEnv -> ModuleName -> IO FindResult -> IO FindResult
156 homeSearchCache hsc_env mod_name do_this = do
157 let mod = mkModule (thisPackage (hsc_dflags hsc_env)) mod_name
158 modLocationCache hsc_env mod do_this
159
160 findExposedPackageModule :: HscEnv -> ModuleName -> Maybe FastString
161 -> IO FindResult
162 findExposedPackageModule hsc_env mod_name mb_pkg
163 = case lookupModuleWithSuggestions (hsc_dflags hsc_env) mod_name mb_pkg of
164 LookupFound m pkg_conf ->
165 findPackageModule_ hsc_env m pkg_conf
166 LookupMultiple rs ->
167 return (FoundMultiple rs)
168 LookupHidden pkg_hiddens mod_hiddens ->
169 return (NotFound{ fr_paths = [], fr_pkg = Nothing
170 , fr_pkgs_hidden = map (modulePackageKey.fst) pkg_hiddens
171 , fr_mods_hidden = map (modulePackageKey.fst) mod_hiddens
172 , fr_suggestions = [] })
173 LookupNotFound suggest ->
174 return (NotFound{ fr_paths = [], fr_pkg = Nothing
175 , fr_pkgs_hidden = []
176 , fr_mods_hidden = []
177 , fr_suggestions = suggest })
178
179 modLocationCache :: HscEnv -> Module -> IO FindResult -> IO FindResult
180 modLocationCache hsc_env mod do_this = do
181 m <- lookupFinderCache (hsc_FC hsc_env) mod
182 case m of
183 Just result -> return result
184 Nothing -> do
185 result <- do_this
186 addToFinderCache (hsc_FC hsc_env) mod result
187 return result
188
189 addHomeModuleToFinder :: HscEnv -> ModuleName -> ModLocation -> IO Module
190 addHomeModuleToFinder hsc_env mod_name loc = do
191 let mod = mkModule (thisPackage (hsc_dflags hsc_env)) mod_name
192 addToFinderCache (hsc_FC hsc_env) mod (Found loc mod)
193 return mod
194
195 uncacheModule :: HscEnv -> ModuleName -> IO ()
196 uncacheModule hsc_env mod = do
197 let this_pkg = thisPackage (hsc_dflags hsc_env)
198 removeFromFinderCache (hsc_FC hsc_env) (mkModule this_pkg mod)
199
200 -- -----------------------------------------------------------------------------
201 -- The internal workers
202
203 -- | Implements the search for a module name in the home package only. Calling
204 -- this function directly is usually *not* what you want; currently, it's used
205 -- as a building block for the following operations:
206 --
207 -- 1. When you do a normal package lookup, we first check if the module
208 -- is available in the home module, before looking it up in the package
209 -- database.
210 --
211 -- 2. When you have a package qualified import with package name "this",
212 -- we shortcut to the home module.
213 --
214 -- 3. When we look up an exact 'Module', if the package key associated with
215 -- the module is the current home module do a look up in the home module.
216 --
217 -- 4. Some special-case code in GHCi (ToDo: Figure out why that needs to
218 -- call this.)
219 findHomeModule :: HscEnv -> ModuleName -> IO FindResult
220 findHomeModule hsc_env mod_name =
221 homeSearchCache hsc_env mod_name $
222 let
223 dflags = hsc_dflags hsc_env
224 home_path = importPaths dflags
225 hisuf = hiSuf dflags
226 mod = mkModule (thisPackage dflags) mod_name
227
228 source_exts =
229 [ ("hs", mkHomeModLocationSearched dflags mod_name "hs")
230 , ("lhs", mkHomeModLocationSearched dflags mod_name "lhs")
231 , ("hsig", mkHomeModLocationSearched dflags mod_name "hsig")
232 , ("lhsig", mkHomeModLocationSearched dflags mod_name "lhsig")
233 ]
234
235 hi_exts = [ (hisuf, mkHiOnlyModLocation dflags hisuf)
236 , (addBootSuffix hisuf, mkHiOnlyModLocation dflags hisuf)
237 ]
238
239 -- In compilation manager modes, we look for source files in the home
240 -- package because we can compile these automatically. In one-shot
241 -- compilation mode we look for .hi and .hi-boot files only.
242 exts | isOneShot (ghcMode dflags) = hi_exts
243 | otherwise = source_exts
244 in
245
246 -- special case for GHC.Prim; we won't find it in the filesystem.
247 -- This is important only when compiling the base package (where GHC.Prim
248 -- is a home module).
249 if mod == gHC_PRIM
250 then return (Found (error "GHC.Prim ModLocation") mod)
251 else searchPathExts home_path mod exts
252
253
254 -- | Search for a module in external packages only.
255 findPackageModule :: HscEnv -> Module -> IO FindResult
256 findPackageModule hsc_env mod = do
257 let
258 dflags = hsc_dflags hsc_env
259 pkg_id = modulePackageKey mod
260 --
261 case lookupPackage dflags pkg_id of
262 Nothing -> return (NoPackage pkg_id)
263 Just pkg_conf -> findPackageModule_ hsc_env mod pkg_conf
264
265 -- | Look up the interface file associated with module @mod@. This function
266 -- requires a few invariants to be upheld: (1) the 'Module' in question must
267 -- be the module identifier of the *original* implementation of a module,
268 -- not a reexport (this invariant is upheld by @Packages.hs@) and (2)
269 -- the 'PackageConfig' must be consistent with the package key in the 'Module'.
270 -- The redundancy is to avoid an extra lookup in the package state
271 -- for the appropriate config.
272 findPackageModule_ :: HscEnv -> Module -> PackageConfig -> IO FindResult
273 findPackageModule_ hsc_env mod pkg_conf =
274 ASSERT( modulePackageKey mod == packageConfigId pkg_conf )
275 modLocationCache hsc_env mod $
276
277 -- special case for GHC.Prim; we won't find it in the filesystem.
278 if mod == gHC_PRIM
279 then return (Found (error "GHC.Prim ModLocation") mod)
280 else
281
282 let
283 dflags = hsc_dflags hsc_env
284 tag = buildTag dflags
285
286 -- hi-suffix for packages depends on the build tag.
287 package_hisuf | null tag = "hi"
288 | otherwise = tag ++ "_hi"
289
290 mk_hi_loc = mkHiOnlyModLocation dflags package_hisuf
291
292 import_dirs = importDirs pkg_conf
293 -- we never look for a .hi-boot file in an external package;
294 -- .hi-boot files only make sense for the home package.
295 in
296 case import_dirs of
297 [one] | MkDepend <- ghcMode dflags -> do
298 -- there's only one place that this .hi file can be, so
299 -- don't bother looking for it.
300 let basename = moduleNameSlashes (moduleName mod)
301 loc <- mk_hi_loc one basename
302 return (Found loc mod)
303 _otherwise ->
304 searchPathExts import_dirs mod [(package_hisuf, mk_hi_loc)]
305
306 -- -----------------------------------------------------------------------------
307 -- General path searching
308
309 searchPathExts
310 :: [FilePath] -- paths to search
311 -> Module -- module name
312 -> [ (
313 FileExt, -- suffix
314 FilePath -> BaseName -> IO ModLocation -- action
315 )
316 ]
317 -> IO FindResult
318
319 searchPathExts paths mod exts
320 = do result <- search to_search
321 {-
322 hPutStrLn stderr (showSDoc $
323 vcat [text "Search" <+> ppr mod <+> sep (map (text. fst) exts)
324 , nest 2 (vcat (map text paths))
325 , case result of
326 Succeeded (loc, p) -> text "Found" <+> ppr loc
327 Failed fs -> text "not found"])
328 -}
329 return result
330
331 where
332 basename = moduleNameSlashes (moduleName mod)
333
334 to_search :: [(FilePath, IO ModLocation)]
335 to_search = [ (file, fn path basename)
336 | path <- paths,
337 (ext,fn) <- exts,
338 let base | path == "." = basename
339 | otherwise = path </> basename
340 file = base <.> ext
341 ]
342
343 search [] = return (NotFound { fr_paths = map fst to_search
344 , fr_pkg = Just (modulePackageKey mod)
345 , fr_mods_hidden = [], fr_pkgs_hidden = []
346 , fr_suggestions = [] })
347
348 search ((file, mk_result) : rest) = do
349 b <- doesFileExist file
350 if b
351 then do { loc <- mk_result; return (Found loc mod) }
352 else search rest
353
354 mkHomeModLocationSearched :: DynFlags -> ModuleName -> FileExt
355 -> FilePath -> BaseName -> IO ModLocation
356 mkHomeModLocationSearched dflags mod suff path basename = do
357 mkHomeModLocation2 dflags mod (path </> basename) suff
358
359 -- -----------------------------------------------------------------------------
360 -- Constructing a home module location
361
362 -- This is where we construct the ModLocation for a module in the home
363 -- package, for which we have a source file. It is called from three
364 -- places:
365 --
366 -- (a) Here in the finder, when we are searching for a module to import,
367 -- using the search path (-i option).
368 --
369 -- (b) The compilation manager, when constructing the ModLocation for
370 -- a "root" module (a source file named explicitly on the command line
371 -- or in a :load command in GHCi).
372 --
373 -- (c) The driver in one-shot mode, when we need to construct a
374 -- ModLocation for a source file named on the command-line.
375 --
376 -- Parameters are:
377 --
378 -- mod
379 -- The name of the module
380 --
381 -- path
382 -- (a): The search path component where the source file was found.
383 -- (b) and (c): "."
384 --
385 -- src_basename
386 -- (a): (moduleNameSlashes mod)
387 -- (b) and (c): The filename of the source file, minus its extension
388 --
389 -- ext
390 -- The filename extension of the source file (usually "hs" or "lhs").
391
392 mkHomeModLocation :: DynFlags -> ModuleName -> FilePath -> IO ModLocation
393 mkHomeModLocation dflags mod src_filename = do
394 let (basename,extension) = splitExtension src_filename
395 mkHomeModLocation2 dflags mod basename extension
396
397 mkHomeModLocation2 :: DynFlags
398 -> ModuleName
399 -> FilePath -- Of source module, without suffix
400 -> String -- Suffix
401 -> IO ModLocation
402 mkHomeModLocation2 dflags mod src_basename ext = do
403 let mod_basename = moduleNameSlashes mod
404
405 obj_fn = mkObjPath dflags src_basename mod_basename
406 hi_fn = mkHiPath dflags src_basename mod_basename
407
408 return (ModLocation{ ml_hs_file = Just (src_basename <.> ext),
409 ml_hi_file = hi_fn,
410 ml_obj_file = obj_fn })
411
412 mkHiOnlyModLocation :: DynFlags -> Suffix -> FilePath -> String
413 -> IO ModLocation
414 mkHiOnlyModLocation dflags hisuf path basename
415 = do let full_basename = path </> basename
416 obj_fn = mkObjPath dflags full_basename basename
417 return ModLocation{ ml_hs_file = Nothing,
418 ml_hi_file = full_basename <.> hisuf,
419 -- Remove the .hi-boot suffix from
420 -- hi_file, if it had one. We always
421 -- want the name of the real .hi file
422 -- in the ml_hi_file field.
423 ml_obj_file = obj_fn
424 }
425
426 -- | Constructs the filename of a .o file for a given source file.
427 -- Does /not/ check whether the .o file exists
428 mkObjPath
429 :: DynFlags
430 -> FilePath -- the filename of the source file, minus the extension
431 -> String -- the module name with dots replaced by slashes
432 -> FilePath
433 mkObjPath dflags basename mod_basename = obj_basename <.> osuf
434 where
435 odir = objectDir dflags
436 osuf = objectSuf dflags
437
438 obj_basename | Just dir <- odir = dir </> mod_basename
439 | otherwise = basename
440
441
442 -- | Constructs the filename of a .hi file for a given source file.
443 -- Does /not/ check whether the .hi file exists
444 mkHiPath
445 :: DynFlags
446 -> FilePath -- the filename of the source file, minus the extension
447 -> String -- the module name with dots replaced by slashes
448 -> FilePath
449 mkHiPath dflags basename mod_basename = hi_basename <.> hisuf
450 where
451 hidir = hiDir dflags
452 hisuf = hiSuf dflags
453
454 hi_basename | Just dir <- hidir = dir </> mod_basename
455 | otherwise = basename
456
457
458
459 -- -----------------------------------------------------------------------------
460 -- Filenames of the stub files
461
462 -- We don't have to store these in ModLocations, because they can be derived
463 -- from other available information, and they're only rarely needed.
464
465 mkStubPaths
466 :: DynFlags
467 -> ModuleName
468 -> ModLocation
469 -> FilePath
470
471 mkStubPaths dflags mod location
472 = let
473 stubdir = stubDir dflags
474
475 mod_basename = moduleNameSlashes mod
476 src_basename = dropExtension $ expectJust "mkStubPaths"
477 (ml_hs_file location)
478
479 stub_basename0
480 | Just dir <- stubdir = dir </> mod_basename
481 | otherwise = src_basename
482
483 stub_basename = stub_basename0 ++ "_stub"
484 in
485 stub_basename <.> "h"
486
487 -- -----------------------------------------------------------------------------
488 -- findLinkable isn't related to the other stuff in here,
489 -- but there's no other obvious place for it
490
491 findObjectLinkableMaybe :: Module -> ModLocation -> IO (Maybe Linkable)
492 findObjectLinkableMaybe mod locn
493 = do let obj_fn = ml_obj_file locn
494 maybe_obj_time <- modificationTimeIfExists obj_fn
495 case maybe_obj_time of
496 Nothing -> return Nothing
497 Just obj_time -> liftM Just (findObjectLinkable mod obj_fn obj_time)
498
499 -- Make an object linkable when we know the object file exists, and we know
500 -- its modification time.
501 findObjectLinkable :: Module -> FilePath -> UTCTime -> IO Linkable
502 findObjectLinkable mod obj_fn obj_time = return (LM obj_time mod [DotO obj_fn])
503 -- We used to look for _stub.o files here, but that was a bug (#706)
504 -- Now GHC merges the stub.o into the main .o (#3687)
505
506 -- -----------------------------------------------------------------------------
507 -- Error messages
508
509 cannotFindModule :: DynFlags -> ModuleName -> FindResult -> SDoc
510 cannotFindModule = cantFindErr (sLit "Could not find module")
511 (sLit "Ambiguous module name")
512
513 cannotFindInterface :: DynFlags -> ModuleName -> FindResult -> SDoc
514 cannotFindInterface = cantFindErr (sLit "Failed to load interface for")
515 (sLit "Ambiguous interface for")
516
517 cantFindErr :: LitString -> LitString -> DynFlags -> ModuleName -> FindResult
518 -> SDoc
519 cantFindErr _ multiple_found _ mod_name (FoundMultiple mods)
520 | Just pkgs <- unambiguousPackages
521 = hang (ptext multiple_found <+> quotes (ppr mod_name) <> colon) 2 (
522 sep [ptext (sLit "it was found in multiple packages:"),
523 hsep (map ppr pkgs) ]
524 )
525 | otherwise
526 = hang (ptext multiple_found <+> quotes (ppr mod_name) <> colon) 2 (
527 vcat (map pprMod mods)
528 )
529 where
530 unambiguousPackages = foldl' unambiguousPackage (Just []) mods
531 unambiguousPackage (Just xs) (m, ModOrigin (Just _) _ _ _)
532 = Just (modulePackageKey m : xs)
533 unambiguousPackage _ _ = Nothing
534
535 pprMod (m, o) = ptext (sLit "it is bound as") <+> ppr m <+>
536 ptext (sLit "by") <+> pprOrigin m o
537 pprOrigin _ ModHidden = panic "cantFindErr: bound by mod hidden"
538 pprOrigin m (ModOrigin e res _ f) = sep $ punctuate comma (
539 if e == Just True
540 then [ptext (sLit "package") <+> ppr (modulePackageKey m)]
541 else [] ++
542 map ((ptext (sLit "a reexport in package") <+>)
543 .ppr.packageConfigId) res ++
544 if f then [ptext (sLit "a package flag")] else []
545 )
546
547 cantFindErr cannot_find _ dflags mod_name find_result
548 = ptext cannot_find <+> quotes (ppr mod_name)
549 $$ more_info
550 where
551 more_info
552 = case find_result of
553 NoPackage pkg
554 -> ptext (sLit "no package key matching") <+> quotes (ppr pkg) <+>
555 ptext (sLit "was found") $$ looks_like_srcpkgid pkg
556
557 NotFound { fr_paths = files, fr_pkg = mb_pkg
558 , fr_mods_hidden = mod_hiddens, fr_pkgs_hidden = pkg_hiddens
559 , fr_suggestions = suggest }
560 | Just pkg <- mb_pkg, pkg /= thisPackage dflags
561 -> not_found_in_package pkg files
562
563 | not (null suggest)
564 -> pp_suggestions suggest $$ tried_these files
565
566 | null files && null mod_hiddens && null pkg_hiddens
567 -> ptext (sLit "It is not a module in the current program, or in any known package.")
568
569 | otherwise
570 -> vcat (map pkg_hidden pkg_hiddens) $$
571 vcat (map mod_hidden mod_hiddens) $$
572 tried_these files
573
574 _ -> panic "cantFindErr"
575
576 build_tag = buildTag dflags
577
578 not_found_in_package pkg files
579 | build_tag /= ""
580 = let
581 build = if build_tag == "p" then "profiling"
582 else "\"" ++ build_tag ++ "\""
583 in
584 ptext (sLit "Perhaps you haven't installed the ") <> text build <>
585 ptext (sLit " libraries for package ") <> quotes (ppr pkg) <> char '?' $$
586 tried_these files
587
588 | otherwise
589 = ptext (sLit "There are files missing in the ") <> quotes (ppr pkg) <>
590 ptext (sLit " package,") $$
591 ptext (sLit "try running 'ghc-pkg check'.") $$
592 tried_these files
593
594 tried_these files
595 | null files = Outputable.empty
596 | verbosity dflags < 3 =
597 ptext (sLit "Use -v to see a list of the files searched for.")
598 | otherwise =
599 hang (ptext (sLit "Locations searched:")) 2 $ vcat (map text files)
600
601 pkg_hidden :: PackageKey -> SDoc
602 pkg_hidden pkgid =
603 ptext (sLit "It is a member of the hidden package")
604 <+> quotes (ppr pkgid)
605 --FIXME: we don't really want to show the package key here we should
606 -- show the source package id or installed package id if it's ambiguous
607 <> dot $$ cabal_pkg_hidden_hint pkgid
608 cabal_pkg_hidden_hint pkgid
609 | gopt Opt_BuildingCabalPackage dflags
610 = let pkg = expectJust "pkg_hidden" (lookupPackage dflags pkgid)
611 in ptext (sLit "Perhaps you need to add") <+>
612 quotes (ppr (packageName pkg)) <+>
613 ptext (sLit "to the build-depends in your .cabal file.")
614 | otherwise = Outputable.empty
615
616 looks_like_srcpkgid :: PackageKey -> SDoc
617 looks_like_srcpkgid pk
618 -- Unsafely coerce a package key FastString into a source package ID
619 -- FastString and see if it means anything.
620 | (pkg:pkgs) <- searchPackageId dflags (SourcePackageId (packageKeyFS pk))
621 = parens (text "This package key looks like the source package ID;" $$
622 text "the real package key is" <+> quotes (ftext (packageKeyFS (packageKey pkg))) $$
623 (if null pkgs then Outputable.empty
624 else text "and" <+> int (length pkgs) <+> text "other candidates"))
625 -- Todo: also check if it looks like a package name!
626 | otherwise = Outputable.empty
627
628 mod_hidden pkg =
629 ptext (sLit "it is a hidden module in the package") <+> quotes (ppr pkg)
630
631 pp_suggestions :: [ModuleSuggestion] -> SDoc
632 pp_suggestions sugs
633 | null sugs = Outputable.empty
634 | otherwise = hang (ptext (sLit "Perhaps you meant"))
635 2 (vcat (map pp_sugg sugs))
636
637 -- NB: Prefer the *original* location, and then reexports, and then
638 -- package flags when making suggestions. ToDo: if the original package
639 -- also has a reexport, prefer that one
640 pp_sugg (SuggestVisible m mod o) = ppr m <+> provenance o
641 where provenance ModHidden = Outputable.empty
642 provenance (ModOrigin{ fromOrigPackage = e,
643 fromExposedReexport = res,
644 fromPackageFlag = f })
645 | Just True <- e
646 = parens (ptext (sLit "from") <+> ppr (modulePackageKey mod))
647 | f && moduleName mod == m
648 = parens (ptext (sLit "from") <+> ppr (modulePackageKey mod))
649 | (pkg:_) <- res
650 = parens (ptext (sLit "from") <+> ppr (packageConfigId pkg)
651 <> comma <+> ptext (sLit "reexporting") <+> ppr mod)
652 | f
653 = parens (ptext (sLit "defined via package flags to be")
654 <+> ppr mod)
655 | otherwise = Outputable.empty
656 pp_sugg (SuggestHidden m mod o) = ppr m <+> provenance o
657 where provenance ModHidden = Outputable.empty
658 provenance (ModOrigin{ fromOrigPackage = e,
659 fromHiddenReexport = rhs })
660 | Just False <- e
661 = parens (ptext (sLit "needs flag -package-key")
662 <+> ppr (modulePackageKey mod))
663 | (pkg:_) <- rhs
664 = parens (ptext (sLit "needs flag -package-key")
665 <+> ppr (packageConfigId pkg))
666 | otherwise = Outputable.empty