1 // Copyright 2014 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.
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.
11 use middle::ty::{BuiltinBounds};
12 use middle::ty::{mod, Ty};
13 use middle::ty::TyVar;
14 use middle::infer::combine::*;
15 use middle::infer::{cres};
16 use middle::infer::glb::Glb;
17 use middle::infer::InferCtxt;
18 use middle::infer::lub::Lub;
19 use middle::infer::sub::Sub;
20 use middle::infer::{TypeTrace, Subtype};
21 use middle::infer::type_variable::{EqTo};
22 use util::ppaux::{Repr};
24 use syntax::ast::{Onceness, Unsafety};
26 pub struct Equate<'f, 'tcx: 'f> {
27 fields: CombineFields<'f, 'tcx>
30 #[allow(non_snake_case)]
31 pub fn Equate<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Equate<'f, 'tcx> {
35 impl<'f, 'tcx> Combine<'tcx> for Equate<'f, 'tcx> {
36 fn infcx<'a>(&'a self) -> &'a InferCtxt<'a, 'tcx> { self.fields.infcx }
37 fn tag(&self) -> String { "eq".to_string() }
38 fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
39 fn trace(&self) -> TypeTrace<'tcx> { self.fields.trace.clone() }
41 fn equate<'a>(&'a self) -> Equate<'a, 'tcx> { Equate(self.fields.clone()) }
42 fn sub<'a>(&'a self) -> Sub<'a, 'tcx> { Sub(self.fields.clone()) }
43 fn lub<'a>(&'a self) -> Lub<'a, 'tcx> { Lub(self.fields.clone()) }
44 fn glb<'a>(&'a self) -> Glb<'a, 'tcx> { Glb(self.fields.clone()) }
46 fn contratys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
50 fn contraregions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
54 fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
55 debug!("{}.regions({}, {})",
57 a.repr(self.fields.infcx.tcx),
58 b.repr(self.fields.infcx.tcx));
59 self.infcx().region_vars.make_eqregion(Subtype(self.trace()), a, b);
63 fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
64 debug!("mts({} <: {})",
65 a.repr(self.fields.infcx.tcx),
66 b.repr(self.fields.infcx.tcx));
68 if a.mutbl != b.mutbl { return Err(ty::terr_mutability); }
69 let t = try!(self.tys(a.ty, b.ty));
70 Ok(ty::mt { mutbl: a.mutbl, ty: t })
73 fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
75 Err(ty::terr_unsafety_mismatch(expected_found(self, a, b)))
81 fn oncenesses(&self, a: Onceness, b: Onceness) -> cres<'tcx, Onceness> {
83 Err(ty::terr_onceness_mismatch(expected_found(self, a, b)))
89 fn builtin_bounds(&self,
92 -> cres<'tcx, BuiltinBounds>
94 // More bounds is a subtype of fewer bounds.
96 // e.g., fn:Copy() <: fn(), because the former is a function
97 // that only closes over copyable things, but the latter is
98 // any function at all.
100 Err(ty::terr_builtin_bounds(expected_found(self, a, b)))
106 fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
107 debug!("{}.tys({}, {})", self.tag(),
108 a.repr(self.fields.infcx.tcx), b.repr(self.fields.infcx.tcx));
109 if a == b { return Ok(a); }
111 let infcx = self.fields.infcx;
112 let a = infcx.type_variables.borrow().replace_if_possible(a);
113 let b = infcx.type_variables.borrow().replace_if_possible(b);
114 match (&a.sty, &b.sty) {
115 (&ty::ty_infer(TyVar(a_id)), &ty::ty_infer(TyVar(b_id))) => {
116 infcx.type_variables.borrow_mut().relate_vars(a_id, EqTo, b_id);
120 (&ty::ty_infer(TyVar(a_id)), _) => {
121 try!(self.fields.instantiate(b, EqTo, a_id));
125 (_, &ty::ty_infer(TyVar(b_id))) => {
126 try!(self.fields.instantiate(a, EqTo, b_id));
131 super_tys(self, a, b)
136 fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
137 where T : Combineable<'tcx>
139 try!(self.sub().binders(a, b));
140 self.sub().binders(b, a)