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