-addShortErrLocLine :: SrcLoc -> Message -> ErrMsg
-addErrLocHdrLine :: SrcLoc -> Message -> Message -> ErrMsg
-addShortWarnLocLine :: SrcLoc -> Message -> WarnMsg
-
-addShortErrLocLine locn rest_of_err_msg
- = ( locn
- , hang (ppr locn <> colon)
- 4 rest_of_err_msg
- )
-
-addErrLocHdrLine locn hdr rest_of_err_msg
- = ( locn
- , hang (ppr locn <> colon<+> hdr)
- 4 rest_of_err_msg
- )
-
-addShortWarnLocLine locn rest_of_err_msg
- = ( locn
- , hang (ppr locn <> colon)
- 4 (ptext SLIT("Warning:") <+> rest_of_err_msg)
- )
-
-dontAddErrLoc :: String -> Message -> ErrMsg
-dontAddErrLoc title rest_of_err_msg
- | null title = (noSrcLoc, rest_of_err_msg)
- | otherwise =
- ( noSrcLoc, hang (text title <> colon) 4 rest_of_err_msg )
-
-printErrorsAndWarnings :: Bag ErrMsg -> Bag WarnMsg -> IO ()
- -- Don't print any warnings if there are errors
-printErrorsAndWarnings errs warns
+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
+
+type Messages = (Bag WarnMsg, Bag ErrMsg)
+
+emptyMessages :: Messages
+emptyMessages = (emptyBag, emptyBag)
+
+errorsFound :: DynFlags -> Messages -> Bool
+-- The dyn-flags are used to see if the user has specified
+-- -Werorr, which says that warnings should be fatal
+errorsFound dflags (warns, errs)
+ | dopt Opt_WarnIsError dflags = not (isEmptyBag errs) || not (isEmptyBag warns)
+ | otherwise = not (isEmptyBag errs)
+
+printErrorsAndWarnings :: Messages -> IO ()
+printErrorsAndWarnings (warns, errs)