Skip to content

Commit

Permalink
Auto merge of #45316 - goffrie:exitable-breakable-block, r=nikomatsakis
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Oct 20, 2017
2 parents 354eb16 + 57f03ea commit c0e0a38
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down
24 changes: 24 additions & 0 deletions src/test/run-pass/issue-45124.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit c0e0a38

Please sign in to comment.