From 508eb2dbc75a5e66b6ef49713b07557e5e638c23 Mon Sep 17 00:00:00 2001
From: Rodrigo Mesquita <rodrigo.m.mesquita@gmail.com>
Date: Fri, 27 Sep 2024 12:40:50 +0100
Subject: [PATCH] determinism: Interface re-export list det

In 'DocStructureItem' we want to make sure the 'Avails' are sorted, for
interface file determinism. This commit introduces 'DetOrdAvails', a
newtype that should only be constructed by sorting Avails with
'sortAvails' unless the avails are known to be deterministically
ordered. This newtype is used by 'DocStructureItem' where 'Avails' was
previously used to ensure the list of avails is deterministically sorted
by construction.

Note: Even though we order the constructors and avails in the interface
file, the order of constructors in the haddock output is still
determined from the order of declaration in the source. This was also
true before, when the list of constructors in the interface file <docs>
section was non-deterministic. Some haddock tests such as
"ConstructorArgs" observe this (check the order of constructors in
out/ConstructorArgs.html vs src/ConstructorArgs.hs vs its interface file)

The updated tests are caused by haddock corners where the order in the
source is not preserved (and was non-deterministic before this PR):
    * Module header in the latex backend
    * Re-export of pattern synonyms associated to a datatype (#25342)

Fixes #25304

(cherry picked from commit b3f7fb80781bac756efdd7fdde836bf4742a75fc)
---
 .gitlab/ci.sh                                 |  4 +-
 compiler/GHC/Hs/Doc.hs                        |  4 +-
 compiler/GHC/HsToCore/Docs.hs                 | 10 ++-
 compiler/GHC/Iface/Make.hs                    |  4 +-
 compiler/GHC/Types/Avail.hs                   | 21 ++++-
 testsuite/tests/determinism/T25304/A.hs       | 84 ++++++++++++++++++
 testsuite/tests/determinism/T25304/B.hs       | 86 +++++++++++++++++++
 testsuite/tests/determinism/T25304/Makefile   | 25 ++++++
 .../tests/determinism/T25304/T25304a.stdout   |  8 ++
 testsuite/tests/determinism/T25304/all.T      |  2 +
 .../tests/showIface/DocsInHiFileTH.stdout     |  8 +-
 testsuite/tests/showIface/NoExportList.stdout |  2 +-
 .../src/Haddock/Interface/Create.hs           | 14 ++-
 .../html-test/ref/BundledPatterns2.html       | 80 ++++++++---------
 .../ref/ConstructorArgs/ConstructorArgs.tex   |  2 +-
 .../DefaultSignatures/DefaultSignatures.tex   |  2 +-
 .../GadtConstructorArgs.tex                   |  2 +-
 .../ref/TypeFamilies3/TypeFamilies3.tex       |  2 +-
 18 files changed, 297 insertions(+), 63 deletions(-)
 create mode 100644 testsuite/tests/determinism/T25304/A.hs
 create mode 100644 testsuite/tests/determinism/T25304/B.hs
 create mode 100644 testsuite/tests/determinism/T25304/Makefile
 create mode 100644 testsuite/tests/determinism/T25304/T25304a.stdout
 create mode 100644 testsuite/tests/determinism/T25304/all.T

diff --git a/.gitlab/ci.sh b/.gitlab/ci.sh
index 52106e0787a..0d21d33885c 100755
--- a/.gitlab/ci.sh
+++ b/.gitlab/ci.sh
@@ -714,11 +714,11 @@ function cabal_abi_test() {
 
   start_section "Cabal test: $OUT"
   mkdir -p "$OUT"
-  run "$HC" \
+  "$HC" \
     -hidir tmp -odir tmp -fforce-recomp -haddock \
     -iCabal/Cabal/src -XNoPolyKinds Distribution.Simple -j"$cores" \
     -fobject-determinism \
-    "$@" 2>&1 | tee $OUT/log
+    "$@" 2>&1 | sed '1d' | tee $OUT/log
   summarise_hi_files
   summarise_o_files
   popd
diff --git a/compiler/GHC/Hs/Doc.hs b/compiler/GHC/Hs/Doc.hs
index 0dac7789bed..46ce16feba1 100644
--- a/compiler/GHC/Hs/Doc.hs
+++ b/compiler/GHC/Hs/Doc.hs
@@ -124,7 +124,7 @@ data DocStructureItem
   = DsiSectionHeading !Int !(HsDoc GhcRn)
   | DsiDocChunk !(HsDoc GhcRn)
   | DsiNamedChunkRef !String
-  | DsiExports !Avails
+  | DsiExports !DetOrdAvails
   | DsiModExport
       !(NonEmpty ModuleName) -- ^ We might re-export avails from multiple
                             -- modules with a single export declaration. E.g.
@@ -136,7 +136,7 @@ data DocStructureItem
                             --
                             -- Invariant: This list of ModuleNames must be
                             -- sorted to guarantee interface file determinism.
-      !Avails
+      !DetOrdAvails
                             -- ^ Invariant: This list of Avails must be sorted
                             -- to guarantee interface file determinism.
 
diff --git a/compiler/GHC/HsToCore/Docs.hs b/compiler/GHC/HsToCore/Docs.hs
index fcb62fd82a0..da73bfd1ebc 100644
--- a/compiler/GHC/HsToCore/Docs.hs
+++ b/compiler/GHC/HsToCore/Docs.hs
@@ -160,7 +160,11 @@ mkDocStructureFromExportList mdl import_avails export_list =
       (IEGroup _ level doc, _)         -> DsiSectionHeading level (unLoc doc)
       (IEDoc _ doc, _)                 -> DsiDocChunk (unLoc doc)
       (IEDocNamed _ name, _)           -> DsiNamedChunkRef name
-      (_, avails)                      -> DsiExports (nubAvails avails)
+      (IEThingWith{}, avails)          ->
+        DsiExports $
+          {- For explicit export lists, use the explicit order. It is deterministic by construction -}
+          DefinitelyDeterministicAvails (nubAvails avails)
+      (_, avails)                      -> DsiExports (sortAvails (nubAvails avails))
 
     moduleExport :: ModuleName -- Alias
                  -> Avails
@@ -201,10 +205,10 @@ mkDocStructureFromDecls env all_exports decls =
     avails :: [Located DocStructureItem]
     avails = flip fmap all_exports $ \avail ->
       case M.lookup (availName avail) name_locs of
-        Just loc -> L loc (DsiExports [avail])
+        Just loc -> L loc (DsiExports (sortAvails [avail]))
         -- FIXME: This is just a workaround that we use when handling e.g.
         -- associated data families like in the html-test Instances.hs.
-        Nothing -> noLoc (DsiExports [])
+        Nothing -> noLoc (DsiExports (sortAvails []))
 
         -- This causes the associated data family to be incorrectly documented
         -- separately from its class:
diff --git a/compiler/GHC/Iface/Make.hs b/compiler/GHC/Iface/Make.hs
index fdbea6a9b4f..41132433947 100644
--- a/compiler/GHC/Iface/Make.hs
+++ b/compiler/GHC/Iface/Make.hs
@@ -518,8 +518,8 @@ mkIfaceImports = map go
     go (ImpUserSpec decl (ImpUserExplicit env)) = IfaceImport decl (ImpIfaceExplicit (forceGlobalRdrEnv env))
     go (ImpUserSpec decl (ImpUserEverythingBut ns)) = IfaceImport decl (ImpIfaceEverythingBut ns)
 
-mkIfaceExports :: [AvailInfo] -> [IfaceExport]  -- Sort to make canonical
-mkIfaceExports = sortAvails
+mkIfaceExports :: [AvailInfo] -> [IfaceExport] -- Sort to make canonical
+mkIfaceExports as = case sortAvails as of DefinitelyDeterministicAvails sas -> sas
 
 {-
 Note [Original module]
diff --git a/compiler/GHC/Types/Avail.hs b/compiler/GHC/Types/Avail.hs
index a71e497cafb..2970bf34fb0 100644
--- a/compiler/GHC/Types/Avail.hs
+++ b/compiler/GHC/Types/Avail.hs
@@ -1,5 +1,7 @@
 
 {-# LANGUAGE DeriveDataTypeable #-}
+{-# LANGUAGE DerivingStrategies #-}
+{-# LANGUAGE PatternSynonyms #-}
 --
 -- (c) The University of Glasgow
 --
@@ -20,6 +22,7 @@ module GHC.Types.Avail (
     filterAvails,
     nubAvails,
     sortAvails,
+    DetOrdAvails(DetOrdAvails, DefinitelyDeterministicAvails)
   ) where
 
 import GHC.Prelude
@@ -65,6 +68,20 @@ data AvailInfo
 -- | A collection of 'AvailInfo' - several things that are \"available\"
 type Avails = [AvailInfo]
 
+-- | Occurrences of Avails in interface files must be deterministically ordered
+-- to guarantee interface file determinism.
+--
+-- We guarantee a deterministic order by either using the order explicitly
+-- given by the user (e.g. in an explicit constructor export list) or instead
+-- by sorting the avails with 'sortAvails'.
+newtype DetOrdAvails = DefinitelyDeterministicAvails Avails
+  deriving newtype (Binary, Outputable, NFData)
+
+-- | It's always safe to match on 'DetOrdAvails'
+pattern DetOrdAvails :: Avails -> DetOrdAvails
+pattern DetOrdAvails x <- DefinitelyDeterministicAvails x
+{-# COMPLETE DetOrdAvails #-}
+
 {- Note [Representing pattern synonym fields in AvailInfo]
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Record pattern synonym fields cannot be represented using AvailTC like fields of
@@ -133,8 +150,8 @@ availSubordinateNames avail@(AvailTC _ ns)
   | otherwise              = ns
 
 -- | Sort 'Avails'/'AvailInfo's
-sortAvails :: Avails -> Avails
-sortAvails = sortBy stableAvailCmp . map sort_subs
+sortAvails :: Avails -> DetOrdAvails
+sortAvails = DefinitelyDeterministicAvails . sortBy stableAvailCmp . map sort_subs
   where
     sort_subs :: AvailInfo -> AvailInfo
     sort_subs (Avail n) = Avail n
diff --git a/testsuite/tests/determinism/T25304/A.hs b/testsuite/tests/determinism/T25304/A.hs
new file mode 100644
index 00000000000..3f164c44a12
--- /dev/null
+++ b/testsuite/tests/determinism/T25304/A.hs
@@ -0,0 +1,84 @@
+module A
+  ( MyType(..)
+  ) where
+
+data MyType
+    = A
+    | B
+    | C
+    | D
+    | E
+    | F
+    | G
+    | H
+    | I
+    | J
+    | K
+    | L
+    | M
+    | N
+    | O
+    | P
+    | Q
+    | R
+    | S
+    | T
+    | U
+    | V
+    | W
+    | X
+    | Y
+    | Z
+    | AA
+    | AB
+    | AC
+    | AD
+    | AE
+    | AF
+    | AG
+    | AH
+    | AI
+    | AJ
+    | AK
+    | AL
+    | AM
+    | AN
+    | AO
+    | AP
+    | AQ
+    | AR
+    | AS
+    | AT
+    | AU
+    | AV
+    | AW
+    | AX
+    | AY
+    | AZ
+    | BA
+    | BB
+    | BC
+    | BD
+    | BE
+    | BF
+    | BG
+    | BH
+    | BI
+    | BJ
+    | BK
+    | BL
+    | BM
+    | BN
+    | BO
+    | BP
+    | BQ
+    | BR
+    | BS
+    | BT
+    | BU
+    | BV
+    | BW
+    | BX
+    | BY
+    | BZ
+    | CA
diff --git a/testsuite/tests/determinism/T25304/B.hs b/testsuite/tests/determinism/T25304/B.hs
new file mode 100644
index 00000000000..2f961f66644
--- /dev/null
+++ b/testsuite/tests/determinism/T25304/B.hs
@@ -0,0 +1,86 @@
+module B
+( MyType
+    ( BA
+    , BB
+    , BC
+    , BD
+    , BE
+    , BF
+    , BG
+    , BH
+    , BI
+    , BJ
+    , BK
+    , BL
+    , BM
+    , BN
+    , BO
+    , BP
+    , BQ
+    , BR
+    , BS
+    , BT
+    , BU
+    , BV
+    , BW
+    , BX
+    , BY
+    , BZ
+    , CA
+    , AA
+    , AB
+    , AC
+    , AD
+    , AE
+    , AF
+    , AG
+    , AH
+    , AI
+    , AJ
+    , AK
+    , AL
+    , AM
+    , AN
+    , AO
+    , AP
+    , AQ
+    , AR
+    , AS
+    , AT
+    , AU
+    , AV
+    , AW
+    , AX
+    , AY
+    , AZ
+    , A
+    , B
+    , C
+    , D
+    , E
+    , F
+    , G
+    , H
+    , I
+    , J
+    , K
+    , L
+    , M
+    , N
+    , O
+    , P
+    , Q
+    , R
+    , S
+    , T
+    , U
+    , V
+    , W
+    , X
+    , Y
+    , Z
+    )
+) where
+
+import A
+
diff --git a/testsuite/tests/determinism/T25304/Makefile b/testsuite/tests/determinism/T25304/Makefile
new file mode 100644
index 00000000000..cd29342e8a0
--- /dev/null
+++ b/testsuite/tests/determinism/T25304/Makefile
@@ -0,0 +1,25 @@
+TOP=../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
+
+T25304:
+	$(RM) A.hi A.o B.hi B.o
+	# Use -haddock to get docs: output in the interface file
+	'$(TEST_HC)' $(TEST_HC_OPTS) -v0 -haddock B.hs
+	'$(TEST_HC)' --show-iface A.hi > A_clean_iface
+	'$(TEST_HC)' --show-iface B.hi > B_clean_iface
+	'$(TEST_HC)' $(TEST_HC_OPTS) -dinitial-unique=16777215 -dunique-increment=-1 -v0 -haddock A.hs B.hs -fforce-recomp
+	'$(TEST_HC)' --show-iface A.hi > A_dirty_iface
+	'$(TEST_HC)' --show-iface B.hi > B_dirty_iface
+	diff A_clean_iface A_dirty_iface
+	diff B_clean_iface B_dirty_iface
+
+T25304a:
+	$(RM) A.hi A.o B.hi B.o
+	# Use -haddock to get docs: output in the interface file
+	'$(TEST_HC)' $(TEST_HC_OPTS) -v0 -haddock B.hs
+	'$(TEST_HC)' --show-iface B.hi > B_clean_iface
+	# The goal is to see the export list in the documentation structure of the
+	# interface file preserves the order used in the source
+	cat B_clean_iface | grep -A7 "documentation structure"
+
diff --git a/testsuite/tests/determinism/T25304/T25304a.stdout b/testsuite/tests/determinism/T25304/T25304a.stdout
new file mode 100644
index 00000000000..b92d2b33a85
--- /dev/null
+++ b/testsuite/tests/determinism/T25304/T25304a.stdout
@@ -0,0 +1,8 @@
+       documentation structure:
+         avails:
+           [A.MyType{A.MyType, A.BA, A.BB, A.BC, A.BD, A.BE, A.BF, A.BG, A.BH,
+                     A.BI, A.BJ, A.BK, A.BL, A.BM, A.BN, A.BO, A.BP, A.BQ, A.BR, A.BS,
+                     A.BT, A.BU, A.BV, A.BW, A.BX, A.BY, A.BZ, A.CA, A.AA, A.AB, A.AC,
+                     A.AD, A.AE, A.AF, A.AG, A.AH, A.AI, A.AJ, A.AK, A.AL, A.AM, A.AN,
+                     A.AO, A.AP, A.AQ, A.AR, A.AS, A.AT, A.AU, A.AV, A.AW, A.AX, A.AY,
+                     A.AZ, A.A, A.B, A.C, A.D, A.E, A.F, A.G, A.H, A.I, A.J, A.K, A.L,
diff --git a/testsuite/tests/determinism/T25304/all.T b/testsuite/tests/determinism/T25304/all.T
new file mode 100644
index 00000000000..7d7f21f5f24
--- /dev/null
+++ b/testsuite/tests/determinism/T25304/all.T
@@ -0,0 +1,2 @@
+test('T25304', [extra_files(['A.hs', 'B.hs'])], makefile_test, ['T25304'])
+test('T25304a', [extra_files(['A.hs', 'B.hs'])], makefile_test, ['T25304a'])
diff --git a/testsuite/tests/showIface/DocsInHiFileTH.stdout b/testsuite/tests/showIface/DocsInHiFileTH.stdout
index 650829d4de6..58f75a58468 100644
--- a/testsuite/tests/showIface/DocsInHiFileTH.stdout
+++ b/testsuite/tests/showIface/DocsInHiFileTH.stdout
@@ -187,7 +187,7 @@ docs:
          avails:
            [i]
          avails:
-           [WD11{WD11, WD11Bool, WD11Int, WD11Foo}]
+           [WD11{WD11, WD11Bool, WD11Foo, WD11Int}]
          avails:
            [WD13{WD13}]
          avails:
@@ -221,11 +221,11 @@ docs:
          avails:
            [Pretty{Pretty, prettyPrint}]
          avails:
-           [Corge{Corge, runCorge, Corge}]
+           [Corge{Corge, Corge, runCorge}]
          avails:
-           [Quuz{Quuz, quuz1_a, Quuz}]
+           [Quuz{Quuz, Quuz, quuz1_a}]
          avails:
-           [Quux{Quux, Quux2, Quux1}]
+           [Quux{Quux, Quux1, Quux2}]
          avails:
            [Tup2]
          avails:
diff --git a/testsuite/tests/showIface/NoExportList.stdout b/testsuite/tests/showIface/NoExportList.stdout
index 83fd28fa68d..aed34318ebb 100644
--- a/testsuite/tests/showIface/NoExportList.stdout
+++ b/testsuite/tests/showIface/NoExportList.stdout
@@ -32,7 +32,7 @@ docs:
 -- Actually we have only one type.
            identifiers:
          avails:
-           [R{R, fβ, fα, R}]
+           [R{R, R, fα, fβ}]
          section heading, level 1:
            text:
              -- * Functions
diff --git a/utils/haddock/haddock-api/src/Haddock/Interface/Create.hs b/utils/haddock/haddock-api/src/Haddock/Interface/Create.hs
index 2fb1003ac72..bf384e1b44c 100644
--- a/utils/haddock/haddock-api/src/Haddock/Interface/Create.hs
+++ b/utils/haddock/haddock-api/src/Haddock/Interface/Create.hs
@@ -201,7 +201,15 @@ createInterface1' flags unit_state dflags hie_file mod_iface ifaces inst_ifaces
   -- See Note [Exporting built-in items]
   let builtinTys = DsiSectionHeading 1 (WithHsDocIdentifiers (mkGeneratedHsDocString "Builtin syntax") [])
       bonus_ds mods
-        | mdl == gHC_PRIM = [builtinTys, DsiExports funAvail] <> mods
+        | mdl == gHC_PRIM =
+            [ builtinTys
+            , DsiExports $
+                {- Haddock does not want to sort avails, the order should be
+                    deterministically /derived from the source/.
+                   In this particular case, sorting funAvail would be a no-op anyway. -}
+                DefinitelyDeterministicAvails
+                  funAvail
+            ] <> mods
         | otherwise = mods
 
   let
@@ -461,11 +469,11 @@ mkExportItems
             Just hsDoc' -> do
               doc <- processDocStringParas parserOpts sDocContext pkgName hsDoc'
               pure [ExportDoc doc]
-        DsiExports avails ->
+        DsiExports (DetOrdAvails avails) ->
           -- TODO: We probably don't need nubAvails here.
           -- mkDocStructureFromExportList already uses it.
           concat <$> traverse availExport (nubAvails avails)
-        DsiModExport mod_names avails -> do
+        DsiModExport mod_names (DetOrdAvails avails) -> do
           -- only consider exporting a module if we are sure we are really
           -- exporting the whole module and not some subset.
           (unrestricted_mods, remaining_avails) <- unrestrictedModExports sDocContext thisMod modMap instIfaceMap avails (NE.toList mod_names)
diff --git a/utils/haddock/html-test/ref/BundledPatterns2.html b/utils/haddock/html-test/ref/BundledPatterns2.html
index 68261f3945b..6d71e0a7aa1 100644
--- a/utils/haddock/html-test/ref/BundledPatterns2.html
+++ b/utils/haddock/html-test/ref/BundledPatterns2.html
@@ -96,14 +96,6 @@
 	      >where</span
 	      ><ul class="subs"
 	      ><li
-		><span class="keyword"
-		  >pattern</span
-		  > <a href="#"
-		  >LR</a
-		  > :: a -&gt; <a href="#" title="BundledPatterns2"
-		  >RTree</a
-		  > 0 a</li
-		><li
 		><span class="keyword"
 		  >pattern</span
 		  > <a href="#"
@@ -117,6 +109,14 @@
 		  > (d <a href="#" title="GHC.TypeLits"
 		  >+</a
 		  > 1) a</li
+		><li
+		><span class="keyword"
+		  >pattern</span
+		  > <a href="#"
+		  >LR</a
+		  > :: a -&gt; <a href="#" title="BundledPatterns2"
+		  >RTree</a
+		  > 0 a</li
 		></ul
 	      ></li
 	    ></ul
@@ -318,28 +318,34 @@
 	      ><td class="src"
 		><span class="keyword"
 		  >pattern</span
-		  > <a id="v:LR" class="def"
-		  >LR</a
-		  > :: a -&gt; <a href="#" title="BundledPatterns2"
+		  > <a id="v:BR" class="def"
+		  >BR</a
+		  > :: <a href="#" title="BundledPatterns2"
 		  >RTree</a
-		  > 0 a</td
+		  > d a -&gt; <a href="#" title="BundledPatterns2"
+		  >RTree</a
+		  > d a -&gt; <a href="#" title="BundledPatterns2"
+		  >RTree</a
+		  > (d <a href="#" title="GHC.TypeLits"
+		  >+</a
+		  > 1) a</td
 		><td class="doc"
 		><p
-		  >Leaf of a perfect depth tree</p
+		  >Branch of a perfect depth tree</p
 		  ><pre class="screen"
 		  ><code class="prompt"
 		    >&gt;&gt;&gt; </code
 		    ><strong class="userinput"
 		    ><code
-		      >LR 1
+		      >BR (LR 1) (LR 2)
 </code
 		      ></strong
-		    >1
+		    >&lt;1,2&gt;
 <code class="prompt"
 		    >&gt;&gt;&gt; </code
 		    ><strong class="userinput"
 		    ><code
-		      >let x = LR 1
+		      >let x = BR (LR 1) (LR 2)
 </code
 		      ></strong
 		    ><code class="prompt"
@@ -349,16 +355,16 @@
 		      >:t x
 </code
 		      ></strong
-		    >x :: Num a =&gt; RTree 0 a
+		    >x :: Num a =&gt; RTree 1 a
 </pre
 		  ><p
-		  >Can be used as a pattern:</p
+		  >Case be used a pattern:</p
 		  ><pre class="screen"
 		  ><code class="prompt"
 		    >&gt;&gt;&gt; </code
 		    ><strong class="userinput"
 		    ><code
-		      >let f (LR a) (LR b) = a + b
+		      >let f (BR (LR a) (LR b)) = LR (a + b)
 </code
 		      ></strong
 		    ><code class="prompt"
@@ -368,12 +374,12 @@
 		      >:t f
 </code
 		      ></strong
-		    >f :: Num a =&gt; RTree 0 a -&gt; RTree 0 a -&gt; a
+		    >f :: Num a =&gt; RTree 1 a -&gt; RTree 0 a
 <code class="prompt"
 		    >&gt;&gt;&gt; </code
 		    ><strong class="userinput"
 		    ><code
-		      >f (LR 1) (LR 2)
+		      >f (BR (LR 1) (LR 2))
 </code
 		      ></strong
 		    >3
@@ -384,34 +390,28 @@
 	      ><td class="src"
 		><span class="keyword"
 		  >pattern</span
-		  > <a id="v:BR" class="def"
-		  >BR</a
-		  > :: <a href="#" title="BundledPatterns2"
-		  >RTree</a
-		  > d a -&gt; <a href="#" title="BundledPatterns2"
-		  >RTree</a
-		  > d a -&gt; <a href="#" title="BundledPatterns2"
+		  > <a id="v:LR" class="def"
+		  >LR</a
+		  > :: a -&gt; <a href="#" title="BundledPatterns2"
 		  >RTree</a
-		  > (d <a href="#" title="GHC.TypeLits"
-		  >+</a
-		  > 1) a</td
+		  > 0 a</td
 		><td class="doc"
 		><p
-		  >Branch of a perfect depth tree</p
+		  >Leaf of a perfect depth tree</p
 		  ><pre class="screen"
 		  ><code class="prompt"
 		    >&gt;&gt;&gt; </code
 		    ><strong class="userinput"
 		    ><code
-		      >BR (LR 1) (LR 2)
+		      >LR 1
 </code
 		      ></strong
-		    >&lt;1,2&gt;
+		    >1
 <code class="prompt"
 		    >&gt;&gt;&gt; </code
 		    ><strong class="userinput"
 		    ><code
-		      >let x = BR (LR 1) (LR 2)
+		      >let x = LR 1
 </code
 		      ></strong
 		    ><code class="prompt"
@@ -421,16 +421,16 @@
 		      >:t x
 </code
 		      ></strong
-		    >x :: Num a =&gt; RTree 1 a
+		    >x :: Num a =&gt; RTree 0 a
 </pre
 		  ><p
-		  >Case be used a pattern:</p
+		  >Can be used as a pattern:</p
 		  ><pre class="screen"
 		  ><code class="prompt"
 		    >&gt;&gt;&gt; </code
 		    ><strong class="userinput"
 		    ><code
-		      >let f (BR (LR a) (LR b)) = LR (a + b)
+		      >let f (LR a) (LR b) = a + b
 </code
 		      ></strong
 		    ><code class="prompt"
@@ -440,12 +440,12 @@
 		      >:t f
 </code
 		      ></strong
-		    >f :: Num a =&gt; RTree 1 a -&gt; RTree 0 a
+		    >f :: Num a =&gt; RTree 0 a -&gt; RTree 0 a -&gt; a
 <code class="prompt"
 		    >&gt;&gt;&gt; </code
 		    ><strong class="userinput"
 		    ><code
-		      >f (BR (LR 1) (LR 2))
+		      >f (LR 1) (LR 2)
 </code
 		      ></strong
 		    >3
diff --git a/utils/haddock/latex-test/ref/ConstructorArgs/ConstructorArgs.tex b/utils/haddock/latex-test/ref/ConstructorArgs/ConstructorArgs.tex
index b7870fa99a2..b919b272209 100644
--- a/utils/haddock/latex-test/ref/ConstructorArgs/ConstructorArgs.tex
+++ b/utils/haddock/latex-test/ref/ConstructorArgs/ConstructorArgs.tex
@@ -3,7 +3,7 @@
 \haddockbeginheader
 {\haddockverb\begin{verbatim}
 module ConstructorArgs (
-    Foo((:|), Rec, x, y, Baz, Boa, (:*)), Boo(Foo, Foa, Fo, Fo'), pattern Bo,
+    Foo((:*), (:|), Baz, Boa, Rec, x, y), Boo(Foo, Foa, Fo, Fo'), pattern Bo,
     pattern Bo'
   ) where\end{verbatim}}
 \haddockendheader
diff --git a/utils/haddock/latex-test/ref/DefaultSignatures/DefaultSignatures.tex b/utils/haddock/latex-test/ref/DefaultSignatures/DefaultSignatures.tex
index 161320835d5..674b25bffcf 100644
--- a/utils/haddock/latex-test/ref/DefaultSignatures/DefaultSignatures.tex
+++ b/utils/haddock/latex-test/ref/DefaultSignatures/DefaultSignatures.tex
@@ -3,7 +3,7 @@
 \haddockbeginheader
 {\haddockverb\begin{verbatim}
 module DefaultSignatures (
-    Foo(baz', baz, bar)
+    Foo(bar, baz, baz')
   ) where\end{verbatim}}
 \haddockendheader
 
diff --git a/utils/haddock/latex-test/ref/GadtConstructorArgs/GadtConstructorArgs.tex b/utils/haddock/latex-test/ref/GadtConstructorArgs/GadtConstructorArgs.tex
index e5b2123df10..d53cd410b67 100644
--- a/utils/haddock/latex-test/ref/GadtConstructorArgs/GadtConstructorArgs.tex
+++ b/utils/haddock/latex-test/ref/GadtConstructorArgs/GadtConstructorArgs.tex
@@ -3,7 +3,7 @@
 \haddockbeginheader
 {\haddockverb\begin{verbatim}
 module GadtConstructorArgs (
-    Boo(Fot, x, y, Fob, w, z)
+    Boo(Fob, Fot, w, x, y, z)
   ) where\end{verbatim}}
 \haddockendheader
 
diff --git a/utils/haddock/latex-test/ref/TypeFamilies3/TypeFamilies3.tex b/utils/haddock/latex-test/ref/TypeFamilies3/TypeFamilies3.tex
index 6ceb36864af..2881b14b927 100644
--- a/utils/haddock/latex-test/ref/TypeFamilies3/TypeFamilies3.tex
+++ b/utils/haddock/latex-test/ref/TypeFamilies3/TypeFamilies3.tex
@@ -3,7 +3,7 @@
 \haddockbeginheader
 {\haddockverb\begin{verbatim}
 module TypeFamilies3 (
-    Foo, Bar, Baz(Baz3, Baz2, Baz1)
+    Foo, Bar, Baz(Baz1, Baz2, Baz3)
   ) where\end{verbatim}}
 \haddockendheader
 
-- 
GitLab