Skip to content

Commit 1940c31

Browse files
authored
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.
2 parents c104db4 + 8c9ad8d commit 1940c31

File tree

8 files changed

+38
-16
lines changed

8 files changed

+38
-16
lines changed

src/librustc_errors/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,14 @@ 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_note_diag<'a>(&'a self,
392+
sp: Span,
393+
msg: &str)
394+
-> DiagnosticBuilder<'a> {
395+
let mut db = DiagnosticBuilder::new(self, Note, msg);
396+
db.set_span(sp);
397+
db
398+
}
391399
pub fn span_unimpl<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
392400
self.span_bug(sp, &format!("unimplemented {}", msg));
393401
}

src/libsyntax/ext/base.rs

Lines changed: 12 additions & 0 deletions
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,6 +768,15 @@ 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
}
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+
}
779+
}
768780
pub fn bug(&self, msg: &str) -> ! {
769781
self.parse_sess.span_diagnostic.bug(msg);
770782
}

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 4 deletions
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,15 +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-
println!("{}! {{ {} }}", 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));
9799
}
98100

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

src/test/run-make/trace-macros-flag/Makefile

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/test/run-make/trace-macros-flag/hello.trace

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/test/run-make/trace-macros-flag/hello.rs renamed to src/test/ui/macros/trace-macro.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
note: trace_macro
2+
--> $DIR/trace-macro.rs:14:5
3+
|
4+
14 | println!("Hello, World!");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: expands to `println! { "Hello, World!" }`
8+
= note: expands to `print! { concat ! ( "Hello, World!" , "/n" ) }`
9+

0 commit comments

Comments
 (0)