]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/infer/bivariate.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / librustc / middle / infer / bivariate.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 //! Applies the "bivariance relationship" to two types and/or regions.
12 //! If (A,B) are bivariant then either A <: B or B <: A. It occurs
13 //! when type/lifetime parameters are unconstrained. Usually this is
14 //! an error, but we permit it in the specific case where a type
15 //! parameter is constrained in a where-clause via an associated type.
16 //!
17 //! There are several ways one could implement bivariance. You could
18 //! just do nothing at all, for example, or you could fully verify
19 //! that one of the two subtyping relationships hold. We choose to
20 //! thread a middle line: we relate types up to regions, but ignore
21 //! all region relationships.
22 //!
23 //! At one point, handling bivariance in this fashion was necessary
24 //! for inference, but I'm actually not sure if that is true anymore.
25 //! In particular, it might be enough to say (A,B) are bivariant for
26 //! all (A,B).
27
28 use middle::ty::{BuiltinBounds};
29 use middle::ty::{self, Ty};
30 use middle::ty::TyVar;
31 use middle::infer::combine::*;
32 use middle::infer::{cres};
33 use middle::infer::type_variable::{BiTo};
34 use util::ppaux::{Repr};
35
36 use syntax::ast::{Unsafety};
37
38 pub struct Bivariate<'f, 'tcx: 'f> {
39     fields: CombineFields<'f, 'tcx>
40 }
41
42 #[allow(non_snake_case)]
43 pub fn Bivariate<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Bivariate<'f, 'tcx> {
44     Bivariate { fields: cf }
45 }
46
47 impl<'f, 'tcx> Combine<'tcx> for Bivariate<'f, 'tcx> {
48     fn tag(&self) -> String { "Bivariate".to_string() }
49     fn fields<'a>(&'a self) -> &'a CombineFields<'a, 'tcx> { &self.fields }
50
51     fn tys_with_variance(&self, v: ty::Variance, a: Ty<'tcx>, b: Ty<'tcx>)
52                          -> cres<'tcx, Ty<'tcx>>
53     {
54         match v {
55             ty::Invariant => self.equate().tys(a, b),
56             ty::Covariant => self.tys(a, b),
57             ty::Contravariant => self.tys(a, b),
58             ty::Bivariant => self.tys(a, b),
59         }
60     }
61
62     fn regions_with_variance(&self, v: ty::Variance, a: ty::Region, b: ty::Region)
63                              -> cres<'tcx, ty::Region>
64     {
65         match v {
66             ty::Invariant => self.equate().regions(a, b),
67             ty::Covariant => self.regions(a, b),
68             ty::Contravariant => self.regions(a, b),
69             ty::Bivariant => self.regions(a, b),
70         }
71     }
72
73     fn regions(&self, a: ty::Region, _: ty::Region) -> cres<'tcx, ty::Region> {
74         Ok(a)
75     }
76
77     fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
78         debug!("mts({} <: {})",
79                a.repr(self.fields.infcx.tcx),
80                b.repr(self.fields.infcx.tcx));
81
82         if a.mutbl != b.mutbl { return Err(ty::terr_mutability); }
83         let t = try!(self.tys(a.ty, b.ty));
84         Ok(ty::mt { mutbl: a.mutbl, ty: t })
85     }
86
87     fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
88         if a != b {
89             Err(ty::terr_unsafety_mismatch(expected_found(self, a, b)))
90         } else {
91             Ok(a)
92         }
93     }
94
95     fn builtin_bounds(&self,
96                       a: BuiltinBounds,
97                       b: BuiltinBounds)
98                       -> cres<'tcx, BuiltinBounds>
99     {
100         if a != b {
101             Err(ty::terr_builtin_bounds(expected_found(self, a, b)))
102         } else {
103             Ok(a)
104         }
105     }
106
107     fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
108         debug!("{}.tys({}, {})", self.tag(),
109                a.repr(self.fields.infcx.tcx), b.repr(self.fields.infcx.tcx));
110         if a == b { return Ok(a); }
111
112         let infcx = self.fields.infcx;
113         let a = infcx.type_variables.borrow().replace_if_possible(a);
114         let b = infcx.type_variables.borrow().replace_if_possible(b);
115         match (&a.sty, &b.sty) {
116             (&ty::ty_infer(TyVar(a_id)), &ty::ty_infer(TyVar(b_id))) => {
117                 infcx.type_variables.borrow_mut().relate_vars(a_id, BiTo, b_id);
118                 Ok(a)
119             }
120
121             (&ty::ty_infer(TyVar(a_id)), _) => {
122                 try!(self.fields.instantiate(b, BiTo, a_id));
123                 Ok(a)
124             }
125
126             (_, &ty::ty_infer(TyVar(b_id))) => {
127                 try!(self.fields.instantiate(a, BiTo, b_id));
128                 Ok(a)
129             }
130
131             _ => {
132                 super_tys(self, a, b)
133             }
134         }
135     }
136
137     fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
138         where T : Combineable<'tcx>
139     {
140         let a1 = ty::erase_late_bound_regions(self.tcx(), a);
141         let b1 = ty::erase_late_bound_regions(self.tcx(), b);
142         let c = try!(Combineable::combine(self, &a1, &b1));
143         Ok(ty::Binder(c))
144     }
145 }