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