Skip to content

Commit cafcdbb

Browse files
authored
Rollup merge of rust-lang#44088 - bjorn3:better_trace_macros, r=jseyfried
Fix "new trace_macros doesn't work if there's an error during expansion" Fixes rust-lang#43493
2 parents badd0bc + 0a2c95b commit cafcdbb

7 files changed

+126
-3
lines changed

src/libsyntax/ext/base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -792,14 +792,16 @@ impl<'a> ExtCtxt<'a> {
792792
pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
793793
self.parse_sess.span_diagnostic.span_bug(sp, msg);
794794
}
795-
pub fn trace_macros_diag(&self) {
795+
pub fn trace_macros_diag(&mut self) {
796796
for (sp, notes) in self.expansions.iter() {
797797
let mut db = self.parse_sess.span_diagnostic.span_note_diag(*sp, "trace_macro");
798798
for note in notes {
799799
db.note(note);
800800
}
801801
db.emit();
802802
}
803+
// Fixme: does this result in errors?
804+
self.expansions.clear();
803805
}
804806
pub fn bug(&self, msg: &str) -> ! {
805807
self.parse_sess.span_diagnostic.bug(msg);

src/libsyntax/ext/expand.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
384384
if self.cx.current_expansion.depth > self.cx.ecfg.recursion_limit {
385385
let info = self.cx.current_expansion.mark.expn_info().unwrap();
386386
let suggested_limit = self.cx.ecfg.recursion_limit * 2;
387-
let mut err = self.cx.struct_span_fatal(info.call_site,
387+
let mut err = self.cx.struct_span_err(info.call_site,
388388
&format!("recursion limit reached while expanding the macro `{}`",
389389
info.callee.name()));
390390
err.help(&format!(
391391
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
392392
suggested_limit));
393393
err.emit();
394+
self.cx.trace_macros_diag();
394395
panic!(FatalError);
395396
}
396397

@@ -439,11 +440,13 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
439440
}
440441
ProcMacroDerive(..) | BuiltinDerive(..) => {
441442
self.cx.span_err(attr.span, &format!("`{}` is a derive mode", attr.path));
443+
self.cx.trace_macros_diag();
442444
kind.dummy(attr.span)
443445
}
444446
_ => {
445447
let msg = &format!("macro `{}` may not be used in attributes", attr.path);
446448
self.cx.span_err(attr.span, msg);
449+
self.cx.trace_macros_diag();
447450
kind.dummy(attr.span)
448451
}
449452
}
@@ -482,6 +485,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
482485
if let Err(msg) = validate_and_set_expn_info(def_span.map(|(_, s)| s),
483486
false, false) {
484487
self.cx.span_err(path.span, &msg);
488+
self.cx.trace_macros_diag();
485489
return kind.dummy(span);
486490
}
487491
kind.make_from(expand.expand(self.cx, span, mac.node.stream()))
@@ -497,6 +501,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
497501
allow_internal_unstable,
498502
allow_internal_unsafe) {
499503
self.cx.span_err(path.span, &msg);
504+
self.cx.trace_macros_diag();
500505
return kind.dummy(span);
501506
}
502507
kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
@@ -506,6 +511,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
506511
if ident.name == keywords::Invalid.name() {
507512
self.cx.span_err(path.span,
508513
&format!("macro {}! expects an ident argument", path));
514+
self.cx.trace_macros_diag();
509515
return kind.dummy(span);
510516
};
511517

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

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

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

@@ -564,6 +573,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
564573
let msg = format!("non-{kind} macro in {kind} position: {name}",
565574
name = path.segments[0].identifier.name, kind = kind.name());
566575
self.cx.span_err(path.span, &msg);
576+
self.cx.trace_macros_diag();
567577
kind.dummy(span)
568578
})
569579
}
@@ -617,6 +627,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
617627
_ => {
618628
let msg = &format!("macro `{}` may not be used for derive attributes", attr.path);
619629
self.cx.span_err(span, msg);
630+
self.cx.trace_macros_diag();
620631
kind.dummy(span)
621632
}
622633
}
@@ -629,6 +640,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
629640
Ok(expansion) => expansion,
630641
Err(mut err) => {
631642
err.emit();
643+
self.cx.trace_macros_diag();
632644
return kind.dummy(span);
633645
}
634646
};
@@ -739,6 +751,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
739751
if !traits.is_empty() &&
740752
(kind == ExpansionKind::TraitItems || kind == ExpansionKind::ImplItems) {
741753
self.cx.span_err(traits[0].span, "`derive` can be only be applied to items");
754+
self.cx.trace_macros_diag();
742755
return kind.expect_from_annotatables(::std::iter::once(item));
743756
}
744757
self.collect(kind, InvocationKind::Attr { attr: attr, traits: traits, item: item })

src/libsyntax/ext/tt/macro_rules.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
172172
}
173173

174174
let best_fail_msg = parse_failure_msg(best_fail_tok.expect("ran no matchers"));
175-
cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg);
175+
cx.span_err(best_fail_spot.substitute_dummy(sp), &best_fail_msg);
176+
cx.trace_macros_diag();
177+
DummyResult::any(sp)
176178
}
177179

178180
// Note that macro-by-example's input is also matched against a token tree:

src/test/ui/macros/assert_eq_trailing_comma.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ error: unexpected end of macro invocation
44
12 | assert_eq!(1, 1,);
55
| ^
66

7+
error: aborting due to previous error
8+

src/test/ui/macros/assert_ne_trailing_comma.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ error: unexpected end of macro invocation
44
12 | assert_ne!(1, 2,);
55
| ^
66

7+
error: aborting due to previous error
8+
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z trace-macros
12+
13+
#![recursion_limit="4"]
14+
15+
macro_rules! my_faulty_macro {
16+
() => {
17+
my_faulty_macro!(bcd);
18+
};
19+
}
20+
21+
macro_rules! pat_macro {
22+
() => {
23+
pat_macro!(A{a:a, b:0, c:_, ..});
24+
};
25+
($a:pat) => {
26+
$a
27+
};
28+
}
29+
30+
macro_rules! my_recursive_macro {
31+
() => {
32+
my_recursive_macro!();
33+
};
34+
}
35+
36+
macro_rules! my_macro {
37+
() => {
38+
39+
};
40+
}
41+
42+
fn main() {
43+
my_faulty_macro!();
44+
my_recursive_macro!();
45+
test!();
46+
non_exisiting!();
47+
derive!(Debug);
48+
let a = pat_macro!();
49+
}
50+
51+
#[my_macro]
52+
fn use_bang_macro_as_attr(){}
53+
54+
#[derive(Debug)]
55+
fn use_derive_macro_as_attr(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error: no rules expected the token `bcd`
2+
--> $DIR/trace_faulty_macros.rs:17:26
3+
|
4+
17 | my_faulty_macro!(bcd);
5+
| ^^^
6+
...
7+
43 | my_faulty_macro!();
8+
| ------------------- in this macro invocation
9+
10+
note: trace_macro
11+
--> $DIR/trace_faulty_macros.rs:43:5
12+
|
13+
43 | my_faulty_macro!();
14+
| ^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: expanding `my_faulty_macro! { }`
17+
= note: to `my_faulty_macro ! ( bcd ) ;`
18+
= note: expanding `my_faulty_macro! { bcd }`
19+
20+
error: recursion limit reached while expanding the macro `my_recursive_macro`
21+
--> $DIR/trace_faulty_macros.rs:32:9
22+
|
23+
32 | my_recursive_macro!();
24+
| ^^^^^^^^^^^^^^^^^^^^^^
25+
...
26+
44 | my_recursive_macro!();
27+
| ---------------------- in this macro invocation
28+
|
29+
= help: consider adding a `#![recursion_limit="8"]` attribute to your crate
30+
31+
note: trace_macro
32+
--> $DIR/trace_faulty_macros.rs:44:5
33+
|
34+
44 | my_recursive_macro!();
35+
| ^^^^^^^^^^^^^^^^^^^^^^
36+
|
37+
= note: expanding `my_recursive_macro! { }`
38+
= note: to `my_recursive_macro ! ( ) ;`
39+
= note: expanding `my_recursive_macro! { }`
40+
= note: to `my_recursive_macro ! ( ) ;`
41+
= note: expanding `my_recursive_macro! { }`
42+
= note: to `my_recursive_macro ! ( ) ;`
43+
= note: expanding `my_recursive_macro! { }`
44+
= note: to `my_recursive_macro ! ( ) ;`
45+
= note: expanding `my_recursive_macro! { }`
46+
= note: to `my_recursive_macro ! ( ) ;`
47+

0 commit comments

Comments
 (0)