about summary refs log tree commit diff
path: root/compiler/rustc_expand/src/mbe
diff options
context:
space:
mode:
authorJosh Triplett <josh@joshtriplett.org>2025-07-06 16:22:25 -0700
committerJosh Triplett <josh@joshtriplett.org>2025-07-06 20:49:12 -0700
commit493cdf226922495b861c6fd6f0c4ddfcedaa99d9 (patch)
tree167b3aa1887ddbb2eb41a4bac0e550ab62472ee6 /compiler/rustc_expand/src/mbe
parent87cd178159205edf5245fd42b55a4620dadcc8d1 (diff)
mbe: Factor out a helper to check for unexpected EOF in definition
Will get called additional times when expanding parsing to cover
attributes
Diffstat (limited to 'compiler/rustc_expand/src/mbe')
-rw-r--r--compiler/rustc_expand/src/mbe/macro_rules.rs21
1 files changed, 14 insertions, 7 deletions
diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs
index f310e7264ac..26b37caac48 100644
--- a/compiler/rustc_expand/src/mbe/macro_rules.rs
+++ b/compiler/rustc_expand/src/mbe/macro_rules.rs
@@ -401,13 +401,7 @@ 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();
+        if let Some(guar) = check_no_eof(sess, &p, "expected right-hand side of macro rule") {
             return dummy_syn_ext(guar);
         }
         let rhs_tt = p.parse_token_tree();
@@ -453,6 +447,19 @@ pub fn compile_declarative_macro(
     (mk_syn_ext(expander), nrules)
 }
 
+fn check_no_eof(sess: &Session, p: &Parser<'_>, msg: &'static str) -> Option<ErrorGuaranteed> {
+    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, msg)
+            .emit();
+        return Some(guar);
+    }
+    None
+}
+
 fn check_lhs(sess: &Session, node_id: NodeId, lhs: &mbe::TokenTree) -> Result<(), ErrorGuaranteed> {
     let e1 = check_lhs_nt_follows(sess, node_id, lhs);
     let e2 = check_lhs_no_empty_seq(sess, slice::from_ref(lhs));