gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

Have error classes for simpler emission and more factoring

Open CohenArthur opened this issue 1 year ago • 3 comments

Something we might start to think about doing is for every error diagnostic have some kind of class for them all, so we can enforce good error messages so for example we might have:

class ErrorDiagnostics { static void StaticMutableUnsafe(Location) { } }

Originally posted by @philberty in https://github.com/Rust-GCC/gccrs/pull/1417#pullrequestreview-1052766761

I think a good interface would be the following:

class Emittable {
    Location locus;
    std::string msg;
    unsigned error_code;

    void emit() {
        rust_error_at(locus, msg); // or similar
    }
}

class ErrorStaticMutableUnsafe: public Emittable {
    /* ... */
}

I wonder if there is something we can do better with an enum class or similar

CohenArthur avatar Jul 27 '22 20:07 CohenArthur

ACK. In context of thinking about re-using the rustc error codes (that was even before @davidmalcolm's #1408 "Experiment: add optional error codes to diagnostics"), I also had this thought: whether there's a point to be made to maintain all GCC/Rust front end diagnostics in a central place.

Might also help with getting a feel for how many of the rustc codes we support, eventually, and where the differences/omissions are?

tschwinge avatar Jul 28 '22 08:07 tschwinge

I'm all for factorisation - it's always easier when searching for where an error occurs to search in a central place, especially with format strings and so on. Plus, if we do want to change an error message, it's always better to change it in a single location.

Might also help with getting a feel for how many of the rustc codes we support, eventually, and where the differences/omissions are?

Yes, that'd be great. That's why I think we'd need to keep an enum for error codes as it would be very easy to see which ones we support

CohenArthur avatar Jul 28 '22 08:07 CohenArthur

FWIW, something I've done in the C/C++ frontends is to have some custom rich_location subclasses, for highlighting the things of interest to particular kinds of diagnostic. See e.g. class binary_op_rich_location in gcc/gcc-rich-location.h. Though this might be orthogonal to this suggestion.

davidmalcolm avatar Jul 28 '22 12:07 davidmalcolm

This issue was fixed by these PR's:

  • [x] https://github.com/Rust-GCC/gccrs/pull/2468
  • [x] https://github.com/Rust-GCC/gccrs/pull/2480
  • [x] https://github.com/Rust-GCC/gccrs/pull/2518
  • [x] https://github.com/Rust-GCC/gccrs/pull/2395

MahadMuhammad avatar Aug 03 '23 17:08 MahadMuhammad