Skip to content

Commit

Permalink
Rollup merge of rust-lang#66264 - guanqun:fix-mbe-missing-close-delim…
Browse files Browse the repository at this point in the history
…, r=estebank

fix an ICE in macro's diagnostic message

This has two small fixes:
1. for the left brace, we don't need `<space>{`, simply `{` is enough.
2. for the right brace, it tries to peel off one character even when the close delim is missing. Without this fix, it would crash in some cases. (as shown in the new test case)

r? @estebank
  • Loading branch information
JohnTitor committed Nov 14, 2019
2 parents 79e2afc + 292ba98 commit 187e911
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/librustc_parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1742,14 +1742,25 @@ impl<'a> Parser<'a> {
}

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(
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/parser/macros-no-semicolon-items.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -26,7 +26,7 @@ LL | | )
|
help: change the delimiters to curly braces
|
LL | bar! {
LL | bar!{
LL | blah
LL | blah
LL | blah
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/parser/mbe_missing_right_paren.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// ignore-tidy-trailing-newlines
// error-pattern: aborting due to 3 previous errors
macro_rules! abc(ؼ
31 changes: 31 additions & 0 deletions src/test/ui/parser/mbe_missing_right_paren.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 187e911

Please sign in to comment.