on master help sign in
Fixed the bug in the de-duplicating algorithm for import lists
commit3fc444a70b97de11eeffef09d77eb1eaee48575f authorMark Karpov <markkarpov92@gmail.com> dateWed 17 Jun 2020, 14:31 (6 years ago) parent35f4fb3
4 files changed+29−8
CHANGELOG.md+3 data/examples/import/deduplication-bug-out.hs+2 data/examples/import/deduplication-bug.hs+2 src/Ormolu/Imports.hs+22−8
CHANGELOG.md+3
@@ -29,6 +29,9 @@
2929 inside of `where` clauses—can be formatted. [Issue
3030 572](https://github.com/tweag/ormolu/issues/572).
3131
32+* Fixed the bug in the de-duplicating algorithm for import lists. [Issue
33+ 620](https://github.com/tweag/ormolu/issues/620).
34+
3235## Ormolu 0.1.0.0
3336
3437* Fixed rendering of type signatures concerning several identifiers. [Issue
data/examples/import/deduplication-bug-out.hsnew file+2
@@ -0,0 +1,2 @@
1+import Foo1 (Bar1 (..), Baz1)
2+import Foo2 (Bar2 (..), Baz2)
data/examples/import/deduplication-bug.hsnew file+2
@@ -0,0 +1,2 @@
1+import Foo1 (Bar1, Baz1, Bar1(..))
2+import Foo2 (Bar2(..), Baz2, Bar2)
src/Ormolu/Imports.hs+22−8
@@ -49,7 +49,7 @@ compareIdecl (L _ m0) (L _ m1) =
4949sortLies :: [LIE GhcPs] -> [LIE GhcPs]
5050sortLies =
5151 nubBy (\x y -> compareLIE x y == EQ)
52− . sortBy compareLIE
52+ . sortBy compareLIEAll
5353 . fmap (fmap sortThings)
5454
5555-- | Sort imports\/exports inside of 'IEThingWith'.
@@ -59,17 +59,31 @@ sortThings = \case
5959 IEThingWith NoExtField x w (sortBy (compareIewn `on` unLoc) xs) fl
6060 other -> other
6161
62−-- | Compare a pair of located imports or exports.
62+-- | Compare a pair of located imports or exports taking into account
63+-- wildcards.
64+compareLIEAll :: LIE GhcPs -> LIE GhcPs -> Ordering
65+compareLIEAll = f `on` getIewn . unLoc
66+ where
67+ f (x, xall) (y, yall) =
68+ case x `compareIewn` y of
69+ EQ -> xall `compare` yall
70+ ordering -> ordering
71+
72+-- | The same as 'compareLIEAll'.
6373compareLIE :: LIE GhcPs -> LIE GhcPs -> Ordering
64−compareLIE = compareIewn `on` getIewn . unLoc
74+compareLIE = compareIewn `on` fst . getIewn . unLoc
75+
76+-- | Indication whether or not a wildcard is used by an import item.
77+data IEWild = All | NoWild
78+ deriving (Eq, Ord)
6579
6680-- | Project @'IEWrappedName' 'RdrName'@ from @'IE' 'GhcPs'@.
67−getIewn :: IE GhcPs -> IEWrappedName RdrName
81+getIewn :: IE GhcPs -> (IEWrappedName RdrName, IEWild)
6882getIewn = \case
69− IEVar NoExtField x -> unLoc x
70− IEThingAbs NoExtField x -> unLoc x
71− IEThingAll NoExtField x -> unLoc x
72− IEThingWith NoExtField x _ _ _ -> unLoc x
83+ IEVar NoExtField x -> (unLoc x, NoWild)
84+ IEThingAbs NoExtField x -> (unLoc x, NoWild)
85+ IEThingAll NoExtField x -> (unLoc x, All)
86+ IEThingWith NoExtField x _ _ _ -> (unLoc x, NoWild)
7387 IEModuleContents NoExtField _ -> notImplemented "IEModuleContents"
7488 IEGroup NoExtField _ _ -> notImplemented "IEGroup"
7589 IEDoc NoExtField _ -> notImplemented "IEDoc"