From 8544db0faa9e0f7a70323ad5f3e75358bba6820d Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Tue, 23 Oct 2018 10:07:11 -0700 Subject: Add macro call span when lacking any other span in diagnostic --- src/libsyntax/ext/tt/macro_rules.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 87ade278c68..b70a7989275 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -50,7 +50,12 @@ pub struct ParserAnyMacro<'a> { impl<'a> ParserAnyMacro<'a> { pub fn make(mut self: Box>, kind: AstFragmentKind) -> AstFragment { let ParserAnyMacro { site_span, macro_ident, ref mut parser } = *self; - let fragment = panictry!(parser.parse_ast_fragment(kind, true)); + let fragment = panictry!(parser.parse_ast_fragment(kind, true).map_err(|mut e| { + if e.span.is_dummy() { // Get around lack of span in error (#30128) + e.set_span(site_span); + } + e + })); // We allow semicolons at the end of expressions -- e.g. the semicolon in // `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`, -- cgit 1.4.1-3-g733a5 From ad144ac3c1da0d4a25d8ca95113ac6f29bc0fce5 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Tue, 23 Oct 2018 10:07:34 -0700 Subject: Modify invalid macro in expression context diagnostic --- src/libsyntax/ext/expand.rs | 24 +++++++++++++++--- src/test/ui/issues/issue-30007.stderr | 9 +++---- src/test/ui/macros/macro-context.stderr | 29 +++++++++++----------- .../ui/macros/macro-in-expression-context.fixed | 15 +++++++++++ src/test/ui/macros/macro-in-expression-context.rs | 15 +++++++++++ .../ui/macros/macro-in-expression-context.stderr | 15 +++++++++++ .../ui/parser/macro/macro-incomplete-parse.stderr | 18 ++++++-------- 7 files changed, 91 insertions(+), 34 deletions(-) create mode 100644 src/test/ui/macros/macro-in-expression-context.fixed create mode 100644 src/test/ui/macros/macro-in-expression-context.rs create mode 100644 src/test/ui/macros/macro-in-expression-context.stderr (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 9e06384f5a8..436e0b53a76 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1036,10 +1036,26 @@ impl<'a> Parser<'a> { // Avoid emitting backtrace info twice. let def_site_span = self.span.with_ctxt(SyntaxContext::empty()); let mut err = self.diagnostic().struct_span_err(def_site_span, &msg); - let msg = format!("caused by the macro expansion here; the usage \ - of `{}!` is likely invalid in {} context", - macro_path, kind_name); - err.span_note(span, &msg).emit(); + err.span_label(span, "caused by the macro expansion here"); + let msg = format!( + "the usage of `{}!` is likely invalid in {} context", + macro_path, + kind_name, + ); + err.note(&msg); + let semi_span = self.sess.source_map().next_point(span); + match self.sess.source_map().span_to_snippet(semi_span) { + Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => { + err.span_suggestion_with_applicability( + semi_span, + "you might be missing a semicolon here", + ";".to_owned(), + Applicability::MaybeIncorrect, + ); + } + _ => {} + } + err.emit(); } } } diff --git a/src/test/ui/issues/issue-30007.stderr b/src/test/ui/issues/issue-30007.stderr index a467ff6dd0a..028ed048d65 100644 --- a/src/test/ui/issues/issue-30007.stderr +++ b/src/test/ui/issues/issue-30007.stderr @@ -3,12 +3,11 @@ error: macro expansion ignores token `;` and any following | LL | () => ( String ; ); //~ ERROR macro expansion ignores token `;` | ^ - | -note: caused by the macro expansion here; the usage of `t!` is likely invalid in type context - --> $DIR/issue-30007.rs:16:16 - | +... LL | let i: Vec; - | ^^^^ + | ---- caused by the macro expansion here + | + = note: the usage of `t!` is likely invalid in type context error: aborting due to previous error diff --git a/src/test/ui/macros/macro-context.stderr b/src/test/ui/macros/macro-context.stderr index b3e67fb2607..5ea7825080c 100644 --- a/src/test/ui/macros/macro-context.stderr +++ b/src/test/ui/macros/macro-context.stderr @@ -3,36 +3,35 @@ error: macro expansion ignores token `;` and any following | LL | () => ( i ; typeof ); //~ ERROR expected expression, found reserved keyword `typeof` | ^ - | -note: caused by the macro expansion here; the usage of `m!` is likely invalid in type context - --> $DIR/macro-context.rs:20:12 - | +... LL | let a: m!(); - | ^^^^ + | ---- caused by the macro expansion here + | + = note: the usage of `m!` is likely invalid in type context error: macro expansion ignores token `typeof` and any following --> $DIR/macro-context.rs:13:17 | LL | () => ( i ; typeof ); //~ ERROR expected expression, found reserved keyword `typeof` | ^^^^^^ - | -note: caused by the macro expansion here; the usage of `m!` is likely invalid in expression context - --> $DIR/macro-context.rs:21:13 - | +... LL | let i = m!(); - | ^^^^ + | ----- help: you might be missing a semicolon here: `;` + | | + | caused by the macro expansion here + | + = note: the usage of `m!` is likely invalid in expression context error: macro expansion ignores token `;` and any following --> $DIR/macro-context.rs:13:15 | LL | () => ( i ; typeof ); //~ ERROR expected expression, found reserved keyword `typeof` | ^ - | -note: caused by the macro expansion here; the usage of `m!` is likely invalid in pattern context - --> $DIR/macro-context.rs:23:9 - | +... LL | m!() => {} - | ^^^^ + | ---- caused by the macro expansion here + | + = note: the usage of `m!` is likely invalid in pattern context error: expected expression, found reserved keyword `typeof` --> $DIR/macro-context.rs:13:17 diff --git a/src/test/ui/macros/macro-in-expression-context.fixed b/src/test/ui/macros/macro-in-expression-context.fixed new file mode 100644 index 00000000000..df36db0f49e --- /dev/null +++ b/src/test/ui/macros/macro-in-expression-context.fixed @@ -0,0 +1,15 @@ +// run-rustfix + +macro_rules! foo { + () => { + assert_eq!("A", "A"); + assert_eq!("B", "B"); + } + //~^^ ERROR macro expansion ignores token `assert_eq` and any following + //~| NOTE the usage of `foo!` is likely invalid in expression context +} + +fn main() { + foo!(); + //~^ NOTE caused by the macro expansion here +} diff --git a/src/test/ui/macros/macro-in-expression-context.rs b/src/test/ui/macros/macro-in-expression-context.rs new file mode 100644 index 00000000000..b3f5e568967 --- /dev/null +++ b/src/test/ui/macros/macro-in-expression-context.rs @@ -0,0 +1,15 @@ +// run-rustfix + +macro_rules! foo { + () => { + assert_eq!("A", "A"); + assert_eq!("B", "B"); + } + //~^^ ERROR macro expansion ignores token `assert_eq` and any following + //~| NOTE the usage of `foo!` is likely invalid in expression context +} + +fn main() { + foo!() + //~^ NOTE caused by the macro expansion here +} diff --git a/src/test/ui/macros/macro-in-expression-context.stderr b/src/test/ui/macros/macro-in-expression-context.stderr new file mode 100644 index 00000000000..d27d6fbaef7 --- /dev/null +++ b/src/test/ui/macros/macro-in-expression-context.stderr @@ -0,0 +1,15 @@ +error: macro expansion ignores token `assert_eq` and any following + --> $DIR/macro-in-expression-context.rs:6:9 + | +LL | assert_eq!("B", "B"); + | ^^^^^^^^^ +... +LL | foo!() + | ------- help: you might be missing a semicolon here: `;` + | | + | caused by the macro expansion here + | + = note: the usage of `foo!` is likely invalid in expression context + +error: aborting due to previous error + diff --git a/src/test/ui/parser/macro/macro-incomplete-parse.stderr b/src/test/ui/parser/macro/macro-incomplete-parse.stderr index 198730dc07a..806aca511d0 100644 --- a/src/test/ui/parser/macro/macro-incomplete-parse.stderr +++ b/src/test/ui/parser/macro/macro-incomplete-parse.stderr @@ -3,12 +3,11 @@ error: macro expansion ignores token `,` and any following | LL | , //~ ERROR macro expansion ignores token `,` | ^ - | -note: caused by the macro expansion here; the usage of `ignored_item!` is likely invalid in item context - --> $DIR/macro-incomplete-parse.rs:31:1 - | +... LL | ignored_item!(); - | ^^^^^^^^^^^^^^^^ + | ---------------- caused by the macro expansion here + | + = note: the usage of `ignored_item!` is likely invalid in item context error: expected one of `.`, `;`, `?`, `}`, or an operator, found `,` --> $DIR/macro-incomplete-parse.rs:22:14 @@ -24,12 +23,11 @@ error: macro expansion ignores token `,` and any following | LL | () => ( 1, 2 ) //~ ERROR macro expansion ignores token `,` | ^ - | -note: caused by the macro expansion here; the usage of `ignored_pat!` is likely invalid in pattern context - --> $DIR/macro-incomplete-parse.rs:36:9 - | +... LL | ignored_pat!() => (), - | ^^^^^^^^^^^^^^ + | -------------- caused by the macro expansion here + | + = note: the usage of `ignored_pat!` is likely invalid in pattern context error: aborting due to 3 previous errors -- cgit 1.4.1-3-g733a5 From f8818cbf8fc30ac626b2a0a306736c1293257209 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 24 Oct 2018 12:52:24 -0700 Subject: Fix incorrect semicolon suggestion --- src/libsyntax/ext/expand.rs | 4 +++- src/test/ui/macros/macro-context.stderr | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 436e0b53a76..4deeb4a43d9 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1044,7 +1044,9 @@ impl<'a> Parser<'a> { ); err.note(&msg); let semi_span = self.sess.source_map().next_point(span); - match self.sess.source_map().span_to_snippet(semi_span) { + + let semi_full_span = semi_span.to(self.sess.source_map().next_point(semi_span)); + match self.sess.source_map().span_to_snippet(semi_full_span) { Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => { err.span_suggestion_with_applicability( semi_span, diff --git a/src/test/ui/macros/macro-context.stderr b/src/test/ui/macros/macro-context.stderr index 5ea7825080c..005e1d1c8e7 100644 --- a/src/test/ui/macros/macro-context.stderr +++ b/src/test/ui/macros/macro-context.stderr @@ -16,9 +16,7 @@ LL | () => ( i ; typeof ); //~ ERROR expected expression, found reserved k | ^^^^^^ ... LL | let i = m!(); - | ----- help: you might be missing a semicolon here: `;` - | | - | caused by the macro expansion here + | ---- caused by the macro expansion here | = note: the usage of `m!` is likely invalid in expression context -- cgit 1.4.1-3-g733a5