Improve the error message for missing else clauses in if expressions
This commit is contained in:
parent
4a382d7c47
commit
43e5d10428
5 changed files with 63 additions and 31 deletions
|
@ -2999,8 +2999,6 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
|
|||
expected: Expectation) {
|
||||
check_expr_has_type(fcx, cond_expr, ty::mk_bool());
|
||||
|
||||
let branches_ty = match opt_else_expr {
|
||||
Some(ref else_expr) => {
|
||||
// Disregard "castable to" expectations because they
|
||||
// can lead us astray. Consider for example `if cond
|
||||
// {22} else {c} as u8` -- if we propagate the
|
||||
|
@ -3028,6 +3026,9 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
|
|||
};
|
||||
check_block_with_expected(fcx, then_blk, expected);
|
||||
let then_ty = fcx.node_ty(then_blk.id);
|
||||
|
||||
let branches_ty = match opt_else_expr {
|
||||
Some(ref else_expr) => {
|
||||
check_expr_with_expectation(fcx, &**else_expr, expected);
|
||||
let else_ty = fcx.expr_ty(&**else_expr);
|
||||
infer::common_supertype(fcx.infcx(),
|
||||
|
@ -3037,8 +3038,11 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
|
|||
else_ty)
|
||||
}
|
||||
None => {
|
||||
check_block_no_value(fcx, then_blk);
|
||||
ty::mk_nil()
|
||||
infer::common_supertype(fcx.infcx(),
|
||||
infer::IfExpressionWithNoElse(sp),
|
||||
false,
|
||||
then_ty,
|
||||
ty::mk_nil())
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -366,6 +366,7 @@ impl<'a, 'tcx> ErrorReporting for InferCtxt<'a, 'tcx> {
|
|||
infer::RelateOutputImplTypes(_) => "mismatched types",
|
||||
infer::MatchExpressionArm(_, _) => "match arms have incompatible types",
|
||||
infer::IfExpression(_) => "if and else have incompatible types",
|
||||
infer::IfExpressionWithNoElse(_) => "if may be missing an else clause",
|
||||
};
|
||||
|
||||
self.tcx.sess.span_err(
|
||||
|
@ -1486,6 +1487,9 @@ impl<'a, 'tcx> ErrorReportingHelpers for InferCtxt<'a, 'tcx> {
|
|||
infer::IfExpression(_) => {
|
||||
format!("if and else have compatible types")
|
||||
}
|
||||
infer::IfExpressionWithNoElse(_) => {
|
||||
format!("if may be missing an else clause")
|
||||
}
|
||||
};
|
||||
|
||||
match self.values_str(&trace.values) {
|
||||
|
|
|
@ -121,6 +121,9 @@ pub enum TypeOrigin {
|
|||
|
||||
// Computing common supertype in an if expression
|
||||
IfExpression(Span),
|
||||
|
||||
// Computing common supertype of an if expression with no else counter-part
|
||||
IfExpressionWithNoElse(Span)
|
||||
}
|
||||
|
||||
/// See `error_reporting.rs` for more details
|
||||
|
@ -1001,6 +1004,7 @@ impl TypeOrigin {
|
|||
RelateOutputImplTypes(span) => span,
|
||||
MatchExpressionArm(match_span, _) => match_span,
|
||||
IfExpression(span) => span,
|
||||
IfExpressionWithNoElse(span) => span
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1030,6 +1034,9 @@ impl Repr for TypeOrigin {
|
|||
IfExpression(a) => {
|
||||
format!("IfExpression({})", a.repr(tcx))
|
||||
}
|
||||
IfExpressionWithNoElse(a) => {
|
||||
format!("IfExpressionWithNoElse({})", a.repr(tcx))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,11 +8,10 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// error-pattern:mismatched types: expected `()`, found `bool`
|
||||
|
||||
extern crate debug;
|
||||
|
||||
fn main() {
|
||||
let a = if true { true };
|
||||
//~^ ERROR if may be missing an else clause: expected `()`, found `bool` (expected (), found bool)
|
||||
println!("{:?}", a);
|
||||
}
|
||||
|
|
18
src/test/compile-fail/issue-4201.rs
Normal file
18
src/test/compile-fail/issue-4201.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
// Copyright 2014 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.
|
||||
|
||||
fn main() {
|
||||
let a = if true {
|
||||
0
|
||||
} else if false {
|
||||
//~^ ERROR if may be missing an else clause: expected `()`, found `<generic integer #1>`
|
||||
1
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue