From 2c76710ecedb459d36ae110882931143a6fd5dd1 Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Fri, 2 Jan 2015 03:38:02 +0200 Subject: [PATCH] rustc_typeck: make Expectation more composable by replacing map and map_to_option with to_option. --- src/librustc_typeck/check/closure.rs | 2 +- src/librustc_typeck/check/mod.rs | 45 ++++++++++------------------ 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 00791157569..bfe43086aab 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -33,7 +33,7 @@ pub fn check_expr_closure<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>, expr.repr(fcx.tcx()), expected.repr(fcx.tcx())); - let expected_sig_and_kind = expected.map_to_option(fcx, |ty| { + let expected_sig_and_kind = expected.to_option(fcx).and_then(|ty| { deduce_unboxed_closure_expectations_from_expected_type(fcx, ty) }); diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 1d184131ded..5c25b55287a 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2844,7 +2844,7 @@ fn check_lit<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, ast::LitInt(_, ast::SignedIntLit(t, _)) => ty::mk_mach_int(tcx, t), ast::LitInt(_, ast::UnsignedIntLit(t)) => ty::mk_mach_uint(tcx, t), ast::LitInt(_, ast::UnsuffixedIntLit(_)) => { - let opt_ty = expected.map_to_option(fcx, |ty| { + let opt_ty = expected.to_option(fcx).and_then(|ty| { match ty.sty { ty::ty_int(_) | ty::ty_uint(_) => Some(ty), ty::ty_char => Some(tcx.types.u8), @@ -2858,7 +2858,7 @@ fn check_lit<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, } ast::LitFloat(_, t) => ty::mk_mach_float(tcx, t), ast::LitFloatUnsuffixed(_) => { - let opt_ty = expected.map_to_option(fcx, |ty| { + let opt_ty = expected.to_option(fcx).and_then(|ty| { match ty.sty { ty::ty_float(_) => Some(ty), _ => None @@ -3761,7 +3761,7 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, } } ast::ExprUnary(unop, ref oprnd) => { - let expected_inner = expected.map(fcx, |ty| { + let expected_inner = expected.to_option(fcx).map_or(NoExpectation, |ty| { match unop { ast::UnUniq => match ty.sty { ty::ty_uniq(ty) => { @@ -3851,8 +3851,7 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, fcx.write_ty(id, oprnd_t); } ast::ExprAddrOf(mutbl, ref oprnd) => { - let expected = expected.only_has_type(); - let hint = expected.map(fcx, |ty| { + let hint = expected.only_has_type(fcx).map_or(NoExpectation, |ty| { match ty.sty { ty::ty_rptr(_, ref mt) | ty::ty_ptr(ref mt) => { if ty::expr_is_lval(fcx.tcx(), &**oprnd) { @@ -4065,7 +4064,7 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, check_cast(fcx, expr, &**e, &**t); } ast::ExprVec(ref args) => { - let uty = expected.map_to_option(fcx, |uty| { + let uty = expected.to_option(fcx).and_then(|uty| { match uty.sty { ty::ty_vec(ty, _) => Some(ty), _ => None @@ -4134,8 +4133,7 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, } } ast::ExprTup(ref elts) => { - let expected = expected.only_has_type(); - let flds = expected.map_to_option(fcx, |ty| { + let flds = expected.only_has_type(fcx).and_then(|ty| { match ty.sty { ty::ty_tup(ref flds) => Some(&flds[]), _ => None @@ -4428,13 +4426,6 @@ fn rvalue_hint(ty: Ty<'tcx>) -> Expectation<'tcx> { } } - fn only_has_type(self) -> Expectation<'tcx> { - match self { - ExpectHasType(t) => ExpectHasType(t), - _ => NoExpectation - } - } - // Resolves `expected` by a single level if it is a variable. If // there is no expected type or resolution is not possible (e.g., // no constraints yet present), just returns `None`. @@ -4458,25 +4449,19 @@ fn resolve<'a>(self, fcx: &FnCtxt<'a, 'tcx>) -> Expectation<'tcx> { } } - fn map<'a, F>(self, fcx: &FnCtxt<'a, 'tcx>, unpack: F) -> Expectation<'tcx> where - F: FnOnce(Ty<'tcx>) -> Expectation<'tcx> - { - match self.resolve(fcx) { - NoExpectation => NoExpectation, - ExpectCastableToType(ty) | - ExpectHasType(ty) | - ExpectRvalueLikeUnsized(ty) => unpack(ty), - } - } - - fn map_to_option<'a, O, F>(self, fcx: &FnCtxt<'a, 'tcx>, unpack: F) -> Option where - F: FnOnce(Ty<'tcx>) -> Option, - { + fn to_option<'a>(self, fcx: &FnCtxt<'a, 'tcx>) -> Option> { match self.resolve(fcx) { NoExpectation => None, ExpectCastableToType(ty) | ExpectHasType(ty) | - ExpectRvalueLikeUnsized(ty) => unpack(ty), + ExpectRvalueLikeUnsized(ty) => Some(ty), + } + } + + fn only_has_type<'a>(self, fcx: &FnCtxt<'a, 'tcx>) -> Option> { + match self.resolve(fcx) { + ExpectHasType(ty) => Some(ty), + _ => None } } }