Skip to content

Commit f68741b

Browse files
committed
Stop checking err_count in macro_rules validity checking
All errors are local anyway, so we can track them directly
1 parent 5bd5d21 commit f68741b

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,9 @@ pub fn compile_declarative_macro(
485485
)
486486
.pop()
487487
.unwrap();
488-
valid &= check_lhs_nt_follows(sess, def, &tt);
488+
// We don't handle errors here, the driver will abort
489+
// after parsing/expansion. we can report every error in every macro this way.
490+
valid &= check_lhs_nt_follows(sess, def, &tt).is_ok();
489491
return tt;
490492
}
491493
sess.dcx().span_bug(def.span, "wrong-structured lhs")
@@ -589,18 +591,19 @@ pub fn compile_declarative_macro(
589591
(mk_syn_ext(expander), rule_spans)
590592
}
591593

592-
fn check_lhs_nt_follows(sess: &Session, def: &ast::Item, lhs: &mbe::TokenTree) -> bool {
594+
fn check_lhs_nt_follows(
595+
sess: &Session,
596+
def: &ast::Item,
597+
lhs: &mbe::TokenTree,
598+
) -> Result<(), ErrorGuaranteed> {
593599
// lhs is going to be like TokenTree::Delimited(...), where the
594600
// entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.
595601
if let mbe::TokenTree::Delimited(.., delimited) = lhs {
596602
check_matcher(sess, def, &delimited.tts)
597603
} else {
598604
let msg = "invalid macro matcher; matchers must be contained in balanced delimiters";
599-
sess.dcx().span_err(lhs.span(), msg);
600-
false
605+
Err(sess.dcx().span_err(lhs.span(), msg))
601606
}
602-
// we don't abort on errors on rejection, the driver will do that for us
603-
// after parsing/expansion. we can report every error in every macro this way.
604607
}
605608

606609
fn is_empty_token_tree(sess: &Session, seq: &mbe::SequenceRepetition) -> bool {
@@ -675,12 +678,15 @@ fn check_rhs(sess: &Session, rhs: &mbe::TokenTree) -> bool {
675678
false
676679
}
677680

678-
fn check_matcher(sess: &Session, def: &ast::Item, matcher: &[mbe::TokenTree]) -> bool {
681+
fn check_matcher(
682+
sess: &Session,
683+
def: &ast::Item,
684+
matcher: &[mbe::TokenTree],
685+
) -> Result<(), ErrorGuaranteed> {
679686
let first_sets = FirstSets::new(matcher);
680687
let empty_suffix = TokenSet::empty();
681-
let err = sess.dcx().err_count();
682-
check_matcher_core(sess, def, &first_sets, matcher, &empty_suffix);
683-
err == sess.dcx().err_count()
688+
check_matcher_core(sess, def, &first_sets, matcher, &empty_suffix)?;
689+
Ok(())
684690
}
685691

686692
fn has_compile_error_macro(rhs: &mbe::TokenTree) -> bool {
@@ -1020,11 +1026,13 @@ fn check_matcher_core<'tt>(
10201026
first_sets: &FirstSets<'tt>,
10211027
matcher: &'tt [mbe::TokenTree],
10221028
follow: &TokenSet<'tt>,
1023-
) -> TokenSet<'tt> {
1029+
) -> Result<TokenSet<'tt>, ErrorGuaranteed> {
10241030
use mbe::TokenTree;
10251031

10261032
let mut last = TokenSet::empty();
10271033

1034+
let mut errored = Ok(());
1035+
10281036
// 2. For each token and suffix [T, SUFFIX] in M:
10291037
// ensure that T can be followed by SUFFIX, and if SUFFIX may be empty,
10301038
// then ensure T can also be followed by any element of FOLLOW.
@@ -1068,7 +1076,7 @@ fn check_matcher_core<'tt>(
10681076
token::CloseDelim(d.delim),
10691077
span.close,
10701078
));
1071-
check_matcher_core(sess, def, first_sets, &d.tts, &my_suffix);
1079+
check_matcher_core(sess, def, first_sets, &d.tts, &my_suffix)?;
10721080
// don't track non NT tokens
10731081
last.replace_with_irrelevant();
10741082

@@ -1100,7 +1108,7 @@ fn check_matcher_core<'tt>(
11001108
// At this point, `suffix_first` is built, and
11011109
// `my_suffix` is some TokenSet that we can use
11021110
// for checking the interior of `seq_rep`.
1103-
let next = check_matcher_core(sess, def, first_sets, &seq_rep.tts, my_suffix);
1111+
let next = check_matcher_core(sess, def, first_sets, &seq_rep.tts, my_suffix)?;
11041112
if next.maybe_empty {
11051113
last.add_all(&next);
11061114
} else {
@@ -1206,14 +1214,15 @@ fn check_matcher_core<'tt>(
12061214
));
12071215
}
12081216
}
1209-
err.emit();
1217+
errored = Err(err.emit());
12101218
}
12111219
}
12121220
}
12131221
}
12141222
}
12151223
}
1216-
last
1224+
errored?;
1225+
Ok(last)
12171226
}
12181227

12191228
fn token_can_be_followed_by_any(tok: &mbe::TokenTree) -> bool {

0 commit comments

Comments
 (0)