Skip to content

Commit 2cde070

Browse files
authored
Rollup merge of #143408 - joshtriplett:fix-mbe-parser, r=compiler-errors
mbe: Gracefully handle macro rules that end after `=>` Add a test for various cases of invalid macro definitions. Closes: #143351
2 parents 567c51d + 0403990 commit 2cde070

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,15 @@ pub fn compile_declarative_macro(
411411
if let Err(e) = p.expect(exp!(FatArrow)) {
412412
return dummy_syn_ext(e.emit());
413413
}
414+
if p.token == token::Eof {
415+
let err_sp = p.token.span.shrink_to_hi();
416+
let guar = sess
417+
.dcx()
418+
.struct_span_err(err_sp, "macro definition ended unexpectedly")
419+
.with_span_label(err_sp, "expected right-hand side of macro rule")
420+
.emit();
421+
return dummy_syn_ext(guar);
422+
}
414423
let rhs_tt = p.parse_token_tree();
415424
let rhs_tt = mbe::quoted::parse(
416425
&TokenStream::new(vec![rhs_tt]),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![crate_type = "lib"]
2+
3+
macro_rules! a { {} => }
4+
//~^ ERROR: macro definition ended unexpectedly
5+
6+
macro_rules! b { 0 => }
7+
//~^ ERROR: macro definition ended unexpectedly
8+
//~| ERROR: invalid macro matcher
9+
10+
macro_rules! c { x => }
11+
//~^ ERROR: macro definition ended unexpectedly
12+
//~| ERROR: invalid macro matcher
13+
14+
macro_rules! d { _ => }
15+
//~^ ERROR: macro definition ended unexpectedly
16+
//~| ERROR: invalid macro matcher
17+
18+
macro_rules! e { {} }
19+
//~^ ERROR: expected `=>`, found end of macro arguments
20+
21+
macro_rules! f {}
22+
//~^ ERROR: macros must contain at least one rule
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
error: macro definition ended unexpectedly
2+
--> $DIR/bad-macro-definition.rs:3:23
3+
|
4+
LL | macro_rules! a { {} => }
5+
| ^ expected right-hand side of macro rule
6+
7+
error: invalid macro matcher; matchers must be contained in balanced delimiters
8+
--> $DIR/bad-macro-definition.rs:6:18
9+
|
10+
LL | macro_rules! b { 0 => }
11+
| ^
12+
13+
error: macro definition ended unexpectedly
14+
--> $DIR/bad-macro-definition.rs:6:22
15+
|
16+
LL | macro_rules! b { 0 => }
17+
| ^ expected right-hand side of macro rule
18+
19+
error: invalid macro matcher; matchers must be contained in balanced delimiters
20+
--> $DIR/bad-macro-definition.rs:10:18
21+
|
22+
LL | macro_rules! c { x => }
23+
| ^
24+
25+
error: macro definition ended unexpectedly
26+
--> $DIR/bad-macro-definition.rs:10:22
27+
|
28+
LL | macro_rules! c { x => }
29+
| ^ expected right-hand side of macro rule
30+
31+
error: invalid macro matcher; matchers must be contained in balanced delimiters
32+
--> $DIR/bad-macro-definition.rs:14:18
33+
|
34+
LL | macro_rules! d { _ => }
35+
| ^
36+
37+
error: macro definition ended unexpectedly
38+
--> $DIR/bad-macro-definition.rs:14:22
39+
|
40+
LL | macro_rules! d { _ => }
41+
| ^ expected right-hand side of macro rule
42+
43+
error: expected `=>`, found end of macro arguments
44+
--> $DIR/bad-macro-definition.rs:18:20
45+
|
46+
LL | macro_rules! e { {} }
47+
| ^ expected `=>`
48+
49+
error: macros must contain at least one rule
50+
--> $DIR/bad-macro-definition.rs:21:1
51+
|
52+
LL | macro_rules! f {}
53+
| ^^^^^^^^^^^^^^^^^
54+
55+
error: aborting due to 9 previous errors
56+

0 commit comments

Comments
 (0)