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