Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit 879d1fe

Browse files
committed
Stable sort for (data/newtype) instances
1 parent 6b7fe26 commit 879d1fe

File tree

12 files changed

+1001
-993
lines changed

12 files changed

+1001
-993
lines changed

haddock-api/src/Haddock/Interface/AttachInstances.hs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,28 @@ findFixity iface ifaceMap instIfaceMap = \name ->
179179
-- Collecting and sorting instances
180180
--------------------------------------------------------------------------------
181181

182+
-- | Stable name for stable comparisons. GHC's `Name` uses unstable
183+
-- ordering based on their `Unique`'s.
184+
newtype SName = SName Name
185+
186+
instance Eq SName where
187+
SName n1 == SName n2 = n1 `stableNameCmp` n2 == EQ
188+
189+
instance Ord SName where
190+
SName n1 `compare` SName n2 = n1 `stableNameCmp` n2
182191

183192
-- | Simplified type for sorting types, ignoring qualification (not visible
184193
-- in Haddock output) and unifying special tycons with normal ones.
185194
-- For the benefit of the user (looks nice and predictable) and the
186195
-- tests (which prefer output to be deterministic).
187-
data SimpleType = SimpleType Name [SimpleType]
196+
data SimpleType = SimpleType SName [SimpleType]
188197
| SimpleTyLit TyLit
189198
deriving (Eq,Ord)
190199

191200

192-
instHead :: ([TyVar], [PredType], Class, [Type]) -> ([Int], Name, [SimpleType])
201+
instHead :: ([TyVar], [PredType], Class, [Type]) -> ([Int], SName, [SimpleType])
193202
instHead (_, _, cls, args)
194-
= (map argCount args, className cls, map simplify args)
203+
= (map argCount args, SName (className cls), map simplify args)
195204

196205
argCount :: Type -> Int
197206
argCount (AppTy t _) = argCount t + 1
@@ -202,12 +211,12 @@ argCount (CastTy t _) = argCount t
202211
argCount _ = 0
203212

204213
simplify :: Type -> SimpleType
205-
simplify (FunTy _ _ t1 t2) = SimpleType funTyConName [simplify t1, simplify t2]
214+
simplify (FunTy _ _ t1 t2) = SimpleType (SName funTyConName) [simplify t1, simplify t2]
206215
simplify (ForAllTy _ t) = simplify t
207216
simplify (AppTy t1 t2) = SimpleType s (ts ++ maybeToList (simplify_maybe t2))
208217
where (SimpleType s ts) = simplify t1
209-
simplify (TyVarTy v) = SimpleType (tyVarName v) []
210-
simplify (TyConApp tc ts) = SimpleType (tyConName tc)
218+
simplify (TyVarTy v) = SimpleType (SName (tyVarName v)) []
219+
simplify (TyConApp tc ts) = SimpleType (SName (tyConName tc))
211220
(mapMaybe simplify_maybe ts)
212221
simplify (LitTy l) = SimpleTyLit l
213222
simplify (CastTy ty _) = simplify ty
@@ -218,9 +227,9 @@ simplify_maybe (CoercionTy {}) = Nothing
218227
simplify_maybe ty = Just (simplify ty)
219228

220229
-- Used for sorting
221-
instFam :: FamInst -> ([Int], Name, [SimpleType], Int, SimpleType)
230+
instFam :: FamInst -> ([Int], SName, [SimpleType], Int, SimpleType)
222231
instFam FamInst { fi_fam = n, fi_tys = ts, fi_rhs = t }
223-
= (map argCount ts, n, map simplify ts, argCount t, simplify t)
232+
= (map argCount ts, SName n, map simplify ts, argCount t, simplify t)
224233

225234

226235
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)