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`
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?
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)
- 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.
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.
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!