@@ -19,17 +19,25 @@ use crate::errors::{
1919 OutsideLoopSuggestion , UnlabeledCfInWhileCondition , UnlabeledInLabeledBlock ,
2020} ;
2121
22+ /// The context in which a block is encountered.
2223#[ derive( Clone , Copy , Debug , PartialEq ) ]
2324enum Context {
2425 Normal ,
2526 Fn ,
2627 Loop ( hir:: LoopSource ) ,
2728 Closure ( Span ) ,
28- Coroutine { coroutine_span : Span , kind : hir:: CoroutineDesugaring , source : hir:: CoroutineSource } ,
29+ Coroutine {
30+ coroutine_span : Span ,
31+ kind : hir:: CoroutineDesugaring ,
32+ source : hir:: CoroutineSource ,
33+ } ,
2934 UnlabeledBlock ( Span ) ,
3035 UnlabeledIfBlock ( Span ) ,
3136 LabeledBlock ,
32- Constant ,
37+ /// E.g. The labeled block inside `['_'; 'block: { break 'block 1 + 2; }]`.
38+ AnonConst ,
39+ /// E.g. `const { ... }`.
40+ ConstBlock ,
3341}
3442
3543#[ derive( Clone ) ]
@@ -90,11 +98,11 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
9098 }
9199
92100 fn visit_anon_const ( & mut self , c : & ' hir hir:: AnonConst ) {
93- self . with_context ( Constant , |v| intravisit:: walk_anon_const ( v, c) ) ;
101+ self . with_context ( AnonConst , |v| intravisit:: walk_anon_const ( v, c) ) ;
94102 }
95103
96104 fn visit_inline_const ( & mut self , c : & ' hir hir:: ConstBlock ) {
97- self . with_context ( Constant , |v| intravisit:: walk_inline_const ( v, c) ) ;
105+ self . with_context ( ConstBlock , |v| intravisit:: walk_inline_const ( v, c) ) ;
98106 }
99107
100108 fn visit_fn (
@@ -128,7 +136,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
128136 && matches ! (
129137 ck_loop. cx_stack. last( ) ,
130138 Some ( & Normal )
131- | Some ( & Constant )
139+ | Some ( & AnonConst )
132140 | Some ( & UnlabeledBlock ( _) )
133141 | Some ( & UnlabeledIfBlock ( _) )
134142 )
@@ -175,13 +183,21 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
175183 hir:: ExprKind :: Block ( ref b, Some ( _label) ) => {
176184 self . with_context ( LabeledBlock , |v| v. visit_block ( b) ) ;
177185 }
178- hir:: ExprKind :: Block ( ref b, None ) if matches ! ( self . cx_stack. last( ) , Some ( & Fn ) ) => {
186+ hir:: ExprKind :: Block (
187+ ref b @ hir:: Block { rules : hir:: BlockCheckMode :: UnsafeBlock ( _) , .. } ,
188+ None ,
189+ ) => {
190+ self . with_context ( Normal , |v| v. visit_block ( b) ) ;
191+ }
192+ hir:: ExprKind :: Block ( ref b, None )
193+ if matches ! ( self . cx_stack. last( ) , Some ( & Fn ) | Some ( & ConstBlock ) ) =>
194+ {
179195 self . with_context ( Normal , |v| v. visit_block ( b) ) ;
180196 }
181197 hir:: ExprKind :: Block ( ref b, None )
182198 if matches ! (
183199 self . cx_stack. last( ) ,
184- Some ( & Normal ) | Some ( & Constant ) | Some ( & UnlabeledBlock ( _) )
200+ Some ( & Normal ) | Some ( & AnonConst ) | Some ( & UnlabeledBlock ( _) )
185201 ) =>
186202 {
187203 self . with_context ( UnlabeledBlock ( b. span . shrink_to_lo ( ) ) , |v| v. visit_block ( b) ) ;
@@ -353,7 +369,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
353369 UnlabeledIfBlock ( _) if br_cx_kind == BreakContextKind :: Break => {
354370 self . require_break_cx ( br_cx_kind, span, break_span, cx_pos - 1 ) ;
355371 }
356- Normal | Constant | Fn | UnlabeledBlock ( _) | UnlabeledIfBlock ( _) => {
372+ Normal | AnonConst | Fn | UnlabeledBlock ( _) | UnlabeledIfBlock ( _) | ConstBlock => {
357373 self . sess . dcx ( ) . emit_err ( OutsideLoop {
358374 spans : vec ! [ span] ,
359375 name : & br_cx_kind. to_string ( ) ,
@@ -365,7 +381,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
365381 }
366382
367383 fn require_label_in_labeled_block (
368- & mut self ,
384+ & self ,
369385 span : Span ,
370386 label : & Destination ,
371387 cf_type : & str ,
@@ -380,7 +396,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
380396 false
381397 }
382398
383- fn report_outside_loop_error ( & mut self ) {
399+ fn report_outside_loop_error ( & self ) {
384400 for ( s, block) in & self . block_breaks {
385401 self . sess . dcx ( ) . emit_err ( OutsideLoop {
386402 spans : block. spans . clone ( ) ,
0 commit comments