Skip to content

Commit d5727c0

Browse files
joaosreismrkkrp
andauthored
Do not place Prelude at the end with NoImplicitPrelude (#1216)
Co-authored-by: Mark Karpov <markkarpov92@gmail.com>
1 parent d38bac0 commit d5727c0

9 files changed

Lines changed: 57 additions & 10 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
* Fixed preservation of the position of comments around the `where` keyword.
77
[Issue 784](https://github.com/tweag/ormolu/issues/784).
88

9+
* Do not sort `Prelude` to the end of the import list when the
10+
`NoImplicitPrelude` extension is enabled; instead sort it like any other
11+
import. [Issue 1189](https://github.com/tweag/ormolu/issues/1189). Cherry-picked [mrkkrp/ormolu@e67dbfe](https://github.com/mrkkrp/ormolu/commit/e67dbfe2faca57769755bb3d2240c0f6cc07a0a5).
12+
913
## Ormolu 0.8.1.1
1014

1115
* Add missing braces for case expressions in single‑line do blocks. [Issue
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{-# LANGUAGE PackageImports #-}
2+
3+
import "base" Control.Applicative (Alternative, (<|>))
4+
import "base" Data.Maybe (Maybe (Nothing), maybe)
5+
import "base" System.IO (IO)
6+
import "yaya" Yaya.Fold (ana, cata)
7+
import "base" Prelude ((+))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{-# LANGUAGE PackageImports #-}
2+
3+
import "base" System.IO (IO)
4+
import "base" Prelude ((+))
5+
import "yaya" Yaya.Fold (ana, cata)
6+
import "base" Control.Applicative (Alternative, (<|>))
7+
import "base" Data.Maybe (Maybe (Nothing), maybe)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
3+
import Control.Applicative (Alternative, (<|>))
4+
import Data.Maybe (Maybe (Nothing), maybe)
5+
import Prelude ((+))
6+
import System.IO (IO)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{-# LANGUAGE PackageImports #-}
2+
{-# LANGUAGE NoImplicitPrelude #-}
3+
4+
import "base" Control.Applicative (Alternative, (<|>))
5+
import "base" Data.Maybe (Maybe (Nothing), maybe)
6+
import "base" Prelude ((+))
7+
import "base" System.IO (IO)
8+
import "yaya" Yaya.Fold (ana, cata)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
{-# LANGUAGE PackageImports #-}
3+
4+
import "base" System.IO (IO)
5+
import "base" Prelude ((+))
6+
import "yaya" Yaya.Fold (ana, cata)
7+
import "base" Control.Applicative (Alternative, (<|>))
8+
import "base" Data.Maybe (Maybe (Nothing), maybe)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
3+
import System.IO (IO)
4+
import Prelude ((+))
5+
import Control.Applicative (Alternative, (<|>))
6+
import Data.Maybe (Maybe (Nothing), maybe)

src/Ormolu/Imports.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ import GHC.Types.SrcLoc
2727
import Ormolu.Utils (notImplemented, showOutputable)
2828

2929
-- | Sort and normalize imports.
30-
normalizeImports :: [LImportDecl GhcPs] -> [LImportDecl GhcPs]
31-
normalizeImports =
30+
normalizeImports :: Bool -> [LImportDecl GhcPs] -> [LImportDecl GhcPs]
31+
normalizeImports implicitPrelude =
3232
fmap snd
3333
. M.toAscList
3434
. M.fromListWith combineImports
35-
. fmap (\x -> (importId x, g x))
35+
. fmap (\x -> (importId implicitPrelude x, g x))
3636
where
3737
g :: LImportDecl GhcPs -> LImportDecl GhcPs
3838
g (L l ImportDecl {..}) =
@@ -118,8 +118,8 @@ instance Ord ImportListInterpretationOrd where
118118
toBool EverythingBut = True
119119

120120
-- | Obtain an 'ImportId' for a given import.
121-
importId :: LImportDecl GhcPs -> ImportId
122-
importId (L _ ImportDecl {..}) =
121+
importId :: Bool -> LImportDecl GhcPs -> ImportId
122+
importId implicitPrelude (L _ ImportDecl {..}) =
123123
ImportId
124124
{ importIsPrelude = isPrelude,
125125
importIdName = moduleName,
@@ -135,7 +135,7 @@ importId (L _ ImportDecl {..}) =
135135
importLevel = importLevelOf ideclLevelSpec
136136
}
137137
where
138-
isPrelude = moduleNameString moduleName == "Prelude"
138+
isPrelude = implicitPrelude && moduleNameString moduleName == "Prelude"
139139
moduleName = unLoc ideclName
140140
importLevelOf = \case
141141
LevelStylePre l -> Just (ImportDeclLevelOrd l)

src/Ormolu/Parser.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,13 @@ parseModuleSnippet Config {..} modFixityMap dynFlags path rawInput = liftIO $ do
143143
parser = case cfgSourceType of
144144
ModuleSource -> GHC.parseModule
145145
SignatureSource -> GHC.parseSignature
146+
implicitPrelude = EnumSet.member ImplicitPrelude (GHC.extensionFlags dynFlags)
146147
r = case runParser parser dynFlags path input of
147148
GHC.PFailed pstate ->
148149
case pStateErrors pstate of
149150
Just err -> Left err
150151
Nothing -> error "PFailed does not have an error"
151-
GHC.POk pstate (L _ (normalizeModule -> hsModule)) ->
152+
GHC.POk pstate (L _ (normalizeModule implicitPrelude -> hsModule)) ->
152153
case pStateErrors pstate of
153154
-- Some parse errors (pattern/arrow syntax in expr context)
154155
-- do not cause a parse error, but they are replaced with "_"
@@ -173,8 +174,8 @@ parseModuleSnippet Config {..} modFixityMap dynFlags path rawInput = liftIO $ do
173174

174175
-- | Normalize a 'HsModule' by sorting its import\/export lists, dropping
175176
-- blank comments, etc.
176-
normalizeModule :: HsModule GhcPs -> HsModule GhcPs
177-
normalizeModule hsmod =
177+
normalizeModule :: Bool -> HsModule GhcPs -> HsModule GhcPs
178+
normalizeModule implicitPrelude hsmod =
178179
everywhere
179180
( mkT dropBlankTypeHaddocks
180181
`extT` dropBlankDataDeclHaddocks
@@ -183,7 +184,7 @@ normalizeModule hsmod =
183184
)
184185
hsmod
185186
{ hsmodImports =
186-
normalizeImports (hsmodImports hsmod),
187+
normalizeImports implicitPrelude (hsmodImports hsmod),
187188
hsmodDecls =
188189
filter (not . isBlankDocD . unLoc) (hsmodDecls hsmod),
189190
hsmodExt =

0 commit comments

Comments
 (0)