A crate to print cute rustc-like compiler errors?


#1

I’m writing a compiler and now it comes to printing error messages. I really love rustc’s error/warning messages like this:

warning: unused import: `std::path::Path`
 --> src/utils.rs:1:5
  |
1 | use std::path::Path;
  |     ^^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

So I’m looking for a crate which could help me with this. Is there any?


#2

I had looked for one in the past (about 9 months ago), but there wasn’t a “ready made, plug things in” crate. Rustc’s source for this (librustc_errors) is coupled to its own syntax crate (libsyntax_pos).

That said, if you were going to roll your own, Rustc uses termcolor. I did wish for something that allowed you to go, given:

  • (a list of) Files + contents (may be read on the fly?)
  • a list of "Problem"s, which hold:
    • Files that the problem relates to
    • Info about the location / indices of where the problem exists in the file(s)
    • Severity
    • Error code
    • Suggestion / hint / help

Print out the pretty message. Which means the crate’s responsibility is to calculate the spacing and do the colour formatting etcetera.

Ya, I thought about it but haven’t done it =D.


#3

Somebody announced an annotate-snippets crate a while back on internals as an effort to externalize rustc’s diagnostics, but it kinda fizzled out. Maybe it could use some more love.


#4

You may want to have a look at the codespan crate and codespan-reporting. It looks like they took a lot of inspiration from how rustc does things, and I’ve been super happy with it in my side projects!