@@ -827,6 +827,65 @@ impl<'a> Parser<'a> {
827
827
None
828
828
}
829
829
830
+ pub ( super ) fn recover_closure_body (
831
+ & mut self ,
832
+ mut err : DiagnosticBuilder < ' a , ErrorGuaranteed > ,
833
+ before : token:: Token ,
834
+ prev : token:: Token ,
835
+ token : token:: Token ,
836
+ lo : Span ,
837
+ decl_hi : Span ,
838
+ ) -> PResult < ' a , P < Expr > > {
839
+ err. span_label ( lo. to ( decl_hi) , "while parsing the body of this closure" ) ;
840
+ match before. kind {
841
+ token:: OpenDelim ( Delimiter :: Brace )
842
+ if !matches ! ( token. kind, token:: OpenDelim ( Delimiter :: Brace ) ) =>
843
+ {
844
+ // `{ || () }` should have been `|| { () }`
845
+ err. multipart_suggestion (
846
+ "you might have meant to open the body of the closure, instead of enclosing \
847
+ the closure in a block",
848
+ vec ! [
849
+ ( before. span, String :: new( ) ) ,
850
+ ( prev. span. shrink_to_hi( ) , " {" . to_string( ) ) ,
851
+ ] ,
852
+ Applicability :: MaybeIncorrect ,
853
+ ) ;
854
+ err. emit ( ) ;
855
+ self . eat_to_tokens ( & [ & token:: CloseDelim ( Delimiter :: Brace ) ] ) ;
856
+ }
857
+ token:: OpenDelim ( Delimiter :: Parenthesis )
858
+ if !matches ! ( token. kind, token:: OpenDelim ( Delimiter :: Brace ) ) =>
859
+ {
860
+ // We are within a function call or tuple, we can emit the error
861
+ // and recover.
862
+ self . eat_to_tokens ( & [ & token:: CloseDelim ( Delimiter :: Parenthesis ) , & token:: Comma ] ) ;
863
+
864
+ err. multipart_suggestion_verbose (
865
+ "you might have meant to open the body of the closure" ,
866
+ vec ! [
867
+ ( prev. span. shrink_to_hi( ) , " {" . to_string( ) ) ,
868
+ ( self . token. span. shrink_to_lo( ) , "}" . to_string( ) ) ,
869
+ ] ,
870
+ Applicability :: MaybeIncorrect ,
871
+ ) ;
872
+ err. emit ( ) ;
873
+ }
874
+ _ if !matches ! ( token. kind, token:: OpenDelim ( Delimiter :: Brace ) ) => {
875
+ // We don't have a heuristic to correctly identify where the block
876
+ // should be closed.
877
+ err. multipart_suggestion_verbose (
878
+ "you might have meant to open the body of the closure" ,
879
+ vec ! [ ( prev. span. shrink_to_hi( ) , " {" . to_string( ) ) ] ,
880
+ Applicability :: HasPlaceholders ,
881
+ ) ;
882
+ return Err ( err) ;
883
+ }
884
+ _ => return Err ( err) ,
885
+ }
886
+ Ok ( self . mk_expr_err ( lo. to ( self . token . span ) ) )
887
+ }
888
+
830
889
/// Eats and discards tokens until one of `kets` is encountered. Respects token trees,
831
890
/// passes through any errors encountered. Used for error recovery.
832
891
pub ( super ) fn eat_to_tokens ( & mut self , kets : & [ & TokenKind ] ) {
0 commit comments