@@ -56,6 +56,8 @@ pub(crate) struct ParserAnyMacro<'a> {
5656 arm_span : Span ,
5757 /// Whether or not this macro is defined in the current crate
5858 is_local : bool ,
59+ bindings : & ' a [ MacroRule ] ,
60+ matched_rule_bindings : & ' a [ MatcherLoc ] ,
5961}
6062
6163impl < ' a > ParserAnyMacro < ' a > {
@@ -68,13 +70,22 @@ impl<'a> ParserAnyMacro<'a> {
6870 arm_span,
6971 is_trailing_mac,
7072 is_local,
73+ bindings,
74+ matched_rule_bindings,
7175 } = * self ;
7276 let snapshot = & mut parser. create_snapshot_for_diagnostic ( ) ;
7377 let fragment = match parse_ast_fragment ( parser, kind) {
7478 Ok ( f) => f,
7579 Err ( err) => {
7680 let guar = diagnostics:: emit_frag_parse_err (
77- err, parser, snapshot, site_span, arm_span, kind,
81+ err,
82+ parser,
83+ snapshot,
84+ site_span,
85+ arm_span,
86+ kind,
87+ bindings,
88+ matched_rule_bindings,
7889 ) ;
7990 return kind. dummy ( site_span, guar) ;
8091 }
@@ -101,14 +112,17 @@ impl<'a> ParserAnyMacro<'a> {
101112 fragment
102113 }
103114
104- #[ instrument( skip( cx, tts) ) ]
115+ #[ instrument( skip( cx, tts, bindings , matched_rule_bindings ) ) ]
105116 pub ( crate ) fn from_tts < ' cx > (
106117 cx : & ' cx mut ExtCtxt < ' a > ,
107118 tts : TokenStream ,
108119 site_span : Span ,
109120 arm_span : Span ,
110121 is_local : bool ,
111122 macro_ident : Ident ,
123+ // bindings and lhs is for diagnostics
124+ bindings : & ' a [ MacroRule ] ,
125+ matched_rule_bindings : & ' a [ MatcherLoc ] ,
112126 ) -> Self {
113127 Self {
114128 parser : Parser :: new ( & cx. sess . psess , tts, None ) ,
@@ -122,6 +136,8 @@ impl<'a> ParserAnyMacro<'a> {
122136 is_trailing_mac : cx. current_expansion . is_trailing_mac ,
123137 arm_span,
124138 is_local,
139+ bindings,
140+ matched_rule_bindings,
125141 }
126142 }
127143}
@@ -360,7 +376,7 @@ fn expand_macro<'cx>(
360376
361377 match try_success_result {
362378 Ok ( ( rule_index, rule, named_matches) ) => {
363- let MacroRule :: Func { rhs, .. } = rule else {
379+ let MacroRule :: Func { lhs , rhs, .. } = rule else {
364380 panic ! ( "try_match_macro returned non-func rule" ) ;
365381 } ;
366382 let mbe:: TokenTree :: Delimited ( rhs_span, _, rhs) = rhs else {
@@ -388,8 +404,23 @@ fn expand_macro<'cx>(
388404 cx. resolver . record_macro_rule_usage ( node_id, rule_index) ;
389405 }
390406
407+ // let mut bindings = vec![];
408+ // for rule in rules {
409+ // let MacroRule::Func { lhs, .. } = rule else { continue };
410+ // for param in lhs {
411+ // let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
412+ // bindings.push(*bind);
413+ // }
414+ // }
415+ //
416+ // let mut matched_rule_bindings = vec![];
417+ // for param in lhs {
418+ // let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
419+ // matched_rule_bindings.push(*bind);
420+ // }
421+
391422 // Let the context choose how to interpret the result. Weird, but useful for X-macros.
392- Box :: new ( ParserAnyMacro :: from_tts ( cx, tts, sp, arm_span, is_local, name) )
423+ Box :: new ( ParserAnyMacro :: from_tts ( cx, tts, sp, arm_span, is_local, name, rules , lhs ) )
393424 }
394425 Err ( CanRetry :: No ( guar) ) => {
395426 debug ! ( "Will not retry matching as an error was emitted already" ) ;
0 commit comments