Add unreachable attribute to Err branch

This commit is contained in:
Andrew Cann 2017-01-21 00:08:37 +08:00
parent c07aa33834
commit e7e7c360af
2 changed files with 37 additions and 16 deletions

View File

@ -1854,12 +1854,8 @@ impl<'a> LoweringContext<'a> {
P(self.expr_call(e.span, path, hir_vec![sub_expr]))
};
// Ok(val) => { #[allow(unreachable_code)] val }
let ok_arm = {
let val_ident = self.str_to_ident("val");
let val_pat = self.pat_ident(e.span, val_ident);
// #[allow(unreachable_code)]
let val_attr = {
let attr = {
// allow(unreachable_code)
let allow = {
let allow_ident = self.str_to_ident("allow");
@ -1871,11 +1867,16 @@ impl<'a> LoweringContext<'a> {
};
attr::mk_spanned_attr_outer(e.span, attr::mk_attr_id(), allow)
};
let attrs = From::from(vec![val_attr]);
let attrs = vec![attr];
// Ok(val) => { #[allow(unreachable_code)] val }
let ok_arm = {
let val_ident = self.str_to_ident("val");
let val_pat = self.pat_ident(e.span, val_ident);
let val_expr = P(self.expr_ident_with_attrs(e.span,
val_ident,
val_pat.id,
attrs));
From::from(attrs.clone())));
let ok_pat = self.pat_ok(e.span, val_pat);
self.arm(hir_vec![ok_pat], val_expr)
@ -1901,7 +1902,7 @@ impl<'a> LoweringContext<'a> {
let ret_expr = P(self.expr(e.span,
hir::Expr_::ExprRet(Some(from_err_expr)),
ThinVec::new()));
From::from(attrs)));
let err_pat = self.pat_err(e.span, err_local);
self.arm(hir_vec![err_pat], ret_expr)

View File

@ -12,6 +12,14 @@
#![deny(unreachable_code)]
#![deny(unreachable_patterns)]
enum Void {}
impl From<Void> for i32 {
fn from(v: Void) -> i32 {
match v {}
}
}
fn bar(x: Result<!, i32>) -> Result<u32, i32> {
x?
}
@ -23,8 +31,20 @@ fn foo(x: Result<!, i32>) -> Result<u32, i32> {
Ok(y)
}
fn qux(x: Result<u32, Void>) -> Result<u32, i32> {
Ok(x?)
}
fn vom(x: Result<u32, Void>) -> Result<u32, i32> {
let y = (match x { Ok(n) => Ok(n), Err(e) => Err(e) })?;
//~^ ERROR unreachable pattern
Ok(y)
}
fn main() {
let _ = bar(Err(123));
let _ = foo(Err(123));
let _ = qux(Ok(123));
let _ = vom(Ok(123));
}