]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/equate.rs
Rollup merge of #102781 - StackOverflowExcept1on:master, r=joshtriplett
[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 param_env(&self) -> ty::ParamEnv<'tcx> {
36         self.fields.param_env
37     }
38
39     fn a_is_expected(&self) -> bool {
40         self.a_is_expected
41     }
42
43     fn relate_item_substs(
44         &mut self,
45         _item_def_id: DefId,
46         a_subst: SubstsRef<'tcx>,
47         b_subst: SubstsRef<'tcx>,
48     ) -> RelateResult<'tcx, SubstsRef<'tcx>> {
49         // N.B., once we are equating types, we don't care about
50         // variance, so don't try to lookup the variance here. This
51         // also avoids some cycles (e.g., #41849) since looking up
52         // variance requires computing types which can require
53         // performing trait matching (which then performs equality
54         // unification).
55
56         relate::relate_substs(self, a_subst, b_subst)
57     }
58
59     fn relate_with_variance<T: Relate<'tcx>>(
60         &mut self,
61         _: ty::Variance,
62         _info: ty::VarianceDiagInfo<'tcx>,
63         a: T,
64         b: T,
65     ) -> RelateResult<'tcx, T> {
66         self.relate(a, b)
67     }
68
69     #[instrument(skip(self), level = "debug")]
70     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
71         if a == b {
72             return Ok(a);
73         }
74
75         trace!(a = ?a.kind(), b = ?b.kind());
76
77         let infcx = self.fields.infcx;
78
79         let a = infcx.inner.borrow_mut().type_variables().replace_if_possible(a);
80         let b = infcx.inner.borrow_mut().type_variables().replace_if_possible(b);
81
82         match (a.kind(), b.kind()) {
83             (&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {
84                 infcx.inner.borrow_mut().type_variables().equate(a_id, b_id);
85             }
86
87             (&ty::Infer(TyVar(a_id)), _) => {
88                 self.fields.instantiate(b, RelationDir::EqTo, a_id, self.a_is_expected)?;
89             }
90
91             (_, &ty::Infer(TyVar(b_id))) => {
92                 self.fields.instantiate(a, RelationDir::EqTo, b_id, self.a_is_expected)?;
93             }
94
95             (&ty::Opaque(a_def_id, _), &ty::Opaque(b_def_id, _)) if a_def_id == b_def_id => {
96                 self.fields.infcx.super_combine_tys(self, a, b)?;
97             }
98             (&ty::Opaque(did, ..), _) | (_, &ty::Opaque(did, ..))
99                 if self.fields.define_opaque_types && did.is_local() =>
100             {
101                 self.fields.obligations.extend(
102                     infcx
103                         .handle_opaque_type(
104                             a,
105                             b,
106                             self.a_is_expected(),
107                             &self.fields.trace.cause,
108                             self.param_env(),
109                         )?
110                         .obligations,
111                 );
112             }
113             // Optimization of GeneratorWitness relation since we know that all
114             // free regions are replaced with bound regions during construction.
115             // This greatly speeds up equating of GeneratorWitness.
116             (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
117                 let a_types = infcx.tcx.anonymize_bound_vars(a_types);
118                 let b_types = infcx.tcx.anonymize_bound_vars(b_types);
119                 if a_types.bound_vars() == b_types.bound_vars() {
120                     let (a_types, b_types) = infcx.replace_bound_vars_with_placeholders(
121                         a_types.map_bound(|a_types| (a_types, b_types.skip_binder())),
122                     );
123                     for (a, b) in std::iter::zip(a_types, b_types) {
124                         self.relate(a, b)?;
125                     }
126                 } else {
127                     return Err(ty::error::TypeError::Sorts(ty::relate::expected_found(
128                         self, a, b,
129                     )));
130                 }
131             }
132
133             _ => {
134                 self.fields.infcx.super_combine_tys(self, a, b)?;
135             }
136         }
137
138         Ok(a)
139     }
140
141     fn regions(
142         &mut self,
143         a: ty::Region<'tcx>,
144         b: ty::Region<'tcx>,
145     ) -> RelateResult<'tcx, ty::Region<'tcx>> {
146         debug!("{}.regions({:?}, {:?})", self.tag(), a, b);
147         let origin = Subtype(Box::new(self.fields.trace.clone()));
148         self.fields
149             .infcx
150             .inner
151             .borrow_mut()
152             .unwrap_region_constraints()
153             .make_eqregion(origin, a, b);
154         Ok(a)
155     }
156
157     fn consts(
158         &mut self,
159         a: ty::Const<'tcx>,
160         b: ty::Const<'tcx>,
161     ) -> RelateResult<'tcx, ty::Const<'tcx>> {
162         self.fields.infcx.super_combine_consts(self, a, b)
163     }
164
165     fn binders<T>(
166         &mut self,
167         a: ty::Binder<'tcx, T>,
168         b: ty::Binder<'tcx, T>,
169     ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
170     where
171         T: Relate<'tcx>,
172     {
173         if a.skip_binder().has_escaping_bound_vars() || b.skip_binder().has_escaping_bound_vars() {
174             self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
175             self.fields.higher_ranked_sub(b, a, self.a_is_expected)?;
176         } else {
177             // Fast path for the common case.
178             self.relate(a.skip_binder(), b.skip_binder())?;
179         }
180         Ok(a)
181     }
182 }
183
184 impl<'tcx> ConstEquateRelation<'tcx> for Equate<'_, '_, 'tcx> {
185     fn const_equate_obligation(&mut self, a: ty::Const<'tcx>, b: ty::Const<'tcx>) {
186         self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
187     }
188 }