@@ -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 : Vec < Ident > ,
60+ matched_rule_bindings : Vec < Ident > ,
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 }
@@ -109,6 +120,9 @@ impl<'a> ParserAnyMacro<'a> {
109120 arm_span : Span ,
110121 is_local : bool ,
111122 macro_ident : Ident ,
123+ // bindings and lhs is for diagnostics
124+ bindings : Vec < Ident > ,
125+ matched_rule_bindings : Vec < Ident > ,
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,33 @@ 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+ tracing:: info!( ?bind) ;
413+ bindings. push ( * bind) ;
414+ }
415+ }
416+
417+ let mut matched_rule_bindings = vec ! [ ] ;
418+ for param in lhs {
419+ let MatcherLoc :: MetaVarDecl { bind, .. } = param else { continue } ;
420+ matched_rule_bindings. push ( * bind) ;
421+ }
422+
391423 // 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) )
424+ Box :: new ( ParserAnyMacro :: from_tts (
425+ cx,
426+ tts,
427+ sp,
428+ arm_span,
429+ is_local,
430+ name,
431+ bindings,
432+ matched_rule_bindings,
433+ ) )
393434 }
394435 Err ( CanRetry :: No ( guar) ) => {
395436 debug ! ( "Will not retry matching as an error was emitted already" ) ;
0 commit comments