commit
eb47e1e446
10 changed files with 64 additions and 24 deletions
|
@ -1157,9 +1157,9 @@ impl LintPass for UnusedParens {
|
||||||
ast::ExprIf(ref cond, _, _) => (cond, "`if` condition", true),
|
ast::ExprIf(ref cond, _, _) => (cond, "`if` condition", true),
|
||||||
ast::ExprWhile(ref cond, _, _) => (cond, "`while` condition", true),
|
ast::ExprWhile(ref cond, _, _) => (cond, "`while` condition", true),
|
||||||
ast::ExprMatch(ref head, _, source) => match source {
|
ast::ExprMatch(ref head, _, source) => match source {
|
||||||
ast::MatchNormal => (head, "`match` head expression", true),
|
ast::MatchSource::Normal => (head, "`match` head expression", true),
|
||||||
ast::MatchIfLetDesugar => (head, "`if let` head expression", true),
|
ast::MatchSource::IfLetDesugar { .. } => (head, "`if let` head expression", true),
|
||||||
ast::MatchWhileLetDesugar => (head, "`while let` head expression", true),
|
ast::MatchSource::WhileLetDesugar => (head, "`while let` head expression", true),
|
||||||
},
|
},
|
||||||
ast::ExprRet(Some(ref value)) => (value, "`return` value", false),
|
ast::ExprRet(Some(ref value)) => (value, "`return` value", false),
|
||||||
ast::ExprAssign(_, ref value) => (value, "assigned value", false),
|
ast::ExprAssign(_, ref value) => (value, "assigned value", false),
|
||||||
|
|
|
@ -307,7 +307,7 @@ fn check_arms(cx: &MatchCheckCtxt,
|
||||||
match is_useful(cx, &seen, v.as_slice(), LeaveOutWitness) {
|
match is_useful(cx, &seen, v.as_slice(), LeaveOutWitness) {
|
||||||
NotUseful => {
|
NotUseful => {
|
||||||
match source {
|
match source {
|
||||||
ast::MatchIfLetDesugar => {
|
ast::MatchSource::IfLetDesugar { .. } => {
|
||||||
if printed_if_let_err {
|
if printed_if_let_err {
|
||||||
// we already printed an irrefutable if-let pattern error.
|
// we already printed an irrefutable if-let pattern error.
|
||||||
// We don't want two, that's just confusing.
|
// We don't want two, that's just confusing.
|
||||||
|
@ -321,7 +321,7 @@ fn check_arms(cx: &MatchCheckCtxt,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
ast::MatchWhileLetDesugar => {
|
ast::MatchSource::WhileLetDesugar => {
|
||||||
// find the first arm pattern so we can use its span
|
// find the first arm pattern so we can use its span
|
||||||
let &(ref first_arm_pats, _) = &arms[0];
|
let &(ref first_arm_pats, _) = &arms[0];
|
||||||
let first_pat = &first_arm_pats[0];
|
let first_pat = &first_arm_pats[0];
|
||||||
|
@ -329,7 +329,7 @@ fn check_arms(cx: &MatchCheckCtxt,
|
||||||
span_err!(cx.tcx.sess, span, E0165, "irrefutable while-let pattern");
|
span_err!(cx.tcx.sess, span, E0165, "irrefutable while-let pattern");
|
||||||
},
|
},
|
||||||
|
|
||||||
ast::MatchNormal => {
|
ast::MatchSource::Normal => {
|
||||||
span_err!(cx.tcx.sess, pat.span, E0001, "unreachable pattern")
|
span_err!(cx.tcx.sess, pat.span, E0001, "unreachable pattern")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,8 +93,9 @@ pub fn explain_region_and_span(cx: &ctxt, region: ty::Region)
|
||||||
ast::ExprMethodCall(..) => {
|
ast::ExprMethodCall(..) => {
|
||||||
explain_span(cx, "method call", expr.span)
|
explain_span(cx, "method call", expr.span)
|
||||||
},
|
},
|
||||||
ast::ExprMatch(_, _, ast::MatchIfLetDesugar) => explain_span(cx, "if let", expr.span),
|
ast::ExprMatch(_, _, ast::MatchSource::IfLetDesugar { .. }) =>
|
||||||
ast::ExprMatch(_, _, ast::MatchWhileLetDesugar) => {
|
explain_span(cx, "if let", expr.span),
|
||||||
|
ast::ExprMatch(_, _, ast::MatchSource::WhileLetDesugar) => {
|
||||||
explain_span(cx, "while let", expr.span)
|
explain_span(cx, "while let", expr.span)
|
||||||
},
|
},
|
||||||
ast::ExprMatch(..) => explain_span(cx, "match", expr.span),
|
ast::ExprMatch(..) => explain_span(cx, "match", expr.span),
|
||||||
|
|
|
@ -238,7 +238,8 @@ pub fn check_match<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
|
||||||
expr: &ast::Expr,
|
expr: &ast::Expr,
|
||||||
discrim: &ast::Expr,
|
discrim: &ast::Expr,
|
||||||
arms: &[ast::Arm],
|
arms: &[ast::Arm],
|
||||||
expected: Expectation<'tcx>) {
|
expected: Expectation<'tcx>,
|
||||||
|
match_src: ast::MatchSource) {
|
||||||
let tcx = fcx.ccx.tcx;
|
let tcx = fcx.ccx.tcx;
|
||||||
|
|
||||||
let discrim_ty = fcx.infcx().next_ty_var();
|
let discrim_ty = fcx.infcx().next_ty_var();
|
||||||
|
@ -290,12 +291,27 @@ pub fn check_match<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
|
||||||
if ty::type_is_error(result_ty) || ty::type_is_error(bty) {
|
if ty::type_is_error(result_ty) || ty::type_is_error(bty) {
|
||||||
ty::mk_err()
|
ty::mk_err()
|
||||||
} else {
|
} else {
|
||||||
|
let (origin, expected, found) = match match_src {
|
||||||
|
/* if-let construct without an else block */
|
||||||
|
ast::MatchSource::IfLetDesugar { contains_else_clause }
|
||||||
|
if !contains_else_clause => (
|
||||||
|
infer::IfExpressionWithNoElse(expr.span),
|
||||||
|
bty,
|
||||||
|
result_ty,
|
||||||
|
),
|
||||||
|
_ => (
|
||||||
|
infer::MatchExpressionArm(expr.span, arm.body.span),
|
||||||
|
result_ty,
|
||||||
|
bty,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
infer::common_supertype(
|
infer::common_supertype(
|
||||||
fcx.infcx(),
|
fcx.infcx(),
|
||||||
infer::MatchExpressionArm(expr.span, arm.body.span),
|
origin,
|
||||||
true, // result_ty is "expected" here
|
true,
|
||||||
result_ty,
|
expected,
|
||||||
bty
|
found,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -3919,8 +3919,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
|
||||||
fcx.write_nil(id);
|
fcx.write_nil(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::ExprMatch(ref discrim, ref arms, _) => {
|
ast::ExprMatch(ref discrim, ref arms, match_src) => {
|
||||||
_match::check_match(fcx, expr, &**discrim, arms.as_slice(), expected);
|
_match::check_match(fcx, expr, &**discrim, arms.as_slice(), expected, match_src);
|
||||||
}
|
}
|
||||||
ast::ExprClosure(_, opt_kind, ref decl, ref body) => {
|
ast::ExprClosure(_, opt_kind, ref decl, ref body) => {
|
||||||
closure::check_expr_closure(fcx, expr, opt_kind, &**decl, &**body, expected);
|
closure::check_expr_closure(fcx, expr, opt_kind, &**decl, &**body, expected);
|
||||||
|
|
|
@ -32,7 +32,6 @@ pub use self::LitIntType::*;
|
||||||
pub use self::LocalSource::*;
|
pub use self::LocalSource::*;
|
||||||
pub use self::Mac_::*;
|
pub use self::Mac_::*;
|
||||||
pub use self::MacStmtStyle::*;
|
pub use self::MacStmtStyle::*;
|
||||||
pub use self::MatchSource::*;
|
|
||||||
pub use self::MetaItem_::*;
|
pub use self::MetaItem_::*;
|
||||||
pub use self::Method_::*;
|
pub use self::Method_::*;
|
||||||
pub use self::Mutability::*;
|
pub use self::Mutability::*;
|
||||||
|
@ -760,9 +759,9 @@ pub struct QPath {
|
||||||
|
|
||||||
#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
|
#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
|
||||||
pub enum MatchSource {
|
pub enum MatchSource {
|
||||||
MatchNormal,
|
Normal,
|
||||||
MatchIfLetDesugar,
|
IfLetDesugar { contains_else_clause: bool },
|
||||||
MatchWhileLetDesugar,
|
WhileLetDesugar,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
|
#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
|
||||||
|
|
|
@ -868,7 +868,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
|
fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
|
||||||
self.expr(span, ast::ExprMatch(arg, arms, ast::MatchNormal))
|
self.expr(span, ast::ExprMatch(arg, arms, ast::MatchSource::Normal))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expr_if(&self, span: Span, cond: P<ast::Expr>,
|
fn expr_if(&self, span: Span, cond: P<ast::Expr>,
|
||||||
|
|
|
@ -97,7 +97,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
|
||||||
// `match <expr> { ... }`
|
// `match <expr> { ... }`
|
||||||
let arms = vec![pat_arm, break_arm];
|
let arms = vec![pat_arm, break_arm];
|
||||||
let match_expr = fld.cx.expr(span,
|
let match_expr = fld.cx.expr(span,
|
||||||
ast::ExprMatch(expr, arms, ast::MatchWhileLetDesugar));
|
ast::ExprMatch(expr, arms, ast::MatchSource::WhileLetDesugar));
|
||||||
|
|
||||||
// `[opt_ident]: loop { ... }`
|
// `[opt_ident]: loop { ... }`
|
||||||
let loop_block = fld.cx.block_expr(match_expr);
|
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> {
|
||||||
arms
|
arms
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let contains_else_clause = elseopt.is_some();
|
||||||
|
|
||||||
// `_ => [<elseopt> | ()]`
|
// `_ => [<elseopt> | ()]`
|
||||||
let else_arm = {
|
let else_arm = {
|
||||||
let pat_under = fld.cx.pat_wild(span);
|
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> {
|
||||||
arms.extend(else_if_arms.into_iter());
|
arms.extend(else_if_arms.into_iter());
|
||||||
arms.push(else_arm);
|
arms.push(else_arm);
|
||||||
|
|
||||||
let match_expr = fld.cx.expr(span, ast::ExprMatch(expr, arms, ast::MatchIfLetDesugar));
|
let match_expr = fld.cx.expr(span,
|
||||||
|
ast::ExprMatch(expr, arms,
|
||||||
|
ast::MatchSource::IfLetDesugar {
|
||||||
|
contains_else_clause: contains_else_clause,
|
||||||
|
}));
|
||||||
fld.fold_expr(match_expr)
|
fld.fold_expr(match_expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ use ast::{LifetimeDef, Lit, Lit_};
|
||||||
use ast::{LitBool, LitChar, LitByte, LitBinary};
|
use ast::{LitBool, LitChar, LitByte, LitBinary};
|
||||||
use ast::{LitStr, LitInt, Local, LocalLet};
|
use ast::{LitStr, LitInt, Local, LocalLet};
|
||||||
use ast::{MacStmtWithBraces, MacStmtWithSemicolon, MacStmtWithoutBraces};
|
use ast::{MacStmtWithBraces, MacStmtWithSemicolon, MacStmtWithoutBraces};
|
||||||
use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, MatchNormal};
|
use ast::{MutImmutable, MutMutable, Mac_, MacInvocTT, MatchSource};
|
||||||
use ast::{Method, MutTy, BiMul, Mutability};
|
use ast::{Method, MutTy, BiMul, Mutability};
|
||||||
use ast::{MethodImplItem, NamedField, UnNeg, NoReturn, NodeId, UnNot};
|
use ast::{MethodImplItem, NamedField, UnNeg, NoReturn, NodeId, UnNot};
|
||||||
use ast::{Pat, PatEnum, PatIdent, PatLit, PatRange, PatRegion, PatStruct};
|
use ast::{Pat, PatEnum, PatIdent, PatLit, PatRange, PatRegion, PatStruct};
|
||||||
|
@ -3111,7 +3111,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
let hi = self.span.hi;
|
let hi = self.span.hi;
|
||||||
self.bump();
|
self.bump();
|
||||||
return self.mk_expr(lo, hi, ExprMatch(discriminant, arms, MatchNormal));
|
return self.mk_expr(lo, hi, ExprMatch(discriminant, arms, MatchSource::Normal));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_arm(&mut self) -> Arm {
|
pub fn parse_arm(&mut self) -> Arm {
|
||||||
|
|
18
src/test/compile-fail/issue-19991.rs
Normal file
18
src/test/compile-fail/issue-19991.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.
|
||||||
|
|
||||||
|
// Test if the sugared if-let construct correctly prints "missing an else clause" when an else
|
||||||
|
// clause does not exist, instead of the unsympathetic "match arms have incompatible types"
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
if let Some(homura) = Some("madoka") { //~ ERROR missing an else clause: expected `()`
|
||||||
|
765i32
|
||||||
|
};
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue