Skip to content

Commit eb47e1e

Browse files
committed
rollup merge of #20039: barosl/if-let-friendly-error
Fixes #19991.
2 parents cfc815b + 314ed2d commit eb47e1e

File tree

10 files changed

+64
-24
lines changed

10 files changed

+64
-24
lines changed

src/librustc/lint/builtin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,9 +1157,9 @@ impl LintPass for UnusedParens {
11571157
ast::ExprIf(ref cond, _, _) => (cond, "`if` condition", true),
11581158
ast::ExprWhile(ref cond, _, _) => (cond, "`while` condition", true),
11591159
ast::ExprMatch(ref head, _, source) => match source {
1160-
ast::MatchNormal => (head, "`match` head expression", true),
1161-
ast::MatchIfLetDesugar => (head, "`if let` head expression", true),
1162-
ast::MatchWhileLetDesugar => (head, "`while let` head expression", true),
1160+
ast::MatchSource::Normal => (head, "`match` head expression", true),
1161+
ast::MatchSource::IfLetDesugar { .. } => (head, "`if let` head expression", true),
1162+
ast::MatchSource::WhileLetDesugar => (head, "`while let` head expression", true),
11631163
},
11641164
ast::ExprRet(Some(ref value)) => (value, "`return` value", false),
11651165
ast::ExprAssign(_, ref value) => (value, "assigned value", false),

src/librustc/middle/check_match.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fn check_arms(cx: &MatchCheckCtxt,
307307
match is_useful(cx, &seen, v.as_slice(), LeaveOutWitness) {
308308
NotUseful => {
309309
match source {
310-
ast::MatchIfLetDesugar => {
310+
ast::MatchSource::IfLetDesugar { .. } => {
311311
if printed_if_let_err {
312312
// we already printed an irrefutable if-let pattern error.
313313
// We don't want two, that's just confusing.
@@ -321,15 +321,15 @@ fn check_arms(cx: &MatchCheckCtxt,
321321
}
322322
},
323323

324-
ast::MatchWhileLetDesugar => {
324+
ast::MatchSource::WhileLetDesugar => {
325325
// find the first arm pattern so we can use its span
326326
let &(ref first_arm_pats, _) = &arms[0];
327327
let first_pat = &first_arm_pats[0];
328328
let span = first_pat.span;
329329
span_err!(cx.tcx.sess, span, E0165, "irrefutable while-let pattern");
330330
},
331331

332-
ast::MatchNormal => {
332+
ast::MatchSource::Normal => {
333333
span_err!(cx.tcx.sess, pat.span, E0001, "unreachable pattern")
334334
},
335335
}

src/librustc/util/ppaux.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ pub fn explain_region_and_span(cx: &ctxt, region: ty::Region)
9393
ast::ExprMethodCall(..) => {
9494
explain_span(cx, "method call", expr.span)
9595
},
96-
ast::ExprMatch(_, _, ast::MatchIfLetDesugar) => explain_span(cx, "if let", expr.span),
97-
ast::ExprMatch(_, _, ast::MatchWhileLetDesugar) => {
96+
ast::ExprMatch(_, _, ast::MatchSource::IfLetDesugar { .. }) =>
97+
explain_span(cx, "if let", expr.span),
98+
ast::ExprMatch(_, _, ast::MatchSource::WhileLetDesugar) => {
9899
explain_span(cx, "while let", expr.span)
99100
},
100101
ast::ExprMatch(..) => explain_span(cx, "match", expr.span),

src/librustc_typeck/check/_match.rs

Lines changed: 21 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,27 @@ 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::MatchSource::IfLetDesugar { contains_else_clause }
297+
if !contains_else_clause => (
298+
infer::IfExpressionWithNoElse(expr.span),
299+
bty,
300+
result_ty,
301+
),
302+
_ => (
303+
infer::MatchExpressionArm(expr.span, arm.body.span),
304+
result_ty,
305+
bty,
306+
),
307+
};
308+
293309
infer::common_supertype(
294310
fcx.infcx(),
295-
infer::MatchExpressionArm(expr.span, arm.body.span),
296-
true, // result_ty is "expected" here
297-
result_ty,
298-
bty
311+
origin,
312+
true,
313+
expected,
314+
found,
299315
)
300316
}
301317
});

src/librustc_typeck/check/mod.rs

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

src/libsyntax/ast.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub use self::LitIntType::*;
3232
pub use self::LocalSource::*;
3333
pub use self::Mac_::*;
3434
pub use self::MacStmtStyle::*;
35-
pub use self::MatchSource::*;
3635
pub use self::MetaItem_::*;
3736
pub use self::Method_::*;
3837
pub use self::Mutability::*;
@@ -760,9 +759,9 @@ pub struct QPath {
760759

761760
#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
762761
pub enum MatchSource {
763-
MatchNormal,
764-
MatchIfLetDesugar,
765-
MatchWhileLetDesugar,
762+
Normal,
763+
IfLetDesugar { contains_else_clause: bool },
764+
WhileLetDesugar,
766765
}
767766

768767
#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)]

src/libsyntax/ext/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
868868
}
869869

870870
fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
871-
self.expr(span, ast::ExprMatch(arg, arms, ast::MatchNormal))
871+
self.expr(span, ast::ExprMatch(arg, arms, ast::MatchSource::Normal))
872872
}
873873

874874
fn expr_if(&self, span: Span, cond: P<ast::Expr>,

src/libsyntax/ext/expand.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
9797
// `match <expr> { ... }`
9898
let arms = vec![pat_arm, break_arm];
9999
let match_expr = fld.cx.expr(span,
100-
ast::ExprMatch(expr, arms, ast::MatchWhileLetDesugar));
100+
ast::ExprMatch(expr, arms, ast::MatchSource::WhileLetDesugar));
101101

102102
// `[opt_ident]: loop { ... }`
103103
let loop_block = fld.cx.block_expr(match_expr);
@@ -158,6 +158,8 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
158158
arms
159159
};
160160

161+
let contains_else_clause = elseopt.is_some();
162+
161163
// `_ => [<elseopt> | ()]`
162164
let else_arm = {
163165
let pat_under = fld.cx.pat_wild(span);
@@ -170,7 +172,11 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
170172
arms.extend(else_if_arms.into_iter());
171173
arms.push(else_arm);
172174

173-
let match_expr = fld.cx.expr(span, ast::ExprMatch(expr, arms, ast::MatchIfLetDesugar));
175+
let match_expr = fld.cx.expr(span,
176+
ast::ExprMatch(expr, arms,
177+
ast::MatchSource::IfLetDesugar {
178+
contains_else_clause: contains_else_clause,
179+
}));
174180
fld.fold_expr(match_expr)
175181
}
176182

src/libsyntax/parse/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use ast::{LifetimeDef, Lit, Lit_};
4141
use ast::{LitBool, LitChar, LitByte, LitBinary};
4242
use ast::{LitStr, LitInt, Local, LocalLet};
4343
use ast::{MacStmtWithBraces, MacStmtWithSemicolon, MacStmtWithoutBraces};
44-
use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, MatchNormal};
44+
use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, MatchSource};
4545
use ast::{Method, MutTy, BiMul, Mutability};
4646
use ast::{MethodImplItem, NamedField, UnNeg, NoReturn, NodeId, UnNot};
4747
use ast::{Pat, PatEnum, PatIdent, PatLit, PatRange, PatRegion, PatStruct};
@@ -3111,7 +3111,7 @@ impl<'a> Parser<'a> {
31113111
}
31123112
let hi = self.span.hi;
31133113
self.bump();
3114-
return self.mk_expr(lo, hi, ExprMatch(discriminant, arms, MatchNormal));
3114+
return self.mk_expr(lo, hi, ExprMatch(discriminant, arms, MatchSource::Normal));
31153115
}
31163116

31173117
pub fn parse_arm(&mut self) -> Arm {

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)