Skip to content

Commit c0e0a38

Browse files
committed
Auto merge of #45316 - goffrie:exitable-breakable-block, r=nikomatsakis
Mark block exits as reachable if the block can break. This only happens when desugaring `catch` expressions for now, but regular blocks (in HIR) can be broken from - respect that when doing reachability analysis. Fixes #45124.
2 parents 354eb16 + 57f03ea commit c0e0a38

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
@@ -4291,6 +4291,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
42914291
CoerceMany::with_coercion_sites(coerce_to_ty, tail_expr)
42924292
};
42934293

4294+
let prev_diverges = self.diverges.get();
42944295
let ctxt = BreakableCtxt {
42954296
coerce: Some(coerce),
42964297
may_break: false,
@@ -4340,6 +4341,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
43404341
}
43414342
});
43424343

4344+
if ctxt.may_break {
4345+
// If we can break from the block, then the block's exit is always reachable
4346+
// (... as long as the entry is reachable) - regardless of the tail of the block.
4347+
self.diverges.set(prev_diverges);
4348+
}
4349+
43434350
let mut ty = ctxt.coerce.unwrap().complete(self);
43444351

43454352
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)