Skip to content

Commit

Permalink
Rollup merge of rust-lang#44088 - bjorn3:better_trace_macros, r=jseyf…
Browse files Browse the repository at this point in the history
…ried

Fix "new trace_macros doesn't work if there's an error during expansion"

Fixes rust-lang#43493
  • Loading branch information
TimNN committed Sep 17, 2017
2 parents f20fbab + 0a2c95b commit ef1f04f
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,14 +792,16 @@ impl<'a> ExtCtxt<'a> {
pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
self.parse_sess.span_diagnostic.span_bug(sp, msg);
}
pub fn trace_macros_diag(&self) {
pub fn trace_macros_diag(&mut self) {
for (sp, notes) in self.expansions.iter() {
let mut db = self.parse_sess.span_diagnostic.span_note_diag(*sp, "trace_macro");
for note in notes {
db.note(note);
}
db.emit();
}
// Fixme: does this result in errors?
self.expansions.clear();
}
pub fn bug(&self, msg: &str) -> ! {
self.parse_sess.span_diagnostic.bug(msg);
Expand Down
15 changes: 14 additions & 1 deletion src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
if self.cx.current_expansion.depth > self.cx.ecfg.recursion_limit {
let info = self.cx.current_expansion.mark.expn_info().unwrap();
let suggested_limit = self.cx.ecfg.recursion_limit * 2;
let mut err = self.cx.struct_span_fatal(info.call_site,
let mut err = self.cx.struct_span_err(info.call_site,
&format!("recursion limit reached while expanding the macro `{}`",
info.callee.name()));
err.help(&format!(
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
suggested_limit));
err.emit();
self.cx.trace_macros_diag();
panic!(FatalError);
}

Expand Down Expand Up @@ -439,11 +440,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}
ProcMacroDerive(..) | BuiltinDerive(..) => {
self.cx.span_err(attr.span, &format!("`{}` is a derive mode", attr.path));
self.cx.trace_macros_diag();
kind.dummy(attr.span)
}
_ => {
let msg = &format!("macro `{}` may not be used in attributes", attr.path);
self.cx.span_err(attr.span, msg);
self.cx.trace_macros_diag();
kind.dummy(attr.span)
}
}
Expand Down Expand Up @@ -482,6 +485,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
if let Err(msg) = validate_and_set_expn_info(def_span.map(|(_, s)| s),
false, false) {
self.cx.span_err(path.span, &msg);
self.cx.trace_macros_diag();
return kind.dummy(span);
}
kind.make_from(expand.expand(self.cx, span, mac.node.stream()))
Expand All @@ -497,6 +501,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
allow_internal_unstable,
allow_internal_unsafe) {
self.cx.span_err(path.span, &msg);
self.cx.trace_macros_diag();
return kind.dummy(span);
}
kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
Expand All @@ -506,6 +511,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
if ident.name == keywords::Invalid.name() {
self.cx.span_err(path.span,
&format!("macro {}! expects an ident argument", path));
self.cx.trace_macros_diag();
return kind.dummy(span);
};

Expand All @@ -526,11 +532,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
MultiDecorator(..) | MultiModifier(..) | AttrProcMacro(..) => {
self.cx.span_err(path.span,
&format!("`{}` can only be used in attributes", path));
self.cx.trace_macros_diag();
return kind.dummy(span);
}

ProcMacroDerive(..) | BuiltinDerive(..) => {
self.cx.span_err(path.span, &format!("`{}` is a derive mode", path));
self.cx.trace_macros_diag();
return kind.dummy(span);
}

Expand All @@ -539,6 +547,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
let msg =
format!("macro {}! expects no ident argument, given '{}'", path, ident);
self.cx.span_err(path.span, &msg);
self.cx.trace_macros_diag();
return kind.dummy(span);
}

Expand All @@ -564,6 +573,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
let msg = format!("non-{kind} macro in {kind} position: {name}",
name = path.segments[0].identifier.name, kind = kind.name());
self.cx.span_err(path.span, &msg);
self.cx.trace_macros_diag();
kind.dummy(span)
})
}
Expand Down Expand Up @@ -617,6 +627,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
_ => {
let msg = &format!("macro `{}` may not be used for derive attributes", attr.path);
self.cx.span_err(span, msg);
self.cx.trace_macros_diag();
kind.dummy(span)
}
}
Expand All @@ -629,6 +640,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
Ok(expansion) => expansion,
Err(mut err) => {
err.emit();
self.cx.trace_macros_diag();
return kind.dummy(span);
}
};
Expand Down Expand Up @@ -739,6 +751,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
if !traits.is_empty() &&
(kind == ExpansionKind::TraitItems || kind == ExpansionKind::ImplItems) {
self.cx.span_err(traits[0].span, "`derive` can be only be applied to items");
self.cx.trace_macros_diag();
return kind.expect_from_annotatables(::std::iter::once(item));
}
self.collect(kind, InvocationKind::Attr { attr: attr, traits: traits, item: item })
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax/ext/tt/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
}

let best_fail_msg = parse_failure_msg(best_fail_tok.expect("ran no matchers"));
cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg);
cx.span_err(best_fail_spot.substitute_dummy(sp), &best_fail_msg);
cx.trace_macros_diag();
DummyResult::any(sp)
}

// Note that macro-by-example's input is also matched against a token tree:
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/macros/assert_eq_trailing_comma.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ error: unexpected end of macro invocation
12 | assert_eq!(1, 1,);
| ^

error: aborting due to previous error

2 changes: 2 additions & 0 deletions src/test/ui/macros/assert_ne_trailing_comma.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ error: unexpected end of macro invocation
12 | assert_ne!(1, 2,);
| ^

error: aborting due to previous error

55 changes: 55 additions & 0 deletions src/test/ui/macros/trace_faulty_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -Z trace-macros

#![recursion_limit="4"]

macro_rules! my_faulty_macro {
() => {
my_faulty_macro!(bcd);
};
}

macro_rules! pat_macro {
() => {
pat_macro!(A{a:a, b:0, c:_, ..});
};
($a:pat) => {
$a
};
}

macro_rules! my_recursive_macro {
() => {
my_recursive_macro!();
};
}

macro_rules! my_macro {
() => {

};
}

fn main() {
my_faulty_macro!();
my_recursive_macro!();
test!();
non_exisiting!();
derive!(Debug);
let a = pat_macro!();
}

#[my_macro]
fn use_bang_macro_as_attr(){}

#[derive(Debug)]
fn use_derive_macro_as_attr(){}
47 changes: 47 additions & 0 deletions src/test/ui/macros/trace_faulty_macros.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
error: no rules expected the token `bcd`
--> $DIR/trace_faulty_macros.rs:17:26
|
17 | my_faulty_macro!(bcd);
| ^^^
...
43 | my_faulty_macro!();
| ------------------- in this macro invocation

note: trace_macro
--> $DIR/trace_faulty_macros.rs:43:5
|
43 | my_faulty_macro!();
| ^^^^^^^^^^^^^^^^^^^
|
= note: expanding `my_faulty_macro! { }`
= note: to `my_faulty_macro ! ( bcd ) ;`
= note: expanding `my_faulty_macro! { bcd }`

error: recursion limit reached while expanding the macro `my_recursive_macro`
--> $DIR/trace_faulty_macros.rs:32:9
|
32 | my_recursive_macro!();
| ^^^^^^^^^^^^^^^^^^^^^^
...
44 | my_recursive_macro!();
| ---------------------- in this macro invocation
|
= help: consider adding a `#![recursion_limit="8"]` attribute to your crate

note: trace_macro
--> $DIR/trace_faulty_macros.rs:44:5
|
44 | my_recursive_macro!();
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: expanding `my_recursive_macro! { }`
= note: to `my_recursive_macro ! ( ) ;`
= note: expanding `my_recursive_macro! { }`
= note: to `my_recursive_macro ! ( ) ;`
= note: expanding `my_recursive_macro! { }`
= note: to `my_recursive_macro ! ( ) ;`
= note: expanding `my_recursive_macro! { }`
= note: to `my_recursive_macro ! ( ) ;`
= note: expanding `my_recursive_macro! { }`
= note: to `my_recursive_macro ! ( ) ;`

0 comments on commit ef1f04f

Please sign in to comment.