+mkLocMessage :: SrcSpan -> Message -> Message
+mkLocMessage locn msg
+ | opt_ErrorSpans = hang (ppr locn <> colon) 4 msg
+ | otherwise = hang (ppr (srcSpanStart locn) <> colon) 4 msg
+ -- always print the location, even if it is unhelpful. Error messages
+ -- are supposed to be in a standard format, and one without a location
+ -- would look strange. Better to say explicitly "<no location info>".
+
+printError :: SrcSpan -> Message -> IO ()
+printError span msg = printErrs (mkLocMessage span msg $ defaultErrStyle)
+
+
+-- -----------------------------------------------------------------------------
+-- Collecting up messages for later ordering and printing.
+
+data ErrMsg = ErrMsg {
+ errMsgSpans :: [SrcSpan],
+ errMsgContext :: PrintUnqualified,
+ errMsgShortDoc :: Message,
+ errMsgExtraInfo :: Message
+ }
+ -- The SrcSpan is used for sorting errors into line-number order
+ -- NB Pretty.Doc not SDoc: we deal with the printing style (in ptic
+ -- whether to qualify an External Name) at the error occurrence
+
+type WarnMsg = ErrMsg
+
+-- A short (one-line) error message, with context to tell us whether
+-- to qualify names in the message or not.
+mkErrMsg :: SrcSpan -> PrintUnqualified -> Message -> ErrMsg
+mkErrMsg locn print_unqual msg
+ = ErrMsg [locn] print_unqual msg empty
+
+-- Variant that doesn't care about qualified/unqualified names
+mkPlainErrMsg :: SrcSpan -> Message -> ErrMsg
+mkPlainErrMsg locn msg
+ = ErrMsg [locn] alwaysQualify msg empty
+
+-- A long (multi-line) error message, with context to tell us whether
+-- to qualify names in the message or not.
+mkLongErrMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> ErrMsg
+mkLongErrMsg locn print_unqual msg extra
+ = ErrMsg [locn] print_unqual msg extra
+
+-- A long (multi-line) error message, with context to tell us whether
+-- to qualify names in the message or not.
+mkLongMultiLocErrMsg :: [SrcSpan] -> PrintUnqualified -> Message -> Message -> ErrMsg
+mkLongMultiLocErrMsg locns print_unqual msg extra
+ = ErrMsg locns print_unqual msg extra
+
+mkWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> WarnMsg
+mkWarnMsg = mkErrMsg
+
+mkLongWarnMsg :: SrcSpan -> PrintUnqualified -> Message -> Message -> WarnMsg
+mkLongWarnMsg = mkLongErrMsg