@@ -2286,7 +2286,7 @@ impl<'a> Parser<'a> {
22862286 } ) ;
22872287 }
22882288
2289- let ( attrs, blk) = self . parse_block_common ( lo, blk_mode, true ) ?;
2289+ let ( attrs, blk) = self . parse_block_common ( lo, blk_mode, true , None ) ?;
22902290 Ok ( self . mk_expr_with_attrs ( blk. span , ExprKind :: Block ( blk, opt_label) , attrs) )
22912291 }
22922292
@@ -2851,7 +2851,11 @@ impl<'a> Parser<'a> {
28512851 ) ) ;
28522852 }
28532853
2854- let ( attrs, loop_block) = self . parse_inner_attrs_and_block ( ) ?;
2854+ let ( attrs, loop_block) = self . parse_inner_attrs_and_block (
2855+ // Only suggest moving erroneous block label to the loop header
2856+ // if there is not already a label there
2857+ opt_label. is_none ( ) . then_some ( lo) ,
2858+ ) ?;
28552859
28562860 let kind = ExprKind :: ForLoop { pat, iter : expr, body : loop_block, label : opt_label, kind } ;
28572861
@@ -2894,11 +2898,17 @@ impl<'a> Parser<'a> {
28942898 err. span_label ( lo, "while parsing the condition of this `while` expression" ) ;
28952899 err
28962900 } ) ?;
2897- let ( attrs, body) = self . parse_inner_attrs_and_block ( ) . map_err ( |mut err| {
2898- err. span_label ( lo, "while parsing the body of this `while` expression" ) ;
2899- err. span_label ( cond. span , "this `while` condition successfully parsed" ) ;
2900- err
2901- } ) ?;
2901+ let ( attrs, body) = self
2902+ . parse_inner_attrs_and_block (
2903+ // Only suggest moving erroneous block label to the loop header
2904+ // if there is not already a label there
2905+ opt_label. is_none ( ) . then_some ( lo) ,
2906+ )
2907+ . map_err ( |mut err| {
2908+ err. span_label ( lo, "while parsing the body of this `while` expression" ) ;
2909+ err. span_label ( cond. span , "this `while` condition successfully parsed" ) ;
2910+ err
2911+ } ) ?;
29022912
29032913 self . recover_loop_else ( "while" , lo) ?;
29042914
@@ -2912,7 +2922,11 @@ impl<'a> Parser<'a> {
29122922 /// Parses `loop { ... }` (`loop` token already eaten).
29132923 fn parse_expr_loop ( & mut self , opt_label : Option < Label > , lo : Span ) -> PResult < ' a , P < Expr > > {
29142924 let loop_span = self . prev_token . span ;
2915- let ( attrs, body) = self . parse_inner_attrs_and_block ( ) ?;
2925+ let ( attrs, body) = self . parse_inner_attrs_and_block (
2926+ // Only suggest moving erroneous block label to the loop header
2927+ // if there is not already a label there
2928+ opt_label. is_none ( ) . then_some ( lo) ,
2929+ ) ?;
29162930 self . recover_loop_else ( "loop" , lo) ?;
29172931 Ok ( self . mk_expr_with_attrs (
29182932 lo. to ( self . prev_token . span ) ,
@@ -2962,7 +2976,7 @@ impl<'a> Parser<'a> {
29622976 Applicability :: MaybeIncorrect , // speculative
29632977 ) ;
29642978 }
2965- if self . maybe_recover_unexpected_block_label ( ) {
2979+ if self . maybe_recover_unexpected_block_label ( None ) {
29662980 e. cancel ( ) ;
29672981 self . bump ( ) ;
29682982 } else {
@@ -3376,7 +3390,7 @@ impl<'a> Parser<'a> {
33763390
33773391 /// Parses a `try {...}` expression (`try` token already eaten).
33783392 fn parse_try_block ( & mut self , span_lo : Span ) -> PResult < ' a , P < Expr > > {
3379- let ( attrs, body) = self . parse_inner_attrs_and_block ( ) ?;
3393+ let ( attrs, body) = self . parse_inner_attrs_and_block ( None ) ?;
33803394 if self . eat_keyword ( exp ! ( Catch ) ) {
33813395 Err ( self . dcx ( ) . create_err ( errors:: CatchAfterTry { span : self . prev_token . span } ) )
33823396 } else {
@@ -3424,7 +3438,7 @@ impl<'a> Parser<'a> {
34243438 }
34253439 let capture_clause = self . parse_capture_clause ( ) ?;
34263440 let decl_span = lo. to ( self . prev_token . span ) ;
3427- let ( attrs, body) = self . parse_inner_attrs_and_block ( ) ?;
3441+ let ( attrs, body) = self . parse_inner_attrs_and_block ( None ) ?;
34283442 let kind = ExprKind :: Gen ( capture_clause, body, kind, decl_span) ;
34293443 Ok ( self . mk_expr_with_attrs ( lo. to ( self . prev_token . span ) , kind, attrs) )
34303444 }
0 commit comments