]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/infer/equate.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / src / librustc / middle / infer / equate.rs
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.
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 use middle::ty::{BuiltinBounds};
12 use middle::ty::{self, 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};
23
24 use syntax::ast::Unsafety;
25
26 pub struct Equate<'f, 'tcx: 'f> {
27     fields: CombineFields<'f, 'tcx>
28 }
29
30 #[allow(non_snake_case)]
31 pub fn Equate<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Equate<'f, 'tcx> {
32     Equate { fields: cf }
33 }
34
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() }
40
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()) }
45
46     fn contratys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
47         self.tys(a, b)
48     }
49
50     fn contraregions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
51         self.regions(a, b)
52     }
53
54     fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
55         debug!("{}.regions({}, {})",
56                self.tag(),
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);
60         Ok(a)
61     }
62
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));
67
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 })
71     }
72
73     fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
74         if a != b {
75             Err(ty::terr_unsafety_mismatch(expected_found(self, a, b)))
76         } else {
77             Ok(a)
78         }
79     }
80
81     fn builtin_bounds(&self,
82                       a: BuiltinBounds,
83                       b: BuiltinBounds)
84                       -> cres<'tcx, BuiltinBounds>
85     {
86         // More bounds is a subtype of fewer bounds.
87         //
88         // e.g., fn:Copy() <: fn(), because the former is a function
89         // that only closes over copyable things, but the latter is
90         // any function at all.
91         if a != b {
92             Err(ty::terr_builtin_bounds(expected_found(self, a, b)))
93         } else {
94             Ok(a)
95         }
96     }
97
98     fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
99         debug!("{}.tys({}, {})", self.tag(),
100                a.repr(self.fields.infcx.tcx), b.repr(self.fields.infcx.tcx));
101         if a == b { return Ok(a); }
102
103         let infcx = self.fields.infcx;
104         let a = infcx.type_variables.borrow().replace_if_possible(a);
105         let b = infcx.type_variables.borrow().replace_if_possible(b);
106         match (&a.sty, &b.sty) {
107             (&ty::ty_infer(TyVar(a_id)), &ty::ty_infer(TyVar(b_id))) => {
108                 infcx.type_variables.borrow_mut().relate_vars(a_id, EqTo, b_id);
109                 Ok(a)
110             }
111
112             (&ty::ty_infer(TyVar(a_id)), _) => {
113                 try!(self.fields.instantiate(b, EqTo, a_id));
114                 Ok(a)
115             }
116
117             (_, &ty::ty_infer(TyVar(b_id))) => {
118                 try!(self.fields.instantiate(a, EqTo, b_id));
119                 Ok(a)
120             }
121
122             _ => {
123                 super_tys(self, a, b)
124             }
125         }
126     }
127
128     fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
129         where T : Combineable<'tcx>
130     {
131         try!(self.sub().binders(a, b));
132         self.sub().binders(b, a)
133     }
134 }