Skip to content

Commit 187e911

Browse files
authored
Rollup merge of rust-lang#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
2 parents 79e2afc + 292ba98 commit 187e911

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

src/librustc_parse/parser/item.rs

Lines changed: 13 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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
Lines changed: 3 additions & 0 deletions
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(ؼ
Lines changed: 31 additions & 0 deletions
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)