]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/typeck/infer/lattice.rs
6e6c631f007490382f221058a5c68814448cb750
[rust.git] / src / librustc / middle / typeck / infer / lattice.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 /*!
12  * # Lattice Variables
13  *
14  * This file contains generic code for operating on inference variables
15  * that are characterized by an upper- and lower-bound.  The logic and
16  * reasoning is explained in detail in the large comment in `infer.rs`.
17  *
18  * The code in here is defined quite generically so that it can be
19  * applied both to type variables, which represent types being inferred,
20  * and fn variables, which represent function types being inferred.
21  * It may eventually be applied to their types as well, who knows.
22  * In some cases, the functions are also generic with respect to the
23  * operation on the lattice (GLB vs LUB).
24  *
25  * Although all the functions are generic, we generally write the
26  * comments in a way that is specific to type variables and the LUB
27  * operation.  It's just easier that way.
28  *
29  * In general all of the functions are defined parametrically
30  * over a `LatticeValue`, which is a value defined with respect to
31  * a lattice.
32  */
33
34 use middle::ty::{TyVar};
35 use middle::ty::{mod, Ty};
36 use middle::typeck::infer::*;
37 use middle::typeck::infer::combine::*;
38 use middle::typeck::infer::glb::Glb;
39 use middle::typeck::infer::lub::Lub;
40 use util::ppaux::Repr;
41
42 pub trait LatticeDir<'tcx> {
43     // Relates the type `v` to `a` and `b` such that `v` represents
44     // the LUB/GLB of `a` and `b` as appropriate.
45     fn relate_bound(&self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, ()>;
46 }
47
48 impl<'a, 'tcx> LatticeDir<'tcx> for Lub<'a, 'tcx> {
49     fn relate_bound(&self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, ()> {
50         let sub = self.sub();
51         try!(sub.tys(a, v));
52         try!(sub.tys(b, v));
53         Ok(())
54     }
55 }
56
57 impl<'a, 'tcx> LatticeDir<'tcx> for Glb<'a, 'tcx> {
58     fn relate_bound(&self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, ()> {
59         let sub = self.sub();
60         try!(sub.tys(v, a));
61         try!(sub.tys(v, b));
62         Ok(())
63     }
64 }
65
66 pub fn super_lattice_tys<'tcx, L:LatticeDir<'tcx>+Combine<'tcx>>(this: &L,
67                                                                  a: Ty<'tcx>,
68                                                                  b: Ty<'tcx>)
69                                                                  -> cres<'tcx, Ty<'tcx>>
70 {
71     debug!("{}.lattice_tys({}, {})",
72            this.tag(),
73            a.repr(this.infcx().tcx),
74            b.repr(this.infcx().tcx));
75
76     if a == b {
77         return Ok(a);
78     }
79
80     let infcx = this.infcx();
81     let a = infcx.type_variables.borrow().replace_if_possible(a);
82     let b = infcx.type_variables.borrow().replace_if_possible(b);
83     match (&a.sty, &b.sty) {
84         (&ty::ty_infer(TyVar(..)), &ty::ty_infer(TyVar(..)))
85             if infcx.type_var_diverges(a) && infcx.type_var_diverges(b) => {
86             let v = infcx.next_diverging_ty_var();
87             try!(this.relate_bound(v, a, b));
88             Ok(v)
89         }
90
91         (&ty::ty_infer(TyVar(..)), _) |
92         (_, &ty::ty_infer(TyVar(..))) => {
93             let v = infcx.next_ty_var();
94             try!(this.relate_bound(v, a, b));
95             Ok(v)
96         }
97
98         _ => {
99             super_tys(this, a, b)
100         }
101     }
102 }