]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/glb.rs
kill old-style-lub warnings
[rust.git] / src / librustc / infer / glb.rs
1 // Copyright 2012 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 super::combine::CombineFields;
12 use super::InferCtxt;
13 use super::lattice::{self, LatticeDir};
14 use super::Subtype;
15
16 use traits::ObligationCause;
17 use ty::{self, Ty, TyCtxt};
18 use ty::relate::{Relate, RelateResult, TypeRelation};
19
20 /// "Greatest lower bound" (common subtype)
21 pub struct Glb<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
22     fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
23     a_is_expected: bool,
24 }
25
26 impl<'combine, 'infcx, 'gcx, 'tcx> Glb<'combine, 'infcx, 'gcx, 'tcx> {
27     pub fn new(fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
28         -> Glb<'combine, 'infcx, 'gcx, 'tcx>
29     {
30         Glb { fields: fields, a_is_expected: a_is_expected }
31     }
32 }
33
34 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
35     for Glb<'combine, 'infcx, 'gcx, 'tcx>
36 {
37     fn tag(&self) -> &'static str { "Glb" }
38
39     fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
40
41     fn a_is_expected(&self) -> bool { self.a_is_expected }
42
43     fn relate_with_variance<T: Relate<'tcx>>(&mut self,
44                                              variance: ty::Variance,
45                                              a: &T,
46                                              b: &T)
47                                              -> RelateResult<'tcx, T>
48     {
49         match variance {
50             ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
51             ty::Covariant => self.relate(a, b),
52             // FIXME(#41044) -- not correct, need test
53             ty::Bivariant => Ok(a.clone()),
54             ty::Contravariant => self.fields.lub(self.a_is_expected).relate(a, b),
55         }
56     }
57
58     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
59         lattice::super_lattice_tys(self, a, b)
60     }
61
62     fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
63                -> RelateResult<'tcx, ty::Region<'tcx>> {
64         debug!("{}.regions({:?}, {:?})",
65                self.tag(),
66                a,
67                b);
68
69         let origin = Subtype(self.fields.trace.clone());
70         Ok(self.fields.infcx.borrow_region_constraints().glb_regions(self.tcx(), origin, a, b))
71     }
72
73     fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
74                   -> RelateResult<'tcx, ty::Binder<T>>
75         where T: Relate<'tcx>
76     {
77         debug!("binders(a={:?}, b={:?})", a, b);
78
79         // When higher-ranked types are involved, computing the LUB is
80         // very challenging, switch to invariance. This is obviously
81         // overly conservative but works ok in practice.
82         self.relate_with_variance(ty::Variance::Invariant, a, b)?;
83         Ok(a.clone())
84     }
85 }
86
87 impl<'combine, 'infcx, 'gcx, 'tcx> LatticeDir<'infcx, 'gcx, 'tcx>
88     for Glb<'combine, 'infcx, 'gcx, 'tcx>
89 {
90     fn infcx(&self) -> &'infcx InferCtxt<'infcx, 'gcx, 'tcx> {
91         self.fields.infcx
92     }
93
94     fn cause(&self) -> &ObligationCause<'tcx> {
95         &self.fields.trace.cause
96     }
97
98     fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
99         let mut sub = self.fields.sub(self.a_is_expected);
100         sub.relate(&v, &a)?;
101         sub.relate(&v, &b)?;
102         Ok(())
103     }
104 }