Simon Hengel pushed to branch wip/sol/reexported-error-message at Glasgow Haskell Compiler / GHC Commits: 26cc1e97 by Simon Hengel at 2026-06-22T03:47:11+07:00 Reference correct package in error messages for reexported modules (fixes #27417) - - - - - 11 changed files: - + changelog.d/reexported-module-errors - compiler/GHC/Iface/Errors.hs - compiler/GHC/Iface/Errors/Ppr.hs - compiler/GHC/Iface/Errors/Types.hs - compiler/GHC/Unit/Finder.hs - compiler/GHC/Unit/Finder/Types.hs - compiler/GHC/Unit/State.hs - ghc/GHCi/UI/Exception.hs - + testsuite/tests/package/ImportReexport.hs - + testsuite/tests/package/ImportReexport.stderr - testsuite/tests/package/all.T Changes: ===================================== changelog.d/reexported-module-errors ===================================== @@ -0,0 +1,4 @@ +section: compiler +synopsis: Reference the correct package in error messages when trying to import a reexported module from a hidden package. +issues: #27417 +mrs: !16229 ===================================== compiler/GHC/Iface/Errors.hs ===================================== @@ -128,7 +128,7 @@ cantFindErr unit_env profile mod_name find_result | otherwise -> GenericMissing - (map ((\uid -> (uid, lookupUnit (ue_homeUnitState unit_env) uid))) pkg_hiddens) + pkg_hiddens mod_hiddens unusables files _ -> panic "cantFindErr" ===================================== compiler/GHC/Iface/Errors/Ppr.hs ===================================== @@ -261,13 +261,13 @@ cantFindErrorX pkg_hidden_hint may_show_locations mod_or_interface (CantFindInst .ppr.mkUnit) res ++ if f then [text "a package flag"] else [] ) - pkg_hidden :: (Unit, Maybe UnitInfo) -> SDoc - pkg_hidden (uid, uif) = + pkg_hidden :: UnitInfo -> SDoc + pkg_hidden unit = text "It is a member of the hidden package" - <+> quotes (ppr uid) + <+> quotes (ppr $ unitId unit) --FIXME: we don't really want to show the unit id here we should -- show the source package id or installed package id if it's ambiguous - <> dot $$ maybe empty pkg_hidden_hint uif + <> dot $$ pkg_hidden_hint unit mod_hidden pkg = ===================================== compiler/GHC/Iface/Errors/Types.hs ===================================== @@ -73,7 +73,7 @@ data CantFindInstalledReason | NotAModule | CouldntFindInFiles [FilePath] | GenericMissing - [(Unit, Maybe UnitInfo)] [Unit] + [UnitInfo] [Unit] [UnusableUnit] [FilePath] | MultiplePackages [(Module, ModuleOrigin)] deriving Generic ===================================== compiler/GHC/Unit/Finder.hs ===================================== @@ -510,9 +510,9 @@ findLookupResult fc fopts r = case r of , fr_suggestions = []}) LookupMultiple rs -> return (FoundMultiple rs) - LookupHidden pkg_hiddens mod_hiddens -> + LookupHidden fr_pkgs_hidden mod_hiddens -> return (NotFound{ fr_paths = [], fr_pkg = Nothing - , fr_pkgs_hidden = map (moduleUnit.fst) pkg_hiddens + , fr_pkgs_hidden , fr_mods_hidden = map (moduleUnit.fst) mod_hiddens , fr_unusables = [] , fr_suggestions = [] }) ===================================== compiler/GHC/Unit/Finder/Types.hs ===================================== @@ -70,7 +70,7 @@ data FindResult , fr_mods_hidden :: [Unit] -- ^ Module is in these units, -- but the *module* is hidden - , fr_pkgs_hidden :: [Unit] -- ^ Module is in these units, + , fr_pkgs_hidden :: [UnitInfo] -- ^ Module is in these units, -- but the *unit* is hidden -- | Module is in these units, but it is unusable ===================================== compiler/GHC/Unit/State.hs ===================================== @@ -1905,7 +1905,7 @@ data LookupResult = -- | No modules found, but there were some hidden ones with -- an exact name match. First is due to package hidden, second -- is due to module being hidden - | LookupHidden [(Module, ModuleOrigin)] [(Module, ModuleOrigin)] + | LookupHidden [UnitInfo] [(Module, ModuleOrigin)] -- | No modules found, but there were some unusable ones with -- an exact name match | LookupUnusable [(Module, ModuleOrigin)] @@ -1954,8 +1954,8 @@ lookupModuleWithSuggestions' :: UnitState -> ModuleName -> PkgQual -> LookupResult -lookupModuleWithSuggestions' pkgs mod_map m mb_pn - = case lookupUniqMap mod_map m of +lookupModuleWithSuggestions' pkgs mod_map name mb_pn + = case lookupUniqMap mod_map name of Nothing -> LookupNotFound suggestions Just xs -> case foldl' classify ([],[],[], []) (sortOn fst $ nonDetUniqMapToList xs) of @@ -1969,19 +1969,26 @@ lookupModuleWithSuggestions' pkgs mod_map m mb_pn classify (hidden_pkg, hidden_mod, unusable, exposed) (m, origin0) = let origin = filterOrigin mb_pn (mod_unit m) origin0 x = (m, origin) + + originUnit :: [UnitInfo] -> [UnitInfo] + originUnit + | moduleName m == name, Just pkg <- lookupUnit pkgs (moduleUnit m) = (pkg :) + | otherwise = id + in case origin of ModHidden -> (hidden_pkg, x:hidden_mod, unusable, exposed) ModUnusable _ -> (hidden_pkg, hidden_mod, x:unusable, exposed) - _ | originEmpty origin + ModOrigin _ _ reexports _ + | originEmpty origin -> (hidden_pkg, hidden_mod, unusable, exposed) | originVisible origin -> (hidden_pkg, hidden_mod, unusable, x:exposed) | otherwise - -> (x:hidden_pkg, hidden_mod, unusable, exposed) + -> (reexports ++ originUnit hidden_pkg, hidden_mod, unusable, exposed) - unit_lookup p = lookupUnit pkgs p `orElse` pprPanic "lookupModuleWithSuggestions" (ppr p <+> ppr m) + unit_lookup p = lookupUnit pkgs p `orElse` pprPanic "lookupModuleWithSuggestions" (ppr p <+> ppr name) mod_unit = unit_lookup . moduleUnit -- Filters out origins which are not associated with the given package @@ -2011,7 +2018,7 @@ lookupModuleWithSuggestions' pkgs mod_map m mb_pn , fromPackageFlag = False -- always excluded } - suggestions = fuzzyLookup (moduleNameString m) all_mods + suggestions = fuzzyLookup (moduleNameString name) all_mods all_mods :: [(String, ModuleSuggestion)] -- All modules all_mods = sortBy (comparing fst) $ ===================================== ghc/GHCi/UI/Exception.hs ===================================== @@ -222,11 +222,13 @@ ghciDiagnosticMessage ghc_opts msg = Just (pprWithUnitState us $ cantFindErrorX pkg_hidden_hint may_show_locations module_or_interface cfi) _ -> Nothing where - + may_show_locations :: [String] -> SDoc may_show_locations = mayShowLocations ":set -v" (ifaceShowTriedFiles opts) + pkg_hidden_hint :: UnitInfo -> SDoc pkg_hidden_hint = pkgHiddenHint hidden_msg (ifaceBuildingCabalPackage opts) where + hidden_msg :: UnitInfo -> SDoc hidden_msg pkg = text "You can run" <+> quotes (text ":set -package " <> ppr (unitPackageName pkg)) <+> ===================================== testsuite/tests/package/ImportReexport.hs ===================================== @@ -0,0 +1,2 @@ +module ImportReexport where +import GHC.Types -- reexported by ghc-prim from ghc-internal ===================================== testsuite/tests/package/ImportReexport.stderr ===================================== @@ -0,0 +1,5 @@ +ImportReexport.hs:2:1: error: [GHC-87110] + Could not load module ‘GHC.Types’. + It is a member of the hidden package ‘ghc-prim-0.14.0’. + Use -v to see a list of the files searched for. + ===================================== testsuite/tests/package/all.T ===================================== @@ -13,6 +13,7 @@ test('package04', normal, compile, [incr_containers]) test('package05', normal, compile, [incr_ghc + inc_ghc]) test('package06', normal, compile, [incr_ghc]) test('package06e', normalise_version('ghc'), compile_fail, [incr_ghc]) +test('ImportReexport', normalise_version('ghc'), compile_fail, ['-hide-all-packages -XNoImplicitPrelude']) test('package07e', normalise_version('ghc'), compile_fail, [incr_ghc + inc_ghc + hide_ghc]) test('package08e', normalise_version('ghc'), compile_fail, [incr_ghc + hide_ghc]) test('package09e', normal, compile_fail, ['-package "containers (Data.Map as M, Data.Set as M)"']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/26cc1e979319c1827c2418af2819a5f7... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/26cc1e979319c1827c2418af2819a5f7... You're receiving this email because of your account on gitlab.haskell.org.