Skip to content

Commit 43e5d10

Browse files
author
Jakub Wieczorek
committed
Improve the error message for missing else clauses in if expressions
1 parent 4a382d7 commit 43e5d10

File tree

5 files changed

+63
-31
lines changed

5 files changed

+63
-31
lines changed

src/librustc/middle/typeck/check/mod.rs

+33-29
Original file line numberDiff line numberDiff line change
@@ -2999,35 +2999,36 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
29992999
expected: Expectation) {
30003000
check_expr_has_type(fcx, cond_expr, ty::mk_bool());
30013001

3002+
// Disregard "castable to" expectations because they
3003+
// can lead us astray. Consider for example `if cond
3004+
// {22} else {c} as u8` -- if we propagate the
3005+
// "castable to u8" constraint to 22, it will pick the
3006+
// type 22u8, which is overly constrained (c might not
3007+
// be a u8). In effect, the problem is that the
3008+
// "castable to" expectation is not the tightest thing
3009+
// we can say, so we want to drop it in this case.
3010+
// The tightest thing we can say is "must unify with
3011+
// else branch". Note that in the case of a "has type"
3012+
// constraint, this limitation does not hold.
3013+
3014+
// If the expected type is just a type variable, then don't use
3015+
// an expected type. Otherwise, we might write parts of the type
3016+
// when checking the 'then' block which are incompatible with the
3017+
// 'else' branch.
3018+
let expected = match expected.only_has_type() {
3019+
ExpectHasType(ety) => {
3020+
match infer::resolve_type(fcx.infcx(), Some(sp), ety, force_tvar) {
3021+
Ok(rty) if !ty::type_is_ty_var(rty) => ExpectHasType(rty),
3022+
_ => NoExpectation
3023+
}
3024+
}
3025+
_ => NoExpectation
3026+
};
3027+
check_block_with_expected(fcx, then_blk, expected);
3028+
let then_ty = fcx.node_ty(then_blk.id);
3029+
30023030
let branches_ty = match opt_else_expr {
30033031
Some(ref else_expr) => {
3004-
// Disregard "castable to" expectations because they
3005-
// can lead us astray. Consider for example `if cond
3006-
// {22} else {c} as u8` -- if we propagate the
3007-
// "castable to u8" constraint to 22, it will pick the
3008-
// type 22u8, which is overly constrained (c might not
3009-
// be a u8). In effect, the problem is that the
3010-
// "castable to" expectation is not the tightest thing
3011-
// we can say, so we want to drop it in this case.
3012-
// The tightest thing we can say is "must unify with
3013-
// else branch". Note that in the case of a "has type"
3014-
// constraint, this limitation does not hold.
3015-
3016-
// If the expected type is just a type variable, then don't use
3017-
// an expected type. Otherwise, we might write parts of the type
3018-
// when checking the 'then' block which are incompatible with the
3019-
// 'else' branch.
3020-
let expected = match expected.only_has_type() {
3021-
ExpectHasType(ety) => {
3022-
match infer::resolve_type(fcx.infcx(), Some(sp), ety, force_tvar) {
3023-
Ok(rty) if !ty::type_is_ty_var(rty) => ExpectHasType(rty),
3024-
_ => NoExpectation
3025-
}
3026-
}
3027-
_ => NoExpectation
3028-
};
3029-
check_block_with_expected(fcx, then_blk, expected);
3030-
let then_ty = fcx.node_ty(then_blk.id);
30313032
check_expr_with_expectation(fcx, &**else_expr, expected);
30323033
let else_ty = fcx.expr_ty(&**else_expr);
30333034
infer::common_supertype(fcx.infcx(),
@@ -3037,8 +3038,11 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
30373038
else_ty)
30383039
}
30393040
None => {
3040-
check_block_no_value(fcx, then_blk);
3041-
ty::mk_nil()
3041+
infer::common_supertype(fcx.infcx(),
3042+
infer::IfExpressionWithNoElse(sp),
3043+
false,
3044+
then_ty,
3045+
ty::mk_nil())
30423046
}
30433047
};
30443048

src/librustc/middle/typeck/infer/error_reporting.rs

+4
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ impl<'a, 'tcx> ErrorReporting for InferCtxt<'a, 'tcx> {
366366
infer::RelateOutputImplTypes(_) => "mismatched types",
367367
infer::MatchExpressionArm(_, _) => "match arms have incompatible types",
368368
infer::IfExpression(_) => "if and else have incompatible types",
369+
infer::IfExpressionWithNoElse(_) => "if may be missing an else clause",
369370
};
370371

371372
self.tcx.sess.span_err(
@@ -1486,6 +1487,9 @@ impl<'a, 'tcx> ErrorReportingHelpers for InferCtxt<'a, 'tcx> {
14861487
infer::IfExpression(_) => {
14871488
format!("if and else have compatible types")
14881489
}
1490+
infer::IfExpressionWithNoElse(_) => {
1491+
format!("if may be missing an else clause")
1492+
}
14891493
};
14901494

14911495
match self.values_str(&trace.values) {

src/librustc/middle/typeck/infer/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ pub enum TypeOrigin {
121121

122122
// Computing common supertype in an if expression
123123
IfExpression(Span),
124+
125+
// Computing common supertype of an if expression with no else counter-part
126+
IfExpressionWithNoElse(Span)
124127
}
125128

126129
/// See `error_reporting.rs` for more details
@@ -1001,6 +1004,7 @@ impl TypeOrigin {
10011004
RelateOutputImplTypes(span) => span,
10021005
MatchExpressionArm(match_span, _) => match_span,
10031006
IfExpression(span) => span,
1007+
IfExpressionWithNoElse(span) => span
10041008
}
10051009
}
10061010
}
@@ -1030,6 +1034,9 @@ impl Repr for TypeOrigin {
10301034
IfExpression(a) => {
10311035
format!("IfExpression({})", a.repr(tcx))
10321036
}
1037+
IfExpressionWithNoElse(a) => {
1038+
format!("IfExpressionWithNoElse({})", a.repr(tcx))
1039+
}
10331040
}
10341041
}
10351042
}

src/test/compile-fail/if-without-else-result.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:mismatched types: expected `()`, found `bool`
12-
1311
extern crate debug;
1412

1513
fn main() {
1614
let a = if true { true };
15+
//~^ ERROR if may be missing an else clause: expected `()`, found `bool` (expected (), found bool)
1716
println!("{:?}", a);
1817
}

src/test/compile-fail/issue-4201.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let a = if true {
13+
0
14+
} else if false {
15+
//~^ ERROR if may be missing an else clause: expected `()`, found `<generic integer #1>`
16+
1
17+
};
18+
}

0 commit comments

Comments
 (0)