Skip to content

Commit 493cdf2

Browse files
committed
mbe: Factor out a helper to check for unexpected EOF in definition
Will get called additional times when expanding parsing to cover attributes
1 parent 87cd178 commit 493cdf2

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,7 @@ pub fn compile_declarative_macro(
401401
if let Err(e) = p.expect(exp!(FatArrow)) {
402402
return dummy_syn_ext(e.emit());
403403
}
404-
if p.token == token::Eof {
405-
let err_sp = p.token.span.shrink_to_hi();
406-
let guar = sess
407-
.dcx()
408-
.struct_span_err(err_sp, "macro definition ended unexpectedly")
409-
.with_span_label(err_sp, "expected right-hand side of macro rule")
410-
.emit();
404+
if let Some(guar) = check_no_eof(sess, &p, "expected right-hand side of macro rule") {
411405
return dummy_syn_ext(guar);
412406
}
413407
let rhs_tt = p.parse_token_tree();
@@ -453,6 +447,19 @@ pub fn compile_declarative_macro(
453447
(mk_syn_ext(expander), nrules)
454448
}
455449

450+
fn check_no_eof(sess: &Session, p: &Parser<'_>, msg: &'static str) -> Option<ErrorGuaranteed> {
451+
if p.token == token::Eof {
452+
let err_sp = p.token.span.shrink_to_hi();
453+
let guar = sess
454+
.dcx()
455+
.struct_span_err(err_sp, "macro definition ended unexpectedly")
456+
.with_span_label(err_sp, msg)
457+
.emit();
458+
return Some(guar);
459+
}
460+
None
461+
}
462+
456463
fn check_lhs(sess: &Session, node_id: NodeId, lhs: &mbe::TokenTree) -> Result<(), ErrorGuaranteed> {
457464
let e1 = check_lhs_nt_follows(sess, node_id, lhs);
458465
let e2 = check_lhs_no_empty_seq(sess, slice::from_ref(lhs));

0 commit comments

Comments
 (0)