Rollup merge of #41520 - estebank:trace-macro, r=nikomatsakis

Use diagnostics for trace_macro instead of println

When using `trace_macro`, use `span_label`s instead of `println`:

```rust
note: trace_macro
  --> $DIR/trace-macro.rs:14:5
   |
14 |     println!("Hello, World!");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: expands to `println! { "Hello, World!" }`
   = note: expands to `print! { concat ! ( "Hello, World!" , "\n" ) }`
```

Fix #22597.
This commit is contained in:
Corey Farwell 2017-05-08 22:34:47 -04:00 committed by GitHub
commit 1940c31c92
8 changed files with 38 additions and 16 deletions

View file

@ -388,6 +388,14 @@ 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_note_diag<'a>(&'a self,
sp: Span,
msg: &str)
-> DiagnosticBuilder<'a> {
let mut db = DiagnosticBuilder::new(self, Note, msg);
db.set_span(sp);
db
}
pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
self.span_bug(sp, &format!("unimplemented {}", msg));
}

View file

@ -24,6 +24,7 @@ use ptr::P;
use symbol::Symbol;
use util::small_vector::SmallVector;
use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;
use std::default::Default;
@ -643,6 +644,7 @@ pub struct ExtCtxt<'a> {
pub resolver: &'a mut Resolver,
pub resolve_err_count: usize,
pub current_expansion: ExpansionData,
pub expansions: HashMap<Span, Vec<String>>,
}
impl<'a> ExtCtxt<'a> {
@ -662,6 +664,7 @@ impl<'a> ExtCtxt<'a> {
module: Rc::new(ModuleData { mod_path: Vec::new(), directory: PathBuf::new() }),
directory_ownership: DirectoryOwnership::Owned,
},
expansions: HashMap::new(),
}
}
@ -765,6 +768,15 @@ impl<'a> ExtCtxt<'a> {
pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
self.parse_sess.span_diagnostic.span_bug(sp, msg);
}
pub fn trace_macros_diag(&self) {
for (sp, notes) in self.expansions.iter() {
let mut db = self.parse_sess.span_diagnostic.span_note_diag(*sp, &"trace_macro");
for note in notes {
db.note(&note);
}
db.emit();
}
}
pub fn bug(&self, msg: &str) -> ! {
self.parse_sess.span_diagnostic.bug(msg);
}

View file

@ -231,7 +231,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
},
_ => unreachable!(),
};
self.cx.trace_macros_diag();
krate
}

View file

@ -27,8 +27,8 @@ use symbol::Symbol;
use tokenstream::{TokenStream, TokenTree};
use std::cell::RefCell;
use std::collections::{HashMap};
use std::collections::hash_map::{Entry};
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::rc::Rc;
pub struct ParserAnyMacro<'a> {
@ -85,7 +85,7 @@ impl TTMacroExpander for MacroRulesMacroExpander {
}
/// Given `lhses` and `rhses`, this is the new macro we create
fn generic_extension<'cx>(cx: &'cx ExtCtxt,
fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
sp: Span,
name: ast::Ident,
arg: TokenStream,
@ -93,7 +93,9 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
rhses: &[quoted::TokenTree])
-> Box<MacResult+'cx> {
if cx.trace_macros() {
println!("{}! {{ {} }}", name, arg);
let sp = sp.macro_backtrace().last().map(|trace| trace.call_site).unwrap_or(sp);
let mut values: &mut Vec<String> = cx.expansions.entry(sp).or_insert(vec![]);
values.push(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

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-flags: -Z trace-macros
fn main() {
println!("Hello, World!");
}

View file

@ -0,0 +1,9 @@
note: trace_macro
--> $DIR/trace-macro.rs:14:5
|
14 | println!("Hello, World!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: expands to `println! { "Hello, World!" }`
= note: expands to `print! { concat ! ( "Hello, World!" , "/n" ) }`