Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix an ICE in macro's diagnostic message #66264

Merged
merged 1 commit into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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