Rollup merge of #66264 - guanqun:fix-mbe-missing-close-delim, 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
This commit is contained in:
Yuki Okushi 2019-11-14 14:16:17 +09:00 committed by GitHub
commit 187e9118cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 5 deletions

View File

@ -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(

View File

@ -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

View File

@ -0,0 +1,3 @@
// ignore-tidy-trailing-newlines
// error-pattern: aborting due to 3 previous errors
macro_rules! abc(ؼ

View File

@ -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