@@ -2286,7 +2286,7 @@ impl<'a> Parser<'a> {
2286
2286
} ) ;
2287
2287
}
2288
2288
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 ) ?;
2290
2290
Ok ( self . mk_expr_with_attrs ( blk. span , ExprKind :: Block ( blk, opt_label) , attrs) )
2291
2291
}
2292
2292
@@ -2851,7 +2851,11 @@ impl<'a> Parser<'a> {
2851
2851
) ) ;
2852
2852
}
2853
2853
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
+ ) ?;
2855
2859
2856
2860
let kind = ExprKind :: ForLoop { pat, iter : expr, body : loop_block, label : opt_label, kind } ;
2857
2861
@@ -2894,11 +2898,17 @@ impl<'a> Parser<'a> {
2894
2898
err. span_label ( lo, "while parsing the condition of this `while` expression" ) ;
2895
2899
err
2896
2900
} ) ?;
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
+ } ) ?;
2902
2912
2903
2913
self . recover_loop_else ( "while" , lo) ?;
2904
2914
@@ -2912,7 +2922,11 @@ impl<'a> Parser<'a> {
2912
2922
/// Parses `loop { ... }` (`loop` token already eaten).
2913
2923
fn parse_expr_loop ( & mut self , opt_label : Option < Label > , lo : Span ) -> PResult < ' a , P < Expr > > {
2914
2924
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
+ ) ?;
2916
2930
self . recover_loop_else ( "loop" , lo) ?;
2917
2931
Ok ( self . mk_expr_with_attrs (
2918
2932
lo. to ( self . prev_token . span ) ,
@@ -2962,7 +2976,7 @@ impl<'a> Parser<'a> {
2962
2976
Applicability :: MaybeIncorrect , // speculative
2963
2977
) ;
2964
2978
}
2965
- if self . maybe_recover_unexpected_block_label ( ) {
2979
+ if self . maybe_recover_unexpected_block_label ( None ) {
2966
2980
e. cancel ( ) ;
2967
2981
self . bump ( ) ;
2968
2982
} else {
@@ -3376,7 +3390,7 @@ impl<'a> Parser<'a> {
3376
3390
3377
3391
/// Parses a `try {...}` expression (`try` token already eaten).
3378
3392
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 ) ?;
3380
3394
if self . eat_keyword ( exp ! ( Catch ) ) {
3381
3395
Err ( self . dcx ( ) . create_err ( errors:: CatchAfterTry { span : self . prev_token . span } ) )
3382
3396
} else {
@@ -3424,7 +3438,7 @@ impl<'a> Parser<'a> {
3424
3438
}
3425
3439
let capture_clause = self . parse_capture_clause ( ) ?;
3426
3440
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 ) ?;
3428
3442
let kind = ExprKind :: Gen ( capture_clause, body, kind, decl_span) ;
3429
3443
Ok ( self . mk_expr_with_attrs ( lo. to ( self . prev_token . span ) , kind, attrs) )
3430
3444
}
0 commit comments