@@ -2382,22 +2382,39 @@ impl<'a> Parser<'a> {
23822382 Misplaced ( Span ) ,
23832383 }
23842384
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;
23852389 // This will allow the machine fix to directly place the keyword in the correct place or to indicate
23862390 // that the keyword is already present and the second instance should be removed.
23872391 let wrong_kw = if self . check_keyword ( kw:: Const ) {
23882392 match constness {
23892393 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+ }
23912398 }
23922399 } else if self . check_keyword ( kw:: Async ) {
23932400 match asyncness {
23942401 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+ }
23962410 }
23972411 } else if self . check_keyword ( kw:: Unsafe ) {
23982412 match unsafety {
23992413 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+ }
24012418 }
24022419 } else {
24032420 None
@@ -2467,6 +2484,23 @@ impl<'a> Parser<'a> {
24672484 }
24682485 }
24692486 }
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+
24702504 return Err ( err) ;
24712505 }
24722506 }
0 commit comments