Skip to content

Commit 8c9ad8d

Browse files
committed
Group "macro expansion" notes per call span
1 parent 5641144 commit 8c9ad8d

File tree

6 files changed

+30
-22
lines changed

6 files changed

+30
-22
lines changed

src/librustc_errors/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,13 @@ impl Handler {
388388
pub fn span_note_without_error<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
389389
self.emit(&sp.into(), msg, Note);
390390
}
391-
pub fn span_label_without_error(&self, sp: Span, msg: &str, lbl: &str) {
391+
pub fn span_note_diag<'a>(&'a self,
392+
sp: Span,
393+
msg: &str)
394+
-> DiagnosticBuilder<'a> {
392395
let mut db = DiagnosticBuilder::new(self, Note, msg);
393396
db.set_span(sp);
394-
db.span_label(sp, &lbl);
395-
db.emit();
397+
db
396398
}
397399
pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
398400
self.span_bug(sp, &format!("unimplemented {}", msg));

src/libsyntax/ext/base.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use ptr::P;
2424
use symbol::Symbol;
2525
use util::small_vector::SmallVector;
2626

27+
use std::collections::HashMap;
2728
use std::path::PathBuf;
2829
use std::rc::Rc;
2930
use std::default::Default;
@@ -643,6 +644,7 @@ pub struct ExtCtxt<'a> {
643644
pub resolver: &'a mut Resolver,
644645
pub resolve_err_count: usize,
645646
pub current_expansion: ExpansionData,
647+
pub expansions: HashMap<Span, Vec<String>>,
646648
}
647649

648650
impl<'a> ExtCtxt<'a> {
@@ -662,6 +664,7 @@ impl<'a> ExtCtxt<'a> {
662664
module: Rc::new(ModuleData { mod_path: Vec::new(), directory: PathBuf::new() }),
663665
directory_ownership: DirectoryOwnership::Owned,
664666
},
667+
expansions: HashMap::new(),
665668
}
666669
}
667670

@@ -765,8 +768,14 @@ impl<'a> ExtCtxt<'a> {
765768
pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
766769
self.parse_sess.span_diagnostic.span_bug(sp, msg);
767770
}
768-
pub fn span_label_without_error(&self, sp: Span, msg: &str, label: &str) {
769-
self.parse_sess.span_diagnostic.span_label_without_error(sp, msg, label);
771+
pub fn trace_macros_diag(&self) {
772+
for (sp, notes) in self.expansions.iter() {
773+
let mut db = self.parse_sess.span_diagnostic.span_note_diag(*sp, &"trace_macro");
774+
for note in notes {
775+
db.note(&note);
776+
}
777+
db.emit();
778+
}
770779
}
771780
pub fn bug(&self, msg: &str) -> ! {
772781
self.parse_sess.span_diagnostic.bug(msg);

src/libsyntax/ext/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
231231
},
232232
_ => unreachable!(),
233233
};
234-
234+
self.cx.trace_macros_diag();
235235
krate
236236
}
237237

src/libsyntax/ext/tt/macro_rules.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use symbol::Symbol;
2727
use tokenstream::{TokenStream, TokenTree};
2828

2929
use std::cell::RefCell;
30-
use std::collections::{HashMap};
31-
use std::collections::hash_map::{Entry};
30+
use std::collections::HashMap;
31+
use std::collections::hash_map::Entry;
3232
use std::rc::Rc;
3333

3434
pub struct ParserAnyMacro<'a> {
@@ -85,17 +85,17 @@ impl TTMacroExpander for MacroRulesMacroExpander {
8585
}
8686

8787
/// Given `lhses` and `rhses`, this is the new macro we create
88-
fn generic_extension<'cx>(cx: &'cx ExtCtxt,
88+
fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
8989
sp: Span,
9090
name: ast::Ident,
9191
arg: TokenStream,
9292
lhses: &[quoted::TokenTree],
9393
rhses: &[quoted::TokenTree])
9494
-> Box<MacResult+'cx> {
9595
if cx.trace_macros() {
96-
cx.span_label_without_error(sp,
97-
&"trace_macro",
98-
&format!("expands to `{}! {{ {} }}`", name, arg));
96+
let sp = sp.macro_backtrace().last().map(|trace| trace.call_site).unwrap_or(sp);
97+
let mut values: &mut Vec<String> = cx.expansions.entry(sp).or_insert(vec![]);
98+
values.push(format!("expands to `{}! {{ {} }}`", name, arg));
9999
}
100100

101101
// Which arm's failure should we report? (the one furthest along)

src/test/ui/macros/trace-macro.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: -Z trace-macros
12+
1113
fn main() {
1214
println!("Hello, World!");
1315
}

src/test/ui/macros/trace-macro.stderr

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
note: trace_macro
2-
--> $DIR/trace-macro.rs:12:5
2+
--> $DIR/trace-macro.rs:14:5
33
|
4-
12 | println!("Hello, World!");
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expands to `println! { "Hello, World!" }`
6-
7-
note: trace_macro
8-
--> $DIR/trace-macro.rs:12:5
9-
|
10-
12 | println!("Hello, World!");
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expands to `print! { concat ! ( "Hello, World!" , "\n" ) }`
4+
14 | println!("Hello, World!");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
126
|
13-
= note: this error originates in a macro outside of the current crate
7+
= note: expands to `println! { "Hello, World!" }`
8+
= note: expands to `print! { concat ! ( "Hello, World!" , "/n" ) }`
149

0 commit comments

Comments
 (0)