Skip to content

Use diagnostic infrastructure in GHC.Tc.Errors

sheaf requested to merge sheaf/ghc:tc-errors into master

This MR changes GHC.Tc.Errors to use the diagnostic infrastructure instead of directly handling SDocs.

Currently, this is achieved as follows:

  • introduce the TcReportMsg datatype (defined in GHC.Tc.Errors.Types), whose constructors correspond to the various different errors we would throw. (The constructors can still be improved, so please let me know how you would like me to change things.)
  • Define TcReportInfo, which contains extra information that is attached to a ReportMsg. Note that this is different from the supplementary info contained in a Report (now renamed to SolverReport and defined in GHC.Tc.Errors.Types), which is printed after the context info. See the documentation to SolverReport.
  • A new TcRnMessage constructor which stores the above messages, TcRnSolverReport.
  • A few smaller datatypes:
    • HoleError for errors reported by mkHoleError,
    • NotInScopeError for various "not-in-scope" errors,
    • CoercibleMsg for some additional informative messages relating to representational equality,
    • ValidHoleFits to keep track of suggestions from the hole-fits mechanism,
    • ImportSuggestion for informational messages about whether names are available to be imported from,
    • ...
  • A few other new TcRnMessages:
    • TcRnRedundantConstraints,
    • TcRnInaccessibleCode,
    • TcRnNotInScope,
    • TcRnUntickedPromotedConstructor,
    • TcRnIllegalBuiltinSyntax.
  • A few new GhcHints:
    • SuggestDumpSplices for Template Haskell exact name errors,
    • SugestAddTick,
    • SuggestMoveToDeclarationSite,
    • SuggestSimilarNames,
    • a slight modification to SuggestUseTypeFromDataKind to accomodate other places which printed a similar message.
  • Two sum types defined in GHC.Tc.Types.Origin to avoid SDocs, TypeEqThing and BinderThing (happy to take suggestions for renaming these, but I do genuinely think it's much better than having SDocs).


  • Rebase on top of !6943 (closed) once that is merged.
  • The "inaccessible code" message is missing some information, although I think it is also somewhat improved. See e.g. T7293.
  • Some constructors of TcReportMsg are missing documentation, examples or relevant test cases.
  • Some constructors of GhcHint are missing documentation.
  • The "redundant constraints" message seems to be missing some constraints, see e.g. T20602.
  • Figure out how to avoid the import cycle that lead me to create GHC.Tc.Errors.hs-boot and seems to have greatly increased the number of dependencies of the parser.
  • Update the notes around StarIsType in GHC.Types.Name.Reader.
  • Remove the use of TcRnUnknownMessage in GHC.Rename.HsType. It calls withHsDocContext which I wasn't immediately sure how to handle; happy to take suggestions.
  • Remove SDoc from TcRnIllegalBuiltinSyntax.
  • Remove SDoc from MissingBinding constructor of NotInScopeError.
  • Remove SDoc from UnknownSubordinate constructor of NotInScopeError.

The above are concrete things that I have yet to address, but there is still a fair bit of iteration left to do on the design too, I think. Some of the code might look a bit strange because I'm contorting myself to avoid changing the existing error messages as much as possible. I think once this is in place we will be in a much better shape to refactor some of these errors in a robust way.

Happy to hear what you all think @rae @simonpj @adinapoli (and others).

Edited by sheaf

Merge request reports