Skip to content

Commit 0776082

Browse files
committed
mbe: Fold calls to check_meta_variables into the parser loop
1 parent 6c04e0a commit 0776082

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

compiler/rustc_expand/src/mbe/macro_check.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@
105105
//! stored when entering a macro definition starting from the state in which the meta-variable is
106106
//! bound.
107107
108-
use std::iter;
109-
110108
use rustc_ast::token::{Delimiter, IdentIsRaw, Token, TokenKind};
111109
use rustc_ast::{DUMMY_NODE_ID, NodeId};
112110
use rustc_data_structures::fx::FxHashMap;
@@ -190,29 +188,22 @@ struct MacroState<'a> {
190188
ops: SmallVec<[KleeneToken; 1]>,
191189
}
192190

193-
/// Checks that meta-variables are used correctly in a macro definition.
191+
/// Checks that meta-variables are used correctly in one rule of a macro definition.
194192
///
195193
/// Arguments:
196194
/// - `psess` is used to emit diagnostics and lints
197195
/// - `node_id` is used to emit lints
198-
/// - `span` is used when no spans are available
199-
/// - `lhses` and `rhses` should have the same length and represent the macro definition
196+
/// - `lhs` and `rhs` represent the rule
200197
pub(super) fn check_meta_variables(
201198
psess: &ParseSess,
202199
node_id: NodeId,
203-
span: Span,
204-
lhses: &[TokenTree],
205-
rhses: &[TokenTree],
200+
lhs: &TokenTree,
201+
rhs: &TokenTree,
206202
) -> Result<(), ErrorGuaranteed> {
207-
if lhses.len() != rhses.len() {
208-
psess.dcx().span_bug(span, "length mismatch between LHSes and RHSes")
209-
}
210203
let mut guar = None;
211-
for (lhs, rhs) in iter::zip(lhses, rhses) {
212-
let mut binders = Binders::default();
213-
check_binders(psess, node_id, lhs, &Stack::Empty, &mut binders, &Stack::Empty, &mut guar);
214-
check_occurrences(psess, node_id, rhs, &Stack::Empty, &binders, &Stack::Empty, &mut guar);
215-
}
204+
let mut binders = Binders::default();
205+
check_binders(psess, node_id, lhs, &Stack::Empty, &mut binders, &Stack::Empty, &mut guar);
206+
check_occurrences(psess, node_id, rhs, &Stack::Empty, &binders, &Stack::Empty, &mut guar);
216207
guar.map_or(Ok(()), Err)
217208
}
218209

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ pub fn compile_declarative_macro(
423423
.pop()
424424
.unwrap();
425425
check_emission(check_rhs(sess, &rhs_tt));
426+
check_emission(macro_check::check_meta_variables(&sess.psess, node_id, &lhs_tt, &rhs_tt));
426427
lhses.push(lhs_tt);
427428
rhses.push(rhs_tt);
428429
if p.token == token::Eof {
@@ -438,8 +439,6 @@ pub fn compile_declarative_macro(
438439
return dummy_syn_ext(guar);
439440
}
440441

441-
check_emission(macro_check::check_meta_variables(&sess.psess, node_id, span, &lhses, &rhses));
442-
443442
let transparency = find_attr!(attrs, AttributeKind::MacroTransparency(x) => *x)
444443
.unwrap_or(Transparency::fallback(macro_rules));
445444

0 commit comments

Comments
 (0)