@@ -55,6 +55,8 @@ 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 : Vec < Ident > ,
59+ matched_rule_bindings : Vec < Ident > ,
5860}
5961
6062impl < ' a > ParserAnyMacro < ' a > {
@@ -67,13 +69,22 @@ impl<'a> ParserAnyMacro<'a> {
6769 arm_span,
6870 is_trailing_mac,
6971 is_local,
72+ bindings,
73+ matched_rule_bindings,
7074 } = * self ;
7175 let snapshot = & mut parser. create_snapshot_for_diagnostic ( ) ;
7276 let fragment = match parse_ast_fragment ( parser, kind) {
7377 Ok ( f) => f,
7478 Err ( err) => {
7579 let guar = diagnostics:: emit_frag_parse_err (
76- err, parser, snapshot, site_span, arm_span, kind,
80+ err,
81+ parser,
82+ snapshot,
83+ site_span,
84+ arm_span,
85+ kind,
86+ bindings,
87+ matched_rule_bindings,
7788 ) ;
7889 return kind. dummy ( site_span, guar) ;
7990 }
@@ -108,6 +119,9 @@ impl<'a> ParserAnyMacro<'a> {
108119 arm_span : Span ,
109120 is_local : bool ,
110121 macro_ident : Ident ,
122+ // bindings and lhs is for diagnostics
123+ bindings : Vec < Ident > ,
124+ matched_rule_bindings : Vec < Ident > ,
111125 ) -> Self {
112126 Self {
113127 parser : Parser :: new ( & cx. sess . psess , tts, None ) ,
@@ -121,6 +135,8 @@ impl<'a> ParserAnyMacro<'a> {
121135 is_trailing_mac : cx. current_expansion . is_trailing_mac ,
122136 arm_span,
123137 is_local,
138+ bindings,
139+ matched_rule_bindings,
124140 }
125141 }
126142}
@@ -359,7 +375,7 @@ fn expand_macro<'cx>(
359375
360376 match try_success_result {
361377 Ok ( ( rule_index, rule, named_matches) ) => {
362- let MacroRule :: Func { rhs, .. } = rule else {
378+ let MacroRule :: Func { lhs , rhs, .. } = rule else {
363379 panic ! ( "try_match_macro returned non-func rule" ) ;
364380 } ;
365381 let mbe:: TokenTree :: Delimited ( rhs_span, _, rhs) = rhs else {
@@ -387,8 +403,32 @@ fn expand_macro<'cx>(
387403 cx. resolver . record_macro_rule_usage ( node_id, rule_index) ;
388404 }
389405
406+ let mut bindings = vec ! [ ] ;
407+ for rule in rules {
408+ let MacroRule :: Func { lhs, .. } = rule else { continue } ;
409+ for param in lhs {
410+ let MatcherLoc :: MetaVarDecl { bind, .. } = param else { continue } ;
411+ bindings. push ( * bind) ;
412+ }
413+ }
414+
415+ let mut matched_rule_bindings = vec ! [ ] ;
416+ for param in lhs {
417+ let MatcherLoc :: MetaVarDecl { bind, .. } = param else { continue } ;
418+ matched_rule_bindings. push ( * bind) ;
419+ }
420+
390421 // 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) )
422+ Box :: new ( ParserAnyMacro :: from_tts (
423+ cx,
424+ tts,
425+ sp,
426+ arm_span,
427+ is_local,
428+ name,
429+ bindings,
430+ matched_rule_bindings,
431+ ) )
392432 }
393433 Err ( CanRetry :: No ( guar) ) => {
394434 debug ! ( "Will not retry matching as an error was emitted already" ) ;
0 commit comments