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