Skip to content

Commit 2c76710

Browse files
committed
rustc_typeck: make Expectation more composable by replacing map and map_to_option with to_option.
1 parent 2e4cef4 commit 2c76710

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

src/librustc_typeck/check/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn check_expr_closure<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
3333
expr.repr(fcx.tcx()),
3434
expected.repr(fcx.tcx()));
3535

36-
let expected_sig_and_kind = expected.map_to_option(fcx, |ty| {
36+
let expected_sig_and_kind = expected.to_option(fcx).and_then(|ty| {
3737
deduce_unboxed_closure_expectations_from_expected_type(fcx, ty)
3838
});
3939

src/librustc_typeck/check/mod.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,7 +2844,7 @@ fn check_lit<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
28442844
ast::LitInt(_, ast::SignedIntLit(t, _)) => ty::mk_mach_int(tcx, t),
28452845
ast::LitInt(_, ast::UnsignedIntLit(t)) => ty::mk_mach_uint(tcx, t),
28462846
ast::LitInt(_, ast::UnsuffixedIntLit(_)) => {
2847-
let opt_ty = expected.map_to_option(fcx, |ty| {
2847+
let opt_ty = expected.to_option(fcx).and_then(|ty| {
28482848
match ty.sty {
28492849
ty::ty_int(_) | ty::ty_uint(_) => Some(ty),
28502850
ty::ty_char => Some(tcx.types.u8),
@@ -2858,7 +2858,7 @@ fn check_lit<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
28582858
}
28592859
ast::LitFloat(_, t) => ty::mk_mach_float(tcx, t),
28602860
ast::LitFloatUnsuffixed(_) => {
2861-
let opt_ty = expected.map_to_option(fcx, |ty| {
2861+
let opt_ty = expected.to_option(fcx).and_then(|ty| {
28622862
match ty.sty {
28632863
ty::ty_float(_) => Some(ty),
28642864
_ => None
@@ -3761,7 +3761,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
37613761
}
37623762
}
37633763
ast::ExprUnary(unop, ref oprnd) => {
3764-
let expected_inner = expected.map(fcx, |ty| {
3764+
let expected_inner = expected.to_option(fcx).map_or(NoExpectation, |ty| {
37653765
match unop {
37663766
ast::UnUniq => match ty.sty {
37673767
ty::ty_uniq(ty) => {
@@ -3851,8 +3851,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
38513851
fcx.write_ty(id, oprnd_t);
38523852
}
38533853
ast::ExprAddrOf(mutbl, ref oprnd) => {
3854-
let expected = expected.only_has_type();
3855-
let hint = expected.map(fcx, |ty| {
3854+
let hint = expected.only_has_type(fcx).map_or(NoExpectation, |ty| {
38563855
match ty.sty {
38573856
ty::ty_rptr(_, ref mt) | ty::ty_ptr(ref mt) => {
38583857
if ty::expr_is_lval(fcx.tcx(), &**oprnd) {
@@ -4065,7 +4064,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
40654064
check_cast(fcx, expr, &**e, &**t);
40664065
}
40674066
ast::ExprVec(ref args) => {
4068-
let uty = expected.map_to_option(fcx, |uty| {
4067+
let uty = expected.to_option(fcx).and_then(|uty| {
40694068
match uty.sty {
40704069
ty::ty_vec(ty, _) => Some(ty),
40714070
_ => None
@@ -4134,8 +4133,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
41344133
}
41354134
}
41364135
ast::ExprTup(ref elts) => {
4137-
let expected = expected.only_has_type();
4138-
let flds = expected.map_to_option(fcx, |ty| {
4136+
let flds = expected.only_has_type(fcx).and_then(|ty| {
41394137
match ty.sty {
41404138
ty::ty_tup(ref flds) => Some(&flds[]),
41414139
_ => None
@@ -4428,13 +4426,6 @@ impl<'tcx> Expectation<'tcx> {
44284426
}
44294427
}
44304428

4431-
fn only_has_type(self) -> Expectation<'tcx> {
4432-
match self {
4433-
ExpectHasType(t) => ExpectHasType(t),
4434-
_ => NoExpectation
4435-
}
4436-
}
4437-
44384429
// Resolves `expected` by a single level if it is a variable. If
44394430
// there is no expected type or resolution is not possible (e.g.,
44404431
// no constraints yet present), just returns `None`.
@@ -4458,25 +4449,19 @@ impl<'tcx> Expectation<'tcx> {
44584449
}
44594450
}
44604451

4461-
fn map<'a, F>(self, fcx: &FnCtxt<'a, 'tcx>, unpack: F) -> Expectation<'tcx> where
4462-
F: FnOnce(Ty<'tcx>) -> Expectation<'tcx>
4463-
{
4452+
fn to_option<'a>(self, fcx: &FnCtxt<'a, 'tcx>) -> Option<Ty<'tcx>> {
44644453
match self.resolve(fcx) {
4465-
NoExpectation => NoExpectation,
4454+
NoExpectation => None,
44664455
ExpectCastableToType(ty) |
44674456
ExpectHasType(ty) |
4468-
ExpectRvalueLikeUnsized(ty) => unpack(ty),
4457+
ExpectRvalueLikeUnsized(ty) => Some(ty),
44694458
}
44704459
}
44714460

4472-
fn map_to_option<'a, O, F>(self, fcx: &FnCtxt<'a, 'tcx>, unpack: F) -> Option<O> where
4473-
F: FnOnce(Ty<'tcx>) -> Option<O>,
4474-
{
4461+
fn only_has_type<'a>(self, fcx: &FnCtxt<'a, 'tcx>) -> Option<Ty<'tcx>> {
44754462
match self.resolve(fcx) {
4476-
NoExpectation => None,
4477-
ExpectCastableToType(ty) |
4478-
ExpectHasType(ty) |
4479-
ExpectRvalueLikeUnsized(ty) => unpack(ty),
4463+
ExpectHasType(ty) => Some(ty),
4464+
_ => None
44804465
}
44814466
}
44824467
}

0 commit comments

Comments
 (0)