]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/infer/lub.rs
Auto merge of #23934 - lfairy:write-no-deref, r=alexcrichton
[rust.git] / src / librustc / middle / infer / lub.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::higher_ranked::HigherRankedRelations;
13 use super::InferCtxt;
14 use super::lattice::{self, LatticeDir};
15 use super::Subtype;
16
17 use middle::ty::{self, Ty};
18 use middle::ty_relate::{Relate, RelateResult, TypeRelation};
19 use util::ppaux::Repr;
20
21 /// "Least upper bound" (common supertype)
22 pub struct Lub<'a, 'tcx: 'a> {
23     fields: CombineFields<'a, 'tcx>
24 }
25
26 impl<'a, 'tcx> Lub<'a, 'tcx> {
27     pub fn new(fields: CombineFields<'a, 'tcx>) -> Lub<'a, 'tcx> {
28         Lub { fields: fields }
29     }
30 }
31
32 impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Lub<'a, 'tcx> {
33     fn tag(&self) -> &'static str { "Lub" }
34
35     fn tcx(&self) -> &'a ty::ctxt<'tcx> { self.fields.tcx() }
36
37     fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
38
39     fn relate_with_variance<T:Relate<'a,'tcx>>(&mut self,
40                                                variance: ty::Variance,
41                                                a: &T,
42                                                b: &T)
43                                                -> RelateResult<'tcx, T>
44     {
45         match variance {
46             ty::Invariant => self.fields.equate().relate(a, b),
47             ty::Covariant => self.relate(a, b),
48             ty::Bivariant => self.fields.bivariate().relate(a, b),
49             ty::Contravariant => self.fields.glb().relate(a, b),
50         }
51     }
52
53     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
54         lattice::super_lattice_tys(self, a, b)
55     }
56
57     fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
58         debug!("{}.regions({}, {})",
59                self.tag(),
60                a.repr(self.tcx()),
61                b.repr(self.tcx()));
62
63         let origin = Subtype(self.fields.trace.clone());
64         Ok(self.fields.infcx.region_vars.lub_regions(origin, a, b))
65     }
66
67     fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
68                   -> RelateResult<'tcx, ty::Binder<T>>
69         where T: Relate<'a, 'tcx>
70     {
71         self.fields.higher_ranked_lub(a, b)
72     }
73 }
74
75 impl<'a, 'tcx> LatticeDir<'a,'tcx> for Lub<'a, 'tcx> {
76     fn infcx(&self) -> &'a InferCtxt<'a,'tcx> {
77         self.fields.infcx
78     }
79
80     fn relate_bound(&self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
81         let mut sub = self.fields.sub();
82         try!(sub.relate(&a, &v));
83         try!(sub.relate(&b, &v));
84         Ok(())
85     }
86 }
87