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.
diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs
index 54aecb4..848da96 100644
--- a/src/librustc/hir/lowering.rs
+++ b/src/librustc/hir/lowering.rs
@@ -2547,7 +2547,7 @@
};
// Err(err) => #[allow(unreachable_code)]
- // return Carrier::from_error(From::from(err)),
+ // return Try::from_error(From::from(err)),
let err_arm = {
let err_ident = self.str_to_ident("err");
let err_local = self.pat_ident(e.span, err_ident);
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index 0ebccbc..d4616a7 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -4291,6 +4291,7 @@
CoerceMany::with_coercion_sites(coerce_to_ty, tail_expr)
};
+ let prev_diverges = self.diverges.get();
let ctxt = BreakableCtxt {
coerce: Some(coerce),
may_break: false,
@@ -4340,6 +4341,12 @@
}
});
+ if ctxt.may_break {
+ // If we can break from the block, then the block's exit is always reachable
+ // (... as long as the entry is reachable) - regardless of the tail of the block.
+ self.diverges.set(prev_diverges);
+ }
+
let mut ty = ctxt.coerce.unwrap().complete(self);
if self.has_errors.get() || ty.references_error() {
diff --git a/src/test/run-pass/issue-45124.rs b/src/test/run-pass/issue-45124.rs
new file mode 100644
index 0000000..c65823e
--- /dev/null
+++ b/src/test/run-pass/issue-45124.rs
@@ -0,0 +1,24 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(catch_expr)]
+
+fn main() {
+ let mut a = 0;
+ let () = {
+ let _: Result<(), ()> = do catch {
+ let _ = Err(())?;
+ return
+ };
+ a += 1;
+ };
+ a += 2;
+ assert_eq!(a, 3);
+}