diff --git a/src/librustc_parse/parser/item.rs b/src/librustc_parse/parser/item.rs index 3e21436d313..1f386b8bbbf 100644 --- a/src/librustc_parse/parser/item.rs +++ b/src/librustc_parse/parser/item.rs @@ -1742,14 +1742,25 @@ fn complain_if_pub_macro(&self, vis: &VisibilityKind, sp: Span) { } fn report_invalid_macro_expansion_item(&self) { + let has_close_delim = self.sess.source_map() + .span_to_snippet(self.prev_span) + .map(|s| s.ends_with(")") || s.ends_with("]")) + .unwrap_or(false); + let right_brace_span = if has_close_delim { + // it's safe to peel off one character only when it has the close delim + self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)) + } else { + self.sess.source_map().next_point(self.prev_span) + }; + self.struct_span_err( self.prev_span, "macros that expand to items must be delimited with braces or followed by a semicolon", ).multipart_suggestion( "change the delimiters to curly braces", vec![ - (self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), String::from(" {")), - (self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()), + (self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), "{".to_string()), + (right_brace_span, '}'.to_string()), ], Applicability::MaybeIncorrect, ).span_suggestion( diff --git a/src/test/ui/parser/macros-no-semicolon-items.stderr b/src/test/ui/parser/macros-no-semicolon-items.stderr index 980ceeed8c6..f9019b78c8d 100644 --- a/src/test/ui/parser/macros-no-semicolon-items.stderr +++ b/src/test/ui/parser/macros-no-semicolon-items.stderr @@ -6,8 +6,8 @@ LL | macro_rules! foo() | help: change the delimiters to curly braces | -LL | macro_rules! foo {} - | ^^ +LL | macro_rules! foo{} + | ^^ help: add a semicolon | LL | macro_rules! foo(); @@ -26,7 +26,7 @@ LL | | ) | help: change the delimiters to curly braces | -LL | bar! { +LL | bar!{ LL | blah LL | blah LL | blah diff --git a/src/test/ui/parser/mbe_missing_right_paren.rs b/src/test/ui/parser/mbe_missing_right_paren.rs new file mode 100644 index 00000000000..689176b3eb7 --- /dev/null +++ b/src/test/ui/parser/mbe_missing_right_paren.rs @@ -0,0 +1,3 @@ +// ignore-tidy-trailing-newlines +// error-pattern: aborting due to 3 previous errors +macro_rules! abc(ؼ \ No newline at end of file diff --git a/src/test/ui/parser/mbe_missing_right_paren.stderr b/src/test/ui/parser/mbe_missing_right_paren.stderr new file mode 100644 index 00000000000..4504fc0eb00 --- /dev/null +++ b/src/test/ui/parser/mbe_missing_right_paren.stderr @@ -0,0 +1,31 @@ +error: this file contains an un-closed delimiter + --> $DIR/mbe_missing_right_paren.rs:3:19 + | +LL | macro_rules! abc(ؼ + | - ^ + | | + | un-closed delimiter + +error: macros that expand to items must be delimited with braces or followed by a semicolon + --> $DIR/mbe_missing_right_paren.rs:3:17 + | +LL | macro_rules! abc(ؼ + | ^^ + | +help: change the delimiters to curly braces + | +LL | macro_rules! abc{ؼ} + | ^ ^ +help: add a semicolon + | +LL | macro_rules! abc(ؼ; + | ^ + +error: unexpected end of macro invocation + --> $DIR/mbe_missing_right_paren.rs:3:1 + | +LL | macro_rules! abc(ؼ + | ^^^^^^^^^^^^^^^^^^ missing tokens in macro arguments + +error: aborting due to 3 previous errors +