Skip to content

Commit 7ad1c62

Browse files
committed
Fix an ICE using break and continue as array lengths
1 parent c6bbee8 commit 7ad1c62

File tree

7 files changed

+60
-6
lines changed

7 files changed

+60
-6
lines changed

src/librustc_driver/driver.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,7 @@ where
12111211
.set(derive_registrar::find(&hir_map));
12121212

12131213
time(sess, "loop checking", || loops::check_crate(sess, &hir_map));
1214+
sess.abort_if_errors();
12141215

12151216
let mut local_providers = ty::query::Providers::default();
12161217
default_provide(&mut local_providers);

src/librustc_mir/build/scope.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
541541
/// Finds the breakable scope for a given label. This is used for
542542
/// resolving `break` and `continue`.
543543
pub fn find_breakable_scope(&self,
544-
span: Span,
545-
label: region::Scope)
546-
-> &BreakableScope<'tcx> {
544+
span: Span,
545+
label: region::Scope)
546+
-> &BreakableScope<'tcx> {
547547
// find the loop-scope with the correct id
548548
self.breakable_scopes.iter()
549549
.rev()

src/librustc_passes/loops.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ enum Context {
4040
Loop(LoopKind),
4141
Closure,
4242
LabeledBlock,
43+
AnonConst,
4344
}
4445

4546
#[derive(Copy, Clone)]
@@ -71,6 +72,10 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
7172
self.with_context(Normal, |v| intravisit::walk_impl_item(v, i));
7273
}
7374

75+
fn visit_anon_const(&mut self, c: &'hir hir::AnonConst) {
76+
self.with_context(AnonConst, |v| intravisit::walk_anon_const(v, c));
77+
}
78+
7479
fn visit_expr(&mut self, e: &'hir hir::Expr) {
7580
match e.node {
7681
hir::ExprWhile(ref e, ref b, _) => {
@@ -194,7 +199,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
194199
.span_label(span, "cannot break inside of a closure")
195200
.emit();
196201
}
197-
Normal => {
202+
Normal | AnonConst => {
198203
struct_span_err!(self.sess, span, E0268, "`{}` outside of loop", name)
199204
.span_label(span, "cannot break outside of a loop")
200205
.emit();

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

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
loop {
13+
|_: [_; break]| {} //~ ERROR: `break` outside of loop
14+
}
15+
16+
loop {
17+
|_: [_; continue]| {} //~ ERROR: `continue` outside of loop
18+
}
19+
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0268]: `break` outside of loop
2+
--> $DIR/array-break-length.rs:13:17
3+
|
4+
LL | |_: [_; break]| {} //~ ERROR: `break` outside of loop
5+
| ^^^^^ cannot break outside of a loop
6+
7+
error[E0268]: `continue` outside of loop
8+
--> $DIR/array-break-length.rs:17:17
9+
|
10+
LL | |_: [_; continue]| {} //~ ERROR: `continue` outside of loop
11+
| ^^^^^^^^ cannot break outside of a loop
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0268`.

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

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

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

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

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,25 @@ error[E0590]: `break` or `continue` with no label in the condition of a `while`
1010
LL | while |_: [_; continue]| {} {} //~ ERROR: `break` or `continue` with no label
1111
| ^^^^^^^^ unlabeled `continue` in the condition of a `while` loop
1212

13+
error[E0268]: `continue` outside of loop
14+
--> $DIR/closure-array-break-length.rs:14:19
15+
|
16+
LL | while |_: [_; continue]| {} {} //~ ERROR: `break` or `continue` with no label
17+
| ^^^^^^^^ cannot break outside of a loop
18+
1319
error[E0590]: `break` or `continue` with no label in the condition of a `while` loop
14-
--> $DIR/closure-array-break-length.rs:16:19
20+
--> $DIR/closure-array-break-length.rs:17:19
1521
|
1622
LL | while |_: [_; break]| {} {} //~ ERROR: `break` or `continue` with no label
1723
| ^^^^^ unlabeled `break` in the condition of a `while` loop
1824

19-
error: aborting due to 3 previous errors
25+
error[E0268]: `break` outside of loop
26+
--> $DIR/closure-array-break-length.rs:17:19
27+
|
28+
LL | while |_: [_; break]| {} {} //~ ERROR: `break` or `continue` with no label
29+
| ^^^^^ cannot break outside of a loop
30+
31+
error: aborting due to 5 previous errors
2032

2133
Some errors occurred: E0268, E0590.
2234
For more information about an error, try `rustc --explain E0268`.

0 commit comments

Comments
 (0)