]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/demand.rs
rollup merge of #20518: nagisa/weighted-bool
[rust.git] / src / librustc_typeck / check / demand.rs
1 // Copyright 2012 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
12 use check::FnCtxt;
13 use middle::ty::{self, Ty};
14 use middle::infer;
15
16 use std::result::Result::{Err, Ok};
17 use syntax::ast;
18 use syntax::codemap::Span;
19 use util::ppaux::Repr;
20
21 // Requires that the two types unify, and prints an error message if
22 // they don't.
23 pub fn suptype<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span,
24                          ty_expected: Ty<'tcx>, ty_actual: Ty<'tcx>) {
25     suptype_with_fn(fcx, sp, false, ty_expected, ty_actual,
26         |sp, e, a, s| { fcx.report_mismatched_types(sp, e, a, s) })
27 }
28
29 /// As `suptype`, but call `handle_err` if unification for subtyping fails.
30 pub fn suptype_with_fn<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
31                                     sp: Span,
32                                     b_is_expected: bool,
33                                     ty_a: Ty<'tcx>,
34                                     ty_b: Ty<'tcx>,
35                                     handle_err: F) where
36     F: FnOnce(Span, Ty<'tcx>, Ty<'tcx>, &ty::type_err<'tcx>),
37 {
38     // n.b.: order of actual, expected is reversed
39     match infer::mk_subty(fcx.infcx(), b_is_expected, infer::Misc(sp),
40                           ty_b, ty_a) {
41       Ok(()) => { /* ok */ }
42       Err(ref err) => {
43           handle_err(sp, ty_a, ty_b, err);
44       }
45     }
46 }
47
48 pub fn eqtype<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span,
49                         expected: Ty<'tcx>, actual: Ty<'tcx>) {
50     match infer::mk_eqty(fcx.infcx(), false, infer::Misc(sp), actual, expected) {
51         Ok(()) => { /* ok */ }
52         Err(ref err) => { fcx.report_mismatched_types(sp, expected, actual, err); }
53     }
54 }
55
56 // Checks that the type `actual` can be coerced to `expected`.
57 pub fn coerce<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span,
58                         expected: Ty<'tcx>, expr: &ast::Expr) {
59     let expr_ty = fcx.expr_ty(expr);
60     debug!("demand::coerce(expected = {}, expr_ty = {})",
61            expected.repr(fcx.ccx.tcx),
62            expr_ty.repr(fcx.ccx.tcx));
63     let expected = fcx.infcx().resolve_type_vars_if_possible(&expected);
64     match fcx.mk_assignty(expr, expr_ty, expected) {
65       Ok(()) => { /* ok */ }
66       Err(ref err) => {
67         fcx.report_mismatched_types(sp, expected, expr_ty, err);
68       }
69     }
70 }