From 4135696ea7d5b09f30f79676a3795f91610a025d Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 18 Apr 2024 11:00:22 +0200 Subject: [PATCH] Cleanup --- crates/hir-expand/src/db.rs | 4 ++-- crates/mbe/src/expander.rs | 6 +++--- crates/mbe/src/lib.rs | 5 ++++- crates/span/src/map.rs | 2 ++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index 33c0373e48..d7233a8923 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -3,7 +3,7 @@ use base_db::{salsa, CrateId, FileId, SourceDatabase}; use either::Either; use limit::Limit; -use mbe::syntax_node_to_token_tree; +use mbe::{syntax_node_to_token_tree, MatchedArmIndex}; use rustc_hash::FxHashSet; use span::{AstIdMap, Span, SyntaxContextData, SyntaxContextId}; use syntax::{ast, AstNode, Parse, SyntaxElement, SyntaxError, SyntaxNode, SyntaxToken, T}; @@ -546,7 +546,7 @@ fn macro_expand( db: &dyn ExpandDatabase, macro_call_id: MacroCallId, loc: MacroCallLoc, -) -> ExpandResult<(CowArc, Option)> { +) -> ExpandResult<(CowArc, MatchedArmIndex)> { let _p = tracing::span!(tracing::Level::INFO, "macro_expand").entered(); let (ExpandResult { value: (tt, matched_arm), err }, span) = match loc.def.kind { diff --git a/crates/mbe/src/expander.rs b/crates/mbe/src/expander.rs index b1468e5bef..cfad8bcc0b 100644 --- a/crates/mbe/src/expander.rs +++ b/crates/mbe/src/expander.rs @@ -9,7 +9,7 @@ use rustc_hash::FxHashMap; use span::{Edition, Span}; use syntax::SmolStr; -use crate::{parser::MetaVarKind, ExpandError, ExpandResult}; +use crate::{parser::MetaVarKind, ExpandError, ExpandResult, MatchedArmIndex}; pub(crate) fn expand_rules( rules: &[crate::Rule], @@ -18,7 +18,7 @@ pub(crate) fn expand_rules( new_meta_vars: bool, call_site: Span, def_site_edition: Edition, -) -> ExpandResult<(tt::Subtree, Option)> { +) -> ExpandResult<(tt::Subtree, MatchedArmIndex)> { let mut match_: Option<(matcher::Match, &crate::Rule, usize)> = None; for (idx, rule) in rules.iter().enumerate() { let new_match = matcher::match_(&rule.lhs, input, def_site_edition); @@ -53,7 +53,7 @@ pub(crate) fn expand_rules( // if we got here, there was no match without errors let ExpandResult { value, err: transcribe_err } = transcriber::transcribe(&rule.rhs, &match_.bindings, marker, new_meta_vars, call_site); - ExpandResult { value: (value, Some(idx as u32)), err: match_.err.or(transcribe_err) } + ExpandResult { value: (value, idx.try_into().ok()), err: match_.err.or(transcribe_err) } } else { ExpandResult::new( ( diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index 77e6490317..d5de56312a 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs @@ -122,6 +122,9 @@ impl fmt::Display for CountError { } } +/// Index of the matched macro arm on successful expansion. +pub type MatchedArmIndex = Option; + /// This struct contains AST for a single `macro_rules` definition. What might /// be very confusing is that AST has almost exactly the same shape as /// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident` @@ -251,7 +254,7 @@ impl DeclarativeMacro { new_meta_vars: bool, call_site: Span, def_site_edition: Edition, - ) -> ExpandResult<(tt::Subtree, Option)> { + ) -> ExpandResult<(tt::Subtree, MatchedArmIndex)> { expander::expand_rules(&self.rules, tt, marker, new_meta_vars, call_site, def_site_edition) } } diff --git a/crates/span/src/map.rs b/crates/span/src/map.rs index e769b6420e..81fc56c961 100644 --- a/crates/span/src/map.rs +++ b/crates/span/src/map.rs @@ -15,6 +15,8 @@ use crate::{ #[derive(Debug, PartialEq, Eq, Clone, Hash)] pub struct SpanMap { spans: Vec<(TextSize, SpanData)>, + /// Index of the matched macro arm on successful expansion for declarative macros. + // FIXME: Does it make sense to have this here? pub matched_arm: Option, }