@@ -2382,22 +2382,39 @@ impl<'a> Parser<'a> {
2382
2382
Misplaced ( Span ) ,
2383
2383
}
2384
2384
2385
+ // We may be able to recover
2386
+ let mut recover_constness = constness;
2387
+ let mut recover_asyncness = asyncness;
2388
+ let mut recover_unsafety = unsafety;
2385
2389
// This will allow the machine fix to directly place the keyword in the correct place or to indicate
2386
2390
// that the keyword is already present and the second instance should be removed.
2387
2391
let wrong_kw = if self . check_keyword ( kw:: Const ) {
2388
2392
match constness {
2389
2393
Const :: Yes ( sp) => Some ( WrongKw :: Duplicated ( sp) ) ,
2390
- Const :: No => Some ( WrongKw :: Misplaced ( async_start_sp) ) ,
2394
+ Const :: No => {
2395
+ recover_constness = Const :: Yes ( self . token . span ) ;
2396
+ Some ( WrongKw :: Misplaced ( async_start_sp) )
2397
+ }
2391
2398
}
2392
2399
} else if self . check_keyword ( kw:: Async ) {
2393
2400
match asyncness {
2394
2401
Async :: Yes { span, .. } => Some ( WrongKw :: Duplicated ( span) ) ,
2395
- Async :: No => Some ( WrongKw :: Misplaced ( unsafe_start_sp) ) ,
2402
+ Async :: No => {
2403
+ recover_asyncness = Async :: Yes {
2404
+ span : self . token . span ,
2405
+ closure_id : DUMMY_NODE_ID ,
2406
+ return_impl_trait_id : DUMMY_NODE_ID ,
2407
+ } ;
2408
+ Some ( WrongKw :: Misplaced ( unsafe_start_sp) )
2409
+ }
2396
2410
}
2397
2411
} else if self . check_keyword ( kw:: Unsafe ) {
2398
2412
match unsafety {
2399
2413
Unsafe :: Yes ( sp) => Some ( WrongKw :: Duplicated ( sp) ) ,
2400
- Unsafe :: No => Some ( WrongKw :: Misplaced ( ext_start_sp) ) ,
2414
+ Unsafe :: No => {
2415
+ recover_unsafety = Unsafe :: Yes ( self . token . span ) ;
2416
+ Some ( WrongKw :: Misplaced ( ext_start_sp) )
2417
+ }
2401
2418
}
2402
2419
} else {
2403
2420
None
@@ -2467,6 +2484,23 @@ impl<'a> Parser<'a> {
2467
2484
}
2468
2485
}
2469
2486
}
2487
+
2488
+ if wrong_kw. is_some ( )
2489
+ && self . may_recover ( )
2490
+ && self . look_ahead ( 1 , |tok| tok. is_keyword_case ( kw:: Fn , case) )
2491
+ {
2492
+ // Advance past the misplaced keyword and `fn`
2493
+ self . bump ( ) ;
2494
+ self . bump ( ) ;
2495
+ err. emit ( ) ;
2496
+ return Ok ( FnHeader {
2497
+ constness : recover_constness,
2498
+ unsafety : recover_unsafety,
2499
+ asyncness : recover_asyncness,
2500
+ ext,
2501
+ } ) ;
2502
+ }
2503
+
2470
2504
return Err ( err) ;
2471
2505
}
2472
2506
}
0 commit comments