Skip to content

Commit 7364022

Browse files
committed
Infer type ! for a loop that can only break out of other loops
Closes #23451. Signed-off-by: Anders Kaseorg <[email protected]>
1 parent bfac337 commit 7364022

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5239,7 +5239,7 @@ pub fn may_break(cx: &ty::ctxt, id: ast::NodeId, b: &ast::Block) -> bool {
52395239
// inside the loop?
52405240
(loop_query(&*b, |e| {
52415241
match *e {
5242-
ast::ExprBreak(_) => true,
5242+
ast::ExprBreak(None) => true,
52435243
_ => false
52445244
}
52455245
})) ||
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 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+
let _: i32 = loop { break }; //~ ERROR mismatched types
14+
}
15+
loop {
16+
let _: i32 = 'inner: loop { break 'inner }; //~ ERROR mismatched types
17+
}
18+
loop {
19+
let _: i32 = 'inner: loop { loop { break 'inner } }; //~ ERROR mismatched types
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 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+
'outer: loop {
13+
let _: i32 = loop { break 'outer };
14+
}
15+
'outer: loop {
16+
let _: i32 = loop { loop { break 'outer } };
17+
}
18+
}

0 commit comments

Comments
 (0)