Skip to content

Commit 57f03ea

Browse files
committed
Mark block exits as reachable if the block can break.
1 parent 2689fd2 commit 57f03ea

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2547,7 +2547,7 @@ impl<'a> LoweringContext<'a> {
25472547
};
25482548

25492549
// Err(err) => #[allow(unreachable_code)]
2550-
// return Carrier::from_error(From::from(err)),
2550+
// return Try::from_error(From::from(err)),
25512551
let err_arm = {
25522552
let err_ident = self.str_to_ident("err");
25532553
let err_local = self.pat_ident(e.span, err_ident);

src/librustc_typeck/check/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -4284,6 +4284,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
42844284
CoerceMany::with_coercion_sites(coerce_to_ty, tail_expr)
42854285
};
42864286

4287+
let prev_diverges = self.diverges.get();
42874288
let ctxt = BreakableCtxt {
42884289
coerce: Some(coerce),
42894290
may_break: false,
@@ -4333,6 +4334,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
43334334
}
43344335
});
43354336

4337+
if ctxt.may_break {
4338+
// If we can break from the block, then the block's exit is always reachable
4339+
// (... as long as the entry is reachable) - regardless of the tail of the block.
4340+
self.diverges.set(prev_diverges);
4341+
}
4342+
43364343
let mut ty = ctxt.coerce.unwrap().complete(self);
43374344

43384345
if self.has_errors.get() || ty.references_error() {

src/test/run-pass/issue-45124.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2017 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+
#![feature(catch_expr)]
12+
13+
fn main() {
14+
let mut a = 0;
15+
let () = {
16+
let _: Result<(), ()> = do catch {
17+
let _ = Err(())?;
18+
return
19+
};
20+
a += 1;
21+
};
22+
a += 2;
23+
assert_eq!(a, 3);
24+
}

0 commit comments

Comments
 (0)