@@ -2401,22 +2401,39 @@ impl<'a> Parser<'a> {
24012401 Misplaced ( Span ) ,
24022402 }
24032403
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;
24042408 // This will allow the machine fix to directly place the keyword in the correct place or to indicate
24052409 // that the keyword is already present and the second instance should be removed.
24062410 let wrong_kw = if self . check_keyword ( kw:: Const ) {
24072411 match constness {
24082412 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+ }
24102417 }
24112418 } else if self . check_keyword ( kw:: Async ) {
24122419 match asyncness {
24132420 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+ }
24152429 }
24162430 } else if self . check_keyword ( kw:: Unsafe ) {
24172431 match unsafety {
24182432 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+ }
24202437 }
24212438 } else {
24222439 None
@@ -2486,6 +2503,23 @@ impl<'a> Parser<'a> {
24862503 }
24872504 }
24882505 }
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+
24892523 return Err ( err) ;
24902524 }
24912525 }
0 commit comments