]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/bivariate.rs
remove type variable defaults code
[rust.git] / src / librustc / 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 super::combine::CombineFields;
29 use super::type_variable::{BiTo};
30
31 use ty::{self, Ty, TyCtxt};
32 use ty::TyVar;
33 use ty::relate::{Relate, RelateResult, TypeRelation};
34
35 pub struct Bivariate<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
36     fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
37     a_is_expected: bool,
38 }
39
40 impl<'combine, 'infcx, 'gcx, 'tcx> Bivariate<'combine, 'infcx, 'gcx, 'tcx> {
41     pub fn new(fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
42         -> Bivariate<'combine, 'infcx, 'gcx, 'tcx>
43     {
44         Bivariate { fields: fields, a_is_expected: a_is_expected }
45     }
46 }
47
48 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
49     for Bivariate<'combine, 'infcx, 'gcx, 'tcx>
50 {
51     fn tag(&self) -> &'static str { "Bivariate" }
52
53     fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
54
55     fn a_is_expected(&self) -> bool { self.a_is_expected }
56
57     fn relate_with_variance<T: Relate<'tcx>>(&mut self,
58                                              variance: ty::Variance,
59                                              a: &T,
60                                              b: &T)
61                                              -> RelateResult<'tcx, T>
62     {
63         match variance {
64             // If we have Foo<A> and Foo is invariant w/r/t A,
65             // and we want to assert that
66             //
67             //     Foo<A> <: Foo<B> ||
68             //     Foo<B> <: Foo<A>
69             //
70             // then still A must equal B.
71             ty::Invariant => self.relate(a, b),
72
73             ty::Covariant => self.relate(a, b),
74             ty::Bivariant => self.relate(a, b),
75             ty::Contravariant => self.relate(a, b),
76         }
77     }
78
79     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
80         debug!("{}.tys({:?}, {:?})", self.tag(),
81                a, b);
82         if a == b { return Ok(a); }
83
84         let infcx = self.fields.infcx;
85         let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
86         let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
87         match (&a.sty, &b.sty) {
88             (&ty::TyInfer(TyVar(a_id)), &ty::TyInfer(TyVar(b_id))) => {
89                 infcx.type_variables.borrow_mut().relate_vars(a_id, BiTo, b_id);
90                 Ok(a)
91             }
92
93             (&ty::TyInfer(TyVar(a_id)), _) => {
94                 self.fields.instantiate(b, BiTo, a_id, self.a_is_expected)?;
95                 Ok(a)
96             }
97
98             (_, &ty::TyInfer(TyVar(b_id))) => {
99                 self.fields.instantiate(a, BiTo, b_id, self.a_is_expected)?;
100                 Ok(a)
101             }
102
103             _ => {
104                 self.fields.infcx.super_combine_tys(self, a, b)
105             }
106         }
107     }
108
109     fn regions(&mut self, a: &'tcx ty::Region, _: &'tcx ty::Region)
110                -> RelateResult<'tcx, &'tcx ty::Region> {
111         Ok(a)
112     }
113
114     fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
115                   -> RelateResult<'tcx, ty::Binder<T>>
116         where T: Relate<'tcx>
117     {
118         let a1 = self.tcx().erase_late_bound_regions(a);
119         let b1 = self.tcx().erase_late_bound_regions(b);
120         let c = self.relate(&a1, &b1)?;
121         Ok(ty::Binder(c))
122     }
123 }