@@ -55,7 +55,7 @@ pub(super) fn failed_to_match_macro<'cx>(
55
55
56
56
let span = token. span . substitute_dummy ( sp) ;
57
57
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 ) ) ;
59
59
err. span_label ( span, label) ;
60
60
if !def_span. is_dummy ( ) && !cx. source_map ( ) . is_imported ( def_span) {
61
61
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, '_> {
200
200
}
201
201
202
202
/// 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
+ }
204
212
205
- impl < ' matcher > Tracker < ' matcher > for FailureForwarder {
213
+ impl < ' matcher > Tracker < ' matcher > for FailureForwarder < ' matcher > {
206
214
type Failure = ( Token , usize , & ' static str ) ;
207
215
208
216
fn build_failure ( tok : Token , position : usize , msg : & ' static str ) -> Self :: Failure {
@@ -212,6 +220,14 @@ impl<'matcher> Tracker<'matcher> for FailureForwarder {
212
220
fn description ( ) -> & ' static str {
213
221
"failure-forwarder"
214
222
}
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
+ }
215
231
}
216
232
217
233
pub ( super ) fn emit_frag_parse_err (
@@ -320,9 +336,19 @@ pub(super) fn annotate_doc_comment(dcx: &DiagCtxt, err: &mut Diag<'_>, sm: &Sour
320
336
321
337
/// Generates an appropriate parsing failure message. For EOF, this is "unexpected end...". For
322
338
/// 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
+ }
327
353
}
328
354
}
0 commit comments