summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Pickering <matthewtpickering@gmail.com>2022-10-26 11:58:13 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2022-12-15 03:54:39 -0500
commit2eb0fb87b921efc8f107eb39a3d34dae08082a3c (patch)
treee9ea102c18ae826d28a1b0751d9af175fc9cea0a
parent9372329008143104b0ae5e8e792e957090dfa743 (diff)
downloadhaskell-2eb0fb87b921efc8f107eb39a3d34dae08082a3c.tar.gz
Package Imports: Get candidate packages also from re-exported modules
Previously we were just looking at the direct imports to try and work out what a package qualifier could apply to but #22333 pointed out we also needed to look for reexported modules. Fixes #22333
-rw-r--r--compiler/GHC/Unit/Finder.hs4
-rw-r--r--compiler/GHC/Unit/State.hs14
-rw-r--r--testsuite/tests/cabal/t22333/Makefile32
-rw-r--r--testsuite/tests/cabal/t22333/Setup.hs2
-rw-r--r--testsuite/tests/cabal/t22333/all.T4
-rw-r--r--testsuite/tests/cabal/t22333/my-exe/CHANGELOG.md5
-rw-r--r--testsuite/tests/cabal/t22333/my-exe/app/Main.hs9
-rw-r--r--testsuite/tests/cabal/t22333/my-exe/my-exe.cabal20
-rw-r--r--testsuite/tests/cabal/t22333/my-package-a/CHANGELOG.md5
-rw-r--r--testsuite/tests/cabal/t22333/my-package-a/Setup.hs2
-rw-r--r--testsuite/tests/cabal/t22333/my-package-a/my-package-a.cabal20
-rw-r--r--testsuite/tests/cabal/t22333/my-package-a/src/MyLib.hs4
-rw-r--r--testsuite/tests/cabal/t22333/my-package-b/CHANGELOG.md5
-rw-r--r--testsuite/tests/cabal/t22333/my-package-b/Setup.hs2
-rw-r--r--testsuite/tests/cabal/t22333/my-package-b/my-package-b.cabal18
-rw-r--r--testsuite/tests/cabal/t22333/my-package-b/src/LibB.hs4
16 files changed, 147 insertions, 3 deletions
diff --git a/compiler/GHC/Unit/Finder.hs b/compiler/GHC/Unit/Finder.hs
index 45057d13e2..e722e432af 100644
--- a/compiler/GHC/Unit/Finder.hs
+++ b/compiler/GHC/Unit/Finder.hs
@@ -136,13 +136,13 @@ lookupFileCache (FinderCache _ ref) key = do
-- that package is searched for the module.
findImportedModule :: HscEnv -> ModuleName -> PkgQual -> IO FindResult
-findImportedModule hsc_env mod fs =
+findImportedModule hsc_env mod pkg_qual =
let fc = hsc_FC hsc_env
mhome_unit = hsc_home_unit_maybe hsc_env
dflags = hsc_dflags hsc_env
fopts = initFinderOpts dflags
in do
- findImportedModuleNoHsc fc fopts (hsc_unit_env hsc_env) mhome_unit mod fs
+ findImportedModuleNoHsc fc fopts (hsc_unit_env hsc_env) mhome_unit mod pkg_qual
findImportedModuleNoHsc
:: FinderCache
diff --git a/compiler/GHC/Unit/State.hs b/compiler/GHC/Unit/State.hs
index ca361d69d2..8123bbaab6 100644
--- a/compiler/GHC/Unit/State.hs
+++ b/compiler/GHC/Unit/State.hs
@@ -571,13 +571,25 @@ resolvePackageImport unit_st mn pn = do
-- 1. Find all modules providing the ModuleName (this accounts for visibility/thinning etc)
providers <- Map.filter originVisible <$> Map.lookup mn (moduleNameProvidersMap unit_st)
-- 2. Get the UnitIds of the candidates
- let candidates_uid = map (toUnitId . moduleUnit) $ Map.keys providers
+ let candidates_uid = concatMap to_uid $ Map.assocs providers
-- 3. Get the package names of the candidates
let candidates_units = map (\ui -> ((unitPackageName ui), unitId ui))
$ mapMaybe (\uid -> Map.lookup uid (unitInfoMap unit_st)) candidates_uid
-- 4. Check to see if the PackageName helps us disambiguate any candidates.
lookup pn candidates_units
+ where
+
+ -- Get the UnitId from which a visible identifier is from
+ to_uid :: (Module, ModuleOrigin) -> [UnitId]
+ to_uid (mod, ModOrigin mo re_exps _ _) =
+ case mo of
+ -- Available directly, but also potentially from re-exports
+ Just True -> (toUnitId (moduleUnit mod)) : map unitId re_exps
+ -- Just available from these re-exports
+ _ -> map unitId re_exps
+ to_uid _ = []
+
-- | Create a Map UnitId UnitInfo
--
-- For each instantiated unit, we add two map keys:
diff --git a/testsuite/tests/cabal/t22333/Makefile b/testsuite/tests/cabal/t22333/Makefile
new file mode 100644
index 0000000000..da159a46ae
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/Makefile
@@ -0,0 +1,32 @@
+TOP=../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
+
+SETUP=../Setup -v0
+CONFIGURE=$(SETUP) configure $(CABAL_MINIMAL_BUILD) --with-ghc='$(TEST_HC)' --ghc-options='$(TEST_HC_OPTS)' --package-db=../tmp.d --prefix='$(PWD)/../dist'
+
+T22333: clean
+ $(MAKE) -s --no-print-directory clean
+ '$(GHC_PKG)' init tmp.d
+ '$(TEST_HC)' $(TEST_HC_OPTS) -v0 --make Setup
+ # build b
+ cd my-package-b; $(CONFIGURE) --cid "my-package-b-0.1" my-package-b
+ cd my-package-b; $(SETUP) build
+ cd my-package-b; $(SETUP) copy
+ cd my-package-b; $(SETUP) register
+ # build a
+ cd my-package-a; $(CONFIGURE) --cid "my-package-a-0.1" my-package-a
+ cd my-package-a; $(SETUP) build
+ cd my-package-a; $(SETUP) copy
+ cd my-package-a; $(SETUP) register
+ # build my-exe
+ cd my-exe; $(CONFIGURE) --cid "my-exe-0.1" my-exe
+ cd my-exe; $(SETUP) build
+ cd my-exe; $(SETUP) copy
+ cd my-exe; $(SETUP) register
+ifneq "$(CLEANUP)" ""
+ $(MAKE) -s --no-print-directory clean
+endif
+
+clean :
+ $(RM) -r tmp.d dist Setup$(exeext)
diff --git a/testsuite/tests/cabal/t22333/Setup.hs b/testsuite/tests/cabal/t22333/Setup.hs
new file mode 100644
index 0000000000..9a994af677
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/testsuite/tests/cabal/t22333/all.T b/testsuite/tests/cabal/t22333/all.T
new file mode 100644
index 0000000000..5664400e4a
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/all.T
@@ -0,0 +1,4 @@
+test('T22333',
+ [extra_files(['Setup.hs', 'my-exe', 'my-package-a', 'my-package-b' ])],
+ makefile_test,
+ [])
diff --git a/testsuite/tests/cabal/t22333/my-exe/CHANGELOG.md b/testsuite/tests/cabal/t22333/my-exe/CHANGELOG.md
new file mode 100644
index 0000000000..0313224bbd
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/my-exe/CHANGELOG.md
@@ -0,0 +1,5 @@
+# Revision history for my-exe
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
diff --git a/testsuite/tests/cabal/t22333/my-exe/app/Main.hs b/testsuite/tests/cabal/t22333/my-exe/app/Main.hs
new file mode 100644
index 0000000000..777495202a
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/my-exe/app/Main.hs
@@ -0,0 +1,9 @@
+{-# LANGUAGE PackageImports #-}
+
+module Main (main) where
+
+import "my-package-a" LibB (someFuncB)
+--import LibB (someFuncB)
+
+main :: IO ()
+main = print someFuncB
diff --git a/testsuite/tests/cabal/t22333/my-exe/my-exe.cabal b/testsuite/tests/cabal/t22333/my-exe/my-exe.cabal
new file mode 100644
index 0000000000..1a1c4c60cb
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/my-exe/my-exe.cabal
@@ -0,0 +1,20 @@
+cabal-version: 3.8
+name: my-exe
+version: 0.1.0.0
+license: NONE
+author: a.pelenitsyn@gmail.com
+maintainer: Artem Pelenitsyn
+build-type: Simple
+extra-doc-files: CHANGELOG.md
+
+common warnings
+ ghc-options: -Wall
+
+executable my-exe
+ import: warnings
+ main-is: Main.hs
+ build-depends: base
+ , my-package-a
+ , my-package-b
+ hs-source-dirs: app
+ default-language: Haskell2010
diff --git a/testsuite/tests/cabal/t22333/my-package-a/CHANGELOG.md b/testsuite/tests/cabal/t22333/my-package-a/CHANGELOG.md
new file mode 100644
index 0000000000..7507aa3dab
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/my-package-a/CHANGELOG.md
@@ -0,0 +1,5 @@
+# Revision history for my-package-a
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
diff --git a/testsuite/tests/cabal/t22333/my-package-a/Setup.hs b/testsuite/tests/cabal/t22333/my-package-a/Setup.hs
new file mode 100644
index 0000000000..9a994af677
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/my-package-a/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/testsuite/tests/cabal/t22333/my-package-a/my-package-a.cabal b/testsuite/tests/cabal/t22333/my-package-a/my-package-a.cabal
new file mode 100644
index 0000000000..bf46792df7
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/my-package-a/my-package-a.cabal
@@ -0,0 +1,20 @@
+cabal-version: 3.8
+name: my-package-a
+version: 0.1.0.0
+license: NONE
+author: a.pelenitsyn@gmail.com
+maintainer: Artem Pelenitsyn
+build-type: Simple
+extra-doc-files: CHANGELOG.md
+
+common warnings
+ ghc-options: -Wall
+
+library
+ import: warnings
+ exposed-modules: MyLib
+ build-depends: base
+ , my-package-b
+ reexported-modules: LibB
+ hs-source-dirs: src
+ default-language: Haskell2010
diff --git a/testsuite/tests/cabal/t22333/my-package-a/src/MyLib.hs b/testsuite/tests/cabal/t22333/my-package-a/src/MyLib.hs
new file mode 100644
index 0000000000..e657c4403f
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/my-package-a/src/MyLib.hs
@@ -0,0 +1,4 @@
+module MyLib (someFunc) where
+
+someFunc :: IO ()
+someFunc = putStrLn "someFunc"
diff --git a/testsuite/tests/cabal/t22333/my-package-b/CHANGELOG.md b/testsuite/tests/cabal/t22333/my-package-b/CHANGELOG.md
new file mode 100644
index 0000000000..b410066be6
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/my-package-b/CHANGELOG.md
@@ -0,0 +1,5 @@
+# Revision history for my-package-b
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
diff --git a/testsuite/tests/cabal/t22333/my-package-b/Setup.hs b/testsuite/tests/cabal/t22333/my-package-b/Setup.hs
new file mode 100644
index 0000000000..9a994af677
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/my-package-b/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/testsuite/tests/cabal/t22333/my-package-b/my-package-b.cabal b/testsuite/tests/cabal/t22333/my-package-b/my-package-b.cabal
new file mode 100644
index 0000000000..7157b9be93
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/my-package-b/my-package-b.cabal
@@ -0,0 +1,18 @@
+cabal-version: 3.8
+name: my-package-b
+version: 0.1.0.0
+license: NONE
+author: a.pelenitsyn@gmail.com
+maintainer: Artem Pelenitsyn
+build-type: Simple
+extra-doc-files: CHANGELOG.md
+
+common warnings
+ ghc-options: -Wall
+
+library
+ import: warnings
+ exposed-modules: LibB
+ build-depends: base
+ hs-source-dirs: src
+ default-language: Haskell2010
diff --git a/testsuite/tests/cabal/t22333/my-package-b/src/LibB.hs b/testsuite/tests/cabal/t22333/my-package-b/src/LibB.hs
new file mode 100644
index 0000000000..8e85cdcea3
--- /dev/null
+++ b/testsuite/tests/cabal/t22333/my-package-b/src/LibB.hs
@@ -0,0 +1,4 @@
+module LibB (someFuncB) where
+
+someFuncB :: Int
+someFuncB = 42