Skip to content

Commit caa2327

Browse files
Rollup merge of rust-lang#42103 - jorendorff:master, r=estebank
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`.
2 parents babc2a0 + f8b66a0 commit caa2327

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/libsyntax/ext/tt/macro_rules.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ impl TTMacroExpander for MacroRulesMacroExpander {
8484
}
8585
}
8686

87+
fn trace_macros_note(cx: &mut ExtCtxt, sp: Span, message: String) {
88+
let sp = sp.macro_backtrace().last().map(|trace| trace.call_site).unwrap_or(sp);
89+
let mut values: &mut Vec<String> = cx.expansions.entry(sp).or_insert_with(Vec::new);
90+
values.push(message);
91+
}
92+
8793
/// Given `lhses` and `rhses`, this is the new macro we create
8894
fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
8995
sp: Span,
@@ -93,9 +99,7 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
9399
rhses: &[quoted::TokenTree])
94100
-> Box<MacResult+'cx> {
95101
if cx.trace_macros() {
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_with(Vec::new);
98-
values.push(format!("expands to `{}! {{ {} }}`", name, arg));
102+
trace_macros_note(cx, sp, format!("expanding `{}! {{ {} }}`", name, arg));
99103
}
100104

101105
// Which arm's failure should we report? (the one furthest along)
@@ -117,6 +121,11 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
117121
};
118122
// rhs has holes ( `$id` and `$(...)` that need filled)
119123
let tts = transcribe(&cx.parse_sess.span_diagnostic, Some(named_matches), rhs);
124+
125+
if cx.trace_macros() {
126+
trace_macros_note(cx, sp, format!("to `{}`", tts));
127+
}
128+
120129
let directory = Directory {
121130
path: cx.current_expansion.module.directory.clone(),
122131
ownership: cx.current_expansion.directory_ownership,

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ note: trace_macro
44
14 | println!("Hello, World!");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: expands to `println! { "Hello, World!" }`
8-
= note: expands to `print! { concat ! ( "Hello, World!" , "/n" ) }`
7+
= note: expanding `println! { "Hello, World!" }`
8+
= note: to `print ! ( concat ! ( "Hello, World!" , "/n" ) )`
9+
= note: expanding `print! { concat ! ( "Hello, World!" , "/n" ) }`
10+
= note: to `$crate :: io :: _print ( format_args ! ( concat ! ( "Hello, World!" , "/n" ) )
11+
)`
912

0 commit comments

Comments
 (0)