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 //! 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.
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.
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
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};
36 use syntax::ast::{Unsafety};
38 pub struct Bivariate<'f, 'tcx: 'f> {
39 fields: CombineFields<'f, 'tcx>
42 #[allow(non_snake_case)]
43 pub fn Bivariate<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Bivariate<'f, 'tcx> {
44 Bivariate { fields: cf }
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 }
51 fn tys_with_variance(&self, v: ty::Variance, a: Ty<'tcx>, b: Ty<'tcx>)
52 -> cres<'tcx, Ty<'tcx>>
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),
62 fn regions_with_variance(&self, v: ty::Variance, a: ty::Region, b: ty::Region)
63 -> cres<'tcx, ty::Region>
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),
73 fn regions(&self, a: ty::Region, _: ty::Region) -> cres<'tcx, ty::Region> {
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));
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 })
87 fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
89 Err(ty::terr_unsafety_mismatch(expected_found(self, a, b)))
95 fn builtin_bounds(&self,
98 -> cres<'tcx, BuiltinBounds>
101 Err(ty::terr_builtin_bounds(expected_found(self, a, b)))
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); }
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);
121 (&ty::ty_infer(TyVar(a_id)), _) => {
122 try!(self.fields.instantiate(b, BiTo, a_id));
126 (_, &ty::ty_infer(TyVar(b_id))) => {
127 try!(self.fields.instantiate(a, BiTo, b_id));
132 super_tys(self, a, b)
137 fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
138 where T : Combineable<'tcx>
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));