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 middle::ty;
13 use middle::typeck::check::FnCtxt;
14 use middle::typeck::infer;
15
16 use std::result::{Err, Ok};
17 use std::result;
18 use syntax::ast;
19 use syntax::codemap::Span;
20
21 // Requires that the two types unify, and prints an error message if they
22 // don't.
23 pub fn suptype(fcx: &FnCtxt, sp: Span, expected: ty::t, actual: ty::t) {
24 suptype_with_fn(fcx, sp, false, expected, actual,
25 |sp, e, a, s| { fcx.report_mismatched_types(sp, e, a, s) })
26 }
27
28 pub fn subtype(fcx: &FnCtxt, sp: Span, expected: ty::t, actual: ty::t) {
29 suptype_with_fn(fcx, sp, true, actual, expected,
30 |sp, a, e, s| { fcx.report_mismatched_types(sp, e, a, s) })
31 }
32
33 pub fn suptype_with_fn(fcx: &FnCtxt,
34 sp: Span,
35 b_is_expected: bool,
36 ty_a: ty::t,
37 ty_b: ty::t,
38 handle_err: |Span, ty::t, ty::t, &ty::type_err|) {
39 // n.b.: order of actual, expected is reversed
40 match infer::mk_subty(fcx.infcx(), b_is_expected, infer::Misc(sp),
41 ty_b, ty_a) {
42 result::Ok(()) => { /* ok */ }
43 result::Err(ref err) => {
44 handle_err(sp, ty_a, ty_b, err);
45 }
46 }
47 }
48
49 pub fn eqtype(fcx: &FnCtxt, sp: Span, expected: ty::t, actual: ty::t) {
50 match infer::mk_eqty(fcx.infcx(), false, infer::Misc(sp), actual, expected) {
51 Ok(()) => { /* ok */ }
52 Err(ref err) => {
53 fcx.report_mismatched_types(sp, expected, actual, err);
54 }
55 }
56 }
57
58 // Checks that the type `actual` can be coerced to `expected`.
59 pub fn coerce(fcx: &FnCtxt, sp: Span, expected: ty::t, expr: &ast::Expr) {
60 let expr_ty = fcx.expr_ty(expr);
61 match fcx.mk_assignty(expr, expr_ty, expected) {
62 result::Ok(()) => { /* ok */ }
63 result::Err(ref err) => {
64 fcx.report_mismatched_types(sp, expected, expr_ty, err);
65 }
66 }
67 }
librustc/middle/typeck/check/demand.rs:32:1-32:1 -fn- definition:
pub fn suptype_with_fn(fcx: &FnCtxt,
sp: Span,
b_is_expected: bool,
references:- 328: pub fn subtype(fcx: &FnCtxt, sp: Span, expected: ty::t, actual: ty::t) {
29: suptype_with_fn(fcx, sp, true, actual, expected,
30: |sp, a, e, s| { fcx.report_mismatched_types(sp, e, a, s) })
librustc/middle/typeck/check/mod.rs:
517: // to be do-block/for-loop confusion
518: demand::suptype_with_fn(&fcx, tail_expr.span, false,
519: fcx.ret_ty, fcx.expr_ty(tail_expr),
librustc/middle/typeck/check/demand.rs:22:10-22:10 -fn- definition:
// don't.
pub fn suptype(fcx: &FnCtxt, sp: Span, expected: ty::t, actual: ty::t) {
suptype_with_fn(fcx, sp, false, expected, actual,
references:- 7librustc/middle/typeck/check/mod.rs:
3983: }, tp, None);
3984: demand::suptype(fcx, sp, ty::mk_err(), tp);
3985: tp
librustc/middle/typeck/check/_match.rs:
473: let const_tpt = ty::lookup_item_type(tcx, const_did);
474: demand::suptype(fcx, pat.span, expected, const_tpt.ty);
475: fcx.write_ty(pat.id, const_tpt.ty);
librustc/middle/typeck/check/mod.rs:
2115: let tvar = fcx.infcx().next_ty_var();
2116: demand::suptype(fcx, expr.span, tvar, lhs_t);
2117: check_expr_has_type(fcx, rhs, tvar);
librustc/middle/typeck/check/demand.rs:27:1-27:1 -fn- definition:
pub fn subtype(fcx: &FnCtxt, sp: Span, expected: ty::t, actual: ty::t) {
suptype_with_fn(fcx, sp, true, actual, expected,
|sp, a, e, s| { fcx.report_mismatched_types(sp, e, a, s) })
references:- 2librustc/middle/typeck/check/_match.rs:
142: let pat_ty = fcx.node_ty(pat.id);
143: demand::subtype(fcx, pat.span, expected, pat_ty);
--
206: let pat_ty = fcx.node_ty(pat.id);
207: demand::subtype(fcx, pat.span, expected, pat_ty);
librustc/middle/typeck/check/demand.rs:58:63-58:63 -fn- definition:
// Checks that the type `actual` can be coerced to `expected`.
pub fn coerce(fcx: &FnCtxt, sp: Span, expected: ty::t, expr: &ast::Expr) {
let expr_ty = fcx.expr_ty(expr);
references:- 2librustc/middle/typeck/check/mod.rs:
3142: _ => {
3143: demand::coerce(fcx, e.span, t_1, e);
3144: }
librustc/middle/typeck/check/demand.rs:48:1-48:1 -fn- definition:
pub fn eqtype(fcx: &FnCtxt, sp: Span, expected: ty::t, actual: ty::t) {
match infer::mk_eqty(fcx.infcx(), false, infer::Misc(sp), actual, expected) {
Ok(()) => { /* ok */ }
references:- 3librustc/middle/typeck/check/_match.rs:
494: ast::BindByValue(_) => {
495: demand::eqtype(fcx, pat.span, expected, typ);
496: }
--
501: let ct = fcx.local_ty(pat.span, canon_id);
502: demand::eqtype(fcx, pat.span, ct, typ);
503: }