1
Fork 0

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.
This commit is contained in:
bors 2017-10-20 05:24:04 +00:00
commit c0e0a38101
3 changed files with 32 additions and 1 deletions

View file

@ -2547,7 +2547,7 @@ impl<'a> LoweringContext<'a> {
};
// 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);

View file

@ -4291,6 +4291,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
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 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
});
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() {

View file

@ -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);
}