]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/equate.rs
46e7813d99e562cd1cff9b39b0224eaa75048fb1
[rust.git] / compiler / rustc_infer / src / infer / equate.rs
1 use super::combine::{CombineFields, ConstEquateRelation, RelationDir};
2 use super::Subtype;
3
4 use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
5 use rustc_middle::ty::subst::SubstsRef;
6 use rustc_middle::ty::TyVar;
7 use rustc_middle::ty::{self, Ty, TyCtxt};
8
9 use rustc_hir::def_id::DefId;
10
11 /// Ensures `a` is made equal to `b`. Returns `a` on success.
12 pub struct Equate<'combine, 'infcx, 'tcx> {
13     fields: &'combine mut CombineFields<'infcx, 'tcx>,
14     a_is_expected: bool,
15 }
16
17 impl<'combine, 'infcx, 'tcx> Equate<'combine, 'infcx, 'tcx> {
18     pub fn new(
19         fields: &'combine mut CombineFields<'infcx, 'tcx>,
20         a_is_expected: bool,
21     ) -> Equate<'combine, 'infcx, 'tcx> {
22         Equate { fields, a_is_expected }
23     }
24 }
25
26 impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
27     fn tag(&self) -> &'static str {
28         "Equate"
29     }
30
31     fn tcx(&self) -> TyCtxt<'tcx> {
32         self.fields.tcx()
33     }
34
35     fn intercrate(&self) -> bool {
36         self.fields.infcx.intercrate
37     }
38
39     fn param_env(&self) -> ty::ParamEnv<'tcx> {
40         self.fields.param_env
41     }
42
43     fn a_is_expected(&self) -> bool {
44         self.a_is_expected
45     }
46
47     fn mark_ambiguous(&mut self) {
48         self.fields.mark_ambiguous();
49     }
50
51     fn relate_item_substs(
52         &mut self,
53         _item_def_id: DefId,
54         a_subst: SubstsRef<'tcx>,
55         b_subst: SubstsRef<'tcx>,
56     ) -> RelateResult<'tcx, SubstsRef<'tcx>> {
57         // N.B., once we are equating types, we don't care about
58         // variance, so don't try to lookup the variance here. This
59         // also avoids some cycles (e.g., #41849) since looking up
60         // variance requires computing types which can require
61         // performing trait matching (which then performs equality
62         // unification).
63
64         relate::relate_substs(self, a_subst, b_subst)
65     }
66
67     fn relate_with_variance<T: Relate<'tcx>>(
68         &mut self,
69         _: ty::Variance,
70         _info: ty::VarianceDiagInfo<'tcx>,
71         a: T,
72         b: T,
73     ) -> RelateResult<'tcx, T> {
74         self.relate(a, b)
75     }
76
77     #[instrument(skip(self), level = "debug")]
78     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
79         if a == b {
80             return Ok(a);
81         }
82
83         trace!(a = ?a.kind(), b = ?b.kind());
84
85         let infcx = self.fields.infcx;
86
87         let a = infcx.inner.borrow_mut().type_variables().replace_if_possible(a);
88         let b = infcx.inner.borrow_mut().type_variables().replace_if_possible(b);
89
90         match (a.kind(), b.kind()) {
91             (&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {
92                 infcx.inner.borrow_mut().type_variables().equate(a_id, b_id);
93             }
94
95             (&ty::Infer(TyVar(a_id)), _) => {
96                 self.fields.instantiate(b, RelationDir::EqTo, a_id, self.a_is_expected)?;
97             }
98
99             (_, &ty::Infer(TyVar(b_id))) => {
100                 self.fields.instantiate(a, RelationDir::EqTo, b_id, self.a_is_expected)?;
101             }
102
103             (
104                 &ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }),
105                 &ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }),
106             ) if a_def_id == b_def_id => {
107                 self.fields.infcx.super_combine_tys(self, a, b)?;
108             }
109             (&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
110             | (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
111                 if self.fields.define_opaque_types && def_id.is_local() =>
112             {
113                 self.fields.obligations.extend(
114                     infcx
115                         .handle_opaque_type(
116                             a,
117                             b,
118                             self.a_is_expected(),
119                             &self.fields.trace.cause,
120                             self.param_env(),
121                         )?
122                         .obligations,
123                 );
124             }
125             // Optimization of GeneratorWitness relation since we know that all
126             // free regions are replaced with bound regions during construction.
127             // This greatly speeds up equating of GeneratorWitness.
128             (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
129                 let a_types = infcx.tcx.anonymize_bound_vars(a_types);
130                 let b_types = infcx.tcx.anonymize_bound_vars(b_types);
131                 if a_types.bound_vars() == b_types.bound_vars() {
132                     let (a_types, b_types) = infcx.replace_bound_vars_with_placeholders(
133                         a_types.map_bound(|a_types| (a_types, b_types.skip_binder())),
134                     );
135                     for (a, b) in std::iter::zip(a_types, b_types) {
136                         self.relate(a, b)?;
137                     }
138                 } else {
139                     return Err(ty::error::TypeError::Sorts(ty::relate::expected_found(
140                         self, a, b,
141                     )));
142                 }
143             }
144
145             _ => {
146                 self.fields.infcx.super_combine_tys(self, a, b)?;
147             }
148         }
149
150         Ok(a)
151     }
152
153     fn regions(
154         &mut self,
155         a: ty::Region<'tcx>,
156         b: ty::Region<'tcx>,
157     ) -> RelateResult<'tcx, ty::Region<'tcx>> {
158         debug!("{}.regions({:?}, {:?})", self.tag(), a, b);
159         let origin = Subtype(Box::new(self.fields.trace.clone()));
160         self.fields
161             .infcx
162             .inner
163             .borrow_mut()
164             .unwrap_region_constraints()
165             .make_eqregion(origin, a, b);
166         Ok(a)
167     }
168
169     fn consts(
170         &mut self,
171         a: ty::Const<'tcx>,
172         b: ty::Const<'tcx>,
173     ) -> RelateResult<'tcx, ty::Const<'tcx>> {
174         self.fields.infcx.super_combine_consts(self, a, b)
175     }
176
177     fn binders<T>(
178         &mut self,
179         a: ty::Binder<'tcx, T>,
180         b: ty::Binder<'tcx, T>,
181     ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
182     where
183         T: Relate<'tcx>,
184     {
185         // A binder is equal to itself if it's structually equal to itself
186         if a == b {
187             return Ok(a);
188         }
189
190         if a.skip_binder().has_escaping_bound_vars() || b.skip_binder().has_escaping_bound_vars() {
191             self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
192             self.fields.higher_ranked_sub(b, a, self.a_is_expected)?;
193         } else {
194             // Fast path for the common case.
195             self.relate(a.skip_binder(), b.skip_binder())?;
196         }
197         Ok(a)
198     }
199 }
200
201 impl<'tcx> ConstEquateRelation<'tcx> for Equate<'_, '_, 'tcx> {
202     fn const_equate_obligation(&mut self, a: ty::Const<'tcx>, b: ty::Const<'tcx>) {
203         self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
204     }
205 }