Skip to content

Commit

Permalink
Rollup merge of rust-lang#42103 - jorendorff:master, r=estebank
Browse files Browse the repository at this point in the history
trace_macro: Show both the macro call and its expansion. rust-lang#42072.

See rust-lang#42072 for the initial motivation behind this.

The change is not the minimal fix, but I want this behavior almost every time I use `trace_macros`.
  • Loading branch information
Mark-Simulacrum committed May 26, 2017
2 parents babc2a0 + f8b66a0 commit caa2327
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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,
Expand All @@ -93,9 +99,7 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
rhses: &[quoted::TokenTree])
-> Box<MacResult+'cx> {
if cx.trace_macros() {
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_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)
Expand All @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions src/test/ui/macros/trace-macro.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) )
)`

0 comments on commit caa2327

Please sign in to comment.