Skip to content

Commit b0a1183

Browse files
Don't Suggest Labeling Inline const Blocks
Previously, both anonymous constant blocks (E.g. The labeled block inside `['_'; 'block: { break 'block 1 + 2; }]`) and inline const blocks (E.g. `const { ... }`) were considered to be the same kind of blocks. This caused the compiler to incorrectly suggest labeling both the blocks when only anonymous constant blocks can be labeled. This PR adds an other enum Variant to `Context` so that both the blocks can be handled appropriately. Also, adds some doc comments and removes unnecessary `&mut` in a couple of places.
1 parent 073ac59 commit b0a1183

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

compiler/rustc_passes/src/loops.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
2324
enum 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,15 @@ 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(ref b, None)
187+
if matches!(self.cx_stack.last(), Some(&Fn) | Some(&ConstBlock)) =>
188+
{
179189
self.with_context(Normal, |v| v.visit_block(b));
180190
}
181191
hir::ExprKind::Block(ref b, None)
182192
if matches!(
183193
self.cx_stack.last(),
184-
Some(&Normal) | Some(&Constant) | Some(&UnlabeledBlock(_))
194+
Some(&Normal) | Some(&AnonConst) | Some(&UnlabeledBlock(_))
185195
) =>
186196
{
187197
self.with_context(UnlabeledBlock(b.span.shrink_to_lo()), |v| v.visit_block(b));
@@ -353,7 +363,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
353363
UnlabeledIfBlock(_) if br_cx_kind == BreakContextKind::Break => {
354364
self.require_break_cx(br_cx_kind, span, break_span, cx_pos - 1);
355365
}
356-
Normal | Constant | Fn | UnlabeledBlock(_) | UnlabeledIfBlock(_) => {
366+
Normal | AnonConst | Fn | UnlabeledBlock(_) | UnlabeledIfBlock(_) | ConstBlock => {
357367
self.sess.dcx().emit_err(OutsideLoop {
358368
spans: vec![span],
359369
name: &br_cx_kind.to_string(),
@@ -365,7 +375,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
365375
}
366376

367377
fn require_label_in_labeled_block(
368-
&mut self,
378+
&self,
369379
span: Span,
370380
label: &Destination,
371381
cf_type: &str,
@@ -380,7 +390,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
380390
false
381391
}
382392

383-
fn report_outside_loop_error(&mut self) {
393+
fn report_outside_loop_error(&self) {
384394
for (s, block) in &self.block_breaks {
385395
self.sess.dcx().emit_err(OutsideLoop {
386396
spans: block.spans.clone(),

0 commit comments

Comments
 (0)