@@ -2401,22 +2401,39 @@ impl<'a> Parser<'a> {
2401
2401
Misplaced ( Span ) ,
2402
2402
}
2403
2403
2404
+ // We may be able to recover
2405
+ let mut recover_constness = constness;
2406
+ let mut recover_asyncness = asyncness;
2407
+ let mut recover_unsafety = unsafety;
2404
2408
// This will allow the machine fix to directly place the keyword in the correct place or to indicate
2405
2409
// that the keyword is already present and the second instance should be removed.
2406
2410
let wrong_kw = if self . check_keyword ( kw:: Const ) {
2407
2411
match constness {
2408
2412
Const :: Yes ( sp) => Some ( WrongKw :: Duplicated ( sp) ) ,
2409
- Const :: No => Some ( WrongKw :: Misplaced ( async_start_sp) ) ,
2413
+ Const :: No => {
2414
+ recover_constness = Const :: Yes ( self . token . span ) ;
2415
+ Some ( WrongKw :: Misplaced ( async_start_sp) )
2416
+ }
2410
2417
}
2411
2418
} else if self . check_keyword ( kw:: Async ) {
2412
2419
match asyncness {
2413
2420
Async :: Yes { span, .. } => Some ( WrongKw :: Duplicated ( span) ) ,
2414
- Async :: No => Some ( WrongKw :: Misplaced ( unsafe_start_sp) ) ,
2421
+ Async :: No => {
2422
+ recover_asyncness = Async :: Yes {
2423
+ span : self . token . span ,
2424
+ closure_id : DUMMY_NODE_ID ,
2425
+ return_impl_trait_id : DUMMY_NODE_ID ,
2426
+ } ;
2427
+ Some ( WrongKw :: Misplaced ( unsafe_start_sp) )
2428
+ }
2415
2429
}
2416
2430
} else if self . check_keyword ( kw:: Unsafe ) {
2417
2431
match unsafety {
2418
2432
Unsafe :: Yes ( sp) => Some ( WrongKw :: Duplicated ( sp) ) ,
2419
- Unsafe :: No => Some ( WrongKw :: Misplaced ( ext_start_sp) ) ,
2433
+ Unsafe :: No => {
2434
+ recover_unsafety = Unsafe :: Yes ( self . token . span ) ;
2435
+ Some ( WrongKw :: Misplaced ( ext_start_sp) )
2436
+ }
2420
2437
}
2421
2438
} else {
2422
2439
None
@@ -2486,6 +2503,23 @@ impl<'a> Parser<'a> {
2486
2503
}
2487
2504
}
2488
2505
}
2506
+
2507
+ if wrong_kw. is_some ( )
2508
+ && self . may_recover ( )
2509
+ && self . look_ahead ( 1 , |tok| tok. is_keyword_case ( kw:: Fn , case) )
2510
+ {
2511
+ // Advance past the misplaced keyword and `fn`
2512
+ self . bump ( ) ;
2513
+ self . bump ( ) ;
2514
+ err. emit ( ) ;
2515
+ return Ok ( FnHeader {
2516
+ constness : recover_constness,
2517
+ unsafety : recover_unsafety,
2518
+ asyncness : recover_asyncness,
2519
+ ext,
2520
+ } ) ;
2521
+ }
2522
+
2489
2523
return Err ( err) ;
2490
2524
}
2491
2525
}
0 commit comments