]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/lattice.rs
Rollup merge of #41249 - GuillaumeGomez:rustdoc-render, r=steveklabnik,frewsxcv
[rust.git] / src / librustc / 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 //! # Lattice Variables
12 //!
13 //! This file contains generic code for operating on inference variables
14 //! that are characterized by an upper- and lower-bound.  The logic and
15 //! reasoning is explained in detail in the large comment in `infer.rs`.
16 //!
17 //! The code in here is defined quite generically so that it can be
18 //! applied both to type variables, which represent types being inferred,
19 //! and fn variables, which represent function types being inferred.
20 //! It may eventually be applied to their types as well, who knows.
21 //! In some cases, the functions are also generic with respect to the
22 //! operation on the lattice (GLB vs LUB).
23 //!
24 //! Although all the functions are generic, we generally write the
25 //! comments in a way that is specific to type variables and the LUB
26 //! operation.  It's just easier that way.
27 //!
28 //! In general all of the functions are defined parametrically
29 //! over a `LatticeValue`, which is a value defined with respect to
30 //! a lattice.
31
32 use super::InferCtxt;
33 use super::type_variable::TypeVariableOrigin;
34
35 use traits::ObligationCause;
36 use ty::TyVar;
37 use ty::{self, Ty};
38 use ty::relate::{RelateResult, TypeRelation};
39
40 pub trait LatticeDir<'f, 'gcx: 'f+'tcx, 'tcx: 'f> : TypeRelation<'f, 'gcx, 'tcx> {
41     fn infcx(&self) -> &'f InferCtxt<'f, 'gcx, 'tcx>;
42
43     fn cause(&self) -> &ObligationCause<'tcx>;
44
45     // Relates the type `v` to `a` and `b` such that `v` represents
46     // the LUB/GLB of `a` and `b` as appropriate.
47     //
48     // Subtle hack: ordering *may* be significant here. This method
49     // relates `v` to `a` first, which may help us to avoid unecessary
50     // type variable obligations. See caller for details.
51     fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()>;
52 }
53
54 pub fn super_lattice_tys<'a, 'gcx, 'tcx, L>(this: &mut L,
55                                             a: Ty<'tcx>,
56                                             b: Ty<'tcx>)
57                                             -> RelateResult<'tcx, Ty<'tcx>>
58     where L: LatticeDir<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
59 {
60     debug!("{}.lattice_tys({:?}, {:?})",
61            this.tag(),
62            a,
63            b);
64
65     if a == b {
66         return Ok(a);
67     }
68
69     let infcx = this.infcx();
70     let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
71     let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
72     match (&a.sty, &b.sty) {
73         (&ty::TyInfer(TyVar(..)), &ty::TyInfer(TyVar(..)))
74             if infcx.type_var_diverges(a) && infcx.type_var_diverges(b) => {
75             let v = infcx.next_diverging_ty_var(
76                 TypeVariableOrigin::LatticeVariable(this.cause().span));
77             this.relate_bound(v, a, b)?;
78             Ok(v)
79         }
80
81         // If one side is known to be a variable and one is not,
82         // create a variable (`v`) to represent the LUB. Make sure to
83         // relate `v` to the non-type-variable first (by passing it
84         // first to `relate_bound`). Otherwise, we would produce a
85         // subtype obligation that must then be processed.
86         //
87         // Example: if the LHS is a type variable, and RHS is
88         // `Box<i32>`, then we current compare `v` to the RHS first,
89         // which will instantiate `v` with `Box<i32>`.  Then when `v`
90         // is compared to the LHS, we instantiate LHS with `Box<i32>`.
91         // But if we did in reverse order, we would create a `v <:
92         // LHS` (or vice versa) constraint and then instantiate
93         // `v`. This would require further processing to achieve same
94         // end-result; in partiular, this screws up some of the logic
95         // in coercion, which expects LUB to figure out that the LHS
96         // is (e.g.) `Box<i32>`. A more obvious solution might be to
97         // iterate on the subtype obligations that are returned, but I
98         // think this suffices. -nmatsakis
99         (&ty::TyInfer(TyVar(..)), _) => {
100             let v = infcx.next_ty_var(TypeVariableOrigin::LatticeVariable(this.cause().span));
101             this.relate_bound(v, b, a)?;
102             Ok(v)
103         }
104         (_, &ty::TyInfer(TyVar(..))) => {
105             let v = infcx.next_ty_var(TypeVariableOrigin::LatticeVariable(this.cause().span));
106             this.relate_bound(v, a, b)?;
107             Ok(v)
108         }
109
110         _ => {
111             infcx.super_combine_tys(this, a, b)
112         }
113     }
114 }