]>
Commit | Line | Data |
---|---|---|
5e7ed085 | 1 | //! # Lattice variables |
1a4d82fc | 2 | //! |
5e7ed085 FG |
3 | //! Generic code for operating on [lattices] of inference variables |
4 | //! that are characterized by an upper- and lower-bound. | |
1a4d82fc | 5 | //! |
5e7ed085 | 6 | //! The code is defined quite generically so that it can be |
1a4d82fc JJ |
7 | //! applied both to type variables, which represent types being inferred, |
8 | //! and fn variables, which represent function types being inferred. | |
5e7ed085 | 9 | //! (It may eventually be applied to their types as well.) |
1a4d82fc JJ |
10 | //! In some cases, the functions are also generic with respect to the |
11 | //! operation on the lattice (GLB vs LUB). | |
12 | //! | |
5e7ed085 | 13 | //! ## Note |
1a4d82fc | 14 | //! |
5e7ed085 FG |
15 | //! Although all the functions are generic, for simplicity, comments in the source code |
16 | //! generally refer to type variables and the LUB operation. | |
17 | //! | |
18 | //! [lattices]: https://en.wikipedia.org/wiki/Lattice_(order) | |
1a4d82fc | 19 | |
dc9dc135 | 20 | use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; |
dfeec247 | 21 | use super::InferCtxt; |
1a4d82fc | 22 | |
5e7ed085 | 23 | use crate::traits::{ObligationCause, PredicateObligation}; |
ba9703b0 XL |
24 | use rustc_middle::ty::relate::{RelateResult, TypeRelation}; |
25 | use rustc_middle::ty::TyVar; | |
26 | use rustc_middle::ty::{self, Ty}; | |
1a4d82fc | 27 | |
5e7ed085 FG |
28 | /// Trait for returning data about a lattice, and for abstracting |
29 | /// over the "direction" of the lattice operation (LUB/GLB). | |
30 | /// | |
31 | /// GLB moves "down" the lattice (to smaller values); LUB moves | |
32 | /// "up" the lattice (to bigger values). | |
dc9dc135 | 33 | pub trait LatticeDir<'f, 'tcx>: TypeRelation<'tcx> { |
2b03887a | 34 | fn infcx(&self) -> &'f InferCtxt<'tcx>; |
c34b1796 | 35 | |
476ff2be SL |
36 | fn cause(&self) -> &ObligationCause<'tcx>; |
37 | ||
5e7ed085 FG |
38 | fn add_obligations(&mut self, obligations: Vec<PredicateObligation<'tcx>>); |
39 | ||
40 | fn define_opaque_types(&self) -> bool; | |
41 | ||
1a4d82fc JJ |
42 | // Relates the type `v` to `a` and `b` such that `v` represents |
43 | // the LUB/GLB of `a` and `b` as appropriate. | |
cc61c64b XL |
44 | // |
45 | // Subtle hack: ordering *may* be significant here. This method | |
3b2f2976 | 46 | // relates `v` to `a` first, which may help us to avoid unnecessary |
cc61c64b | 47 | // type variable obligations. See caller for details. |
5bcae85e | 48 | fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()>; |
1a4d82fc JJ |
49 | } |
50 | ||
5e7ed085 FG |
51 | /// Relates two types using a given lattice. |
52 | #[instrument(skip(this), level = "debug")] | |
dc9dc135 XL |
53 | pub fn super_lattice_tys<'a, 'tcx: 'a, L>( |
54 | this: &mut L, | |
55 | a: Ty<'tcx>, | |
56 | b: Ty<'tcx>, | |
57 | ) -> RelateResult<'tcx, Ty<'tcx>> | |
58 | where | |
59 | L: LatticeDir<'a, 'tcx>, | |
1a4d82fc | 60 | { |
5e7ed085 | 61 | debug!("{}", this.tag()); |
1a4d82fc JJ |
62 | |
63 | if a == b { | |
64 | return Ok(a); | |
65 | } | |
66 | ||
67 | let infcx = this.infcx(); | |
5e7ed085 | 68 | |
f9f354fc XL |
69 | let a = infcx.inner.borrow_mut().type_variables().replace_if_possible(a); |
70 | let b = infcx.inner.borrow_mut().type_variables().replace_if_possible(b); | |
5e7ed085 | 71 | |
1b1a35ee | 72 | match (a.kind(), b.kind()) { |
cc61c64b XL |
73 | // If one side is known to be a variable and one is not, |
74 | // create a variable (`v`) to represent the LUB. Make sure to | |
75 | // relate `v` to the non-type-variable first (by passing it | |
76 | // first to `relate_bound`). Otherwise, we would produce a | |
77 | // subtype obligation that must then be processed. | |
78 | // | |
79 | // Example: if the LHS is a type variable, and RHS is | |
80 | // `Box<i32>`, then we current compare `v` to the RHS first, | |
6522a427 | 81 | // which will instantiate `v` with `Box<i32>`. Then when `v` |
cc61c64b XL |
82 | // is compared to the LHS, we instantiate LHS with `Box<i32>`. |
83 | // But if we did in reverse order, we would create a `v <: | |
84 | // LHS` (or vice versa) constraint and then instantiate | |
85 | // `v`. This would require further processing to achieve same | |
5e7ed085 | 86 | // end-result; in particular, this screws up some of the logic |
cc61c64b XL |
87 | // in coercion, which expects LUB to figure out that the LHS |
88 | // is (e.g.) `Box<i32>`. A more obvious solution might be to | |
89 | // iterate on the subtype obligations that are returned, but I | |
90 | // think this suffices. -nmatsakis | |
b7449926 | 91 | (&ty::Infer(TyVar(..)), _) => { |
dc9dc135 XL |
92 | let v = infcx.next_ty_var(TypeVariableOrigin { |
93 | kind: TypeVariableOriginKind::LatticeVariable, | |
94 | span: this.cause().span, | |
95 | }); | |
cc61c64b XL |
96 | this.relate_bound(v, b, a)?; |
97 | Ok(v) | |
98 | } | |
b7449926 | 99 | (_, &ty::Infer(TyVar(..))) => { |
dc9dc135 XL |
100 | let v = infcx.next_ty_var(TypeVariableOrigin { |
101 | kind: TypeVariableOriginKind::LatticeVariable, | |
102 | span: this.cause().span, | |
103 | }); | |
54a0048b | 104 | this.relate_bound(v, a, b)?; |
1a4d82fc JJ |
105 | Ok(v) |
106 | } | |
107 | ||
6522a427 EL |
108 | ( |
109 | &ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }), | |
110 | &ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }), | |
111 | ) if a_def_id == b_def_id => infcx.super_combine_tys(this, a, b), | |
112 | (&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _) | |
113 | | (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. })) | |
114 | if this.define_opaque_types() && def_id.is_local() => | |
5e7ed085 FG |
115 | { |
116 | this.add_obligations( | |
117 | infcx | |
118 | .handle_opaque_type(a, b, this.a_is_expected(), this.cause(), this.param_env())? | |
119 | .obligations, | |
120 | ); | |
121 | Ok(a) | |
122 | } | |
123 | ||
dfeec247 | 124 | _ => infcx.super_combine_tys(this, a, b), |
1a4d82fc JJ |
125 | } |
126 | } |