Skip to content

Commit 7023bea

Browse files
committed
Print a friendly error for the if-let construct without an else block
Fixes #19991.
1 parent cbe9fb4 commit 7023bea

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

src/librustc_typeck/check/_match.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ pub fn check_match<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
238238
expr: &ast::Expr,
239239
discrim: &ast::Expr,
240240
arms: &[ast::Arm],
241-
expected: Expectation<'tcx>) {
241+
expected: Expectation<'tcx>,
242+
match_src: ast::MatchSource) {
242243
let tcx = fcx.ccx.tcx;
243244

244245
let discrim_ty = fcx.infcx().next_ty_var();
@@ -290,12 +291,26 @@ pub fn check_match<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
290291
if ty::type_is_error(result_ty) || ty::type_is_error(bty) {
291292
ty::mk_err()
292293
} else {
294+
let (origin, expected, found) = match match_src {
295+
/* if-let construct without an else block */
296+
ast::MatchIfLetDesugar(contains_else_arm) if !contains_else_arm => (
297+
infer::IfExpressionWithNoElse(expr.span),
298+
bty,
299+
result_ty,
300+
),
301+
_ => (
302+
infer::MatchExpressionArm(expr.span, arm.body.span),
303+
result_ty,
304+
bty,
305+
),
306+
};
307+
293308
infer::common_supertype(
294309
fcx.infcx(),
295-
infer::MatchExpressionArm(expr.span, arm.body.span),
296-
true, // result_ty is "expected" here
297-
result_ty,
298-
bty
310+
origin,
311+
true,
312+
expected,
313+
found,
299314
)
300315
}
301316
});

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3918,8 +3918,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
39183918
fcx.write_nil(id);
39193919
}
39203920
}
3921-
ast::ExprMatch(ref discrim, ref arms, _) => {
3922-
_match::check_match(fcx, expr, &**discrim, arms.as_slice(), expected);
3921+
ast::ExprMatch(ref discrim, ref arms, match_src) => {
3922+
_match::check_match(fcx, expr, &**discrim, arms.as_slice(), expected, match_src);
39233923
}
39243924
ast::ExprClosure(_, opt_kind, ref decl, ref body) => {
39253925
closure::check_expr_closure(fcx, expr, opt_kind, &**decl, &**body, expected);

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ pub struct QPath {
754754
#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
755755
pub enum MatchSource {
756756
MatchNormal,
757-
MatchIfLetDesugar,
757+
MatchIfLetDesugar(bool /* contains_else_arm */),
758758
MatchWhileLetDesugar,
759759
}
760760

src/libsyntax/ext/expand.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
170170
arms.extend(else_if_arms.into_iter());
171171
arms.push(else_arm);
172172

173-
let match_expr = fld.cx.expr(span, ast::ExprMatch(expr, arms, ast::MatchIfLetDesugar));
173+
let match_expr = fld.cx.expr(span, ast::ExprMatch(expr,
174+
arms,
175+
ast::MatchIfLetDesugar(elseopt.is_some())));
174176
fld.fold_expr(match_expr)
175177
}
176178

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

Lines changed: 18 additions & 0 deletions
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+
// Test if the sugared if-let construct correctly prints "missing an else clause" when an else
12+
// clause does not exist, instead of the unsympathetic "match arms have incompatible types"
13+
14+
fn main() {
15+
if let Some(homura) = Some("madoka") { //~ ERROR missing an else clause: expected `()`
16+
765i32
17+
};
18+
}

0 commit comments

Comments
 (0)