Skip to content

Commit

Permalink
Group "macro expansion" notes per call span
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed May 6, 2017
1 parent 5641144 commit 8c9ad8d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
8 changes: 5 additions & 3 deletions src/librustc_errors/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,13 @@ 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) {
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.span_label(sp, &lbl);
db.emit();
db
}
pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
self.span_bug(sp, &format!("unimplemented {}", msg));
Expand Down
13 changes: 11 additions & 2 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> {
Expand All @@ -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(),
}
}

Expand Down Expand Up @@ -765,8 +768,14 @@ 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 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);
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
},
_ => unreachable!(),
};

self.cx.trace_macros_diag();
krate
}

Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -85,17 +85,17 @@ 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,
lhses: &[quoted::TokenTree],
rhses: &[quoted::TokenTree])
-> Box<MacResult+'cx> {
if cx.trace_macros() {
cx.span_label_without_error(sp,
&"trace_macro",
&format!("expands to `{}! {{ {} }}`", 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)
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/macros/trace-macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
15 changes: 5 additions & 10 deletions src/test/ui/macros/trace-macro.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
note: trace_macro
--> $DIR/trace-macro.rs:12:5
--> $DIR/trace-macro.rs:14: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" ) }`
14 | println!("Hello, World!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate
= note: expands to `println! { "Hello, World!" }`
= note: expands to `print! { concat ! ( "Hello, World!" , "/n" ) }`

0 comments on commit 8c9ad8d

Please sign in to comment.