Skip to content

mbe: Gracefully handle macro rules that end after => #143408

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

Merged
merged 1 commit into from
Jul 5, 2025
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
9 changes: 9 additions & 0 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,15 @@ pub fn compile_declarative_macro(
if let Err(e) = p.expect(exp!(FatArrow)) {
return dummy_syn_ext(e.emit());
}
if p.token == token::Eof {
let err_sp = p.token.span.shrink_to_hi();
let guar = sess
.dcx()
.struct_span_err(err_sp, "macro definition ended unexpectedly")
.with_span_label(err_sp, "expected right-hand side of macro rule")
.emit();
return dummy_syn_ext(guar);
}
let rhs_tt = p.parse_token_tree();
let rhs_tt = mbe::quoted::parse(
&TokenStream::new(vec![rhs_tt]),
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/parser/macro/bad-macro-definition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![crate_type = "lib"]

macro_rules! a { {} => }
//~^ ERROR: macro definition ended unexpectedly

macro_rules! b { 0 => }
//~^ ERROR: macro definition ended unexpectedly
//~| ERROR: invalid macro matcher

macro_rules! c { x => }
//~^ ERROR: macro definition ended unexpectedly
//~| ERROR: invalid macro matcher

macro_rules! d { _ => }
//~^ ERROR: macro definition ended unexpectedly
//~| ERROR: invalid macro matcher

macro_rules! e { {} }
//~^ ERROR: expected `=>`, found end of macro arguments

macro_rules! f {}
//~^ ERROR: macros must contain at least one rule
56 changes: 56 additions & 0 deletions tests/ui/parser/macro/bad-macro-definition.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
error: macro definition ended unexpectedly
--> $DIR/bad-macro-definition.rs:3:23
|
LL | macro_rules! a { {} => }
| ^ expected right-hand side of macro rule

error: invalid macro matcher; matchers must be contained in balanced delimiters
--> $DIR/bad-macro-definition.rs:6:18
|
LL | macro_rules! b { 0 => }
| ^

error: macro definition ended unexpectedly
--> $DIR/bad-macro-definition.rs:6:22
|
LL | macro_rules! b { 0 => }
| ^ expected right-hand side of macro rule

error: invalid macro matcher; matchers must be contained in balanced delimiters
--> $DIR/bad-macro-definition.rs:10:18
|
LL | macro_rules! c { x => }
| ^

error: macro definition ended unexpectedly
--> $DIR/bad-macro-definition.rs:10:22
|
LL | macro_rules! c { x => }
| ^ expected right-hand side of macro rule

error: invalid macro matcher; matchers must be contained in balanced delimiters
--> $DIR/bad-macro-definition.rs:14:18
|
LL | macro_rules! d { _ => }
| ^

error: macro definition ended unexpectedly
--> $DIR/bad-macro-definition.rs:14:22
|
LL | macro_rules! d { _ => }
| ^ expected right-hand side of macro rule

error: expected `=>`, found end of macro arguments
--> $DIR/bad-macro-definition.rs:18:20
|
LL | macro_rules! e { {} }
| ^ expected `=>`

error: macros must contain at least one rule
--> $DIR/bad-macro-definition.rs:21:1
|
LL | macro_rules! f {}
| ^^^^^^^^^^^^^^^^^

error: aborting due to 9 previous errors

Loading