diff --git a/CHANGES b/CHANGES
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,26 @@
+Changes in version 2.16.1
+
+ * Don't default to type constructors for out-of-scope names (#253 and
+   #375)
+
+ * Fix Hoogle display of constructors (#361)
+
+ * Fully qualify names in Hoogle instances output (#263)
+
+ * Output method documentation in Hoogle backend (#259)
+
+ * Don't print instance safety information in Hoogle (#168)
+
+ * Expand response files in arguments (#285)
+
+ * Build the main executable with -threaded (#399)
+
+ * Use SrcSpan of declarations for inferred type sigs (#207)
+
+ * Fix cross-module instance locations (#383)
+
+ * Fix alignment of Source link for instances in Firefox (#384)
+
 Changes in version 2.16.0
 
  * Experimental collapsible header support (#335)
diff --git a/README b/README
--- a/README
+++ b/README
@@ -0,0 +1,72 @@
+# Haddock, a Haskell Documentation Tool
+
+
+#### About haddock
+
+This is Haddock, a tool for automatically generating documentation
+from annotated Haskell source code.  It is primary intended for
+documenting library interfaces, but it should be useful for any kind
+of Haskell code.
+
+Haddock lets you write documentation annotations next to the
+definitions of functions and types in the source code, in a syntax
+that is easy on the eye when writing the source code (no heavyweight
+mark-up). The documentation generated by Haddock is fully hyperlinked
+- click on a type name in a type signature to go straight to the
+definition, and documentation, for that type.
+
+Haddock understands Haskell's module system, so you can structure your
+code however you like without worrying that internal structure will be
+exposed in the generated documentation.  For example, it is common to
+implement a library in several modules, but define the external API by
+having a single module which re-exports parts of these implementation
+modules.  Using Haddock, you can still write documentation annotations
+next to the actual definitions of the functions and types in the
+library, but the documentation annotations from the implementation
+will be propagated to the external API when the documentation is
+generated.  Abstract types and classes are handled correctly.  In
+fact, even without any documentation annotations, Haddock can generate
+useful documentation from your source code.
+
+
+#### Documentation formats
+
+Haddock can generate documentation in multiple formats; currently HTML
+is implemented, and there is partial support for generating LaTeX and
+Hoogle.
+
+
+#### Source code documentation
+
+Full documentation can be found in the doc/ subdirectory, in DocBook
+format.
+
+
+#### Contributing
+
+Please create issues when you have any problems and pull requests if you have some code.
+
+###### Hacking
+
+To get started you'll need a latest GHC release installed. Below is an
+example setup using cabal sandboxes.
+
+```bash
+  git clone https://github.com/haskell/haddock.git
+  cd haddock
+  cabal sandbox init
+  cabal sandbox add-source haddock-library
+  cabal sandbox add-source haddock-api
+  # adjust -j to the number of cores you want to use
+  cabal install -j4 --dependencies-only --enable-tests
+  cabal configure --enable-tests
+  cabal build -j4
+  # run the test suite
+  cabal test
+```
+
+If you're a GHC developer and want to update Haddock to work with your
+changes, you should be working on `ghc-head` branch instead of master.
+See instructions at
+https://ghc.haskell.org/trac/ghc/wiki/WorkingConventions/Git/Submodules
+for an example workflow.
diff --git a/doc/haddock.xml b/doc/haddock.xml
--- a/doc/haddock.xml
+++ b/doc/haddock.xml
@@ -12,7 +12,7 @@
 
 <book id="haddock">
   <bookinfo>
-    <date>2004-08-02</date>
+    <date>2015-06-02</date>
     <title>Haddock User Guide</title>
     <author>
       <firstname>Simon</firstname>
@@ -24,12 +24,21 @@
       <surname>Waern</surname>
     </author>
     <address><email>david.waern@gmail.com</email></address>
+    <author>
+      <firstname>Mateusz</firstname>
+      <surname>Kowalczyk</surname>
+    </author>
+    <address><email>fuuzetsu@fuuzetsu.co.uk</email></address>
     <copyright>
       <year>2010</year>
       <holder>Simon Marlow, David Waern</holder>
     </copyright>
+    <copyright>
+      <year>2013-2015</year>
+      <holder>Mateusz Kowalczyk</holder>
+    </copyright>
     <abstract>
-      <para>This document describes Haddock version 2.15.1, a Haskell
+      <para>This document describes Haddock version 2.16.1, a Haskell
       documentation tool.</para>
     </abstract>
   </bookinfo>
@@ -2029,7 +2038,18 @@
                     2. No newline separation even in indented lists.
 -}
 </programlisting>
+    <para>The indentation of the first list item is honoured. That is,
+    in the following example the items are on the same level. Before
+    Haddock 2.16.1, the second item would have been nested under the
+    first item which was unexpected.
+    </para>
+<programlisting>
+{-|
+    * foo
 
+    * bar
+-}
+</programlisting>
       </section>
 
       <section>
diff --git a/driver/Main.hs b/driver/Main.hs
--- a/driver/Main.hs
+++ b/driver/Main.hs
@@ -1,7 +1,29 @@
+{-# LANGUAGE ScopedTypeVariables #-}
 module Main where
 
-import           Documentation.Haddock (haddock)
-import           System.Environment (getArgs)
+import Control.Exception
+import Documentation.Haddock (haddock)
+import System.Environment (getArgs)
+import System.Exit (exitFailure)
+import System.IO
 
 main :: IO ()
-main = getArgs >>= haddock
+main = getArgs >>= expandResponse >>= haddock
+
+
+-- | Arguments which look like '@foo' will be replaced with the
+-- contents of file @foo@. The contents will be passed through 'words'
+-- and blanks filtered out first.
+--
+-- We quit if the file is not found or reading somehow fails.
+expandResponse :: [String] -> IO [String]
+expandResponse = fmap concat . mapM expand
+  where
+    expand :: String -> IO [String]
+    expand ('@':f) = readFileExc f >>= return . filter (not . null) . words
+    expand x = return [x]
+
+    readFileExc f =
+      readFile f `catch` \(e :: IOException) -> do
+        hPutStrLn stderr $ "Error while expanding response file: " ++ show e
+        exitFailure
diff --git a/haddock-api/src/Haddock.hs b/haddock-api/src/Haddock.hs
--- a/haddock-api/src/Haddock.hs
+++ b/haddock-api/src/Haddock.hs
@@ -39,6 +39,7 @@
 import Haddock.Utils
 
 import Control.Monad hiding (forM_)
+import Control.Applicative
 import Data.Foldable (forM_)
 import Data.List (isPrefixOf)
 import Control.Exception
@@ -250,9 +251,9 @@
     allVisibleIfaces = [ i | i <- allIfaces, OptHide `notElem` instOptions i ]
 
     pkgMod           = ifaceMod (head ifaces)
-    pkgKey            = modulePackageKey pkgMod
+    pkgKey           = modulePackageKey pkgMod
     pkgStr           = Just (packageKeyString pkgKey)
-    (pkgName,pkgVer) = modulePackageInfo dflags flags pkgMod
+    pkgNameVer       = modulePackageInfo dflags flags pkgMod
 
     (srcBase, srcModule, srcEntity, srcLEntity) = sourceUrls flags
     srcMap' = maybe srcMap (\path -> Map.insert pkgKey path srcMap) srcEntity
@@ -288,12 +289,20 @@
   -- TODO: we throw away Meta for both Hoogle and LaTeX right now,
   -- might want to fix that if/when these two get some work on them
   when (Flag_Hoogle `elem` flags) $ do
-    let pkgNameStr | unpackFS pkgNameFS == "main" && title /= []
-                               = title
-                   | otherwise = unpackFS pkgNameFS
-          where PackageName pkgNameFS = pkgName
-    ppHoogle dflags pkgNameStr pkgVer title (fmap _doc prologue) visibleIfaces
-      odir
+    case pkgNameVer of
+      Nothing -> putStrLn . unlines $
+          [ "haddock: Unable to find a package providing module "
+            ++ moduleNameString (moduleName pkgMod) ++ ", skipping Hoogle."
+          , ""
+          , "         Perhaps try specifying the desired package explicitly"
+            ++ " using the --package-name"
+          , "         and --package-version arguments."
+          ]
+      Just (PackageName pkgNameFS, pkgVer) ->
+          let pkgNameStr | unpackFS pkgNameFS == "main" && title /= [] = title
+                         | otherwise = unpackFS pkgNameFS
+          in ppHoogle dflags pkgNameStr pkgVer title (fmap _doc prologue)
+               visibleIfaces odir
 
   when (Flag_LaTeX `elem` flags) $ do
     ppLaTeX title pkgStr visibleIfaces odir (fmap _doc prologue) opt_latex_style
@@ -312,12 +321,12 @@
                             -- contain the package name or version
                             -- provided by the user which we
                             -- prioritise
-                  -> Module -> (PackageName, Data.Version.Version)
+                  -> Module -> Maybe (PackageName, Data.Version.Version)
 modulePackageInfo dflags flags modu =
-  (fromMaybe (packageName pkg) (optPackageName flags),
-   fromMaybe (packageVersion pkg) (optPackageVersion flags))
+    cmdline <|> pkgDb
   where
-    pkg = getPackageDetails dflags (modulePackageKey modu)
+    cmdline = (,) <$> optPackageName flags <*> optPackageVersion flags
+    pkgDb = (\pkg -> (packageName pkg, packageVersion pkg)) <$> lookupPackage dflags (modulePackageKey modu)
 
 
 -------------------------------------------------------------------------------
diff --git a/haddock-api/src/Haddock/Backends/Hoogle.hs b/haddock-api/src/Haddock/Backends/Hoogle.hs
--- a/haddock-api/src/Haddock/Backends/Hoogle.hs
+++ b/haddock-api/src/Haddock/Backends/Hoogle.hs
@@ -15,7 +15,8 @@
     ppHoogle
   ) where
 
-
+import BasicTypes (OverlapFlag(..), OverlapMode(..))
+import InstEnv (ClsInst(..))
 import Haddock.GhcUtils
 import Haddock.Types hiding (Version)
 import Haddock.Utils hiding (out)
@@ -95,18 +96,22 @@
 dropComment [] = []
 
 
-out :: Outputable a => DynFlags -> a -> String
-out dflags = f . unwords . map (dropWhile isSpace) . lines . showSDocUnqual dflags . ppr
+outWith :: Outputable a => (SDoc -> String) -> a -> [Char]
+outWith p = f . unwords . map (dropWhile isSpace) . lines . p . ppr
     where
         f xs | " <document comment>" `isPrefixOf` xs = f $ drop 19 xs
         f (x:xs) = x : f xs
         f [] = []
 
+out :: Outputable a => DynFlags -> a -> String
+out dflags = outWith $ showSDocUnqual dflags
 
 operator :: String -> String
 operator (x:xs) | not (isAlphaNum x) && x `notElem` "_' ([{" = '(' : x:xs ++ ")"
 operator x = x
 
+commaSeparate :: Outputable a => DynFlags -> [a] -> String
+commaSeparate dflags = showSDocUnqual dflags . interpp'SP
 
 ---------------------------------------------------------------------
 -- How to print each export
@@ -119,30 +124,38 @@
     where
         f (TyClD d@DataDecl{})  = ppData dflags d subdocs
         f (TyClD d@SynDecl{})   = ppSynonym dflags d
-        f (TyClD d@ClassDecl{}) = ppClass dflags d
+        f (TyClD d@ClassDecl{}) = ppClass dflags d subdocs
         f (ForD (ForeignImport name typ _ _)) = ppSig dflags $ TypeSig [name] typ []
         f (ForD (ForeignExport name typ _ _)) = ppSig dflags $ TypeSig [name] typ []
         f (SigD sig) = ppSig dflags sig
         f _ = []
 ppExport _ _ = []
 
-
-ppSig :: DynFlags -> Sig Name -> [String]
-ppSig dflags (TypeSig names sig _)
-    = [operator prettyNames ++ " :: " ++ outHsType dflags typ]
+ppSigWithDoc :: DynFlags -> Sig Name -> [(Name, DocForDecl Name)] -> [String]
+ppSigWithDoc dflags (TypeSig names sig _) subdocs
+    = concatMap mkDocSig names
     where
-        prettyNames = intercalate ", " $ map (out dflags) names
+        mkDocSig n = concatMap (ppDocumentation dflags) (getDoc n)
+                     ++ [mkSig n]
+        mkSig n = operator (out dflags n) ++ " :: " ++ outHsType dflags typ
+
+        getDoc :: Located Name -> [Documentation Name]
+        getDoc n = maybe [] (return . fst) (lookup (unL n) subdocs)
+
         typ = case unL sig of
                    HsForAllTy Explicit a b c d  -> HsForAllTy Implicit a b c d
                    HsForAllTy Qualified a b c d -> HsForAllTy Implicit a b c d
                    x -> x
-ppSig _ _ = []
+ppSigWithDoc _ _ _ = []
 
+ppSig :: DynFlags -> Sig Name -> [String]
+ppSig dflags x  = ppSigWithDoc dflags x []
 
+
 -- note: does not yet output documentation for class methods
-ppClass :: DynFlags -> TyClDecl Name -> [String]
-ppClass dflags x = out dflags x{tcdSigs=[]} :
-            concatMap (ppSig dflags . addContext . unL) (tcdSigs x)
+ppClass :: DynFlags -> TyClDecl Name -> [(Name, DocForDecl Name)] -> [String]
+ppClass dflags x subdocs = out dflags x{tcdSigs=[]} :
+            concatMap (flip (ppSigWithDoc dflags) subdocs . addContext . unL) (tcdSigs x)
     where
         addContext (TypeSig name (L l sig) nwcs) = TypeSig name (L l $ f sig) nwcs
         addContext (MinimalSig src sig) = MinimalSig src sig
@@ -156,8 +169,16 @@
 
 
 ppInstance :: DynFlags -> ClsInst -> [String]
-ppInstance dflags x = [dropComment $ out dflags x]
-
+ppInstance dflags x =
+  [dropComment $ outWith (showSDocForUser dflags alwaysQualify) cls]
+  where
+    -- As per #168, we don't want safety information about the class
+    -- in Hoogle output. The easiest way to achieve this is to set the
+    -- safety information to a state where the Outputable instance
+    -- produces no output which means no overlap and unsafe (or [safe]
+    -- is generated).
+    cls = x { is_flag = OverlapFlag { overlapMode = NoOverlap mempty
+                                    , isSafeOverlap = False } }
 
 ppSynonym :: DynFlags -> TyClDecl Name -> [String]
 ppSynonym dflags x = [out dflags x]
@@ -198,7 +219,10 @@
         apps = foldl1 (\x y -> reL $ HsAppTy x y)
 
         typeSig nm flds = operator nm ++ " :: " ++ outHsType dflags (makeExplicit $ unL $ funs flds)
-        name = out dflags $ map unL $ con_names con
+
+        -- We print the constructors as comma-separated list. See GHC
+        -- docs for con_names on why it is a list to begin with.
+        name = commaSeparate dflags . map unL $ con_names con
 
         resType = case con_res con of
             ResTyH98 -> apps $ map (reL . HsTyVar) $
diff --git a/haddock-api/src/Haddock/Backends/LaTeX.hs b/haddock-api/src/Haddock/Backends/LaTeX.hs
--- a/haddock-api/src/Haddock/Backends/LaTeX.hs
+++ b/haddock-api/src/Haddock/Backends/LaTeX.hs
@@ -544,14 +544,14 @@
     (is, rest') = spanWith isUndocdInstance rest
 
 isUndocdInstance :: DocInstance a -> Maybe (InstHead a)
-isUndocdInstance (L _ i,Nothing) = Just i
+isUndocdInstance (i,Nothing,_) = Just i
 isUndocdInstance _ = Nothing
 
 -- | Print a possibly commented instance. The instance header is printed inside
 -- an 'argBox'. The comment is printed to the right of the box in normal comment
 -- style.
 ppDocInstance :: Bool -> DocInstance DocName -> LaTeX
-ppDocInstance unicode (L _ instHead, doc) =
+ppDocInstance unicode (instHead, doc, _) =
   declWithDoc (ppInstDecl unicode instHead) (fmap docToLaTeX $ fmap _doc doc)
 
 
diff --git a/haddock-api/src/Haddock/Backends/Xhtml.hs b/haddock-api/src/Haddock/Backends/Xhtml.hs
--- a/haddock-api/src/Haddock/Backends/Xhtml.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml.hs
@@ -36,7 +36,6 @@
 
 import Control.Monad         ( when, unless )
 import Data.Char             ( toUpper )
-import Data.Functor          ( (<$>) )
 import Data.List             ( sortBy, groupBy, intercalate, isPrefixOf )
 import Data.Maybe
 import System.FilePath hiding ( (</>) )
@@ -289,7 +288,7 @@
 
 
 mkNode :: Qualification -> [String] -> String -> ModuleTree -> Html
-mkNode qual ss p (Node s leaf pkg short ts) =
+mkNode qual ss p (Node s leaf pkg srcPkg short ts) =
   htmlModule <+> shortDescr +++ htmlPkg +++ subtree
   where
     modAttrs = case (ts, leaf) of
@@ -313,7 +312,7 @@
     mdl = intercalate "." (reverse (s:ss))
 
     shortDescr = maybe noHtml (origDocToHtml qual) short
-    htmlPkg = maybe noHtml (thespan ! [theclass "package"] <<) pkg
+    htmlPkg = maybe noHtml (thespan ! [theclass "package"] <<) srcPkg
 
     subtree = mkNodeList qual (s:ss) p ts ! collapseSection p True ""
 
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -27,7 +27,6 @@
 import Haddock.Types
 import Haddock.Doc (combineDocumentation)
 
-import           Control.Applicative
 import           Data.List             ( intersperse, sort )
 import qualified Data.Map as Map
 import           Data.Maybe
@@ -498,12 +497,12 @@
 
 ppInstances :: LinksInfo -> [DocInstance DocName] -> DocName -> Unicode -> Qualification -> Html
 ppInstances links instances baseName unicode qual
-  = subInstances qual instName links True baseName (map instDecl instances)
+  = subInstances qual instName links True (map instDecl instances)
   -- force Splice = True to use line URLs
   where
     instName = getOccString $ getName baseName
-    instDecl :: DocInstance DocName -> (SubDecl,SrcSpan)
-    instDecl (L l inst, maybeDoc) = ((instHead inst, maybeDoc, []),l)
+    instDecl :: DocInstance DocName -> (SubDecl,Located DocName)
+    instDecl (inst, maybeDoc,l) = ((instHead inst, maybeDoc, []),l)
     instHead (n, ks, ts, ClassInst cs) = ppContextNoLocs cs unicode qual
         <+> ppAppNameTypes n ks ts unicode qual
     instHead (n, ks, ts, TypeInst rhs) = keyword "type"
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/DocMarkup.hs b/haddock-api/src/Haddock/Backends/Xhtml/DocMarkup.hs
--- a/haddock-api/src/Haddock/Backends/Xhtml/DocMarkup.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/DocMarkup.hs
@@ -19,8 +19,6 @@
   docElement, docSection, docSection_,
 ) where
 
-import Control.Applicative ((<$>))
-
 import Data.List
 import Haddock.Backends.Xhtml.Names
 import Haddock.Backends.Xhtml.Utils
@@ -64,7 +62,10 @@
                                   then anchor ! [href url]
                                        << fromMaybe url mLabel
                                   else toHtml $ fromMaybe url mLabel,
-  markupAName                = \aname -> namedAnchor aname << "",
+  markupAName                = \aname
+                               -> if insertAnchors
+                                  then namedAnchor aname << ""
+                                  else noHtml,
   markupPic                  = \(Picture uri t) -> image ! ([src uri] ++ fromMaybe [] (return . title <$> t)),
   markupProperty             = pre . toHtml,
   markupExample              = examplesToHtml,
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
--- a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
@@ -44,7 +44,6 @@
 import Haddock.Backends.Xhtml.Utils
 import Haddock.Types
 import Haddock.Utils (makeAnchorId)
-
 import qualified Data.Map as Map
 import Text.XHtml hiding ( name, title, p, quote )
 
@@ -148,20 +147,22 @@
        docElement td << fmap (docToHtml Nothing qual) mdoc)
       : map (cell . (td <<)) subs
 
+
 -- | Sub table with source information (optional).
-subTableSrc :: Qualification -> LinksInfo -> Bool -> DocName -> [(SubDecl,SrcSpan)] -> Maybe Html
-subTableSrc _ _  _ _ [] = Nothing
-subTableSrc qual lnks splice dn decls = Just $ table << aboves (concatMap subRow decls)
+subTableSrc :: Qualification -> LinksInfo -> Bool -> [(SubDecl,Located DocName)] -> Maybe Html
+subTableSrc _ _  _ [] = Nothing
+subTableSrc qual lnks splice decls = Just $ table << aboves (concatMap subRow decls)
   where
-    subRow ((decl, mdoc, subs),loc) =
-      (td ! [theclass "src"] << decl
-      <+> linkHtml loc
+    subRow ((decl, mdoc, subs),L loc dn) =
+      (td ! [theclass "src clearfix"] <<
+        (thespan ! [theclass "inst-left"] << decl)
+        <+> linkHtml loc dn
       <->
       docElement td << fmap (docToHtml Nothing qual) mdoc
       )
       : map (cell . (td <<)) subs
-    linkHtml loc@(RealSrcSpan _) = links lnks loc splice dn
-    linkHtml _ = noHtml
+    linkHtml loc@(RealSrcSpan _) dn = links lnks loc splice dn
+    linkHtml _ _ = noHtml
 
 subBlock :: [Html] -> Maybe Html
 subBlock [] = Nothing
@@ -191,12 +192,12 @@
 -- | Generate sub table for instance declarations, with source
 subInstances :: Qualification
              -> String -- ^ Class name, used for anchor generation
-             -> LinksInfo -> Bool -> DocName
-             -> [(SubDecl,SrcSpan)] -> Html
-subInstances qual nm lnks splice dn = maybe noHtml wrap . instTable
+             -> LinksInfo -> Bool
+             -> [(SubDecl,Located DocName)] -> Html
+subInstances qual nm lnks splice = maybe noHtml wrap . instTable
   where
     wrap = (subSection <<) . (subCaption +++)
-    instTable = fmap (thediv ! collapseSection id_ True [] <<) . subTableSrc qual lnks splice dn
+    instTable = fmap (thediv ! collapseSection id_ True [] <<) . subTableSrc qual lnks splice
     subSection = thediv ! [theclass "subs instances"]
     subCaption = paragraph ! collapseControl id_ True "caption" << "Instances"
     id_ = makeAnchorId $ "i:" ++ nm
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Themes.hs b/haddock-api/src/Haddock/Backends/Xhtml/Themes.hs
--- a/haddock-api/src/Haddock/Backends/Xhtml/Themes.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Themes.hs
@@ -18,7 +18,6 @@
 
 import Haddock.Options
 
-import Control.Applicative
 import Control.Monad (liftM)
 import Data.Char (toLower)
 import Data.Either (lefts, rights)
@@ -206,4 +205,3 @@
 
 concatEither :: [Either a [b]] -> Either a [b]
 concatEither = liftEither concat . sequenceEither
-
diff --git a/haddock-api/src/Haddock/GhcUtils.hs b/haddock-api/src/Haddock/GhcUtils.hs
--- a/haddock-api/src/Haddock/GhcUtils.hs
+++ b/haddock-api/src/Haddock/GhcUtils.hs
@@ -16,7 +16,6 @@
 module Haddock.GhcUtils where
 
 
-import Control.Applicative  ( (<$>) )
 import Control.Arrow
 import Data.Function
 
diff --git a/haddock-api/src/Haddock/Interface/AttachInstances.hs b/haddock-api/src/Haddock/Interface/AttachInstances.hs
--- a/haddock-api/src/Haddock/Interface/AttachInstances.hs
+++ b/haddock-api/src/Haddock/Interface/AttachInstances.hs
@@ -38,6 +38,7 @@
 import Name
 import Outputable (text, sep, (<+>))
 import PrelNames
+import SrcLoc
 import TcRnDriver (tcRnGetInfo)
 import TcType (tcSplitSigmaTy)
 import TyCon
@@ -68,11 +69,11 @@
                    -> Ghc (ExportItem Name)
 attachToExportItem expInfo iface ifaceMap instIfaceMap export =
   case attachFixities export of
-    e@ExportDecl { expItemDecl = L _ (TyClD d) } -> do
+    e@ExportDecl { expItemDecl = L eSpan (TyClD d) } -> do
       mb_info <- getAllInfo (tcdName d)
       insts <- case mb_info of
         Just (_, _, cls_instances, fam_instances) ->
-          let fam_insts = [ (L (getSrcSpan n) $ synifyFamInst i opaque, doc)
+          let fam_insts = [ (synifyFamInst i opaque, doc,spanNameE n (synifyFamInst i opaque) (L eSpan (tcdName d)) )
                           | i <- sortBy (comparing instFam) fam_instances
                           , let n = getName i
                           , let doc = instLookup instDocMap n iface ifaceMap instIfaceMap
@@ -80,14 +81,14 @@
                           , not $ any (isTypeHidden expInfo) (fi_tys i)
                           , let opaque = isTypeHidden expInfo (fi_rhs i)
                           ]
-              cls_insts = [ (L (getSrcSpan n) $ synifyInstHead i, instLookup instDocMap n iface ifaceMap instIfaceMap)
+              cls_insts = [ (synifyInstHead i, instLookup instDocMap n iface ifaceMap instIfaceMap, spanName n (synifyInstHead i) (L eSpan (tcdName d)))
                           | let is = [ (instanceHead' i, getName i) | i <- cls_instances ]
                           , (i@(_,_,cls,tys), n) <- sortBy (comparing $ first instHead) is
                           , not $ isInstanceHidden expInfo cls tys
                           ]
               -- fam_insts but with failing type fams filtered out
-              cleanFamInsts = [ (L l fi, n) | (L l (Right fi), n) <- fam_insts ]
-              famInstErrs = [ errm | (L _ (Left errm), _) <- fam_insts ]
+              cleanFamInsts = [ (fi, n, L l r) | (Right fi, n, L l (Right r)) <- fam_insts ]
+              famInstErrs = [ errm | (Left errm, _, _) <- fam_insts ]
           in do
             dfs <- getDynFlags
             let mkBug = (text "haddock-bug:" <+>) . text
@@ -106,6 +107,18 @@
       ] }
 
     attachFixities e = e
+    -- spanName: attach the location to the name that is the same file as the instance location
+    spanName s (clsn,_,_,_) (L instL instn) =
+        let s1 = getSrcSpan s
+            sn = if srcSpanFileName_maybe s1 == srcSpanFileName_maybe instL
+                    then instn
+                    else clsn
+        in L (getSrcSpan s) sn
+    -- spanName on Either
+    spanNameE s (Left e) _ =  L (getSrcSpan s) (Left e)
+    spanNameE s (Right ok) linst =
+      let L l r = spanName s ok linst
+      in L l (Right r)
 
 
 instLookup :: (InstalledInterface -> Map.Map Name a) -> Name
diff --git a/haddock-api/src/Haddock/Interface/Create.hs b/haddock-api/src/Haddock/Interface/Create.hs
--- a/haddock-api/src/Haddock/Interface/Create.hs
+++ b/haddock-api/src/Haddock/Interface/Create.hs
@@ -517,7 +517,7 @@
       case findDecl t of
         ([L l (ValD _)], (doc, _)) -> do
           -- Top-level binding without type signature
-          export <- hiValExportItem dflags t doc (l `elem` splices) $ M.lookup t fixMap
+          export <- hiValExportItem dflags t l doc (l `elem` splices) $ M.lookup t fixMap
           return [export]
         (ds, docs_) | decl : _ <- filter (not . isValD . unLoc) ds ->
           let declNames = getMainDeclBinder (unL decl)
@@ -620,13 +620,19 @@
                    O.text "-- Please report this on Haddock issue tracker!"
       bugWarn = O.showSDoc dflags . warnLine
 
-hiValExportItem :: DynFlags -> Name -> DocForDecl Name -> Bool -> Maybe Fixity -> ErrMsgGhc (ExportItem Name)
-hiValExportItem dflags name doc splice fixity = do
+-- | This function is called for top-level bindings without type signatures.
+-- It gets the type signature from GHC and that means it's not going to
+-- have a meaningful 'SrcSpan'. So we pass down 'SrcSpan' for the
+-- declaration and use it instead - 'nLoc' here.
+hiValExportItem :: DynFlags -> Name -> SrcSpan -> DocForDecl Name -> Bool
+                -> Maybe Fixity -> ErrMsgGhc (ExportItem Name)
+hiValExportItem dflags name nLoc doc splice fixity = do
   mayDecl <- hiDecl dflags name
   case mayDecl of
     Nothing -> return (ExportNoDecl name [])
-    Just decl -> return (ExportDecl decl doc [] [] fixities splice)
+    Just decl -> return (ExportDecl (fixSpan decl) doc [] [] fixities splice)
   where
+    fixSpan (L l t) = L (SrcLoc.combineSrcSpans l nLoc) t
     fixities = case fixity of
       Just f  -> [(name, f)]
       Nothing -> []
@@ -737,7 +743,7 @@
       | name:_ <- collectHsBindBinders d, Just [L _ (ValD _)] <- M.lookup name declMap =
           -- Top-level binding without type signature.
           let (doc, _) = lookupDocs name warnings docMap argMap subMap in
-          fmap Just (hiValExportItem dflags name doc (l `elem` splices) $ M.lookup name fixMap)
+          fmap Just (hiValExportItem dflags name l doc (l `elem` splices) $ M.lookup name fixMap)
       | otherwise = return Nothing
     mkExportItem decl@(L l (InstD d))
       | Just name <- M.lookup (getInstLoc d) instMap =
diff --git a/haddock-api/src/Haddock/Interface/LexParseRn.hs b/haddock-api/src/Haddock/Interface/LexParseRn.hs
--- a/haddock-api/src/Haddock/Interface/LexParseRn.hs
+++ b/haddock-api/src/Haddock/Interface/LexParseRn.hs
@@ -18,7 +18,6 @@
   , processModuleHeader
   ) where
 
-import Control.Applicative
 import Data.IntSet (toList)
 import Data.List
 import Documentation.Haddock.Doc (metaDocConcat)
@@ -31,6 +30,7 @@
 import Name
 import Outputable (showPpr)
 import RdrName
+import RnEnv (dataTcOccs)
 
 processDocStrings :: DynFlags -> GlobalRdrEnv -> [HsDocString]
                   -> Maybe (MDoc Name)
@@ -74,7 +74,13 @@
   where
     failure = (emptyHaddockModInfo, Nothing)
 
-
+-- | Takes a 'GlobalRdrEnv' which (hopefully) contains all the
+-- definitions and a parsed comment and we attempt to make sense of
+-- where the identifiers in the comment point to. We're in effect
+-- trying to convert 'RdrName's to 'Name's, with some guesswork and
+-- fallbacks in case we can't locate the identifiers.
+--
+-- See the comments in the source for implementation commentary.
 rename :: DynFlags -> GlobalRdrEnv -> Doc RdrName -> Doc Name
 rename dflags gre = rn
   where
@@ -82,19 +88,36 @@
       DocAppend a b -> DocAppend (rn a) (rn b)
       DocParagraph doc -> DocParagraph (rn doc)
       DocIdentifier x -> do
-        let choices = dataTcOccs' x
+        -- Generate the choices for the possible kind of thing this
+        -- is.
+        let choices = dataTcOccs x
+        -- Try to look up all the names in the GlobalRdrEnv that match
+        -- the names.
         let names = concatMap (\c -> map gre_name (lookupGRE_RdrName c gre)) choices
+
         case names of
+          -- We found no names in the env so we start guessing.
           [] ->
             case choices of
               [] -> DocMonospaced (DocString (showPpr dflags x))
-              [a] -> outOfScope dflags a
-              a:b:_ | isRdrTc a -> outOfScope dflags a
-                    | otherwise -> outOfScope dflags b
+              -- There was nothing in the environment so we need to
+              -- pick some default from what's available to us. We
+              -- diverge here from the old way where we would default
+              -- to type constructors as we're much more likely to
+              -- actually want anchors to regular definitions than
+              -- type constructor names (such as in #253). So now we
+              -- only get type constructor links if they are actually
+              -- in scope.
+              a:_ -> outOfScope dflags a
+
+          -- There is only one name in the environment that matches so
+          -- use it.
           [a] -> DocIdentifier a
-          a:b:_ | isTyConName a -> DocIdentifier a | otherwise -> DocIdentifier b
-              -- If an id can refer to multiple things, we give precedence to type
-              -- constructors.
+          -- But when there are multiple names available, default to
+          -- type constructors: somewhat awfully GHC returns the
+          -- values in the list positionally.
+          a:b:_ | isTyConName a -> DocIdentifier a
+                | otherwise -> DocIdentifier b
 
       DocWarning doc -> DocWarning (rn doc)
       DocEmphasis doc -> DocEmphasis (rn doc)
@@ -115,21 +138,14 @@
       DocString str -> DocString str
       DocHeader (Header l t) -> DocHeader $ Header l (rn t)
 
-dataTcOccs' :: RdrName -> [RdrName]
--- If the input is a data constructor, return both it and a type
--- constructor.  This is useful when we aren't sure which we are
--- looking at.
---
--- We use this definition instead of the GHC's to provide proper linking to
--- functions accross modules. See ticket #253 on Haddock Trac.
-dataTcOccs' rdr_name
-  | isDataOcc occ             = [rdr_name, rdr_name_tc]
-  | otherwise                 = [rdr_name]
-  where
-    occ = rdrNameOcc rdr_name
-    rdr_name_tc = setRdrNameSpace rdr_name tcName
-
-
+-- | Wrap an identifier that's out of scope (i.e. wasn't found in
+-- 'GlobalReaderEnv' during 'rename') in an appropriate doc. Currently
+-- we simply monospace the identifier in most cases except when the
+-- identifier is qualified: if the identifier is qualified then we can
+-- still try to guess and generate anchors accross modules but the
+-- users shouldn't rely on this doing the right thing. See tickets
+-- #253 and #375 on the confusion this causes depending on which
+-- default we pick in 'rename'.
 outOfScope :: DynFlags -> RdrName -> Doc a
 outOfScope dflags x =
   case x of
diff --git a/haddock-api/src/Haddock/Interface/ParseModuleHeader.hs b/haddock-api/src/Haddock/Interface/ParseModuleHeader.hs
--- a/haddock-api/src/Haddock/Interface/ParseModuleHeader.hs
+++ b/haddock-api/src/Haddock/Interface/ParseModuleHeader.hs
@@ -11,7 +11,6 @@
 -----------------------------------------------------------------------------
 module Haddock.Interface.ParseModuleHeader (parseModuleHeader) where
 
-import Control.Applicative ((<$>))
 import Control.Monad (mplus)
 import Data.Char
 import DynFlags
diff --git a/haddock-api/src/Haddock/Interface/Rename.hs b/haddock-api/src/Haddock/Interface/Rename.hs
--- a/haddock-api/src/Haddock/Interface/Rename.hs
+++ b/haddock-api/src/Haddock/Interface/Rename.hs
@@ -498,10 +498,11 @@
     decl' <- renameLDecl decl
     doc'  <- renameDocForDecl doc
     subs' <- mapM renameSub subs
-    instances' <- forM instances $ \(L l inst, idoc) -> do
+    instances' <- forM instances $ \(inst, idoc, L l n) -> do
       inst' <- renameInstHead inst
+      n' <- rename n
       idoc' <- mapM renameDoc idoc
-      return (L l inst', idoc')
+      return (inst', idoc',L l n')
     fixities' <- forM fixities $ \(name, fixity) -> do
       name' <- lookupRn name
       return (name', fixity)
diff --git a/haddock-api/src/Haddock/InterfaceFile.hs b/haddock-api/src/Haddock/InterfaceFile.hs
--- a/haddock-api/src/Haddock/InterfaceFile.hs
+++ b/haddock-api/src/Haddock/InterfaceFile.hs
@@ -25,7 +25,6 @@
 
 import Control.Monad
 import Data.Array
-import Data.Functor ((<$>))
 import Data.IORef
 import Data.List
 import qualified Data.Map as Map
diff --git a/haddock-api/src/Haddock/ModuleTree.hs b/haddock-api/src/Haddock/ModuleTree.hs
--- a/haddock-api/src/Haddock/ModuleTree.hs
+++ b/haddock-api/src/Haddock/ModuleTree.hs
@@ -15,41 +15,44 @@
 import Haddock.Types ( MDoc )
 
 import GHC           ( Name )
-import Module        ( Module, moduleNameString, moduleName, modulePackageKey )
+import Module        ( Module, moduleNameString, moduleName, modulePackageKey, packageKeyString )
 import DynFlags      ( DynFlags )
 import Packages      ( lookupPackage )
 import PackageConfig ( sourcePackageIdString )
 
 
-data ModuleTree = Node String Bool (Maybe String) (Maybe (MDoc Name)) [ModuleTree]
+data ModuleTree = Node String Bool (Maybe String) (Maybe String) (Maybe (MDoc Name)) [ModuleTree]
 
 
 mkModuleTree :: DynFlags -> Bool -> [(Module, Maybe (MDoc Name))] -> [ModuleTree]
 mkModuleTree dflags showPkgs mods =
-  foldr fn [] [ (splitModule mdl, modPkg mdl, short) | (mdl, short) <- mods ]
+  foldr fn [] [ (splitModule mdl, modPkg mdl, modSrcPkg mdl, short) | (mdl, short) <- mods ]
   where
-    modPkg mod_ | showPkgs = fmap sourcePackageIdString
-                                  (lookupPackage dflags (modulePackageKey mod_))
+    modPkg mod_ | showPkgs = Just (packageKeyString (modulePackageKey mod_))
                 | otherwise = Nothing
-    fn (mod_,pkg,short) = addToTrees mod_ pkg short
+    modSrcPkg mod_ | showPkgs = fmap sourcePackageIdString
+                                     (lookupPackage dflags (modulePackageKey mod_))
+                   | otherwise = Nothing
+    fn (mod_,pkg,srcPkg,short) = addToTrees mod_ pkg srcPkg short
 
 
-addToTrees :: [String] -> Maybe String -> Maybe (MDoc Name) -> [ModuleTree] -> [ModuleTree]
-addToTrees [] _ _ ts = ts
-addToTrees ss pkg short [] = mkSubTree ss pkg short
-addToTrees (s1:ss) pkg short (t@(Node s2 leaf node_pkg node_short subs) : ts)
-  | s1 >  s2  = t : addToTrees (s1:ss) pkg short ts
-  | s1 == s2  = Node s2 (leaf || null ss) this_pkg this_short (addToTrees ss pkg short subs) : ts
-  | otherwise = mkSubTree (s1:ss) pkg short ++ t : ts
+addToTrees :: [String] -> Maybe String -> Maybe String -> Maybe (MDoc Name) -> [ModuleTree] -> [ModuleTree]
+addToTrees [] _ _ _ ts = ts
+addToTrees ss pkg srcPkg short [] = mkSubTree ss pkg srcPkg short
+addToTrees (s1:ss) pkg srcPkg short (t@(Node s2 leaf node_pkg node_srcPkg node_short subs) : ts)
+  | s1 >  s2  = t : addToTrees (s1:ss) pkg srcPkg short ts
+  | s1 == s2  = Node s2 (leaf || null ss) this_pkg this_srcPkg this_short (addToTrees ss pkg srcPkg short subs) : ts
+  | otherwise = mkSubTree (s1:ss) pkg srcPkg short ++ t : ts
  where
   this_pkg = if null ss then pkg else node_pkg
+  this_srcPkg = if null ss then srcPkg else node_srcPkg
   this_short = if null ss then short else node_short
 
 
-mkSubTree :: [String] -> Maybe String -> Maybe (MDoc Name) -> [ModuleTree]
-mkSubTree []     _   _     = []
-mkSubTree [s]    pkg short = [Node s True pkg short []]
-mkSubTree (s:ss) pkg short = [Node s (null ss) Nothing Nothing (mkSubTree ss pkg short)]
+mkSubTree :: [String] -> Maybe String -> Maybe String -> Maybe (MDoc Name) -> [ModuleTree]
+mkSubTree []     _   _      _     = []
+mkSubTree [s]    pkg srcPkg short = [Node s True pkg srcPkg short []]
+mkSubTree (s:ss) pkg srcPkg short = [Node s (null ss) Nothing Nothing Nothing (mkSubTree ss pkg srcPkg short)]
 
 
 splitModule :: Module -> [String]
diff --git a/haddock-api/src/Haddock/Types.hs b/haddock-api/src/Haddock/Types.hs
--- a/haddock-api/src/Haddock/Types.hs
+++ b/haddock-api/src/Haddock/Types.hs
@@ -300,7 +300,7 @@
   ppr (DataInst  a) = text "DataInst"  <+> ppr a
 
 -- | An instance head that may have documentation and a source location.
-type DocInstance name = (Located (InstHead name), Maybe (MDoc name))
+type DocInstance name = (InstHead name, Maybe (MDoc name), Located name)
 
 -- | The head of an instance. Consists of a class name, a list of kind
 -- parameters, a list of type parameters and an instance type
diff --git a/haddock-library/src/Documentation/Haddock/Parser.hs b/haddock-library/src/Documentation/Haddock/Parser.hs
--- a/haddock-library/src/Documentation/Haddock/Parser.hs
+++ b/haddock-library/src/Documentation/Haddock/Parser.hs
@@ -231,18 +231,20 @@
 
 -- | Paragraph parser, called by 'parseParas'.
 paragraph :: Parser (DocH mod Identifier)
-paragraph = examples <|> skipSpace *> (
-      since
-  <|> unorderedList
-  <|> orderedList
-  <|> birdtracks
-  <|> codeblock
-  <|> property
-  <|> header
-  <|> textParagraphThatStartsWithMarkdownLink
-  <|> definitionList
-  <|> docParagraph <$> textParagraph
-  )
+paragraph = examples <|> do
+  indent <- takeIndent
+  choice
+    [ since
+    , unorderedList indent
+    , orderedList indent
+    , birdtracks
+    , codeblock
+    , property
+    , header
+    , textParagraphThatStartsWithMarkdownLink
+    , definitionList indent
+    , docParagraph <$> textParagraph
+    ]
 
 since :: Parser (DocH mod a)
 since = ("@since " *> version <* skipHorizontalSpace <* endOfLine) >>= setSince >> return DocEmpty
@@ -283,16 +285,16 @@
           | otherwise = " "
 
 -- | Parses unordered (bullet) lists.
-unorderedList :: Parser (DocH mod Identifier)
-unorderedList = DocUnorderedList <$> p
+unorderedList :: BS.ByteString -> Parser (DocH mod Identifier)
+unorderedList indent = DocUnorderedList <$> p
   where
-    p = ("*" <|> "-") *> innerList p
+    p = ("*" <|> "-") *> innerList indent p
 
 -- | Parses ordered lists (numbered or dashed).
-orderedList :: Parser (DocH mod Identifier)
-orderedList = DocOrderedList <$> p
+orderedList :: BS.ByteString -> Parser (DocH mod Identifier)
+orderedList indent = DocOrderedList <$> p
   where
-    p = (paren <|> dot) *> innerList p
+    p = (paren <|> dot) *> innerList indent p
     dot = (decimal :: Parser Int) <* "."
     paren = "(" *> decimal <* ")"
 
@@ -301,23 +303,24 @@
 -- same paragraph. Usually used as
 --
 -- > someListFunction = listBeginning *> innerList someListFunction
-innerList :: Parser [DocH mod Identifier] -> Parser [DocH mod Identifier]
-innerList item = do
+innerList :: BS.ByteString -> Parser [DocH mod Identifier]
+          -> Parser [DocH mod Identifier]
+innerList indent item = do
   c <- takeLine
-  (cs, items) <- more item
+  (cs, items) <- more indent item
   let contents = docParagraph . parseString . dropNLs . unlines $ c : cs
   return $ case items of
     Left p -> [contents `docAppend` p]
     Right i -> contents : i
 
 -- | Parses definition lists.
-definitionList :: Parser (DocH mod Identifier)
-definitionList = DocDefList <$> p
+definitionList :: BS.ByteString -> Parser (DocH mod Identifier)
+definitionList indent = DocDefList <$> p
   where
     p = do
       label <- "[" *> (parseStringBS <$> takeWhile1 (`notElem` ("]\n" :: String))) <* ("]" <* optional ":")
       c <- takeLine
-      (cs, items) <- more p
+      (cs, items) <- more indent p
       let contents = parseString . dropNLs . unlines $ c : cs
       return $ case items of
         Left x -> [(label, contents `docAppend` x)]
@@ -330,32 +333,40 @@
 -- | Main worker for 'innerList' and 'definitionList'.
 -- We need the 'Either' here to be able to tell in the respective functions
 -- whether we're dealing with the next list or a nested paragraph.
-more :: Monoid a => Parser a
+more :: Monoid a => BS.ByteString -> Parser a
      -> Parser ([String], Either (DocH mod Identifier) a)
-more item = innerParagraphs <|> moreListItems item
-            <|> moreContent item <|> pure ([], Right mempty)
+more indent item = innerParagraphs indent
+               <|> moreListItems indent item
+               <|> moreContent indent item
+               <|> pure ([], Right mempty)
 
 -- | Used by 'innerList' and 'definitionList' to parse any nested paragraphs.
-innerParagraphs :: Parser ([String], Either (DocH mod Identifier) a)
-innerParagraphs = (,) [] . Left <$> ("\n" *> indentedParagraphs)
+innerParagraphs :: BS.ByteString
+                -> Parser ([String], Either (DocH mod Identifier) a)
+innerParagraphs indent = (,) [] . Left <$> ("\n" *> indentedParagraphs indent)
 
 -- | Attempts to fetch the next list if possibly. Used by 'innerList' and
 -- 'definitionList' to recursively grab lists that aren't separated by a whole
 -- paragraph.
-moreListItems :: Parser a
+moreListItems :: BS.ByteString -> Parser a
               -> Parser ([String], Either (DocH mod Identifier) a)
-moreListItems item = (,) [] . Right <$> (skipSpace *> item)
+moreListItems indent item = (,) [] . Right <$> indentedItem
+  where
+    indentedItem = string indent *> skipSpace *> item
 
 -- | Helper for 'innerList' and 'definitionList' which simply takes
 -- a line of text and attempts to parse more list content with 'more'.
-moreContent :: Monoid a => Parser a
+moreContent :: Monoid a => BS.ByteString -> Parser a
             -> Parser ([String], Either (DocH mod Identifier) a)
-moreContent item = first . (:) <$> nonEmptyLine <*> more item
+moreContent indent item = first . (:) <$> nonEmptyLine <*> more indent item
 
 -- | Parses an indented paragraph.
 -- The indentation is 4 spaces.
-indentedParagraphs :: Parser (DocH mod Identifier)
-indentedParagraphs = (concat <$> dropFrontOfPara "    ") >>= parseParagraphs
+indentedParagraphs :: BS.ByteString -> Parser (DocH mod Identifier)
+indentedParagraphs indent =
+    (concat <$> dropFrontOfPara indent') >>= parseParagraphs
+  where
+    indent' = string $ BS.append indent "    "
 
 -- | Grab as many fully indented paragraphs as we can.
 dropFrontOfPara :: Parser BS.ByteString -> Parser [String]
@@ -381,6 +392,15 @@
 takeNonEmptyLine :: Parser String
 takeNonEmptyLine = do
     (++ "\n") . decodeUtf8 <$> (takeWhile1 (/= '\n') >>= nonSpace) <* "\n"
+
+-- | Takes indentation of first non-empty line.
+--
+-- More precisely: skips all whitespace-only lines and returns indentation
+-- (horizontal space, might be empty) of that non-empty line.
+takeIndent :: Parser BS.ByteString
+takeIndent = do
+  indent <- takeHorizontalSpace
+  "\n" *> takeIndent <|> return indent
 
 -- | Blocks of text of the form:
 --
diff --git a/haddock.cabal b/haddock.cabal
--- a/haddock.cabal
+++ b/haddock.cabal
@@ -1,5 +1,5 @@
 name:                 haddock
-version:              2.16.0
+version:              2.16.1
 synopsis:             A documentation-generation tool for Haskell libraries
 description:          Haddock is a documentation-generation tool for Haskell
                       libraries
@@ -42,7 +42,7 @@
   default-language:     Haskell2010
   main-is:              Main.hs
   hs-source-dirs:       driver
-  ghc-options:          -funbox-strict-fields -Wall -fwarn-tabs -O2
+  ghc-options:          -funbox-strict-fields -Wall -fwarn-tabs -O2 -threaded
 
   build-depends:
     base >= 4.3 && < 4.9
@@ -57,7 +57,7 @@
       array,
       xhtml >= 3000.2 && < 3000.3,
       Cabal >= 1.10,
-      ghc >= 7.9 && < 7.11,
+      ghc >= 7.9 && < 7.12,
       bytestring,
       transformers
 
@@ -110,7 +110,7 @@
       Haddock.GhcUtils
       Haddock.Convert
   else
-    build-depends:  haddock-api == 2.16.0
+    build-depends:  haddock-api == 2.16.*
 
 test-suite html-test
   type:             exitcode-stdio-1.0
diff --git a/html-test/ref/Bug253.html b/html-test/ref/Bug253.html
new file mode 100644
--- /dev/null
+++ b/html-test/ref/Bug253.html
@@ -0,0 +1,99 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+><head
+  ><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
+     /><title
+    >Bug253</title
+    ><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean"
+     /><script src="haddock-util.js" type="text/javascript"
+    ></script
+    ><script type="text/javascript"
+    >//<![CDATA[
+window.onload = function () {pageLoad();setSynopsis("mini_Bug253.html");};
+//]]>
+</script
+    ></head
+  ><body
+  ><div id="package-header"
+    ><ul class="links" id="page-menu"
+      ><li
+	><a href="index.html"
+	  >Contents</a
+	  ></li
+	><li
+	><a href="doc-index.html"
+	  >Index</a
+	  ></li
+	></ul
+      ><p class="caption empty"
+      >&nbsp;</p
+      ></div
+    ><div id="content"
+    ><div id="module-header"
+      ><table class="info"
+	><tr
+	  ><th
+	    >Safe Haskell</th
+	    ><td
+	    >Safe</td
+	    ></tr
+	  ></table
+	><p class="caption"
+	>Bug253</p
+	></div
+      ><div id="description"
+      ><p class="caption"
+	>Description</p
+	><div class="doc"
+	><p
+	  >This module tests that if we're trying to link to a <em
+	    >qualified</em
+	    >
+ identifier that's not in scope, we get an anchor as if it was a
+ variable. Previous behaviour was to treat it as a type constructor
+ so issue like #253 arose. Also see <code
+	    >rename</code
+	    > function comments in
+ source.</p
+	  ></div
+	></div
+      ><div id="synopsis"
+      ><p id="control.syn" class="caption expander" onclick="toggleSection('syn')"
+	>Synopsis</p
+	><ul id="section.syn" class="hide" onclick="toggleSection('syn')"
+	><li class="src short"
+	  ><a href="#v:foo"
+	    >foo</a
+	    > :: ()</li
+	  ></ul
+	></div
+      ><div id="interface"
+      ><h1
+	>Documentation</h1
+	><div class="top"
+	><p class="src"
+	  ><a name="v:foo" class="def"
+	    >foo</a
+	    > :: ()</p
+	  ><div class="doc"
+	  ><p
+	    >This link should generate <code
+	      >#v</code
+	      > anchor: <code
+	      ><a href="DoesNotExist.html#v:fakeFakeFake"
+		>fakeFakeFake</a
+		></code
+	      ></p
+	    ></div
+	  ></div
+	></div
+      ></div
+    ><div id="footer"
+    ><p
+      >Produced by <a href="http://www.haskell.org/haddock/"
+	>Haddock</a
+	> version 2.16.0</p
+      ></div
+    ></body
+  ></html
+>
diff --git a/html-test/ref/Bug26.html b/html-test/ref/Bug26.html
--- a/html-test/ref/Bug26.html
+++ b/html-test/ref/Bug26.html
@@ -145,10 +145,12 @@
 	    ><div id="section.i:C" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >C</a
-		    > ()</td
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >C</a
+		      > ()</span
+		    ></td
 		  ><td class="doc"
 		  ><p
 		    >instance for ()</p
@@ -168,7 +170,7 @@
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.15.1</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/ref/Bug294.html b/html-test/ref/Bug294.html
--- a/html-test/ref/Bug294.html
+++ b/html-test/ref/Bug294.html
@@ -57,31 +57,35 @@
 	    ><div id="section.i:A" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >DP</a
-		    > <a href=""
-		    >A</a
-		    > = <a name="v:ProblemCtor-39-" class="def"
-		    >ProblemCtor'</a
-		    > <a href=""
-		    >A</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >DP</a
+		      > <a href=""
+		      >A</a
+		      > = <a name="v:ProblemCtor-39-" class="def"
+		      >ProblemCtor'</a
+		      > <a href=""
+		      >A</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > TP <a href=""
-		    >A</a
-		    > = <a name="v:ProblemCtor" class="def"
-		    >ProblemCtor</a
-		    > <a href=""
-		    >A</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > TP <a href=""
+		      >A</a
+		      > = <a name="v:ProblemCtor" class="def"
+		      >ProblemCtor</a
+		      > <a href=""
+		      >A</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
@@ -133,17 +137,19 @@
 	    ><div id="section.i:DP" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >DP</a
-		    > <a href=""
-		    >A</a
-		    > = <a name="v:ProblemCtor-39-" class="def"
-		    >ProblemCtor'</a
-		    > <a href=""
-		    >A</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >DP</a
+		      > <a href=""
+		      >A</a
+		      > = <a name="v:ProblemCtor-39-" class="def"
+		      >ProblemCtor'</a
+		      > <a href=""
+		      >A</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
@@ -158,7 +164,7 @@
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.15.0</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/ref/Bug387.html b/html-test/ref/Bug387.html
new file mode 100644
--- /dev/null
+++ b/html-test/ref/Bug387.html
@@ -0,0 +1,111 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+><head
+  ><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
+     /><title
+    >Bug387</title
+    ><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean"
+     /><script src="haddock-util.js" type="text/javascript"
+    ></script
+    ><script type="text/javascript"
+    >//<![CDATA[
+window.onload = function () {pageLoad();setSynopsis("mini_Bug387.html");};
+//]]>
+</script
+    ></head
+  ><body
+  ><div id="package-header"
+    ><ul class="links" id="page-menu"
+      ><li
+	><a href=""
+	  >Contents</a
+	  ></li
+	><li
+	><a href=""
+	  >Index</a
+	  ></li
+	></ul
+      ><p class="caption empty"
+      >&nbsp;</p
+      ></div
+    ><div id="content"
+    ><div id="module-header"
+      ><table class="info"
+	><tr
+	  ><th
+	    >Safe Haskell</th
+	    ><td
+	    >Safe</td
+	    ></tr
+	  ></table
+	><p class="caption"
+	>Bug387</p
+	></div
+      ><div id="table-of-contents"
+      ><p class="caption"
+	>Contents</p
+	><ul
+	><li
+	  ><a href=""
+	    >Section1</a
+	    ></li
+	  ><li
+	  ><a href=""
+	    >Section2</a
+	    ></li
+	  ></ul
+	></div
+      ><div id="synopsis"
+      ><p id="control.syn" class="caption expander" onclick="toggleSection('syn')"
+	>Synopsis</p
+	><ul id="section.syn" class="hide" onclick="toggleSection('syn')"
+	><li class="src short"
+	  ><a href=""
+	    >test1</a
+	    > :: <a href=""
+	    >Int</a
+	    ></li
+	  ><li class="src short"
+	  ><a href=""
+	    >test2</a
+	    > :: <a href=""
+	    >Int</a
+	    ></li
+	  ></ul
+	></div
+      ><div id="interface"
+      ><h1 id="g:1"
+	>Section1<a name="a:section1"
+	  ></a
+	  ></h1
+	><div class="top"
+	><p class="src"
+	  ><a name="v:test1" class="def"
+	    >test1</a
+	    > :: <a href=""
+	    >Int</a
+	    ></p
+	  ></div
+	><h1 id="g:2"
+	>Section2<a name="a:section2"
+	  ></a
+	  ></h1
+	><div class="top"
+	><p class="src"
+	  ><a name="v:test2" class="def"
+	    >test2</a
+	    > :: <a href=""
+	    >Int</a
+	    ></p
+	  ></div
+	></div
+      ></div
+    ><div id="footer"
+    ><p
+      >Produced by <a href=""
+	>Haddock</a
+	> version 2.16.1</p
+      ></div
+    ></body
+  ></html
+>
diff --git a/html-test/ref/Bug7.html b/html-test/ref/Bug7.html
--- a/html-test/ref/Bug7.html
+++ b/html-test/ref/Bug7.html
@@ -104,13 +104,15 @@
 	    ><div id="section.i:Foo" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Bar</a
-		    > <a href=""
-		    >Foo</a
-		    > <a href=""
-		    >Foo</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Bar</a
+		      > <a href=""
+		      >Foo</a
+		      > <a href=""
+		      >Foo</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -138,13 +140,15 @@
 	    ><div id="section.i:Bar" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Bar</a
-		    > <a href=""
-		    >Foo</a
-		    > <a href=""
-		    >Foo</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Bar</a
+		      > <a href=""
+		      >Foo</a
+		      > <a href=""
+		      >Foo</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -161,7 +165,7 @@
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.15.0</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/ref/Hash.html b/html-test/ref/Hash.html
--- a/html-test/ref/Hash.html
+++ b/html-test/ref/Hash.html
@@ -276,34 +276,40 @@
 	    ><div id="section.i:Hash" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Hash</a
-		    > <a href=""
-		    >Float</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Hash</a
+		      > <a href=""
+		      >Float</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><a href=""
-		    >Hash</a
-		    > <a href=""
-		    >Int</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Hash</a
+		      > <a href=""
+		      >Int</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  >(<a href=""
-		    >Hash</a
-		    > a, <a href=""
-		    >Hash</a
-		    > b) =&gt; <a href=""
-		    >Hash</a
-		    > (a, b)</td
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    >(<a href=""
+		      >Hash</a
+		      > a, <a href=""
+		      >Hash</a
+		      > b) =&gt; <a href=""
+		      >Hash</a
+		      > (a, b)</span
+		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
@@ -317,7 +323,7 @@
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.15.0</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/ref/HiddenInstances.html b/html-test/ref/HiddenInstances.html
--- a/html-test/ref/HiddenInstances.html
+++ b/html-test/ref/HiddenInstances.html
@@ -79,11 +79,13 @@
 	    ><div id="section.i:VisibleClass" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >VisibleClass</a
-		    > <a href=""
-		    >Int</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >VisibleClass</a
+		      > <a href=""
+		      >Int</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -91,11 +93,13 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><a href=""
-		    >VisibleClass</a
-		    > <a href=""
-		    >VisibleData</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >VisibleClass</a
+		      > <a href=""
+		      >VisibleData</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -123,11 +127,13 @@
 	    ><div id="section.i:VisibleData" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Num</a
-		    > <a href=""
-		    >VisibleData</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Num</a
+		      > <a href=""
+		      >VisibleData</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -135,11 +141,13 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><a href=""
-		    >VisibleClass</a
-		    > <a href=""
-		    >VisibleData</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >VisibleClass</a
+		      > <a href=""
+		      >VisibleData</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -156,7 +164,7 @@
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.15.0</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/ref/HiddenInstancesB.html b/html-test/ref/HiddenInstancesB.html
--- a/html-test/ref/HiddenInstancesB.html
+++ b/html-test/ref/HiddenInstancesB.html
@@ -79,11 +79,13 @@
 	    ><div id="section.i:Foo" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Foo</a
-		    > <a href=""
-		    >Bar</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Foo</a
+		      > <a href=""
+		      >Bar</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -111,11 +113,13 @@
 	    ><div id="section.i:Bar" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Foo</a
-		    > <a href=""
-		    >Bar</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Foo</a
+		      > <a href=""
+		      >Bar</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -132,7 +136,7 @@
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.15.0</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/ref/Nesting.html b/html-test/ref/Nesting.html
--- a/html-test/ref/Nesting.html
+++ b/html-test/ref/Nesting.html
@@ -73,6 +73,10 @@
 	  ><a href=""
 	    >j</a
 	    > :: t</li
+	  ><li class="src short"
+	  ><a href=""
+	    >k</a
+	    > :: t</li
 	  ></ul
 	></div
       ><div id="interface"
@@ -285,16 +289,16 @@
 			  ><dd
 			  >No newline separation even in indented lists.
         We can have any paragraph level element that we normally
-        can, like headers<h3
-			    >Level 3 header</h3
-			    ><p
-			    >with some content&#8230;</p
-			    ><ul
-			    ><li
-			      >and even more lists inside</li
-			      ></ul
-			    ></dd
+        can, like headers</dd
 			  ></dl
+			><h3
+			>Level 3 header</h3
+			><p
+			>with some content&#8230;</p
+			><ul
+			><li
+			  >and even more lists inside</li
+			  ></ul
 			></li
 		      ></ol
 		    ></li
@@ -303,13 +307,38 @@
 	      ></dl
 	    ></div
 	  ></div
+	><div class="top"
+	><p class="src"
+	  ><a name="v:k" class="def"
+	    >k</a
+	    > :: t</p
+	  ><div class="doc"
+	  ><ul
+	    ><li
+	      >list may start at arbitrary depth</li
+	      ><li
+	      >and consecutive items at that depth
+      belong to the same list</li
+	      ><li
+	      ><p
+		>of course we can still</p
+		><ul
+		><li
+		  >nest items like we are used to</li
+		  ></ul
+		></li
+	      ><li
+	      >and then get back to initial list</li
+	      ></ul
+	    ></div
+	  ></div
 	></div
       ></div
     ><div id="footer"
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.15.0</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/ref/QuasiExpr.html b/html-test/ref/QuasiExpr.html
--- a/html-test/ref/QuasiExpr.html
+++ b/html-test/ref/QuasiExpr.html
@@ -107,11 +107,13 @@
 	    ><div id="section.i:Expr" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Show</a
-		    > <a href=""
-		    >Expr</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Show</a
+		      > <a href=""
+		      >Expr</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
@@ -171,11 +173,13 @@
 	    ><div id="section.i:BinOp" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Show</a
-		    > <a href=""
-		    >BinOp</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Show</a
+		      > <a href=""
+		      >BinOp</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
@@ -214,7 +218,7 @@
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.15.0</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/ref/SpuriousSuperclassConstraints.html b/html-test/ref/SpuriousSuperclassConstraints.html
--- a/html-test/ref/SpuriousSuperclassConstraints.html
+++ b/html-test/ref/SpuriousSuperclassConstraints.html
@@ -83,24 +83,28 @@
 	    ><div id="section.i:SomeType" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Functor</a
-		    > (<a href=""
-		    >SomeType</a
-		    > f)</td
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Functor</a
+		      > (<a href=""
+		      >SomeType</a
+		      > f)</span
+		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><a href=""
-		    >Applicative</a
-		    > f =&gt; <a href=""
-		    >Applicative</a
-		    > (<a href=""
-		    >SomeType</a
-		    > f)</td
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Applicative</a
+		      > f =&gt; <a href=""
+		      >Applicative</a
+		      > (<a href=""
+		      >SomeType</a
+		      > f)</span
+		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
@@ -114,7 +118,7 @@
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.15.0</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/ref/Test.html b/html-test/ref/Test.html
--- a/html-test/ref/Test.html
+++ b/html-test/ref/Test.html
@@ -1519,21 +1519,25 @@
 	    ><div id="section.i:D" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >D</a
-		    > <a href=""
-		    >Float</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >D</a
+		      > <a href=""
+		      >Float</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><a href=""
-		    >D</a
-		    > <a href=""
-		    >Int</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >D</a
+		      > <a href=""
+		      >Int</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
@@ -2112,7 +2116,7 @@
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.16.0</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/ref/Threaded.html b/html-test/ref/Threaded.html
new file mode 100644
--- /dev/null
+++ b/html-test/ref/Threaded.html
@@ -0,0 +1,94 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+><head
+  ><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
+     /><title
+    >Threaded</title
+    ><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean"
+     /><script src="haddock-util.js" type="text/javascript"
+    ></script
+    ><script type="text/javascript"
+    >//<![CDATA[
+window.onload = function () {pageLoad();setSynopsis("mini_Threaded.html");};
+//]]>
+</script
+    ></head
+  ><body
+  ><div id="package-header"
+    ><ul class="links" id="page-menu"
+      ><li
+	><a href="index.html"
+	  >Contents</a
+	  ></li
+	><li
+	><a href="doc-index.html"
+	  >Index</a
+	  ></li
+	></ul
+      ><p class="caption empty"
+      >&nbsp;</p
+      ></div
+    ><div id="content"
+    ><div id="module-header"
+      ><table class="info"
+	><tr
+	  ><th
+	    >Safe Haskell</th
+	    ><td
+	    >None</td
+	    ></tr
+	  ></table
+	><p class="caption"
+	>Threaded</p
+	></div
+      ><div id="description"
+      ><p class="caption"
+	>Description</p
+	><div class="doc"
+	><p
+	  >Ensures haddock built with <code
+	    >-threaded</code
+	    >.</p
+	  ></div
+	></div
+      ><div id="synopsis"
+      ><p id="control.syn" class="caption expander" onclick="toggleSection('syn')"
+	>Synopsis</p
+	><ul id="section.syn" class="hide" onclick="toggleSection('syn')"
+	><li class="src short"
+	  ><a href="#v:f"
+	    >f</a
+	    > :: <a href=" ${pkgroot}/../../share/doc/ghc/html/libraries/base-4.8.0.0/Prelude.html#t:Integer"
+	    >Integer</a
+	    ></li
+	  ></ul
+	></div
+      ><div id="interface"
+      ><h1
+	>Documentation</h1
+	><div class="top"
+	><p class="src"
+	  ><a name="v:f" class="def"
+	    >f</a
+	    > :: <a href=" ${pkgroot}/../../share/doc/ghc/html/libraries/base-4.8.0.0/Prelude.html#t:Integer"
+	    >Integer</a
+	    ></p
+	  ><div class="doc"
+	  ><p
+	    ><code
+	      >$(forkTH)</code
+	      > fails at compile time if haddock isn't using the
+ threaded RTS.</p
+	    ></div
+	  ></div
+	></div
+      ></div
+    ><div id="footer"
+    ><p
+      >Produced by <a href="http://www.haskell.org/haddock/"
+	>Haddock</a
+	> version 2.16.0</p
+      ></div
+    ></body
+  ></html
+>
diff --git a/html-test/ref/Ticket253_1.html b/html-test/ref/Ticket253_1.html
deleted file mode 100644
--- a/html-test/ref/Ticket253_1.html
+++ /dev/null
@@ -1,91 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml"
-><head
-  ><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
-     /><title
-    >Ticket253_1</title
-    ><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean"
-     /><script src="haddock-util.js" type="text/javascript"
-    ></script
-    ><script type="text/javascript"
-    >//<![CDATA[
-window.onload = function () {pageLoad();setSynopsis("mini_Ticket253_1.html");};
-//]]>
-</script
-    ></head
-  ><body
-  ><div id="package-header"
-    ><ul class="links" id="page-menu"
-      ><li
-	><a href=""
-	  >Contents</a
-	  ></li
-	><li
-	><a href=""
-	  >Index</a
-	  ></li
-	></ul
-      ><p class="caption empty"
-      >&nbsp;</p
-      ></div
-    ><div id="content"
-    ><div id="module-header"
-      ><table class="info"
-	><tr
-	  ><th
-	    >Safe Haskell</th
-	    ><td
-	    >Safe</td
-	    ></tr
-	  ></table
-	><p class="caption"
-	>Ticket253_1</p
-	></div
-      ><div id="synopsis"
-      ><p id="control.syn" class="caption expander" onclick="toggleSection('syn')"
-	>Synopsis</p
-	><ul id="section.syn" class="hide" onclick="toggleSection('syn')"
-	><li class="src short"
-	  ><a href=""
-	    >foo</a
-	    > :: <a href=""
-	    >Int</a
-	    ></li
-	  ></ul
-	></div
-      ><div id="interface"
-      ><h1
-	>Documentation</h1
-	><div class="top"
-	><p class="src"
-	  ><a name="v:foo" class="def"
-	    >foo</a
-	    > :: <a href=""
-	    >Int</a
-	    ></p
-	  ><div class="doc"
-	  ><p
-	    >See <code
-	      ><a href=""
-		>bar</a
-		></code
-	      >.</p
-	    ><p
-	    >Also see <code
-	      ><a href=""
-		>Baz</a
-		></code
-	      ></p
-	    ></div
-	  ></div
-	></div
-      ></div
-    ><div id="footer"
-    ><p
-      >Produced by <a href=""
-	>Haddock</a
-	> version 2.15.0</p
-      ></div
-    ></body
-  ></html
->
diff --git a/html-test/ref/Ticket253_2.html b/html-test/ref/Ticket253_2.html
deleted file mode 100644
--- a/html-test/ref/Ticket253_2.html
+++ /dev/null
@@ -1,111 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml"
-><head
-  ><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
-     /><title
-    >Ticket253_2</title
-    ><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean"
-     /><script src="haddock-util.js" type="text/javascript"
-    ></script
-    ><script type="text/javascript"
-    >//<![CDATA[
-window.onload = function () {pageLoad();setSynopsis("mini_Ticket253_2.html");};
-//]]>
-</script
-    ></head
-  ><body
-  ><div id="package-header"
-    ><ul class="links" id="page-menu"
-      ><li
-	><a href=""
-	  >Contents</a
-	  ></li
-	><li
-	><a href=""
-	  >Index</a
-	  ></li
-	></ul
-      ><p class="caption empty"
-      >&nbsp;</p
-      ></div
-    ><div id="content"
-    ><div id="module-header"
-      ><table class="info"
-	><tr
-	  ><th
-	    >Safe Haskell</th
-	    ><td
-	    >Safe</td
-	    ></tr
-	  ></table
-	><p class="caption"
-	>Ticket253_2</p
-	></div
-      ><div id="synopsis"
-      ><p id="control.syn" class="caption expander" onclick="toggleSection('syn')"
-	>Synopsis</p
-	><ul id="section.syn" class="hide" onclick="toggleSection('syn')"
-	><li class="src short"
-	  ><a href=""
-	    >bar</a
-	    > :: <a href=""
-	    >Int</a
-	    ></li
-	  ><li class="src short"
-	  ><span class="keyword"
-	    >data</span
-	    > <a href=""
-	    >Baz</a
-	    > = <a href=""
-	    >Baz</a
-	    ></li
-	  ></ul
-	></div
-      ><div id="interface"
-      ><h1
-	>Documentation</h1
-	><div class="top"
-	><p class="src"
-	  ><a name="v:bar" class="def"
-	    >bar</a
-	    > :: <a href=""
-	    >Int</a
-	    ></p
-	  ><div class="doc"
-	  ><p
-	    >Comment</p
-	    ></div
-	  ></div
-	><div class="top"
-	><p class="src"
-	  ><span class="keyword"
-	    >data</span
-	    > <a name="t:Baz" class="def"
-	    >Baz</a
-	    ></p
-	  ><div class="subs constructors"
-	  ><p class="caption"
-	    >Constructors</p
-	    ><table
-	    ><tr
-	      ><td class="src"
-		><a name="v:Baz" class="def"
-		  >Baz</a
-		  ></td
-		><td class="doc empty"
-		>&nbsp;</td
-		></tr
-	      ></table
-	    ></div
-	  ></div
-	></div
-      ></div
-    ><div id="footer"
-    ><p
-      >Produced by <a href=""
-	>Haddock</a
-	> version 2.15.0</p
-      ></div
-    ></body
-  ></html
->
diff --git a/html-test/ref/TypeFamilies.html b/html-test/ref/TypeFamilies.html
--- a/html-test/ref/TypeFamilies.html
+++ b/html-test/ref/TypeFamilies.html
@@ -211,11 +211,13 @@
 	    ><div id="section.i:X" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Assoc</a
-		    > * <a href=""
-		    >X</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Assoc</a
+		      > * <a href=""
+		      >X</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -223,11 +225,13 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><a href=""
-		    >Test</a
-		    > * <a href=""
-		    >X</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Test</a
+		      > * <a href=""
+		      >X</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -235,29 +239,33 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><a href=""
-		    >(&gt;&lt;)</a
-		    > <a href=""
-		    >X</a
-		    > <a href=""
-		    >XX</a
-		    > <a href=""
-		    >XXX</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >(&gt;&lt;)</a
+		      > <a href=""
+		      >X</a
+		      > <a href=""
+		      >XX</a
+		      > <a href=""
+		      >XXX</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >Foo</a
-		    > <a href=""
-		    >X</a
-		    > = <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >Foo</a
+		      > <a href=""
+		      >X</a
+		      > = <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -265,69 +273,75 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >AssocD</a
-		    > * <a href=""
-		    >X</a
-		    > = <a name="v:AssocX" class="def"
-		    >AssocX</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >AssocD</a
+		      > * <a href=""
+		      >X</a
+		      > = <a name="v:AssocX" class="def"
+		      >AssocX</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >AssocT</a
-		    > * <a href=""
-		    >X</a
-		    > = <a href=""
-		    >Foo</a
-		    > * <a href=""
-		    >X</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >AssocT</a
+		      > * <a href=""
+		      >X</a
+		      > = <a href=""
+		      >Foo</a
+		      > * <a href=""
+		      >X</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >Bat</a
-		    > * <a href=""
-		    >X</a
-		    > <ul class="inst"
-		    ><li class="inst"
-		      >= <a name="v:BatX" class="def"
-			>BatX</a
-			> <a href=""
-			>X</a
-			></li
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >Bat</a
+		      > * <a href=""
+		      >X</a
+		      > <ul class="inst"
 		      ><li class="inst"
-		      >| <a name="v:BatXX" class="def"
-			>BatXX</a
-			> { <ul class="subs"
-			><li
-			  ><a name="v:aaa" class="def"
-			    >aaa</a
-			    > :: <a href=""
-			    >X</a
-			    ></li
+			>= <a name="v:BatX" class="def"
+			  >BatX</a
+			  > <a href=""
+			  >X</a
+			  ></li
+			><li class="inst"
+			>| <a name="v:BatXX" class="def"
+			  >BatXX</a
+			  > { <ul class="subs"
 			  ><li
-			  ><a name="v:bbb" class="def"
-			    >bbb</a
-			    > :: <a href=""
-			    >Y</a
-			    ></li
-			  ></ul
-			> }</li
-		      ></ul
+			    ><a name="v:aaa" class="def"
+			      >aaa</a
+			      > :: <a href=""
+			      >X</a
+			      ></li
+			    ><li
+			    ><a name="v:bbb" class="def"
+			      >bbb</a
+			      > :: <a href=""
+			      >Y</a
+			      ></li
+			    ></ul
+			  > }</li
+			></ul
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -335,15 +349,17 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >Foo</a
-		    > * <a href=""
-		    >X</a
-		    > = <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >Foo</a
+		      > * <a href=""
+		      >X</a
+		      > = <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -351,33 +367,37 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >(&lt;&gt;)</a
-		    > * <a href=""
-		    >X</a
-		    > a = <a href=""
-		    >X</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >(&lt;&gt;)</a
+		      > * <a href=""
+		      >X</a
+		      > a = <a href=""
+		      >X</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >(&lt;&gt;)</a
-		    > <a href=""
-		    >X</a
-		    > <a href=""
-		    >XXX</a
-		    > <a href=""
-		    >XX</a
-		    > = <a href=""
-		    >X</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >(&lt;&gt;)</a
+		      > <a href=""
+		      >X</a
+		      > <a href=""
+		      >XXX</a
+		      > <a href=""
+		      >XX</a
+		      > = <a href=""
+		      >X</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
@@ -403,11 +423,13 @@
 	    ><div id="section.i:Y" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Assoc</a
-		    > * <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Assoc</a
+		      > * <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -415,11 +437,13 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><a href=""
-		    >Test</a
-		    > * <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Test</a
+		      > * <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -427,59 +451,67 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >Bar</a
-		    > <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >Bar</a
+		      > <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >AssocD</a
-		    > * <a href=""
-		    >Y</a
-		    > = <a name="v:AssocY" class="def"
-		    >AssocY</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >AssocD</a
+		      > * <a href=""
+		      >Y</a
+		      > = <a name="v:AssocY" class="def"
+		      >AssocY</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >AssocT</a
-		    > * <a href=""
-		    >Y</a
-		    > = <a href=""
-		    >Bat</a
-		    > * <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >AssocT</a
+		      > * <a href=""
+		      >Y</a
+		      > = <a href=""
+		      >Bat</a
+		      > * <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >Bat</a
-		    > * <a href=""
-		    >Y</a
-		    > = <a name="v:BatY" class="def"
-		    >BatY</a
-		    > <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >Bat</a
+		      > * <a href=""
+		      >Y</a
+		      > = <a name="v:BatY" class="def"
+		      >BatY</a
+		      > <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -487,15 +519,17 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >Foo</a
-		    > * <a href=""
-		    >Y</a
-		    > = <a href=""
-		    >X</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >Foo</a
+		      > * <a href=""
+		      >Y</a
+		      > = <a href=""
+		      >X</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -503,14 +537,16 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >(&lt;&gt;)</a
-		    > * <a href=""
-		    >Y</a
-		    > a = a</td
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >(&lt;&gt;)</a
+		      > * <a href=""
+		      >Y</a
+		      > a = a</span
+		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
@@ -557,53 +593,55 @@
 	    ><div id="section.i:Z" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >Bat</a
-		    > <a href=""
-		    >Z</a
-		    > <span class="keyword"
-		    >where</span
-		    ><ul class="inst"
-		    ><li class="inst"
-		      ><a name="v:BatZ1" class="def"
-			>BatZ1</a
-			> ::  <a href=""
-			>Z</a
-			> -&gt; <a href=""
-			>Bat</a
-			> <a href=""
-			>Z</a
-			> <a href=""
-			>ZA</a
-			></li
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >Bat</a
+		      > <a href=""
+		      >Z</a
+		      > <span class="keyword"
+		      >where</span
+		      ><ul class="inst"
 		      ><li class="inst"
-		      ><a name="v:BatZ2" class="def"
-			>BatZ2</a
-			> :: { <ul class="subs"
-			><li
-			  ><a name="v:batx" class="def"
-			    >batx</a
-			    > :: <a href=""
-			    >X</a
-			    ></li
+			><a name="v:BatZ1" class="def"
+			  >BatZ1</a
+			  > ::  <a href=""
+			  >Z</a
+			  > -&gt; <a href=""
+			  >Bat</a
+			  > <a href=""
+			  >Z</a
+			  > <a href=""
+			  >ZA</a
+			  ></li
+			><li class="inst"
+			><a name="v:BatZ2" class="def"
+			  >BatZ2</a
+			  > :: { <ul class="subs"
 			  ><li
-			  ><a name="v:baty" class="def"
-			    >baty</a
-			    > :: <a href=""
-			    >Y</a
-			    ></li
-			  ></ul
-			> } -&gt; <a href=""
-			>Bat</a
-			> <a href=""
-			>Z</a
-			> <a href=""
-			>ZB</a
-			></li
-		      ></ul
+			    ><a name="v:batx" class="def"
+			      >batx</a
+			      > :: <a href=""
+			      >X</a
+			      ></li
+			    ><li
+			    ><a name="v:baty" class="def"
+			      >baty</a
+			      > :: <a href=""
+			      >Y</a
+			      ></li
+			    ></ul
+			  > } -&gt; <a href=""
+			  >Bat</a
+			  > <a href=""
+			  >Z</a
+			  > <a href=""
+			  >ZB</a
+			  ></li
+			></ul
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -631,11 +669,13 @@
 	    ><div id="section.i:Test" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Test</a
-		    > * <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Test</a
+		      > * <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -643,11 +683,13 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><a href=""
-		    >Test</a
-		    > * <a href=""
-		    >X</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Test</a
+		      > * <a href=""
+		      >X</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -675,15 +717,17 @@
 	    ><div id="section.i:Foo" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >Foo</a
-		    > * <a href=""
-		    >Y</a
-		    > = <a href=""
-		    >X</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >Foo</a
+		      > * <a href=""
+		      >Y</a
+		      > = <a href=""
+		      >X</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -691,15 +735,17 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >Foo</a
-		    > * <a href=""
-		    >X</a
-		    > = <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >Foo</a
+		      > * <a href=""
+		      >X</a
+		      > = <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -727,53 +773,55 @@
 	    ><div id="section.i:Bat" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >Bat</a
-		    > <a href=""
-		    >Z</a
-		    > <span class="keyword"
-		    >where</span
-		    ><ul class="inst"
-		    ><li class="inst"
-		      ><a name="v:BatZ1" class="def"
-			>BatZ1</a
-			> ::  <a href=""
-			>Z</a
-			> -&gt; <a href=""
-			>Bat</a
-			> <a href=""
-			>Z</a
-			> <a href=""
-			>ZA</a
-			></li
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >Bat</a
+		      > <a href=""
+		      >Z</a
+		      > <span class="keyword"
+		      >where</span
+		      ><ul class="inst"
 		      ><li class="inst"
-		      ><a name="v:BatZ2" class="def"
-			>BatZ2</a
-			> :: { <ul class="subs"
-			><li
-			  ><a name="v:batx" class="def"
-			    >batx</a
-			    > :: <a href=""
-			    >X</a
-			    ></li
+			><a name="v:BatZ1" class="def"
+			  >BatZ1</a
+			  > ::  <a href=""
+			  >Z</a
+			  > -&gt; <a href=""
+			  >Bat</a
+			  > <a href=""
+			  >Z</a
+			  > <a href=""
+			  >ZA</a
+			  ></li
+			><li class="inst"
+			><a name="v:BatZ2" class="def"
+			  >BatZ2</a
+			  > :: { <ul class="subs"
 			  ><li
-			  ><a name="v:baty" class="def"
-			    >baty</a
-			    > :: <a href=""
-			    >Y</a
-			    ></li
-			  ></ul
-			> } -&gt; <a href=""
-			>Bat</a
-			> <a href=""
-			>Z</a
-			> <a href=""
-			>ZB</a
-			></li
-		      ></ul
+			    ><a name="v:batx" class="def"
+			      >batx</a
+			      > :: <a href=""
+			      >X</a
+			      ></li
+			    ><li
+			    ><a name="v:baty" class="def"
+			      >baty</a
+			      > :: <a href=""
+			      >Y</a
+			      ></li
+			    ></ul
+			  > } -&gt; <a href=""
+			  >Bat</a
+			  > <a href=""
+			  >Z</a
+			  > <a href=""
+			  >ZB</a
+			  ></li
+			></ul
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -781,17 +829,19 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >Bat</a
-		    > * <a href=""
-		    >Y</a
-		    > = <a name="v:BatY" class="def"
-		    >BatY</a
-		    > <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >Bat</a
+		      > * <a href=""
+		      >Y</a
+		      > = <a name="v:BatY" class="def"
+		      >BatY</a
+		      > <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -799,39 +849,41 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >Bat</a
-		    > * <a href=""
-		    >X</a
-		    > <ul class="inst"
-		    ><li class="inst"
-		      >= <a name="v:BatX" class="def"
-			>BatX</a
-			> <a href=""
-			>X</a
-			></li
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >Bat</a
+		      > * <a href=""
+		      >X</a
+		      > <ul class="inst"
 		      ><li class="inst"
-		      >| <a name="v:BatXX" class="def"
-			>BatXX</a
-			> { <ul class="subs"
-			><li
-			  ><a name="v:aaa" class="def"
-			    >aaa</a
-			    > :: <a href=""
-			    >X</a
-			    ></li
+			>= <a name="v:BatX" class="def"
+			  >BatX</a
+			  > <a href=""
+			  >X</a
+			  ></li
+			><li class="inst"
+			>| <a name="v:BatXX" class="def"
+			  >BatXX</a
+			  > { <ul class="subs"
 			  ><li
-			  ><a name="v:bbb" class="def"
-			    >bbb</a
-			    > :: <a href=""
-			    >Y</a
-			    ></li
-			  ></ul
-			> }</li
-		      ></ul
+			    ><a name="v:aaa" class="def"
+			      >aaa</a
+			      > :: <a href=""
+			      >X</a
+			      ></li
+			    ><li
+			    ><a name="v:bbb" class="def"
+			      >bbb</a
+			      > :: <a href=""
+			      >Y</a
+			      ></li
+			    ></ul
+			  > }</li
+			></ul
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -883,11 +935,13 @@
 	    ><div id="section.i:Assoc" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >Assoc</a
-		    > * <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Assoc</a
+		      > * <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -895,11 +949,13 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><a href=""
-		    >Assoc</a
-		    > * <a href=""
-		    >X</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >Assoc</a
+		      > * <a href=""
+		      >X</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -963,45 +1019,51 @@
 	    ><div id="section.i:-60--62-" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >(&lt;&gt;)</a
-		    > * <a href=""
-		    >Y</a
-		    > a = a</td
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >(&lt;&gt;)</a
+		      > * <a href=""
+		      >Y</a
+		      > a = a</span
+		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >(&lt;&gt;)</a
-		    > * <a href=""
-		    >X</a
-		    > a = <a href=""
-		    >X</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >(&lt;&gt;)</a
+		      > * <a href=""
+		      >X</a
+		      > a = <a href=""
+		      >X</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >(&lt;&gt;)</a
-		    > <a href=""
-		    >X</a
-		    > <a href=""
-		    >XXX</a
-		    > <a href=""
-		    >XX</a
-		    > = <a href=""
-		    >X</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >(&lt;&gt;)</a
+		      > <a href=""
+		      >X</a
+		      > <a href=""
+		      >XXX</a
+		      > <a href=""
+		      >XX</a
+		      > = <a href=""
+		      >X</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
@@ -1023,15 +1085,17 @@
 	    ><div id="section.i:-62--60-" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><a href=""
-		    >(&gt;&lt;)</a
-		    > <a href=""
-		    >X</a
-		    > <a href=""
-		    >XX</a
-		    > <a href=""
-		    >XXX</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><a href=""
+		      >(&gt;&lt;)</a
+		      > <a href=""
+		      >X</a
+		      > <a href=""
+		      >XX</a
+		      > <a href=""
+		      >XXX</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
@@ -1046,7 +1110,7 @@
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.15.0</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/ref/TypeFamilies2.html b/html-test/ref/TypeFamilies2.html
--- a/html-test/ref/TypeFamilies2.html
+++ b/html-test/ref/TypeFamilies2.html
@@ -85,29 +85,33 @@
 	    ><div id="section.i:W" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >Bar</a
-		    > <a href=""
-		    >W</a
-		    > = <a name="v:BarX" class="def"
-		    >BarX</a
-		    > Z</td
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >Bar</a
+		      > <a href=""
+		      >W</a
+		      > = <a name="v:BarX" class="def"
+		      >BarX</a
+		      > Z</span
+		    ></td
 		  ><td class="doc"
 		  ><p
 		    >Shown because BarX is still exported despite Z being hidden</p
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >Foo</a
-		    > <a href=""
-		    >W</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >Foo</a
+		      > <a href=""
+		      >W</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -135,13 +139,15 @@
 	    ><div id="section.i:Foo" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >Foo</a
-		    > <a href=""
-		    >W</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >Foo</a
+		      > <a href=""
+		      >W</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -149,15 +155,17 @@
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >type</span
-		    > <a href=""
-		    >Foo</a
-		    > <a href=""
-		    >X</a
-		    > = <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >type</span
+		      > <a href=""
+		      >Foo</a
+		      > <a href=""
+		      >X</a
+		      > = <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc"
 		  ><p
@@ -185,29 +193,33 @@
 	    ><div id="section.i:Bar" class="show"
 	    ><table
 	      ><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >Bar</a
-		    > <a href=""
-		    >W</a
-		    > = <a name="v:BarX" class="def"
-		    >BarX</a
-		    > Z</td
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >Bar</a
+		      > <a href=""
+		      >W</a
+		      > = <a name="v:BarX" class="def"
+		      >BarX</a
+		      > Z</span
+		    ></td
 		  ><td class="doc"
 		  ><p
 		    >Shown because BarX is still exported despite Z being hidden</p
 		    ></td
 		  ></tr
 		><tr
-		><td class="src"
-		  ><span class="keyword"
-		    >data</span
-		    > <a href=""
-		    >Bar</a
-		    > <a href=""
-		    >Y</a
+		><td class="src clearfix"
+		  ><span class="inst-left"
+		    ><span class="keyword"
+		      >data</span
+		      > <a href=""
+		      >Bar</a
+		      > <a href=""
+		      >Y</a
+		      ></span
 		    ></td
 		  ><td class="doc empty"
 		  >&nbsp;</td
@@ -222,7 +234,7 @@
     ><p
       >Produced by <a href=""
 	>Haddock</a
-	> version 2.15.0</p
+	> version 2.16.1</p
       ></div
     ></body
   ></html
diff --git a/html-test/run.lhs b/html-test/run.lhs
--- a/html-test/run.lhs
+++ b/html-test/run.lhs
@@ -21,7 +21,6 @@
 import System.FilePath
 import System.Process (ProcessHandle, runProcess, waitForProcess, system)
 
-
 packageRoot, dataDir, haddockPath, baseDir, testDir, outDir :: FilePath
 baseDir = takeDirectory __FILE__
 testDir       = baseDir </> "src"
@@ -112,11 +111,11 @@
       then do
         out <- readFile outfile
         ref <- readFile reffile
-        if not $ haddockEq out ref
+        if not $ haddockEq (outfile, out) (reffile, ref)
           then do
             putStrLn $ "Output for " ++ mod ++ " has changed! Exiting with diff:"
-            let ref' = stripLinks ref
-                out' = stripLinks out
+            let ref' = maybeStripLinks outfile ref
+                out' = maybeStripLinks reffile out
             let reffile' = outDir </> takeFileName reffile ++ ".nolinks"
                 outfile' = outDir </> takeFileName outfile ++ ".ref.nolinks"
             writeFile reffile' ref'
@@ -134,6 +133,10 @@
       else do
         putStrLn $ "Pass: " ++ mod ++ " (no .ref file)"
 
+-- | List of modules in which we don't 'stripLinks'
+preserveLinksModules :: [String]
+preserveLinksModules = map (++ ".html") ["Bug253"]
+
 -- | A rather nasty way to drop the Haddock version string from the
 -- end of the generated HTML files so that we don't have to change
 -- every single test every time we change versions. We rely on the the
@@ -146,9 +149,16 @@
     dropTillP ('p':'<':xs) = xs
     dropTillP (_:xs) = dropTillP xs
 
-haddockEq :: String -> String -> Bool
-haddockEq file1 file2 =
-  stripLinks (dropVersion file1) == stripLinks (dropVersion file2)
+haddockEq :: (FilePath, String) -> (FilePath, String) -> Bool
+haddockEq (fn1, file1) (fn2, file2) =
+  maybeStripLinks fn1 (dropVersion file1)
+  == maybeStripLinks fn2 (dropVersion file2)
+
+maybeStripLinks :: String -- ^ Module we're considering for stripping
+                -> String -> String
+maybeStripLinks m = if any (`isSuffixOf` m) preserveLinksModules
+                    then id
+                    else stripLinks
 
 stripLinks :: String -> String
 stripLinks str =
diff --git a/html-test/src/Bug253.hs b/html-test/src/Bug253.hs
new file mode 100644
--- /dev/null
+++ b/html-test/src/Bug253.hs
@@ -0,0 +1,10 @@
+-- | This module tests that if we're trying to link to a /qualified/
+-- identifier that's not in scope, we get an anchor as if it was a
+-- variable. Previous behaviour was to treat it as a type constructor
+-- so issue like #253 arose. Also see @rename@ function comments in
+-- source.
+module Bug253 where
+
+-- | This link should generate @#v@ anchor: 'DoesNotExist.fakeFakeFake'
+foo :: ()
+foo = ()
diff --git a/html-test/src/Bug387.hs b/html-test/src/Bug387.hs
new file mode 100644
--- /dev/null
+++ b/html-test/src/Bug387.hs
@@ -0,0 +1,12 @@
+module Bug387
+  ( -- * Section1#a:section1#
+    test1
+    -- * Section2#a:section2#
+  , test2
+  ) where
+
+test1 :: Int
+test1 = 223
+
+test2 :: Int
+test2 = 42
diff --git a/html-test/src/Nesting.hs b/html-test/src/Nesting.hs
--- a/html-test/src/Nesting.hs
+++ b/html-test/src/Nesting.hs
@@ -119,3 +119,18 @@
 -}
 j :: t
 j = undefined
+
+{-|
+      - list may start at arbitrary depth
+
+      - and consecutive items at that depth
+      belong to the same list
+
+      - of course we can still
+
+          * nest items like we are used to
+
+      - and then get back to initial list
+-}
+k :: t
+k = undefined
diff --git a/html-test/src/Threaded.hs b/html-test/src/Threaded.hs
new file mode 100644
--- /dev/null
+++ b/html-test/src/Threaded.hs
@@ -0,0 +1,10 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+-- | Ensures haddock built with @-threaded@.
+module Threaded where
+
+import Threaded_TH
+
+-- | @$(forkTH)@ fails at compile time if haddock isn't using the
+-- threaded RTS.
+f = $(forkTH)
diff --git a/html-test/src/Threaded_TH.hs b/html-test/src/Threaded_TH.hs
new file mode 100644
--- /dev/null
+++ b/html-test/src/Threaded_TH.hs
@@ -0,0 +1,13 @@
+-- | Imported by 'Threaded', since a TH splice can't be used in the
+-- module where it is defined.
+module Threaded_TH where
+
+import Control.Concurrent (forkOS)
+import Language.Haskell.TH.Syntax (Exp (LitE), Lit (IntegerL), Q, runIO)
+
+-- | forkOS requires the threaded RTS, so this TH fails if haddock was
+-- built without @-threaded@.
+forkTH :: Q Exp
+forkTH = do
+  _ <- runIO (forkOS (return ()))
+  return (LitE (IntegerL 0))
diff --git a/html-test/src/Ticket253_1.hs b/html-test/src/Ticket253_1.hs
deleted file mode 100644
--- a/html-test/src/Ticket253_1.hs
+++ /dev/null
@@ -1,6 +0,0 @@
-module Ticket253_1 where
--- | See 'Ticket253_2.bar'.
---
--- Also see 'Ticket253_2.Baz'
-foo :: Int
-foo = 0
diff --git a/html-test/src/Ticket253_2.hs b/html-test/src/Ticket253_2.hs
deleted file mode 100644
--- a/html-test/src/Ticket253_2.hs
+++ /dev/null
@@ -1,6 +0,0 @@
-module Ticket253_2 where
--- | Comment
-bar :: Int
-bar = 0
-
-data Baz = Baz
