@@ -55,7 +55,7 @@ pub(super) fn failed_to_match_macro<'cx>(
5555
5656 let span = token. span . substitute_dummy ( sp) ;
5757
58- let mut err = cx. dcx ( ) . struct_span_err ( span, parse_failure_msg ( & token) ) ;
58+ let mut err = cx. dcx ( ) . struct_span_err ( span, parse_failure_msg ( & token, None ) ) ;
5959 err. span_label ( span, label) ;
6060 if !def_span. is_dummy ( ) && !cx. source_map ( ) . is_imported ( def_span) {
6161 err. span_label ( cx. source_map ( ) . guess_head_span ( def_span) , "when calling this macro" ) ;
@@ -200,9 +200,17 @@ impl<'a, 'cx> CollectTrackerAndEmitter<'a, 'cx, '_> {
200200}
201201
202202/// Currently used by macro_rules! compilation to extract a little information from the `Failure` case.
203- pub struct FailureForwarder ;
203+ pub struct FailureForwarder < ' matcher > {
204+ expected_token : Option < & ' matcher Token > ,
205+ }
206+
207+ impl < ' matcher > FailureForwarder < ' matcher > {
208+ pub fn new ( ) -> Self {
209+ Self { expected_token : None }
210+ }
211+ }
204212
205- impl < ' matcher > Tracker < ' matcher > for FailureForwarder {
213+ impl < ' matcher > Tracker < ' matcher > for FailureForwarder < ' matcher > {
206214 type Failure = ( Token , usize , & ' static str ) ;
207215
208216 fn build_failure ( tok : Token , position : usize , msg : & ' static str ) -> Self :: Failure {
@@ -212,6 +220,14 @@ impl<'matcher> Tracker<'matcher> for FailureForwarder {
212220 fn description ( ) -> & ' static str {
213221 "failure-forwarder"
214222 }
223+
224+ fn set_expected_token ( & mut self , tok : & ' matcher Token ) {
225+ self . expected_token = Some ( tok) ;
226+ }
227+
228+ fn get_expected_token ( & self ) -> Option < & ' matcher Token > {
229+ self . expected_token
230+ }
215231}
216232
217233pub ( super ) fn emit_frag_parse_err (
@@ -320,9 +336,19 @@ pub(super) fn annotate_doc_comment(dcx: &DiagCtxt, err: &mut Diag<'_>, sm: &Sour
320336
321337/// Generates an appropriate parsing failure message. For EOF, this is "unexpected end...". For
322338/// other tokens, this is "unexpected token...".
323- pub ( super ) fn parse_failure_msg ( tok : & Token ) -> Cow < ' static , str > {
324- match tok. kind {
325- token:: Eof => Cow :: from ( "unexpected end of macro invocation" ) ,
326- _ => Cow :: from ( format ! ( "no rules expected the token `{}`" , pprust:: token_to_string( tok) ) ) ,
339+ pub ( super ) fn parse_failure_msg ( tok : & Token , expected_token : Option < & Token > ) -> Cow < ' static , str > {
340+ if let Some ( expected_token) = expected_token {
341+ Cow :: from ( format ! (
342+ "expected `{}`, found `{}`" ,
343+ pprust:: token_to_string( expected_token) ,
344+ pprust:: token_to_string( tok) ,
345+ ) )
346+ } else {
347+ match tok. kind {
348+ token:: Eof => Cow :: from ( "unexpected end of macro invocation" ) ,
349+ _ => {
350+ Cow :: from ( format ! ( "no rules expected the token `{}`" , pprust:: token_to_string( tok) ) )
351+ }
352+ }
327353 }
328354}
0 commit comments