diff options
| author | bors <bors@rust-lang.org> | 2017-10-20 05:24:04 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-10-20 05:24:04 +0000 |
| commit | c0e0a38101e750737e431b894167a8202ca8f46c (patch) | |
| tree | c811f3c85b0f5980cc66337e01a9bb8989ffe330 | |
| parent | 354eb160e0753620104d021fc013cc595588d7ff (diff) | |
| parent | 57f03ea5ff899c6567e18ad9e6b1f45288227340 (diff) | |
| download | rust-c0e0a38101e750737e431b894167a8202ca8f46c.tar.gz rust-c0e0a38101e750737e431b894167a8202ca8f46c.zip | |
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.
| -rw-r--r-- | src/librustc/hir/lowering.rs | 2 | ||||
| -rw-r--r-- | src/librustc_typeck/check/mod.rs | 7 | ||||
| -rw-r--r-- | src/test/run-pass/issue-45124.rs | 24 |
3 files changed, 32 insertions, 1 deletions
diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 54aecb4b00f..848da9637a2 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -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); diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 0ebccbc835f..d4616a7c304 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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() { diff --git a/src/test/run-pass/issue-45124.rs b/src/test/run-pass/issue-45124.rs new file mode 100644 index 00000000000..c65823e460b --- /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); +} |
