From f8b66a001d74946565ee2e7df58afc9918455da1 Mon Sep 17 00:00:00 2001 From: Jason Orendorff Date: Fri, 19 May 2017 13:43:06 -0500 Subject: [PATCH] trace_macro: Show both the macro call and its expansion. #42072. --- src/libsyntax/ext/tt/macro_rules.rs | 15 ++++++++++++--- src/test/ui/macros/trace-macro.stderr | 7 +++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index a208f530602a5..d3da32304aa04 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -84,6 +84,12 @@ impl TTMacroExpander for MacroRulesMacroExpander { } } +fn trace_macros_note(cx: &mut ExtCtxt, sp: Span, message: String) { + let sp = sp.macro_backtrace().last().map(|trace| trace.call_site).unwrap_or(sp); + let mut values: &mut Vec = cx.expansions.entry(sp).or_insert_with(Vec::new); + values.push(message); +} + /// Given `lhses` and `rhses`, this is the new macro we create fn generic_extension<'cx>(cx: &'cx mut ExtCtxt, sp: Span, @@ -93,9 +99,7 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt, rhses: &[quoted::TokenTree]) -> Box { if cx.trace_macros() { - let sp = sp.macro_backtrace().last().map(|trace| trace.call_site).unwrap_or(sp); - let mut values: &mut Vec = cx.expansions.entry(sp).or_insert_with(Vec::new); - values.push(format!("expands to `{}! {{ {} }}`", name, arg)); + trace_macros_note(cx, sp, format!("expanding `{}! {{ {} }}`", name, arg)); } // Which arm's failure should we report? (the one furthest along) @@ -117,6 +121,11 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt, }; // rhs has holes ( `$id` and `$(...)` that need filled) let tts = transcribe(&cx.parse_sess.span_diagnostic, Some(named_matches), rhs); + + if cx.trace_macros() { + trace_macros_note(cx, sp, format!("to `{}`", tts)); + } + let directory = Directory { path: cx.current_expansion.module.directory.clone(), ownership: cx.current_expansion.directory_ownership, diff --git a/src/test/ui/macros/trace-macro.stderr b/src/test/ui/macros/trace-macro.stderr index 09117a4ca7404..6cf3b0bd35d5e 100644 --- a/src/test/ui/macros/trace-macro.stderr +++ b/src/test/ui/macros/trace-macro.stderr @@ -4,6 +4,9 @@ note: trace_macro 14 | println!("Hello, World!"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = note: expands to `println! { "Hello, World!" }` - = note: expands to `print! { concat ! ( "Hello, World!" , "/n" ) }` + = note: expanding `println! { "Hello, World!" }` + = note: to `print ! ( concat ! ( "Hello, World!" , "/n" ) )` + = note: expanding `print! { concat ! ( "Hello, World!" , "/n" ) }` + = note: to `$crate :: io :: _print ( format_args ! ( concat ! ( "Hello, World!" , "/n" ) ) + )`