Skip to content

Commit 30fde04

Browse files
committed
Clean up error messages regarding break/continue inside consts
1 parent b00050f commit 30fde04

File tree

3 files changed

+42
-47
lines changed

3 files changed

+42
-47
lines changed

src/librustc/hir/lowering.rs

+35-25
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub struct LoweringContext<'a> {
103103
loop_scopes: Vec<NodeId>,
104104
is_in_loop_condition: bool,
105105
is_in_trait_impl: bool,
106+
is_in_anon_const: bool,
106107

107108
/// What to do when we encounter either an "anonymous lifetime
108109
/// reference". The term "anonymous" is meant to encompass both
@@ -230,6 +231,7 @@ pub fn lower_crate(
230231
node_id_to_hir_id: IndexVec::new(),
231232
is_generator: false,
232233
is_in_trait_impl: false,
234+
is_in_anon_const: false,
233235
lifetimes_to_define: Vec::new(),
234236
is_collecting_in_band_lifetimes: false,
235237
in_scope_lifetimes: Vec::new(),
@@ -968,31 +970,30 @@ impl<'a> LoweringContext<'a> {
968970
}
969971

970972
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
971-
match destination {
972-
Some((id, label)) => {
973-
let target_id = if let Def::Label(loop_id) = self.expect_full_def(id) {
974-
Ok(self.lower_node_id(loop_id).node_id)
975-
} else {
976-
Err(hir::LoopIdError::UnresolvedLabel)
977-
};
978-
hir::Destination {
979-
label: self.lower_label(Some(label)),
980-
target_id,
973+
let target_id = if self.is_in_anon_const {
974+
Err(hir::LoopIdError::OutsideLoopScope)
975+
} else {
976+
match destination {
977+
Some((id, _)) => {
978+
if let Def::Label(loop_id) = self.expect_full_def(id) {
979+
Ok(self.lower_node_id(loop_id).node_id)
980+
} else {
981+
Err(hir::LoopIdError::UnresolvedLabel)
982+
}
981983
}
982-
}
983-
None => {
984-
let target_id = self.loop_scopes
985-
.last()
986-
.map(|innermost_loop_id| *innermost_loop_id)
987-
.map(|id| Ok(self.lower_node_id(id).node_id))
988-
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
989-
.into();
990-
991-
hir::Destination {
992-
label: None,
993-
target_id,
984+
None => {
985+
self.loop_scopes
986+
.last()
987+
.map(|innermost_loop_id| *innermost_loop_id)
988+
.map(|id| Ok(self.lower_node_id(id).node_id))
989+
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
990+
.into()
994991
}
995992
}
993+
};
994+
hir::Destination {
995+
label: self.lower_label(destination.map(|(_, label)| label)),
996+
target_id,
996997
}
997998
}
998999

@@ -3440,13 +3441,22 @@ impl<'a> LoweringContext<'a> {
34403441
}
34413442

34423443
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
3443-
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(c.id);
3444+
let was_in_loop_condition = self.is_in_loop_condition;
3445+
self.is_in_loop_condition = false;
3446+
let was_in_anon_const = self.is_in_anon_const;
3447+
self.is_in_anon_const = true;
34443448

3445-
hir::AnonConst {
3449+
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(c.id);
3450+
let anon_const = hir::AnonConst {
34463451
id: node_id,
34473452
hir_id,
34483453
body: self.lower_body(None, |this| this.lower_expr(&c.value)),
3449-
}
3454+
};
3455+
3456+
self.is_in_anon_const = was_in_anon_const;
3457+
self.is_in_loop_condition = was_in_loop_condition;
3458+
3459+
anon_const
34503460
}
34513461

34523462
fn lower_expr(&mut self, e: &Expr) -> hir::Expr {

src/test/ui/closure-array-break-length.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
fn main() {
1212
|_: [_; continue]| {}; //~ ERROR: `continue` outside of loop
1313

14-
while |_: [_; continue]| {} {} //~ ERROR: `break` or `continue` with no label
15-
//~^ ERROR: `continue` outside of loop
14+
while |_: [_; continue]| {} {} //~ ERROR: `continue` outside of loop
1615

17-
while |_: [_; break]| {} {} //~ ERROR: `break` or `continue` with no label
18-
//~^ ERROR: `break` outside of loop
16+
while |_: [_; break]| {} {} //~ ERROR: `break` outside of loop
1917
}

src/test/ui/closure-array-break-length.stderr

+5-18
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,18 @@ error[E0268]: `continue` outside of loop
44
LL | |_: [_; continue]| {}; //~ ERROR: `continue` outside of loop
55
| ^^^^^^^^ cannot break outside of a loop
66

7-
error[E0590]: `break` or `continue` with no label in the condition of a `while` loop
8-
--> $DIR/closure-array-break-length.rs:14:19
9-
|
10-
LL | while |_: [_; continue]| {} {} //~ ERROR: `break` or `continue` with no label
11-
| ^^^^^^^^ unlabeled `continue` in the condition of a `while` loop
12-
137
error[E0268]: `continue` outside of loop
148
--> $DIR/closure-array-break-length.rs:14:19
159
|
16-
LL | while |_: [_; continue]| {} {} //~ ERROR: `break` or `continue` with no label
10+
LL | while |_: [_; continue]| {} {} //~ ERROR: `continue` outside of loop
1711
| ^^^^^^^^ cannot break outside of a loop
1812

19-
error[E0590]: `break` or `continue` with no label in the condition of a `while` loop
20-
--> $DIR/closure-array-break-length.rs:17:19
21-
|
22-
LL | while |_: [_; break]| {} {} //~ ERROR: `break` or `continue` with no label
23-
| ^^^^^ unlabeled `break` in the condition of a `while` loop
24-
2513
error[E0268]: `break` outside of loop
26-
--> $DIR/closure-array-break-length.rs:17:19
14+
--> $DIR/closure-array-break-length.rs:16:19
2715
|
28-
LL | while |_: [_; break]| {} {} //~ ERROR: `break` or `continue` with no label
16+
LL | while |_: [_; break]| {} {} //~ ERROR: `break` outside of loop
2917
| ^^^^^ cannot break outside of a loop
3018

31-
error: aborting due to 5 previous errors
19+
error: aborting due to 3 previous errors
3220

33-
Some errors occurred: E0268, E0590.
34-
For more information about an error, try `rustc --explain E0268`.
21+
For more information about this error, try `rustc --explain E0268`.

0 commit comments

Comments
 (0)