diff options
author | Aditya Gupta <adityagupta1089@gmail.com> | 2020-08-23 17:53:37 +0530 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2020-08-27 14:19:26 -0400 |
commit | 01ff8c89727a91cbc1571ae54f73f5919d6aaa71 (patch) | |
tree | 383ea51e53a93a8ca3a18fcaa213a84810576bc8 | |
parent | 770100e0266750a313b34a52a60968410fcf0769 (diff) | |
download | haskell-01ff8c89727a91cbc1571ae54f73f5919d6aaa71.tar.gz |
Consolidate imports in getMinimalImports (#18264)
-rw-r--r-- | compiler/GHC/Rename/Names.hs | 25 | ||||
-rw-r--r-- | testsuite/tests/rename/should_compile/Makefile | 5 | ||||
-rw-r--r-- | testsuite/tests/rename/should_compile/T18264.hs | 20 | ||||
-rw-r--r-- | testsuite/tests/rename/should_compile/T18264.stdout | 6 | ||||
-rw-r--r-- | testsuite/tests/rename/should_compile/all.T | 1 |
5 files changed, 54 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 diff --git a/testsuite/tests/rename/should_compile/Makefile b/testsuite/tests/rename/should_compile/Makefile index 495efed5cc..5744d60667 100644 --- a/testsuite/tests/rename/should_compile/Makefile +++ b/testsuite/tests/rename/should_compile/Makefile @@ -60,3 +60,8 @@ T7969: T18497: '$(TEST_HC)' $(TEST_HC_OPTS) -fno-code T18497_Foo.hs T18497_Bar.hs -ddump-minimal-imports cat T18497_Bar.imports-boot + +T18264: + $(RM) T18264.hi T18264.o T18264.imports + '$(TEST_HC)' $(TEST_HC_OPTS) -ddump-minimal-imports -c T18264.hs + cat T18264.imports diff --git a/testsuite/tests/rename/should_compile/T18264.hs b/testsuite/tests/rename/should_compile/T18264.hs new file mode 100644 index 0000000000..487f0c9a00 --- /dev/null +++ b/testsuite/tests/rename/should_compile/T18264.hs @@ -0,0 +1,20 @@ +module T18264 where + +import Data.Char (isDigit) +import Data.Maybe (isJust) +import Data.Char (isPrint) +import Data.List (sortOn) +import Data.Char (isLetter) +import Data.Maybe hiding (isNothing) + +import qualified Data.List as S (sort) +import qualified Data.Char as C --only isDigit & isLetter used later +import qualified Data.List as T (nub) + +test1 x = isDigit x || isLetter x +test2a = isJust +test2b = fromJust +test3 x = C.isDigit x || C.isLetter x +test4 xs = S.sort xs +test5 xs = T.nub xs +test6 f xs = sortOn f xs diff --git a/testsuite/tests/rename/should_compile/T18264.stdout b/testsuite/tests/rename/should_compile/T18264.stdout new file mode 100644 index 0000000000..49fbed571e --- /dev/null +++ b/testsuite/tests/rename/should_compile/T18264.stdout @@ -0,0 +1,6 @@ +import Data.Char ( isDigit, isLetter ) +import Data.List ( sortOn ) +import Data.Maybe ( fromJust, isJust ) +import qualified Data.Char as C ( isLetter, isDigit ) +import qualified Data.List as S ( sort ) +import qualified Data.List as T ( nub ) diff --git a/testsuite/tests/rename/should_compile/all.T b/testsuite/tests/rename/should_compile/all.T index 9a519483d5..ae82bd30c6 100644 --- a/testsuite/tests/rename/should_compile/all.T +++ b/testsuite/tests/rename/should_compile/all.T @@ -175,3 +175,4 @@ test('T17244C', normal, compile, ['']) test('T17832', [], multimod_compile, ['T17832M1', 'T17832M2']) test('T17837', normal, compile, ['']) test('T18497', [], makefile_test, ['T18497']) +test('T18264', [], makefile_test, ['T18264']) |