diff options
| author | bors <bors@rust-lang.org> | 2015-09-15 03:21:30 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-09-15 03:21:30 +0000 |
| commit | f3e6d315386cebdd6679055d9350c258f2a51689 (patch) | |
| tree | c368a43c199c178e9e51766611197bebe4143eab /src/libsyntax | |
| parent | b1c96168821d70992157f55ee9f06190bf299ba4 (diff) | |
| parent | 0be755c24a81f3c7c07dcd50406c9f17bee0d6ac (diff) | |
| download | rust-f3e6d315386cebdd6679055d9350c258f2a51689.tar.gz rust-f3e6d315386cebdd6679055d9350c258f2a51689.zip | |
Auto merge of #28351 - jonas-schievink:macro-bt, r=nrc
The second commit in this PR will stop printing the macro definition site in backtraces, which cuts their length in half and increases readability (the definition site was only correct for local macros).
The third commit will not print an invocation if the last one printed occurred at the same place (span). This will make backtraces caused by a self-recursive macro much shorter.
(A possible alternative would be to capture the backtrace first, then limit it to a few frames at the start and end of the chain and print `...` inbetween. This would also work with multiple macros calling each other, which is not addressed by this PR - although the backtrace will still be halved)
Example:
```rust
macro_rules! m {
( 0 $($t:tt)* ) => ( m!($($t)*); );
() => ( fn main() {0} );
}
m!(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0);
```
On a semi-recent nightly, this yields:
```
test.rs:3:21: 3:22 error: mismatched types:
expected `()`,
found `_`
(expected (),
found integral variable) [E0308]
test.rs:3 () => ( fn main() {0} );
^
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:2:23: 2:34 note: expansion site
test.rs:1:1: 4:2 note: in expansion of m!
test.rs:6:1: 6:35 note: expansion site
test.rs:3:21: 3:22 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to previous error
```
After this patch:
```
test.rs:3:21: 3:22 error: mismatched types:
expected `()`,
found `_`
(expected (),
found integral variable) [E0308]
test.rs:3 () => ( fn main() {0} );
^
test.rs:2:23: 2:34 note: in this expansion of m!
test.rs:6:1: 6:35 note: in this expansion of m!
test.rs:3:21: 3:22 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to previous error
```
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/diagnostic.rs | 59 |
1 files changed, 37 insertions, 22 deletions
diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 067e3fff3eb..c177eb1f00b 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -727,30 +727,45 @@ impl EmitterWriter { cm: &codemap::CodeMap, sp: Span) -> io::Result<()> { - let cs = try!(cm.with_expn_info(sp.expn_id, |expn_info| -> io::Result<_> { - match expn_info { - Some(ei) => { - let ss = ei.callee.span.map_or(String::new(), - |span| cm.span_to_string(span)); - let (pre, post) = match ei.callee.format { - codemap::MacroAttribute(..) => ("#[", "]"), - codemap::MacroBang(..) => ("", "!"), - codemap::CompilerExpansion(..) => ("", ""), - }; - try!(self.print_diagnostic(&ss, Note, - &format!("in expansion of {}{}{}", - pre, - ei.callee.name(), - post), - None)); - let ss = cm.span_to_string(ei.call_site); - try!(self.print_diagnostic(&ss, Note, "expansion site", None)); - Ok(Some(ei.call_site)) + let mut last_span = codemap::DUMMY_SP; + let mut sp_opt = Some(sp); + + while let Some(sp) = sp_opt { + sp_opt = try!(cm.with_expn_info(sp.expn_id, |expn_info| -> io::Result<_> { + match expn_info { + Some(ei) => { + let (pre, post) = match ei.callee.format { + codemap::MacroAttribute(..) => ("#[", "]"), + codemap::MacroBang(..) => ("", "!"), + codemap::CompilerExpansion(..) => ("", ""), + }; + // Don't print recursive invocations + if ei.call_site != last_span { + last_span = ei.call_site; + + let mut diag_string = format!("in this expansion of {}{}{}", + pre, + ei.callee.name(), + post); + + if let Some(def_site_span) = ei.callee.span { + diag_string.push_str(&format!(" (defined in {})", + cm.span_to_filename(def_site_span))); + } + + try!(self.print_diagnostic(&cm.span_to_string(ei.call_site), + Note, + &diag_string, + None)); + } + Ok(Some(ei.call_site)) + } + None => Ok(None) } - None => Ok(None) + })); } - })); - cs.map_or(Ok(()), |call_site| self.print_macro_backtrace(cm, call_site)) + + Ok(()) } } |
