From 1ed962b9e34013942304a64bffef786f61c61f9d Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 25 Aug 2017 20:46:49 +0200 Subject: [PATCH 1/6] Fix #43493 (new trace_macros doesn't work if there's an error during expansion) --- src/libsyntax/ext/expand.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index d1172b1b2ce94..ab0fd678e66f4 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -391,6 +391,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { "consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate", suggested_limit)); err.emit(); + self.cx.trace_macros_diag(); panic!(FatalError); } @@ -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) } } @@ -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())) @@ -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())) @@ -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); }; @@ -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); } @@ -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); } @@ -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) }) } @@ -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) } } @@ -691,6 +702,7 @@ impl<'a> Parser<'a> { of `{}!` is likely invalid in {} context", macro_path, kind_name); err.span_note(span, &msg).emit(); + self.cx.trace_macros_diag(); } } } @@ -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 }) From 878013cd0c33e0aede78a6d0b5a579d9c2733844 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 27 Aug 2017 20:05:26 +0200 Subject: [PATCH 2/6] Fix error --- src/libsyntax/ext/expand.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index ab0fd678e66f4..414b248a26716 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -702,7 +702,6 @@ impl<'a> Parser<'a> { of `{}!` is likely invalid in {} context", macro_path, kind_name); err.span_note(span, &msg).emit(); - self.cx.trace_macros_diag(); } } } From 8bb7dba9c7fee3e55a6aebe73a1e653a3af9f81a Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 2 Sep 2017 17:21:13 +0200 Subject: [PATCH 3/6] Dont abort on first macro error --- src/libsyntax/ext/tt/macro_rules.rs | 4 +- .../ui/macros/assert_eq_trailing_comma.stderr | 2 + .../ui/macros/assert_ne_trailing_comma.stderr | 2 + src/test/ui/macros/trace_faulty_macros.rs | 55 +++++++++++++ src/test/ui/macros/trace_faulty_macros.stderr | 77 +++++++++++++++++++ 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/macros/trace_faulty_macros.rs create mode 100644 src/test/ui/macros/trace_faulty_macros.stderr diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 983b19c5bf073..be6571d7e5550 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -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: diff --git a/src/test/ui/macros/assert_eq_trailing_comma.stderr b/src/test/ui/macros/assert_eq_trailing_comma.stderr index ca590db90e43f..1b46e94584e6b 100644 --- a/src/test/ui/macros/assert_eq_trailing_comma.stderr +++ b/src/test/ui/macros/assert_eq_trailing_comma.stderr @@ -4,3 +4,5 @@ error: unexpected end of macro invocation 12 | assert_eq!(1, 1,); | ^ +error: aborting due to previous error + diff --git a/src/test/ui/macros/assert_ne_trailing_comma.stderr b/src/test/ui/macros/assert_ne_trailing_comma.stderr index ffabcaeb0493c..33d2cb0ed8242 100644 --- a/src/test/ui/macros/assert_ne_trailing_comma.stderr +++ b/src/test/ui/macros/assert_ne_trailing_comma.stderr @@ -4,3 +4,5 @@ error: unexpected end of macro invocation 12 | assert_ne!(1, 2,); | ^ +error: aborting due to previous error + diff --git a/src/test/ui/macros/trace_faulty_macros.rs b/src/test/ui/macros/trace_faulty_macros.rs new file mode 100644 index 0000000000000..3b2d7ee5b757d --- /dev/null +++ b/src/test/ui/macros/trace_faulty_macros.rs @@ -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 or the MIT license +// , 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! nested_pat_macro { + () => { + nested_pat_macro!(inner); + }; + (inner) => { + a | b | 1 ... 3 | _ + } +} + +macro_rules! my_recursive_macro { + () => { + my_recursive_macro!(); + }; +} + +macro_rules! my_macro { + () => { + + }; +} + +fn main() { + my_faulty_macro!(); + nested_pat_macro!(); + my_recursive_macro!(); + test!(); + non_exisiting!(); + derive!(Debug); +} + +#[my_macro] +fn use_bang_macro_as_attr(){} + +#[derive(Debug)] +fn use_derive_macro_as_attr(){} diff --git a/src/test/ui/macros/trace_faulty_macros.stderr b/src/test/ui/macros/trace_faulty_macros.stderr new file mode 100644 index 0000000000000..1fd8511f181ef --- /dev/null +++ b/src/test/ui/macros/trace_faulty_macros.stderr @@ -0,0 +1,77 @@ +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: expected expression, found `_` + --> $DIR/trace_faulty_macros.rs:26:27 + | +26 | a | b | 1 ... 3 | _ + | ^ +... +44 | nested_pat_macro!(); + | -------------------- in this macro invocation + +error: recursion limit reached while expanding the macro `my_recursive_macro` + --> $DIR/trace_faulty_macros.rs:32:9 + | +32 | my_recursive_macro!(); + | ^^^^^^^^^^^^^^^^^^^^^^ +... +45 | 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:45:5 + | +45 | 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 ! ( ) ;` + +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 }` + +note: trace_macro + --> $DIR/trace_faulty_macros.rs:44:5 + | +44 | nested_pat_macro!(); + | ^^^^^^^^^^^^^^^^^^^^ + | + = note: expanding `nested_pat_macro! { }` + = note: to `nested_pat_macro ! ( inner ) ;` + = note: expanding `nested_pat_macro! { inner }` + = note: to `a | b | 1 ... 3 | _` + From 8b71e0bbd596ada5d2e9c00d9249756d04607793 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 2 Sep 2017 18:13:25 +0200 Subject: [PATCH 4/6] Better trace-macro and less span_err_fatal --- src/libsyntax/ext/base.rs | 4 +- src/libsyntax/ext/expand.rs | 3 +- src/test/ui/macros/trace_faulty_macros.rs | 12 +++--- src/test/ui/macros/trace_faulty_macros.stderr | 37 ++----------------- 4 files changed, 15 insertions(+), 41 deletions(-) diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index e57d9c6fe896a..402cdfe27faf0 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -792,7 +792,7 @@ 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 { @@ -800,6 +800,8 @@ impl<'a> ExtCtxt<'a> { } db.emit(); } + // Fixme: does this result in errors? + self.expansions.clear(); } pub fn bug(&self, msg: &str) -> ! { self.parse_sess.span_diagnostic.bug(msg); diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 414b248a26716..7bc9f6b2d8fd1 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -384,7 +384,7 @@ 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!( @@ -640,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); } }; diff --git a/src/test/ui/macros/trace_faulty_macros.rs b/src/test/ui/macros/trace_faulty_macros.rs index 3b2d7ee5b757d..a5b42dc191ed3 100644 --- a/src/test/ui/macros/trace_faulty_macros.rs +++ b/src/test/ui/macros/trace_faulty_macros.rs @@ -18,13 +18,13 @@ macro_rules! my_faulty_macro { }; } -macro_rules! nested_pat_macro { +macro_rules! pat_macro { () => { - nested_pat_macro!(inner); + pat_macro!(A{a:a, b:0, c:_, ..}); + }; + ($a:pat) => { + $a }; - (inner) => { - a | b | 1 ... 3 | _ - } } macro_rules! my_recursive_macro { @@ -41,11 +41,11 @@ macro_rules! my_macro { fn main() { my_faulty_macro!(); - nested_pat_macro!(); my_recursive_macro!(); test!(); non_exisiting!(); derive!(Debug); + let a = pat_macro!(); } #[my_macro] diff --git a/src/test/ui/macros/trace_faulty_macros.stderr b/src/test/ui/macros/trace_faulty_macros.stderr index 1fd8511f181ef..bc289f8daa84d 100644 --- a/src/test/ui/macros/trace_faulty_macros.stderr +++ b/src/test/ui/macros/trace_faulty_macros.stderr @@ -17,30 +17,21 @@ note: trace_macro = note: to `my_faulty_macro ! ( bcd ) ;` = note: expanding `my_faulty_macro! { bcd }` -error: expected expression, found `_` - --> $DIR/trace_faulty_macros.rs:26:27 - | -26 | a | b | 1 ... 3 | _ - | ^ -... -44 | nested_pat_macro!(); - | -------------------- in this macro invocation - error: recursion limit reached while expanding the macro `my_recursive_macro` --> $DIR/trace_faulty_macros.rs:32:9 | 32 | my_recursive_macro!(); | ^^^^^^^^^^^^^^^^^^^^^^ ... -45 | 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:45:5 + --> $DIR/trace_faulty_macros.rs:44:5 | -45 | my_recursive_macro!(); +44 | my_recursive_macro!(); | ^^^^^^^^^^^^^^^^^^^^^^ | = note: expanding `my_recursive_macro! { }` @@ -54,24 +45,4 @@ note: trace_macro = note: expanding `my_recursive_macro! { }` = note: to `my_recursive_macro ! ( ) ;` -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 }` - -note: trace_macro - --> $DIR/trace_faulty_macros.rs:44:5 - | -44 | nested_pat_macro!(); - | ^^^^^^^^^^^^^^^^^^^^ - | - = note: expanding `nested_pat_macro! { }` - = note: to `nested_pat_macro ! ( inner ) ;` - = note: expanding `nested_pat_macro! { inner }` - = note: to `a | b | 1 ... 3 | _` - + \ No newline at end of file From 30603eee57a25624b06150575daa7802e6dae743 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 2 Sep 2017 18:14:14 +0200 Subject: [PATCH 5/6] Fix tidy error --- src/test/ui/macros/trace_faulty_macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/macros/trace_faulty_macros.rs b/src/test/ui/macros/trace_faulty_macros.rs index a5b42dc191ed3..eb7292b0a652b 100644 --- a/src/test/ui/macros/trace_faulty_macros.rs +++ b/src/test/ui/macros/trace_faulty_macros.rs @@ -35,7 +35,7 @@ macro_rules! my_recursive_macro { macro_rules! my_macro { () => { - + }; } From 0a2c95b6eb4ef40be166cc690bf2f4f00e37d7c5 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 3 Sep 2017 10:23:00 +0200 Subject: [PATCH 6/6] Fix test --- src/test/ui/macros/trace_faulty_macros.stderr | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/ui/macros/trace_faulty_macros.stderr b/src/test/ui/macros/trace_faulty_macros.stderr index bc289f8daa84d..f4aeb8332f0b0 100644 --- a/src/test/ui/macros/trace_faulty_macros.stderr +++ b/src/test/ui/macros/trace_faulty_macros.stderr @@ -45,4 +45,3 @@ note: trace_macro = note: expanding `my_recursive_macro! { }` = note: to `my_recursive_macro ! ( ) ;` - \ No newline at end of file