Use diagnostics for trace_macro instead of println

This commit is contained in:
Esteban Küber 2017-04-24 16:27:07 -07:00
parent 96e2c34286
commit 56411443f2
7 changed files with 26 additions and 12 deletions

View file

@ -388,6 +388,12 @@ impl Handler {
pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
self.emit(&sp.into(), msg, Note);
}
pub fn span_label_without_error(&self, sp: Span, msg: &str, lbl: &str) {
let mut db = DiagnosticBuilder::new(self, Note, msg);
db.set_span(sp);
db.span_label(sp, &lbl);
db.emit();
}
pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
self.span_bug(sp, &format!("unimplemented {}", msg));
}

View file

@ -765,6 +765,9 @@ impl<'a> ExtCtxt<'a> {
pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
self.parse_sess.span_diagnostic.span_bug(sp, msg);
}
pub fn span_label_without_error(&self, sp: Span, msg: &str, label: &str) {
self.parse_sess.span_diagnostic.span_label_without_error(sp, msg, label);
}
pub fn bug(&self, msg: &str) -> ! {
self.parse_sess.span_diagnostic.bug(msg);
}

View file

@ -93,7 +93,9 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
rhses: &[quoted::TokenTree])
-> Box<MacResult+'cx> {
if cx.trace_macros() {
println!("{}! {{ {} }}", name, arg);
cx.span_label_without_error(sp,
&"trace_macro",
&format!("expands to `{}! {{ {} }}`", name, arg));
}
// Which arm's failure should we report? (the one furthest along)

View file

@ -1,9 +0,0 @@
# This test verifies that "-Z trace-macros" works as it should. The traditional
# "hello world" program provides a small example of this as not only println! is
# listed, but also print! (since println! expands to this)
-include ../tools.mk
all:
$(RUSTC) -Z trace-macros hello.rs > $(TMPDIR)/hello.out
diff -u $(TMPDIR)/hello.out hello.trace

View file

@ -1,2 +0,0 @@
println! { "Hello, World!" }
print! { concat ! ( "Hello, World!" , "\n" ) }

View file

@ -0,0 +1,14 @@
note: trace_macro
--> $DIR/trace-macro.rs:12:5
|
12 | println!("Hello, World!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expands to `println! { "Hello, World!" }`
note: trace_macro
--> $DIR/trace-macro.rs:12:5
|
12 | println!("Hello, World!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expands to `print! { concat ! ( "Hello, World!" , "\n" ) }`
|
= note: this error originates in a macro outside of the current crate