Skip to content

Commit 292ba98

Browse files
author
Guanqun Lu
committed
fix an ICE in macro's diagnostic message
1 parent 1062b69 commit 292ba98

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

src/librustc_parse/parser/item.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1742,14 +1742,25 @@ impl<'a> Parser<'a> {
17421742
}
17431743

17441744
fn report_invalid_macro_expansion_item(&self) {
1745+
let has_close_delim = self.sess.source_map()
1746+
.span_to_snippet(self.prev_span)
1747+
.map(|s| s.ends_with(")") || s.ends_with("]"))
1748+
.unwrap_or(false);
1749+
let right_brace_span = if has_close_delim {
1750+
// it's safe to peel off one character only when it has the close delim
1751+
self.prev_span.with_lo(self.prev_span.hi() - BytePos(1))
1752+
} else {
1753+
self.sess.source_map().next_point(self.prev_span)
1754+
};
1755+
17451756
self.struct_span_err(
17461757
self.prev_span,
17471758
"macros that expand to items must be delimited with braces or followed by a semicolon",
17481759
).multipart_suggestion(
17491760
"change the delimiters to curly braces",
17501761
vec![
1751-
(self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), String::from(" {")),
1752-
(self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()),
1762+
(self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), "{".to_string()),
1763+
(right_brace_span, '}'.to_string()),
17531764
],
17541765
Applicability::MaybeIncorrect,
17551766
).span_suggestion(

src/test/ui/parser/macros-no-semicolon-items.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ LL | macro_rules! foo()
66
|
77
help: change the delimiters to curly braces
88
|
9-
LL | macro_rules! foo {}
10-
| ^^
9+
LL | macro_rules! foo{}
10+
| ^^
1111
help: add a semicolon
1212
|
1313
LL | macro_rules! foo();
@@ -26,7 +26,7 @@ LL | | )
2626
|
2727
help: change the delimiters to curly braces
2828
|
29-
LL | bar! {
29+
LL | bar!{
3030
LL | blah
3131
LL | blah
3232
LL | blah
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ignore-tidy-trailing-newlines
2+
// error-pattern: aborting due to 3 previous errors
3+
macro_rules! abc(ؼ
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: this file contains an un-closed delimiter
2+
--> $DIR/mbe_missing_right_paren.rs:3:19
3+
|
4+
LL | macro_rules! abc(ؼ
5+
| - ^
6+
| |
7+
| un-closed delimiter
8+
9+
error: macros that expand to items must be delimited with braces or followed by a semicolon
10+
--> $DIR/mbe_missing_right_paren.rs:3:17
11+
|
12+
LL | macro_rules! abc(ؼ
13+
| ^^
14+
|
15+
help: change the delimiters to curly braces
16+
|
17+
LL | macro_rules! abc{ؼ}
18+
| ^ ^
19+
help: add a semicolon
20+
|
21+
LL | macro_rules! abc(ؼ;
22+
| ^
23+
24+
error: unexpected end of macro invocation
25+
--> $DIR/mbe_missing_right_paren.rs:3:1
26+
|
27+
LL | macro_rules! abc(ؼ
28+
| ^^^^^^^^^^^^^^^^^^ missing tokens in macro arguments
29+
30+
error: aborting due to 3 previous errors
31+

0 commit comments

Comments
 (0)