Skip to content

Commit 5abd6d9

Browse files
committed
add suggestions to invalid macro item error
1 parent cf6d881 commit 5abd6d9

File tree

5 files changed

+85
-24
lines changed

5 files changed

+85
-24
lines changed

src/libsyntax/parse/parser.rs

+25-20
Original file line numberDiff line numberDiff line change
@@ -5116,12 +5116,8 @@ impl<'a> Parser<'a> {
51165116

51175117
let ident = self.parse_ident()?;
51185118
let (delim, tokens) = self.expect_delimited_token_tree()?;
5119-
if delim != MacDelimiter::Brace {
5120-
if !self.eat(&token::Semi) {
5121-
let msg = "macros that expand to items must either \
5122-
be surrounded with braces or followed by a semicolon";
5123-
self.span_err(self.prev_span, msg);
5124-
}
5119+
if delim != MacDelimiter::Brace && !self.eat(&token::Semi) {
5120+
self.report_invalid_macro_expansion_item();
51255121
}
51265122

51275123
(ident, ast::MacroDef { tokens: tokens, legacy: true })
@@ -5264,13 +5260,8 @@ impl<'a> Parser<'a> {
52645260
// if it has a special ident, it's definitely an item
52655261
//
52665262
// Require a semicolon or braces.
5267-
if style != MacStmtStyle::Braces {
5268-
if !self.eat(&token::Semi) {
5269-
self.span_err(self.prev_span,
5270-
"macros that expand to items must \
5271-
either be surrounded with braces or \
5272-
followed by a semicolon");
5273-
}
5263+
if style != MacStmtStyle::Braces && !self.eat(&token::Semi) {
5264+
self.report_invalid_macro_expansion_item();
52745265
}
52755266
let span = lo.to(hi);
52765267
Stmt {
@@ -8360,13 +8351,8 @@ impl<'a> Parser<'a> {
83608351
};
83618352
// eat a matched-delimiter token tree:
83628353
let (delim, tts) = self.expect_delimited_token_tree()?;
8363-
if delim != MacDelimiter::Brace {
8364-
if !self.eat(&token::Semi) {
8365-
self.span_err(self.prev_span,
8366-
"macros that expand to items must either \
8367-
be surrounded with braces or followed by \
8368-
a semicolon");
8369-
}
8354+
if delim != MacDelimiter::Brace && !self.eat(&token::Semi) {
8355+
self.report_invalid_macro_expansion_item();
83708356
}
83718357

83728358
let hi = self.prev_span;
@@ -8597,6 +8583,25 @@ impl<'a> Parser<'a> {
85978583
}
85988584
}
85998585
}
8586+
8587+
fn report_invalid_macro_expansion_item(&self) {
8588+
self.struct_span_err(
8589+
self.prev_span,
8590+
"macros that expand to items must be delimited with braces or followed by a semicolon",
8591+
).multipart_suggestion(
8592+
"change the delimiters to curly braces",
8593+
vec![
8594+
(self.prev_span.with_hi(self.prev_span.lo() + BytePos(1)), String::from(" {")),
8595+
(self.prev_span.with_lo(self.prev_span.hi() - BytePos(1)), '}'.to_string()),
8596+
],
8597+
Applicability::MaybeIncorrect,
8598+
).span_suggestion(
8599+
self.sess.source_map.next_point(self.prev_span),
8600+
"add a semicolon",
8601+
';'.to_string(),
8602+
Applicability::MaybeIncorrect,
8603+
).emit();
8604+
}
86008605
}
86018606

86028607
pub fn emit_unclosed_delims(unclosed_delims: &mut Vec<UnmatchedBrace>, handler: &errors::Handler) {

src/test/ui/issues/issue-10536.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn main() {
1212
foo!();
1313

1414
assert!({one! two()});
15-
//~^ ERROR macros that expand to items must either be surrounded with braces or followed by a
15+
//~^ ERROR macros that expand to items
1616
//~| ERROR cannot find macro `one!` in this scope
1717
//~| ERROR mismatched types
1818

src/test/ui/issues/issue-10536.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
error: macros that expand to items must either be surrounded with braces or followed by a semicolon
1+
error: macros that expand to items must be delimited with braces or followed by a semicolon
22
--> $DIR/issue-10536.rs:14:22
33
|
44
LL | assert!({one! two()});
55
| ^^
6+
help: change the delimiters to curly braces
7+
|
8+
LL | assert!({one! two {}});
9+
| ^^
10+
help: add a semicolon
11+
|
12+
LL | assert!({one! two();});
13+
| ^
614

715
error: expected `(` or `{`, found `}`
816
--> $DIR/issue-10536.rs:21:22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
macro_rules! foo() //~ ERROR semicolon
2+
//~| ERROR unexpected end of macro
3+
4+
macro_rules! bar {
5+
($($tokens:tt)*) => {}
6+
}
7+
8+
bar!( //~ ERROR semicolon
9+
blah
10+
blah
11+
blah
12+
)
213

314
fn main() {
415
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,45 @@
1-
error: macros that expand to items must either be surrounded with braces or followed by a semicolon
1+
error: macros that expand to items must be delimited with braces or followed by a semicolon
22
--> $DIR/macros-no-semicolon-items.rs:1:17
33
|
44
LL | macro_rules! foo()
55
| ^^
6+
help: change the delimiters to curly braces
7+
|
8+
LL | macro_rules! foo {}
9+
| ^^
10+
help: add a semicolon
11+
|
12+
LL | macro_rules! foo();
13+
| ^
14+
15+
error: macros that expand to items must be delimited with braces or followed by a semicolon
16+
--> $DIR/macros-no-semicolon-items.rs:8:5
17+
|
18+
LL | bar!(
19+
| _____^
20+
LL | | blah
21+
LL | | blah
22+
LL | | blah
23+
LL | | )
24+
| |_^
25+
help: change the delimiters to curly braces
26+
|
27+
LL | bar! {
28+
LL | blah
29+
LL | blah
30+
LL | blah
31+
LL | }
32+
|
33+
help: add a semicolon
34+
|
35+
LL | );
36+
| ^
37+
38+
error: unexpected end of macro invocation
39+
--> $DIR/macros-no-semicolon-items.rs:1:1
40+
|
41+
LL | macro_rules! foo()
42+
| ^^^^^^^^^^^^^^^^^^ missing tokens in macro arguments
643

7-
error: aborting due to previous error
44+
error: aborting due to 3 previous errors
845

0 commit comments

Comments
 (0)