@@ -19,17 +19,25 @@ use crate::errors::{
19
19
OutsideLoopSuggestion , UnlabeledCfInWhileCondition , UnlabeledInLabeledBlock ,
20
20
} ;
21
21
22
+ /// The context in which a block is encountered.
22
23
#[ derive( Clone , Copy , Debug , PartialEq ) ]
23
24
enum Context {
24
25
Normal ,
25
26
Fn ,
26
27
Loop ( hir:: LoopSource ) ,
27
28
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
+ } ,
29
34
UnlabeledBlock ( Span ) ,
30
35
UnlabeledIfBlock ( Span ) ,
31
36
LabeledBlock ,
32
- Constant ,
37
+ /// E.g. The labeled block inside `['_'; 'block: { break 'block 1 + 2; }]`.
38
+ AnonConst ,
39
+ /// E.g. `const { ... }`.
40
+ ConstBlock ,
33
41
}
34
42
35
43
#[ derive( Clone ) ]
@@ -90,11 +98,11 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
90
98
}
91
99
92
100
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) ) ;
94
102
}
95
103
96
104
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) ) ;
98
106
}
99
107
100
108
fn visit_fn (
@@ -128,7 +136,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
128
136
&& matches ! (
129
137
ck_loop. cx_stack. last( ) ,
130
138
Some ( & Normal )
131
- | Some ( & Constant )
139
+ | Some ( & AnonConst )
132
140
| Some ( & UnlabeledBlock ( _) )
133
141
| Some ( & UnlabeledIfBlock ( _) )
134
142
)
@@ -175,14 +183,18 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
175
183
hir:: ExprKind :: Block ( ref b, Some ( _label) ) => {
176
184
self . with_context ( LabeledBlock , |v| v. visit_block ( b) ) ;
177
185
}
178
- hir:: ExprKind :: Block ( ref b, None ) if matches ! ( self . cx_stack. last( ) , Some ( & Fn ) ) => {
186
+ hir:: ExprKind :: Block ( ref b, None )
187
+ if matches ! ( self . cx_stack. last( ) , Some ( & Fn ) | Some ( & ConstBlock ) ) =>
188
+ {
179
189
self . with_context ( Normal , |v| v. visit_block ( b) ) ;
180
190
}
181
- hir:: ExprKind :: Block ( ref b, None )
182
- if matches ! (
183
- self . cx_stack. last( ) ,
184
- Some ( & Normal ) | Some ( & Constant ) | Some ( & UnlabeledBlock ( _) )
185
- ) =>
191
+ hir:: ExprKind :: Block (
192
+ ref b @ hir:: Block { rules : hir:: BlockCheckMode :: DefaultBlock , .. } ,
193
+ None ,
194
+ ) if matches ! (
195
+ self . cx_stack. last( ) ,
196
+ Some ( & Normal ) | Some ( & AnonConst ) | Some ( & UnlabeledBlock ( _) )
197
+ ) =>
186
198
{
187
199
self . with_context ( UnlabeledBlock ( b. span . shrink_to_lo ( ) ) , |v| v. visit_block ( b) ) ;
188
200
}
@@ -353,7 +365,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
353
365
UnlabeledIfBlock ( _) if br_cx_kind == BreakContextKind :: Break => {
354
366
self . require_break_cx ( br_cx_kind, span, break_span, cx_pos - 1 ) ;
355
367
}
356
- Normal | Constant | Fn | UnlabeledBlock ( _) | UnlabeledIfBlock ( _) => {
368
+ Normal | AnonConst | Fn | UnlabeledBlock ( _) | UnlabeledIfBlock ( _) | ConstBlock => {
357
369
self . sess . dcx ( ) . emit_err ( OutsideLoop {
358
370
spans : vec ! [ span] ,
359
371
name : & br_cx_kind. to_string ( ) ,
@@ -365,7 +377,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
365
377
}
366
378
367
379
fn require_label_in_labeled_block (
368
- & mut self ,
380
+ & self ,
369
381
span : Span ,
370
382
label : & Destination ,
371
383
cf_type : & str ,
@@ -380,7 +392,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
380
392
false
381
393
}
382
394
383
- fn report_outside_loop_error ( & mut self ) {
395
+ fn report_outside_loop_error ( & self ) {
384
396
for ( s, block) in & self . block_breaks {
385
397
self . sess . dcx ( ) . emit_err ( OutsideLoop {
386
398
spans : block. spans . clone ( ) ,
0 commit comments