]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/equate.rs
e94996a0b99a3cd0976527f0d00d7eefc1efc588
[rust.git] / src / librustc / infer / equate.rs
1 use super::combine::{CombineFields, RelationDir, const_unification_error};
2 use super::Subtype;
3
4 use crate::hir::def_id::DefId;
5
6 use crate::ty::{self, Ty, TyCtxt, InferConst};
7 use crate::ty::TyVar;
8 use crate::ty::subst::SubstsRef;
9 use crate::ty::relate::{self, Relate, RelateResult, TypeRelation};
10 use crate::mir::interpret::ConstValue;
11
12 /// Ensures `a` is made equal to `b`. Returns `a` on success.
13 pub struct Equate<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
14     fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
15     a_is_expected: bool,
16 }
17
18 impl<'combine, 'infcx, 'gcx, 'tcx> Equate<'combine, 'infcx, 'gcx, 'tcx> {
19     pub fn new(fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
20         -> Equate<'combine, 'infcx, 'gcx, 'tcx>
21     {
22         Equate { fields: fields, a_is_expected: a_is_expected }
23     }
24 }
25
26 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
27     for Equate<'combine, 'infcx, 'gcx, 'tcx>
28 {
29     fn tag(&self) -> &'static str { "Equate" }
30
31     fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
32
33     fn a_is_expected(&self) -> bool { self.a_is_expected }
34
35     fn relate_item_substs(&mut self,
36                           _item_def_id: DefId,
37                           a_subst: SubstsRef<'tcx>,
38                           b_subst: SubstsRef<'tcx>)
39                           -> RelateResult<'tcx, SubstsRef<'tcx>>
40     {
41         // N.B., once we are equating types, we don't care about
42         // variance, so don't try to lookup the variance here. This
43         // also avoids some cycles (e.g., #41849) since looking up
44         // variance requires computing types which can require
45         // performing trait matching (which then performs equality
46         // unification).
47
48         relate::relate_substs(self, None, a_subst, b_subst)
49     }
50
51     fn relate_with_variance<T: Relate<'tcx>>(&mut self,
52                                              _: ty::Variance,
53                                              a: &T,
54                                              b: &T)
55                                              -> RelateResult<'tcx, T>
56     {
57         self.relate(a, b)
58     }
59
60     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
61         debug!("{}.tys({:?}, {:?})", self.tag(),
62                a, b);
63         if a == b { return Ok(a); }
64
65         let infcx = self.fields.infcx;
66         let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
67         let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
68
69         debug!("{}.tys: replacements ({:?}, {:?})", self.tag(), a, b);
70
71         match (&a.sty, &b.sty) {
72             (&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {
73                 infcx.type_variables.borrow_mut().equate(a_id, b_id);
74             }
75
76             (&ty::Infer(TyVar(a_id)), _) => {
77                 self.fields.instantiate(b, RelationDir::EqTo, a_id, self.a_is_expected)?;
78             }
79
80             (_, &ty::Infer(TyVar(b_id))) => {
81                 self.fields.instantiate(a, RelationDir::EqTo, b_id, self.a_is_expected)?;
82             }
83
84             _ => {
85                 self.fields.infcx.super_combine_tys(self, a, b)?;
86             }
87         }
88
89         Ok(a)
90     }
91
92     fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
93                -> RelateResult<'tcx, ty::Region<'tcx>> {
94         debug!("{}.regions({:?}, {:?})",
95                self.tag(),
96                a,
97                b);
98         let origin = Subtype(self.fields.trace.clone());
99         self.fields.infcx.borrow_region_constraints()
100                          .make_eqregion(origin, a, b);
101         Ok(a)
102     }
103
104     fn consts(
105         &mut self,
106         a: &'tcx ty::LazyConst<'tcx>,
107         b: &'tcx ty::LazyConst<'tcx>,
108     ) -> RelateResult<'tcx, &'tcx ty::LazyConst<'tcx>> {
109         debug!("{}.consts({:?}, {:?})", self.tag(), a, b);
110         if a == b { return Ok(a); }
111
112         let infcx = self.fields.infcx;
113         let a = infcx.const_unification_table.borrow_mut().replace_if_possible(a);
114         let b = infcx.const_unification_table.borrow_mut().replace_if_possible(b);
115         let a_is_expected = self.a_is_expected();
116         if let (&ty::LazyConst::Evaluated(a_eval), &ty::LazyConst::Evaluated(b_eval)) = (a, b) {
117             match (a_eval.val, b_eval.val) {
118                 (ConstValue::Infer(InferConst::Var(a_vid)),
119                  ConstValue::Infer(InferConst::Var(b_vid))) => {
120                     infcx.const_unification_table
121                         .borrow_mut()
122                         .unify_var_var(a_vid, b_vid)
123                         .map_err(|e| const_unification_error(a_is_expected, e))?;
124                     return Ok(a);
125                 }
126
127                 (ConstValue::Infer(InferConst::Var(a_id)), _) => {
128                     self.fields.infcx.unify_const_variable(a_is_expected, a_id, b)?;
129                     return Ok(a);
130                 }
131
132                 (_, ConstValue::Infer(InferConst::Var(b_id))) => {
133                     self.fields.infcx.unify_const_variable(!a_is_expected, b_id, a)?;
134                     return Ok(a);
135                 }
136
137                 _ => {}
138             }
139         }
140
141         self.fields.infcx.super_combine_consts(self, a, b)?;
142         Ok(a)
143     }
144
145     fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
146                   -> RelateResult<'tcx, ty::Binder<T>>
147         where T: Relate<'tcx>
148     {
149         self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
150         self.fields.higher_ranked_sub(b, a, self.a_is_expected)
151     }
152 }