@@ -43,7 +43,7 @@ use crate::mbe::quoted::{RulePart, parse_one_tt};
4343use crate :: mbe:: transcribe:: transcribe;
4444use crate :: mbe:: { self , KleeneOp } ;
4545
46- pub ( crate ) struct ParserAnyMacro < ' a > {
46+ pub ( crate ) struct ParserAnyMacro < ' a , ' b > {
4747 parser : Parser < ' a > ,
4848
4949 /// Span of the expansion site of the macro this parser is for
@@ -55,10 +55,15 @@ pub(crate) struct ParserAnyMacro<'a> {
5555 arm_span : Span ,
5656 /// Whether or not this macro is defined in the current crate
5757 is_local : bool ,
58+ bindings : & ' b [ MacroRule ] ,
59+ matched_rule_bindings : & ' b [ MatcherLoc ] ,
5860}
5961
60- impl < ' a > ParserAnyMacro < ' a > {
61- pub ( crate ) fn make ( mut self : Box < ParserAnyMacro < ' a > > , kind : AstFragmentKind ) -> AstFragment {
62+ impl < ' a , ' b > ParserAnyMacro < ' a , ' b > {
63+ pub ( crate ) fn make (
64+ mut self : Box < ParserAnyMacro < ' a , ' b > > ,
65+ kind : AstFragmentKind ,
66+ ) -> AstFragment {
6267 let ParserAnyMacro {
6368 site_span,
6469 macro_ident,
@@ -67,13 +72,22 @@ impl<'a> ParserAnyMacro<'a> {
6772 arm_span,
6873 is_trailing_mac,
6974 is_local,
75+ bindings,
76+ matched_rule_bindings,
7077 } = * self ;
7178 let snapshot = & mut parser. create_snapshot_for_diagnostic ( ) ;
7279 let fragment = match parse_ast_fragment ( parser, kind) {
7380 Ok ( f) => f,
7481 Err ( err) => {
7582 let guar = diagnostics:: emit_frag_parse_err (
76- err, parser, snapshot, site_span, arm_span, kind,
83+ err,
84+ parser,
85+ snapshot,
86+ site_span,
87+ arm_span,
88+ kind,
89+ bindings,
90+ matched_rule_bindings,
7791 ) ;
7892 return kind. dummy ( site_span, guar) ;
7993 }
@@ -100,14 +114,17 @@ impl<'a> ParserAnyMacro<'a> {
100114 fragment
101115 }
102116
103- #[ instrument( skip( cx, tts) ) ]
117+ #[ instrument( skip( cx, tts, bindings , matched_rule_bindings ) ) ]
104118 pub ( crate ) fn from_tts < ' cx > (
105119 cx : & ' cx mut ExtCtxt < ' a > ,
106120 tts : TokenStream ,
107121 site_span : Span ,
108122 arm_span : Span ,
109123 is_local : bool ,
110124 macro_ident : Ident ,
125+ // bindings and lhs is for diagnostics
126+ bindings : & ' b [ MacroRule ] ,
127+ matched_rule_bindings : & ' b [ MatcherLoc ] ,
111128 ) -> Self {
112129 Self {
113130 parser : Parser :: new ( & cx. sess . psess , tts, None ) ,
@@ -121,11 +138,13 @@ impl<'a> ParserAnyMacro<'a> {
121138 is_trailing_mac : cx. current_expansion . is_trailing_mac ,
122139 arm_span,
123140 is_local,
141+ bindings,
142+ matched_rule_bindings,
124143 }
125144 }
126145}
127146
128- pub ( super ) enum MacroRule {
147+ pub ( crate ) enum MacroRule {
129148 /// A function-style rule, for use with `m!()`
130149 Func { lhs : Vec < MatcherLoc > , lhs_span : Span , rhs : mbe:: TokenTree } ,
131150 /// An attr rule, for use with `#[m]`
@@ -226,8 +245,8 @@ impl MacroRulesMacroExpander {
226245}
227246
228247impl TTMacroExpander for MacroRulesMacroExpander {
229- fn expand < ' cx > (
230- & self ,
248+ fn expand < ' cx , ' a : ' cx > (
249+ & ' a self ,
231250 cx : & ' cx mut ExtCtxt < ' _ > ,
232251 sp : Span ,
233252 input : TokenStream ,
@@ -337,15 +356,15 @@ impl<'matcher> Tracker<'matcher> for NoopTracker {
337356
338357/// Expands the rules based macro defined by `rules` for a given input `arg`.
339358#[ instrument( skip( cx, transparency, arg, rules) ) ]
340- fn expand_macro < ' cx > (
359+ fn expand_macro < ' cx , ' a : ' cx > (
341360 cx : & ' cx mut ExtCtxt < ' _ > ,
342361 sp : Span ,
343362 def_span : Span ,
344363 node_id : NodeId ,
345364 name : Ident ,
346365 transparency : Transparency ,
347366 arg : TokenStream ,
348- rules : & [ MacroRule ] ,
367+ rules : & ' a [ MacroRule ] ,
349368) -> Box < dyn MacResult + ' cx > {
350369 let psess = & cx. sess . psess ;
351370
@@ -359,7 +378,7 @@ fn expand_macro<'cx>(
359378
360379 match try_success_result {
361380 Ok ( ( rule_index, rule, named_matches) ) => {
362- let MacroRule :: Func { rhs, .. } = rule else {
381+ let MacroRule :: Func { lhs , rhs, .. } = rule else {
363382 panic ! ( "try_match_macro returned non-func rule" ) ;
364383 } ;
365384 let mbe:: TokenTree :: Delimited ( rhs_span, _, rhs) = rhs else {
@@ -387,8 +406,23 @@ fn expand_macro<'cx>(
387406 cx. resolver . record_macro_rule_usage ( node_id, rule_index) ;
388407 }
389408
409+ // let mut bindings = vec![];
410+ // for rule in rules {
411+ // let MacroRule::Func { lhs, .. } = rule else { continue };
412+ // for param in lhs {
413+ // let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
414+ // bindings.push(*bind);
415+ // }
416+ // }
417+ //
418+ // let mut matched_rule_bindings = vec![];
419+ // for param in lhs {
420+ // let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
421+ // matched_rule_bindings.push(*bind);
422+ // }
423+
390424 // Let the context choose how to interpret the result. Weird, but useful for X-macros.
391- Box :: new ( ParserAnyMacro :: from_tts ( cx, tts, sp, arm_span, is_local, name) )
425+ Box :: new ( ParserAnyMacro :: from_tts ( cx, tts, sp, arm_span, is_local, name, rules , lhs ) )
392426 }
393427 Err ( CanRetry :: No ( guar) ) => {
394428 debug ! ( "Will not retry matching as an error was emitted already" ) ;
0 commit comments