From 57f03ea5ff899c6567e18ad9e6b1f45288227340 Mon Sep 17 00:00:00 2001 From: Geoffry Song Date: Sun, 15 Oct 2017 21:18:08 -0700 Subject: [PATCH] Mark block exits as reachable if the block can break. --- src/librustc/hir/lowering.rs | 2 +- src/librustc_typeck/check/mod.rs | 7 +++++++ src/test/run-pass/issue-45124.rs | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/test/run-pass/issue-45124.rs diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 54aecb4b00f82..848da9637a299 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 6e263bdb1ad80..0e85ea47d567b 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -4284,6 +4284,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, @@ -4333,6 +4334,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 0000000000000..c65823e460be3 --- /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 or the MIT license +// , 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); +}