fix: Fix pat fragment handling in 2021 edition

This commit is contained in:
Lukas Wirth 2023-04-24 22:21:37 +02:00
parent 5750d81e30
commit d1ca505525
8 changed files with 80 additions and 23 deletions

View file

@ -107,6 +107,9 @@ pub struct DeclarativeMacro {
rules: Vec<Rule>,
/// Highest id of the token we have in TokenMap
shift: Shift,
// This is used for correctly determining the behavior of the pat fragment
// FIXME: This should be tracked by hygiene of the fragment identifier!
is_2021: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
@ -190,7 +193,10 @@ pub enum Origin {
impl DeclarativeMacro {
/// The old, `macro_rules! m {}` flavor.
pub fn parse_macro_rules(tt: &tt::Subtree) -> Result<DeclarativeMacro, ParseError> {
pub fn parse_macro_rules(
tt: &tt::Subtree,
is_2021: bool,
) -> Result<DeclarativeMacro, ParseError> {
// Note: this parsing can be implemented using mbe machinery itself, by
// matching against `$($lhs:tt => $rhs:tt);*` pattern, but implementing
// manually seems easier.
@ -211,11 +217,11 @@ impl DeclarativeMacro {
validate(lhs)?;
}
Ok(DeclarativeMacro { rules, shift: Shift::new(tt) })
Ok(DeclarativeMacro { rules, shift: Shift::new(tt), is_2021 })
}
/// The new, unstable `macro m {}` flavor.
pub fn parse_macro2(tt: &tt::Subtree) -> Result<DeclarativeMacro, ParseError> {
pub fn parse_macro2(tt: &tt::Subtree, is_2021: bool) -> Result<DeclarativeMacro, ParseError> {
let mut src = TtIter::new(tt);
let mut rules = Vec::new();
@ -244,14 +250,14 @@ impl DeclarativeMacro {
validate(lhs)?;
}
Ok(DeclarativeMacro { rules, shift: Shift::new(tt) })
Ok(DeclarativeMacro { rules, shift: Shift::new(tt), is_2021 })
}
pub fn expand(&self, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
// apply shift
let mut tt = tt.clone();
self.shift.shift_all(&mut tt);
expander::expand_rules(&self.rules, &tt)
expander::expand_rules(&self.rules, &tt, self.is_2021)
}
pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId {