Add help diagnostic messages

This adds ‘help’ diagnostic messages to rustc. This is used for anything that
provides help to the user, particularly the `--explain` messages that were
previously integrated into the relevant error message.
This commit is contained in:
P1start 2014-08-29 18:55:35 +12:00
parent 19311b6103
commit 06d9cc1d7a
10 changed files with 52 additions and 22 deletions

View file

@ -105,6 +105,9 @@ impl SpanHandler {
pub fn span_end_note(&self, sp: Span, msg: &str) {
self.handler.custom_emit(&self.cm, FullSpan(sp), msg, Note);
}
pub fn span_help(&self, sp: Span, msg: &str) {
self.handler.emit(Some((&self.cm, sp)), msg, Help);
}
pub fn fileline_note(&self, sp: Span, msg: &str) {
self.handler.custom_emit(&self.cm, FileLine(sp), msg, Note);
}
@ -164,6 +167,9 @@ impl Handler {
pub fn note(&self, msg: &str) {
self.emit.borrow_mut().emit(None, msg, None, Note);
}
pub fn help(&self, msg: &str) {
self.emit.borrow_mut().emit(None, msg, None, Help);
}
pub fn bug(&self, msg: &str) -> ! {
self.emit.borrow_mut().emit(None, msg, None, Bug);
fail!(ExplicitBug);
@ -216,6 +222,7 @@ pub enum Level {
Error,
Warning,
Note,
Help,
}
impl fmt::Show for Level {
@ -227,6 +234,7 @@ impl fmt::Show for Level {
Fatal | Error => "error".fmt(f),
Warning => "warning".fmt(f),
Note => "note".fmt(f),
Help => "help".fmt(f),
}
}
}
@ -236,7 +244,8 @@ impl Level {
match self {
Bug | Fatal | Error => term::color::BRIGHT_RED,
Warning => term::color::BRIGHT_YELLOW,
Note => term::color::BRIGHT_GREEN
Note => term::color::BRIGHT_GREEN,
Help => term::color::BRIGHT_CYAN,
}
}
}
@ -293,15 +302,6 @@ fn print_diagnostic(dst: &mut EmitterWriter, topic: &str, lvl: Level,
Some(code) => {
let style = term::attr::ForegroundColor(term::color::BRIGHT_MAGENTA);
try!(print_maybe_styled(dst, format!(" [{}]", code.clone()).as_slice(), style));
match dst.registry.as_ref().and_then(|registry| registry.find_description(code)) {
Some(_) => {
try!(write!(&mut dst.dst,
" (pass `--explain {}` to see a detailed explanation)",
code
));
}
None => ()
}
}
None => ()
}
@ -401,7 +401,20 @@ fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan,
try!(highlight_lines(dst, cm, sp, lvl, lines));
}
}
print_macro_backtrace(dst, cm, sp)
try!(print_macro_backtrace(dst, cm, sp));
match code {
Some(code) =>
match dst.registry.as_ref().and_then(|registry| registry.find_description(code)) {
Some(_) => {
try!(print_diagnostic(dst, ss.as_slice(), Help,
format!("pass `--explain {}` to see a detailed \
explanation", code).as_slice(), None));
}
None => ()
},
None => (),
}
Ok(())
}
fn highlight_lines(err: &mut EmitterWriter,

View file

@ -575,6 +575,10 @@ impl<'a> ExtCtxt<'a> {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_note(sp, msg);
}
pub fn span_help(&self, sp: Span, msg: &str) {
self.print_backtrace();
self.parse_sess.span_diagnostic.span_help(sp, msg);
}
pub fn bug(&self, msg: &str) -> ! {
self.print_backtrace();
self.parse_sess.span_diagnostic.handler().bug(msg);

View file

@ -959,6 +959,9 @@ impl<'a> Parser<'a> {
pub fn span_note(&mut self, sp: Span, m: &str) {
self.sess.span_diagnostic.span_note(sp, m)
}
pub fn span_help(&mut self, sp: Span, m: &str) {
self.sess.span_diagnostic.span_help(sp, m)
}
pub fn bug(&mut self, m: &str) -> ! {
self.sess.span_diagnostic.span_bug(self.span, m)
}