summaryrefslogtreecommitdiff
path: root/compiler/GHC/Rename
diff options
context:
space:
mode:
authorAditya Gupta <adityagupta1089@gmail.com>2020-08-23 17:53:37 +0530
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-08-27 14:19:26 -0400
commit01ff8c89727a91cbc1571ae54f73f5919d6aaa71 (patch)
tree383ea51e53a93a8ca3a18fcaa213a84810576bc8 /compiler/GHC/Rename
parent770100e0266750a313b34a52a60968410fcf0769 (diff)
downloadhaskell-01ff8c89727a91cbc1571ae54f73f5919d6aaa71.tar.gz
Consolidate imports in getMinimalImports (#18264)
Diffstat (limited to 'compiler/GHC/Rename')
-rw-r--r--compiler/GHC/Rename/Names.hs25
1 files changed, 22 insertions, 3 deletions
diff --git a/compiler/GHC/Rename/Names.hs b/compiler/GHC/Rename/Names.hs
index 7531913a7b..13a592423a 100644
--- a/compiler/GHC/Rename/Names.hs
+++ b/compiler/GHC/Rename/Names.hs
@@ -4,7 +4,7 @@
Extracting imported and top-level names in scope
-}
-{-# LANGUAGE CPP, NondecreasingIndentation, MultiWayIf, NamedFieldPuns #-}
+{-# LANGUAGE CPP, NondecreasingIndentation #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
@@ -72,7 +72,7 @@ import Data.Either ( partitionEithers, isRight, rights )
import Data.Map ( Map )
import qualified Data.Map as Map
import Data.Ord ( comparing )
-import Data.List ( partition, (\\), find, sortBy )
+import Data.List ( partition, (\\), find, sortBy, groupBy, sortOn )
import Data.Function ( on )
import qualified Data.Set as S
import System.FilePath ((</>))
@@ -1570,7 +1570,7 @@ decls, and simply trim their import lists. NB that
-}
getMinimalImports :: [ImportDeclUsage] -> RnM [LImportDecl GhcRn]
-getMinimalImports = mapM mk_minimal
+getMinimalImports = fmap combine . mapM mk_minimal
where
mk_minimal (L l decl, used_gres, unused)
| null unused
@@ -1623,6 +1623,25 @@ getMinimalImports = mapM mk_minimal
all_non_overloaded = all (not . flIsOverloaded)
+ combine :: [LImportDecl GhcRn] -> [LImportDecl GhcRn]
+ combine = map merge . groupBy ((==) `on` getKey) . sortOn getKey
+
+ getKey :: LImportDecl GhcRn -> (Bool, Maybe ModuleName, ModuleName)
+ getKey decl =
+ ( isImportDeclQualified . ideclQualified $ idecl -- is this qualified? (important that this be first)
+ , unLoc <$> ideclAs idecl -- what is the qualifier (inside Maybe monad)
+ , unLoc . ideclName $ idecl -- Module Name
+ )
+ where
+ idecl :: ImportDecl GhcRn
+ idecl = unLoc decl
+
+ merge :: [LImportDecl GhcRn] -> LImportDecl GhcRn
+ merge [] = error "getMinimalImports: unexpected empty list"
+ merge decls@((L l decl) : _) = L l (decl { ideclHiding = Just (False, L l lies) })
+ where lies = concatMap (unLoc . snd) $ mapMaybe (ideclHiding . unLoc) decls
+
+
printMinimalImports :: HscSource -> [ImportDeclUsage] -> RnM ()
-- See Note [Printing minimal imports]
printMinimalImports hsc_src imports_w_usage