]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/combine.rs
Rollup merge of #102962 - flba-eb:remote_test_server_help, r=pietroalbini
[rust.git] / compiler / rustc_infer / src / infer / combine.rs
1 //! There are four type combiners: [Equate], [Sub], [Lub], and [Glb].
2 //! Each implements the trait [TypeRelation] and contains methods for
3 //! combining two instances of various things and yielding a new instance.
4 //! These combiner methods always yield a `Result<T>`. To relate two
5 //! types, you can use `infcx.at(cause, param_env)` which then allows
6 //! you to use the relevant methods of [At](super::at::At).
7 //!
8 //! Combiners mostly do their specific behavior and then hand off the
9 //! bulk of the work to [InferCtxt::super_combine_tys] and
10 //! [InferCtxt::super_combine_consts].
11 //!
12 //! Combining two types may have side-effects on the inference contexts
13 //! which can be undone by using snapshots. You probably want to use
14 //! either [InferCtxt::commit_if_ok] or [InferCtxt::probe].
15 //!
16 //! On success, the  LUB/GLB operations return the appropriate bound. The
17 //! return value of `Equate` or `Sub` shouldn't really be used.
18 //!
19 //! ## Contravariance
20 //!
21 //! We explicitly track which argument is expected using
22 //! [TypeRelation::a_is_expected], so when dealing with contravariance
23 //! this should be correctly updated.
24
25 use super::equate::Equate;
26 use super::glb::Glb;
27 use super::lub::Lub;
28 use super::sub::Sub;
29 use super::type_variable::TypeVariableValue;
30 use super::{InferCtxt, MiscVariable, TypeTrace};
31 use crate::traits::{Obligation, PredicateObligations};
32 use rustc_data_structures::sso::SsoHashMap;
33 use rustc_hir::def_id::DefId;
34 use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
35 use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
36 use rustc_middle::traits::ObligationCause;
37 use rustc_middle::ty::error::{ExpectedFound, TypeError};
38 use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
39 use rustc_middle::ty::subst::SubstsRef;
40 use rustc_middle::ty::{self, InferConst, ToPredicate, Ty, TyCtxt, TypeVisitable};
41 use rustc_middle::ty::{IntType, UintType};
42 use rustc_span::{Span, DUMMY_SP};
43
44 #[derive(Clone)]
45 pub struct CombineFields<'infcx, 'tcx> {
46     pub infcx: &'infcx InferCtxt<'tcx>,
47     pub trace: TypeTrace<'tcx>,
48     pub cause: Option<ty::relate::Cause>,
49     pub param_env: ty::ParamEnv<'tcx>,
50     pub obligations: PredicateObligations<'tcx>,
51     /// Whether we should define opaque types
52     /// or just treat them opaquely.
53     /// Currently only used to prevent predicate
54     /// matching from matching anything against opaque
55     /// types.
56     pub define_opaque_types: bool,
57 }
58
59 #[derive(Copy, Clone, Debug)]
60 pub enum RelationDir {
61     SubtypeOf,
62     SupertypeOf,
63     EqTo,
64 }
65
66 impl<'tcx> InferCtxt<'tcx> {
67     pub fn super_combine_tys<R>(
68         &self,
69         relation: &mut R,
70         a: Ty<'tcx>,
71         b: Ty<'tcx>,
72     ) -> RelateResult<'tcx, Ty<'tcx>>
73     where
74         R: TypeRelation<'tcx>,
75     {
76         let a_is_expected = relation.a_is_expected();
77
78         match (a.kind(), b.kind()) {
79             // Relate integral variables to other types
80             (&ty::Infer(ty::IntVar(a_id)), &ty::Infer(ty::IntVar(b_id))) => {
81                 self.inner
82                     .borrow_mut()
83                     .int_unification_table()
84                     .unify_var_var(a_id, b_id)
85                     .map_err(|e| int_unification_error(a_is_expected, e))?;
86                 Ok(a)
87             }
88             (&ty::Infer(ty::IntVar(v_id)), &ty::Int(v)) => {
89                 self.unify_integral_variable(a_is_expected, v_id, IntType(v))
90             }
91             (&ty::Int(v), &ty::Infer(ty::IntVar(v_id))) => {
92                 self.unify_integral_variable(!a_is_expected, v_id, IntType(v))
93             }
94             (&ty::Infer(ty::IntVar(v_id)), &ty::Uint(v)) => {
95                 self.unify_integral_variable(a_is_expected, v_id, UintType(v))
96             }
97             (&ty::Uint(v), &ty::Infer(ty::IntVar(v_id))) => {
98                 self.unify_integral_variable(!a_is_expected, v_id, UintType(v))
99             }
100
101             // Relate floating-point variables to other types
102             (&ty::Infer(ty::FloatVar(a_id)), &ty::Infer(ty::FloatVar(b_id))) => {
103                 self.inner
104                     .borrow_mut()
105                     .float_unification_table()
106                     .unify_var_var(a_id, b_id)
107                     .map_err(|e| float_unification_error(relation.a_is_expected(), e))?;
108                 Ok(a)
109             }
110             (&ty::Infer(ty::FloatVar(v_id)), &ty::Float(v)) => {
111                 self.unify_float_variable(a_is_expected, v_id, v)
112             }
113             (&ty::Float(v), &ty::Infer(ty::FloatVar(v_id))) => {
114                 self.unify_float_variable(!a_is_expected, v_id, v)
115             }
116
117             // All other cases of inference are errors
118             (&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
119                 Err(TypeError::Sorts(ty::relate::expected_found(relation, a, b)))
120             }
121
122             _ => ty::relate::super_relate_tys(relation, a, b),
123         }
124     }
125
126     pub fn super_combine_consts<R>(
127         &self,
128         relation: &mut R,
129         a: ty::Const<'tcx>,
130         b: ty::Const<'tcx>,
131     ) -> RelateResult<'tcx, ty::Const<'tcx>>
132     where
133         R: ConstEquateRelation<'tcx>,
134     {
135         debug!("{}.consts({:?}, {:?})", relation.tag(), a, b);
136         if a == b {
137             return Ok(a);
138         }
139
140         let a = self.shallow_resolve(a);
141         let b = self.shallow_resolve(b);
142
143         let a_is_expected = relation.a_is_expected();
144
145         match (a.kind(), b.kind()) {
146             (
147                 ty::ConstKind::Infer(InferConst::Var(a_vid)),
148                 ty::ConstKind::Infer(InferConst::Var(b_vid)),
149             ) => {
150                 self.inner.borrow_mut().const_unification_table().union(a_vid, b_vid);
151                 return Ok(a);
152             }
153
154             // All other cases of inference with other variables are errors.
155             (ty::ConstKind::Infer(InferConst::Var(_)), ty::ConstKind::Infer(_))
156             | (ty::ConstKind::Infer(_), ty::ConstKind::Infer(InferConst::Var(_))) => {
157                 bug!("tried to combine ConstKind::Infer/ConstKind::Infer(InferConst::Var)")
158             }
159
160             (ty::ConstKind::Infer(InferConst::Var(vid)), _) => {
161                 return self.unify_const_variable(relation.param_env(), vid, b, a_is_expected);
162             }
163
164             (_, ty::ConstKind::Infer(InferConst::Var(vid))) => {
165                 return self.unify_const_variable(relation.param_env(), vid, a, !a_is_expected);
166             }
167             (ty::ConstKind::Unevaluated(..), _) if self.tcx.lazy_normalization() => {
168                 // FIXME(#59490): Need to remove the leak check to accommodate
169                 // escaping bound variables here.
170                 if !a.has_escaping_bound_vars() && !b.has_escaping_bound_vars() {
171                     relation.const_equate_obligation(a, b);
172                 }
173                 return Ok(b);
174             }
175             (_, ty::ConstKind::Unevaluated(..)) if self.tcx.lazy_normalization() => {
176                 // FIXME(#59490): Need to remove the leak check to accommodate
177                 // escaping bound variables here.
178                 if !a.has_escaping_bound_vars() && !b.has_escaping_bound_vars() {
179                     relation.const_equate_obligation(a, b);
180                 }
181                 return Ok(a);
182             }
183             _ => {}
184         }
185
186         ty::relate::super_relate_consts(relation, a, b)
187     }
188
189     /// Unifies the const variable `target_vid` with the given constant.
190     ///
191     /// This also tests if the given const `ct` contains an inference variable which was previously
192     /// unioned with `target_vid`. If this is the case, inferring `target_vid` to `ct`
193     /// would result in an infinite type as we continuously replace an inference variable
194     /// in `ct` with `ct` itself.
195     ///
196     /// This is especially important as unevaluated consts use their parents generics.
197     /// They therefore often contain unused substs, making these errors far more likely.
198     ///
199     /// A good example of this is the following:
200     ///
201     /// ```compile_fail,E0308
202     /// #![feature(generic_const_exprs)]
203     ///
204     /// fn bind<const N: usize>(value: [u8; N]) -> [u8; 3 + 4] {
205     ///     todo!()
206     /// }
207     ///
208     /// fn main() {
209     ///     let mut arr = Default::default();
210     ///     arr = bind(arr);
211     /// }
212     /// ```
213     ///
214     /// Here `3 + 4` ends up as `ConstKind::Unevaluated` which uses the generics
215     /// of `fn bind` (meaning that its substs contain `N`).
216     ///
217     /// `bind(arr)` now infers that the type of `arr` must be `[u8; N]`.
218     /// The assignment `arr = bind(arr)` now tries to equate `N` with `3 + 4`.
219     ///
220     /// As `3 + 4` contains `N` in its substs, this must not succeed.
221     ///
222     /// See `src/test/ui/const-generics/occurs-check/` for more examples where this is relevant.
223     #[instrument(level = "debug", skip(self))]
224     fn unify_const_variable(
225         &self,
226         param_env: ty::ParamEnv<'tcx>,
227         target_vid: ty::ConstVid<'tcx>,
228         ct: ty::Const<'tcx>,
229         vid_is_expected: bool,
230     ) -> RelateResult<'tcx, ty::Const<'tcx>> {
231         let (for_universe, span) = {
232             let mut inner = self.inner.borrow_mut();
233             let variable_table = &mut inner.const_unification_table();
234             let var_value = variable_table.probe_value(target_vid);
235             match var_value.val {
236                 ConstVariableValue::Known { value } => {
237                     bug!("instantiating {:?} which has a known value {:?}", target_vid, value)
238                 }
239                 ConstVariableValue::Unknown { universe } => (universe, var_value.origin.span),
240             }
241         };
242         let value = ConstInferUnifier { infcx: self, span, param_env, for_universe, target_vid }
243             .relate(ct, ct)?;
244
245         self.inner.borrow_mut().const_unification_table().union_value(
246             target_vid,
247             ConstVarValue {
248                 origin: ConstVariableOrigin {
249                     kind: ConstVariableOriginKind::ConstInference,
250                     span: DUMMY_SP,
251                 },
252                 val: ConstVariableValue::Known { value },
253             },
254         );
255         Ok(value)
256     }
257
258     fn unify_integral_variable(
259         &self,
260         vid_is_expected: bool,
261         vid: ty::IntVid,
262         val: ty::IntVarValue,
263     ) -> RelateResult<'tcx, Ty<'tcx>> {
264         self.inner
265             .borrow_mut()
266             .int_unification_table()
267             .unify_var_value(vid, Some(val))
268             .map_err(|e| int_unification_error(vid_is_expected, e))?;
269         match val {
270             IntType(v) => Ok(self.tcx.mk_mach_int(v)),
271             UintType(v) => Ok(self.tcx.mk_mach_uint(v)),
272         }
273     }
274
275     fn unify_float_variable(
276         &self,
277         vid_is_expected: bool,
278         vid: ty::FloatVid,
279         val: ty::FloatTy,
280     ) -> RelateResult<'tcx, Ty<'tcx>> {
281         self.inner
282             .borrow_mut()
283             .float_unification_table()
284             .unify_var_value(vid, Some(ty::FloatVarValue(val)))
285             .map_err(|e| float_unification_error(vid_is_expected, e))?;
286         Ok(self.tcx.mk_mach_float(val))
287     }
288 }
289
290 impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
291     pub fn tcx(&self) -> TyCtxt<'tcx> {
292         self.infcx.tcx
293     }
294
295     pub fn equate<'a>(&'a mut self, a_is_expected: bool) -> Equate<'a, 'infcx, 'tcx> {
296         Equate::new(self, a_is_expected)
297     }
298
299     pub fn sub<'a>(&'a mut self, a_is_expected: bool) -> Sub<'a, 'infcx, 'tcx> {
300         Sub::new(self, a_is_expected)
301     }
302
303     pub fn lub<'a>(&'a mut self, a_is_expected: bool) -> Lub<'a, 'infcx, 'tcx> {
304         Lub::new(self, a_is_expected)
305     }
306
307     pub fn glb<'a>(&'a mut self, a_is_expected: bool) -> Glb<'a, 'infcx, 'tcx> {
308         Glb::new(self, a_is_expected)
309     }
310
311     /// Here, `dir` is either `EqTo`, `SubtypeOf`, or `SupertypeOf`.
312     /// The idea is that we should ensure that the type `a_ty` is equal
313     /// to, a subtype of, or a supertype of (respectively) the type
314     /// to which `b_vid` is bound.
315     ///
316     /// Since `b_vid` has not yet been instantiated with a type, we
317     /// will first instantiate `b_vid` with a *generalized* version
318     /// of `a_ty`. Generalization introduces other inference
319     /// variables wherever subtyping could occur.
320     #[instrument(skip(self), level = "debug")]
321     pub fn instantiate(
322         &mut self,
323         a_ty: Ty<'tcx>,
324         dir: RelationDir,
325         b_vid: ty::TyVid,
326         a_is_expected: bool,
327     ) -> RelateResult<'tcx, ()> {
328         use self::RelationDir::*;
329
330         // Get the actual variable that b_vid has been inferred to
331         debug_assert!(self.infcx.inner.borrow_mut().type_variables().probe(b_vid).is_unknown());
332
333         // Generalize type of `a_ty` appropriately depending on the
334         // direction.  As an example, assume:
335         //
336         // - `a_ty == &'x ?1`, where `'x` is some free region and `?1` is an
337         //   inference variable,
338         // - and `dir` == `SubtypeOf`.
339         //
340         // Then the generalized form `b_ty` would be `&'?2 ?3`, where
341         // `'?2` and `?3` are fresh region/type inference
342         // variables. (Down below, we will relate `a_ty <: b_ty`,
343         // adding constraints like `'x: '?2` and `?1 <: ?3`.)
344         let Generalization { ty: b_ty, needs_wf } = self.generalize(a_ty, b_vid, dir)?;
345         debug!(?b_ty);
346         self.infcx.inner.borrow_mut().type_variables().instantiate(b_vid, b_ty);
347
348         if needs_wf {
349             self.obligations.push(Obligation::new(
350                 self.trace.cause.clone(),
351                 self.param_env,
352                 ty::Binder::dummy(ty::PredicateKind::WellFormed(b_ty.into()))
353                     .to_predicate(self.infcx.tcx),
354             ));
355         }
356
357         // Finally, relate `b_ty` to `a_ty`, as described in previous comment.
358         //
359         // FIXME(#16847): This code is non-ideal because all these subtype
360         // relations wind up attributed to the same spans. We need
361         // to associate causes/spans with each of the relations in
362         // the stack to get this right.
363         match dir {
364             EqTo => self.equate(a_is_expected).relate(a_ty, b_ty),
365             SubtypeOf => self.sub(a_is_expected).relate(a_ty, b_ty),
366             SupertypeOf => self.sub(a_is_expected).relate_with_variance(
367                 ty::Contravariant,
368                 ty::VarianceDiagInfo::default(),
369                 a_ty,
370                 b_ty,
371             ),
372         }?;
373
374         Ok(())
375     }
376
377     /// Attempts to generalize `ty` for the type variable `for_vid`.
378     /// This checks for cycle -- that is, whether the type `ty`
379     /// references `for_vid`. The `dir` is the "direction" for which we
380     /// a performing the generalization (i.e., are we producing a type
381     /// that can be used as a supertype etc).
382     ///
383     /// Preconditions:
384     ///
385     /// - `for_vid` is a "root vid"
386     #[instrument(skip(self), level = "trace", ret)]
387     fn generalize(
388         &self,
389         ty: Ty<'tcx>,
390         for_vid: ty::TyVid,
391         dir: RelationDir,
392     ) -> RelateResult<'tcx, Generalization<'tcx>> {
393         // Determine the ambient variance within which `ty` appears.
394         // The surrounding equation is:
395         //
396         //     ty [op] ty2
397         //
398         // where `op` is either `==`, `<:`, or `:>`. This maps quite
399         // naturally.
400         let ambient_variance = match dir {
401             RelationDir::EqTo => ty::Invariant,
402             RelationDir::SubtypeOf => ty::Covariant,
403             RelationDir::SupertypeOf => ty::Contravariant,
404         };
405
406         trace!(?ambient_variance);
407
408         let for_universe = match self.infcx.inner.borrow_mut().type_variables().probe(for_vid) {
409             v @ TypeVariableValue::Known { .. } => {
410                 bug!("instantiating {:?} which has a known value {:?}", for_vid, v,)
411             }
412             TypeVariableValue::Unknown { universe } => universe,
413         };
414
415         trace!(?for_universe);
416         trace!(?self.trace);
417
418         let mut generalize = Generalizer {
419             infcx: self.infcx,
420             cause: &self.trace.cause,
421             for_vid_sub_root: self.infcx.inner.borrow_mut().type_variables().sub_root_var(for_vid),
422             for_universe,
423             ambient_variance,
424             needs_wf: false,
425             root_ty: ty,
426             param_env: self.param_env,
427             cache: SsoHashMap::new(),
428         };
429
430         let ty = generalize.relate(ty, ty)?;
431         let needs_wf = generalize.needs_wf;
432         Ok(Generalization { ty, needs_wf })
433     }
434
435     pub fn add_const_equate_obligation(
436         &mut self,
437         a_is_expected: bool,
438         a: ty::Const<'tcx>,
439         b: ty::Const<'tcx>,
440     ) {
441         let predicate = if a_is_expected {
442             ty::PredicateKind::ConstEquate(a, b)
443         } else {
444             ty::PredicateKind::ConstEquate(b, a)
445         };
446         self.obligations.push(Obligation::new(
447             self.trace.cause.clone(),
448             self.param_env,
449             ty::Binder::dummy(predicate).to_predicate(self.tcx()),
450         ));
451     }
452 }
453
454 struct Generalizer<'cx, 'tcx> {
455     infcx: &'cx InferCtxt<'tcx>,
456
457     /// The span, used when creating new type variables and things.
458     cause: &'cx ObligationCause<'tcx>,
459
460     /// The vid of the type variable that is in the process of being
461     /// instantiated; if we find this within the type we are folding,
462     /// that means we would have created a cyclic type.
463     for_vid_sub_root: ty::TyVid,
464
465     /// The universe of the type variable that is in the process of
466     /// being instantiated. Any fresh variables that we create in this
467     /// process should be in that same universe.
468     for_universe: ty::UniverseIndex,
469
470     /// Track the variance as we descend into the type.
471     ambient_variance: ty::Variance,
472
473     /// See the field `needs_wf` in `Generalization`.
474     needs_wf: bool,
475
476     /// The root type that we are generalizing. Used when reporting cycles.
477     root_ty: Ty<'tcx>,
478
479     param_env: ty::ParamEnv<'tcx>,
480
481     cache: SsoHashMap<Ty<'tcx>, Ty<'tcx>>,
482 }
483
484 /// Result from a generalization operation. This includes
485 /// not only the generalized type, but also a bool flag
486 /// indicating whether further WF checks are needed.
487 #[derive(Debug)]
488 struct Generalization<'tcx> {
489     ty: Ty<'tcx>,
490
491     /// If true, then the generalized type may not be well-formed,
492     /// even if the source type is well-formed, so we should add an
493     /// additional check to enforce that it is. This arises in
494     /// particular around 'bivariant' type parameters that are only
495     /// constrained by a where-clause. As an example, imagine a type:
496     ///
497     ///     struct Foo<A, B> where A: Iterator<Item = B> {
498     ///         data: A
499     ///     }
500     ///
501     /// here, `A` will be covariant, but `B` is
502     /// unconstrained. However, whatever it is, for `Foo` to be WF, it
503     /// must be equal to `A::Item`. If we have an input `Foo<?A, ?B>`,
504     /// then after generalization we will wind up with a type like
505     /// `Foo<?C, ?D>`. When we enforce that `Foo<?A, ?B> <: Foo<?C,
506     /// ?D>` (or `>:`), we will wind up with the requirement that `?A
507     /// <: ?C`, but no particular relationship between `?B` and `?D`
508     /// (after all, we do not know the variance of the normalized form
509     /// of `A::Item` with respect to `A`). If we do nothing else, this
510     /// may mean that `?D` goes unconstrained (as in #41677). So, in
511     /// this scenario where we create a new type variable in a
512     /// bivariant context, we set the `needs_wf` flag to true. This
513     /// will force the calling code to check that `WF(Foo<?C, ?D>)`
514     /// holds, which in turn implies that `?C::Item == ?D`. So once
515     /// `?C` is constrained, that should suffice to restrict `?D`.
516     needs_wf: bool,
517 }
518
519 impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
520     fn tcx(&self) -> TyCtxt<'tcx> {
521         self.infcx.tcx
522     }
523     fn param_env(&self) -> ty::ParamEnv<'tcx> {
524         self.param_env
525     }
526
527     fn tag(&self) -> &'static str {
528         "Generalizer"
529     }
530
531     fn a_is_expected(&self) -> bool {
532         true
533     }
534
535     fn binders<T>(
536         &mut self,
537         a: ty::Binder<'tcx, T>,
538         b: ty::Binder<'tcx, T>,
539     ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
540     where
541         T: Relate<'tcx>,
542     {
543         Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
544     }
545
546     fn relate_item_substs(
547         &mut self,
548         item_def_id: DefId,
549         a_subst: SubstsRef<'tcx>,
550         b_subst: SubstsRef<'tcx>,
551     ) -> RelateResult<'tcx, SubstsRef<'tcx>> {
552         if self.ambient_variance == ty::Variance::Invariant {
553             // Avoid fetching the variance if we are in an invariant
554             // context; no need, and it can induce dependency cycles
555             // (e.g., #41849).
556             relate::relate_substs(self, a_subst, b_subst)
557         } else {
558             let tcx = self.tcx();
559             let opt_variances = tcx.variances_of(item_def_id);
560             relate::relate_substs_with_variances(
561                 self,
562                 item_def_id,
563                 &opt_variances,
564                 a_subst,
565                 b_subst,
566             )
567         }
568     }
569
570     fn relate_with_variance<T: Relate<'tcx>>(
571         &mut self,
572         variance: ty::Variance,
573         _info: ty::VarianceDiagInfo<'tcx>,
574         a: T,
575         b: T,
576     ) -> RelateResult<'tcx, T> {
577         let old_ambient_variance = self.ambient_variance;
578         self.ambient_variance = self.ambient_variance.xform(variance);
579
580         let result = self.relate(a, b);
581         self.ambient_variance = old_ambient_variance;
582         result
583     }
584
585     fn tys(&mut self, t: Ty<'tcx>, t2: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
586         assert_eq!(t, t2); // we are abusing TypeRelation here; both LHS and RHS ought to be ==
587
588         if let Some(&result) = self.cache.get(&t) {
589             return Ok(result);
590         }
591         debug!("generalize: t={:?}", t);
592
593         // Check to see whether the type we are generalizing references
594         // any other type variable related to `vid` via
595         // subtyping. This is basically our "occurs check", preventing
596         // us from creating infinitely sized types.
597         let result = match *t.kind() {
598             ty::Infer(ty::TyVar(vid)) => {
599                 let vid = self.infcx.inner.borrow_mut().type_variables().root_var(vid);
600                 let sub_vid = self.infcx.inner.borrow_mut().type_variables().sub_root_var(vid);
601                 if sub_vid == self.for_vid_sub_root {
602                     // If sub-roots are equal, then `for_vid` and
603                     // `vid` are related via subtyping.
604                     Err(TypeError::CyclicTy(self.root_ty))
605                 } else {
606                     let probe = self.infcx.inner.borrow_mut().type_variables().probe(vid);
607                     match probe {
608                         TypeVariableValue::Known { value: u } => {
609                             debug!("generalize: known value {:?}", u);
610                             self.relate(u, u)
611                         }
612                         TypeVariableValue::Unknown { universe } => {
613                             match self.ambient_variance {
614                                 // Invariant: no need to make a fresh type variable.
615                                 ty::Invariant => {
616                                     if self.for_universe.can_name(universe) {
617                                         return Ok(t);
618                                     }
619                                 }
620
621                                 // Bivariant: make a fresh var, but we
622                                 // may need a WF predicate. See
623                                 // comment on `needs_wf` field for
624                                 // more info.
625                                 ty::Bivariant => self.needs_wf = true,
626
627                                 // Co/contravariant: this will be
628                                 // sufficiently constrained later on.
629                                 ty::Covariant | ty::Contravariant => (),
630                             }
631
632                             let origin =
633                                 *self.infcx.inner.borrow_mut().type_variables().var_origin(vid);
634                             let new_var_id = self
635                                 .infcx
636                                 .inner
637                                 .borrow_mut()
638                                 .type_variables()
639                                 .new_var(self.for_universe, origin);
640                             let u = self.tcx().mk_ty_var(new_var_id);
641
642                             // Record that we replaced `vid` with `new_var_id` as part of a generalization
643                             // operation. This is needed to detect cyclic types. To see why, see the
644                             // docs in the `type_variables` module.
645                             self.infcx.inner.borrow_mut().type_variables().sub(vid, new_var_id);
646                             debug!("generalize: replacing original vid={:?} with new={:?}", vid, u);
647                             Ok(u)
648                         }
649                     }
650                 }
651             }
652             ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => {
653                 // No matter what mode we are in,
654                 // integer/floating-point types must be equal to be
655                 // relatable.
656                 Ok(t)
657             }
658             _ => relate::super_relate_tys(self, t, t),
659         }?;
660
661         self.cache.insert(t, result);
662         Ok(result)
663     }
664
665     fn regions(
666         &mut self,
667         r: ty::Region<'tcx>,
668         r2: ty::Region<'tcx>,
669     ) -> RelateResult<'tcx, ty::Region<'tcx>> {
670         assert_eq!(r, r2); // we are abusing TypeRelation here; both LHS and RHS ought to be ==
671
672         debug!("generalize: regions r={:?}", r);
673
674         match *r {
675             // Never make variables for regions bound within the type itself,
676             // nor for erased regions.
677             ty::ReLateBound(..) | ty::ReErased => {
678                 return Ok(r);
679             }
680
681             ty::RePlaceholder(..)
682             | ty::ReVar(..)
683             | ty::ReStatic
684             | ty::ReEarlyBound(..)
685             | ty::ReFree(..) => {
686                 // see common code below
687             }
688         }
689
690         // If we are in an invariant context, we can re-use the region
691         // as is, unless it happens to be in some universe that we
692         // can't name. (In the case of a region *variable*, we could
693         // use it if we promoted it into our universe, but we don't
694         // bother.)
695         if let ty::Invariant = self.ambient_variance {
696             let r_universe = self.infcx.universe_of_region(r);
697             if self.for_universe.can_name(r_universe) {
698                 return Ok(r);
699             }
700         }
701
702         // FIXME: This is non-ideal because we don't give a
703         // very descriptive origin for this region variable.
704         Ok(self.infcx.next_region_var_in_universe(MiscVariable(self.cause.span), self.for_universe))
705     }
706
707     fn consts(
708         &mut self,
709         c: ty::Const<'tcx>,
710         c2: ty::Const<'tcx>,
711     ) -> RelateResult<'tcx, ty::Const<'tcx>> {
712         assert_eq!(c, c2); // we are abusing TypeRelation here; both LHS and RHS ought to be ==
713
714         match c.kind() {
715             ty::ConstKind::Infer(InferConst::Var(vid)) => {
716                 let mut inner = self.infcx.inner.borrow_mut();
717                 let variable_table = &mut inner.const_unification_table();
718                 let var_value = variable_table.probe_value(vid);
719                 match var_value.val {
720                     ConstVariableValue::Known { value: u } => {
721                         drop(inner);
722                         self.relate(u, u)
723                     }
724                     ConstVariableValue::Unknown { universe } => {
725                         if self.for_universe.can_name(universe) {
726                             Ok(c)
727                         } else {
728                             let new_var_id = variable_table.new_key(ConstVarValue {
729                                 origin: var_value.origin,
730                                 val: ConstVariableValue::Unknown { universe: self.for_universe },
731                             });
732                             Ok(self.tcx().mk_const_var(new_var_id, c.ty()))
733                         }
734                     }
735                 }
736             }
737             ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }) => {
738                 let substs = self.relate_with_variance(
739                     ty::Variance::Invariant,
740                     ty::VarianceDiagInfo::default(),
741                     substs,
742                     substs,
743                 )?;
744                 Ok(self.tcx().mk_const(ty::ConstS {
745                     ty: c.ty(),
746                     kind: ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }),
747                 }))
748             }
749             _ => relate::super_relate_consts(self, c, c),
750         }
751     }
752 }
753
754 pub trait ConstEquateRelation<'tcx>: TypeRelation<'tcx> {
755     /// Register an obligation that both constants must be equal to each other.
756     ///
757     /// If they aren't equal then the relation doesn't hold.
758     fn const_equate_obligation(&mut self, a: ty::Const<'tcx>, b: ty::Const<'tcx>);
759 }
760
761 fn int_unification_error<'tcx>(
762     a_is_expected: bool,
763     v: (ty::IntVarValue, ty::IntVarValue),
764 ) -> TypeError<'tcx> {
765     let (a, b) = v;
766     TypeError::IntMismatch(ExpectedFound::new(a_is_expected, a, b))
767 }
768
769 fn float_unification_error<'tcx>(
770     a_is_expected: bool,
771     v: (ty::FloatVarValue, ty::FloatVarValue),
772 ) -> TypeError<'tcx> {
773     let (ty::FloatVarValue(a), ty::FloatVarValue(b)) = v;
774     TypeError::FloatMismatch(ExpectedFound::new(a_is_expected, a, b))
775 }
776
777 struct ConstInferUnifier<'cx, 'tcx> {
778     infcx: &'cx InferCtxt<'tcx>,
779
780     span: Span,
781
782     param_env: ty::ParamEnv<'tcx>,
783
784     for_universe: ty::UniverseIndex,
785
786     /// The vid of the const variable that is in the process of being
787     /// instantiated; if we find this within the const we are folding,
788     /// that means we would have created a cyclic const.
789     target_vid: ty::ConstVid<'tcx>,
790 }
791
792 // We use `TypeRelation` here to propagate `RelateResult` upwards.
793 //
794 // Both inputs are expected to be the same.
795 impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
796     fn tcx(&self) -> TyCtxt<'tcx> {
797         self.infcx.tcx
798     }
799
800     fn param_env(&self) -> ty::ParamEnv<'tcx> {
801         self.param_env
802     }
803
804     fn tag(&self) -> &'static str {
805         "ConstInferUnifier"
806     }
807
808     fn a_is_expected(&self) -> bool {
809         true
810     }
811
812     fn relate_with_variance<T: Relate<'tcx>>(
813         &mut self,
814         _variance: ty::Variance,
815         _info: ty::VarianceDiagInfo<'tcx>,
816         a: T,
817         b: T,
818     ) -> RelateResult<'tcx, T> {
819         // We don't care about variance here.
820         self.relate(a, b)
821     }
822
823     fn binders<T>(
824         &mut self,
825         a: ty::Binder<'tcx, T>,
826         b: ty::Binder<'tcx, T>,
827     ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
828     where
829         T: Relate<'tcx>,
830     {
831         Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
832     }
833
834     #[instrument(level = "debug", skip(self), ret)]
835     fn tys(&mut self, t: Ty<'tcx>, _t: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
836         debug_assert_eq!(t, _t);
837
838         match t.kind() {
839             &ty::Infer(ty::TyVar(vid)) => {
840                 let vid = self.infcx.inner.borrow_mut().type_variables().root_var(vid);
841                 let probe = self.infcx.inner.borrow_mut().type_variables().probe(vid);
842                 match probe {
843                     TypeVariableValue::Known { value: u } => {
844                         debug!("ConstOccursChecker: known value {:?}", u);
845                         self.tys(u, u)
846                     }
847                     TypeVariableValue::Unknown { universe } => {
848                         if self.for_universe.can_name(universe) {
849                             return Ok(t);
850                         }
851
852                         let origin =
853                             *self.infcx.inner.borrow_mut().type_variables().var_origin(vid);
854                         let new_var_id = self
855                             .infcx
856                             .inner
857                             .borrow_mut()
858                             .type_variables()
859                             .new_var(self.for_universe, origin);
860                         Ok(self.tcx().mk_ty_var(new_var_id))
861                     }
862                 }
863             }
864             ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => Ok(t),
865             _ => relate::super_relate_tys(self, t, t),
866         }
867     }
868
869     fn regions(
870         &mut self,
871         r: ty::Region<'tcx>,
872         _r: ty::Region<'tcx>,
873     ) -> RelateResult<'tcx, ty::Region<'tcx>> {
874         debug_assert_eq!(r, _r);
875         debug!("ConstInferUnifier: r={:?}", r);
876
877         match *r {
878             // Never make variables for regions bound within the type itself,
879             // nor for erased regions.
880             ty::ReLateBound(..) | ty::ReErased => {
881                 return Ok(r);
882             }
883
884             ty::RePlaceholder(..)
885             | ty::ReVar(..)
886             | ty::ReStatic
887             | ty::ReEarlyBound(..)
888             | ty::ReFree(..) => {
889                 // see common code below
890             }
891         }
892
893         let r_universe = self.infcx.universe_of_region(r);
894         if self.for_universe.can_name(r_universe) {
895             return Ok(r);
896         } else {
897             // FIXME: This is non-ideal because we don't give a
898             // very descriptive origin for this region variable.
899             Ok(self.infcx.next_region_var_in_universe(MiscVariable(self.span), self.for_universe))
900         }
901     }
902
903     #[instrument(level = "debug", skip(self))]
904     fn consts(
905         &mut self,
906         c: ty::Const<'tcx>,
907         _c: ty::Const<'tcx>,
908     ) -> RelateResult<'tcx, ty::Const<'tcx>> {
909         debug_assert_eq!(c, _c);
910
911         match c.kind() {
912             ty::ConstKind::Infer(InferConst::Var(vid)) => {
913                 // Check if the current unification would end up
914                 // unifying `target_vid` with a const which contains
915                 // an inference variable which is unioned with `target_vid`.
916                 //
917                 // Not doing so can easily result in stack overflows.
918                 if self
919                     .infcx
920                     .inner
921                     .borrow_mut()
922                     .const_unification_table()
923                     .unioned(self.target_vid, vid)
924                 {
925                     return Err(TypeError::CyclicConst(c));
926                 }
927
928                 let var_value =
929                     self.infcx.inner.borrow_mut().const_unification_table().probe_value(vid);
930                 match var_value.val {
931                     ConstVariableValue::Known { value: u } => self.consts(u, u),
932                     ConstVariableValue::Unknown { universe } => {
933                         if self.for_universe.can_name(universe) {
934                             Ok(c)
935                         } else {
936                             let new_var_id =
937                                 self.infcx.inner.borrow_mut().const_unification_table().new_key(
938                                     ConstVarValue {
939                                         origin: var_value.origin,
940                                         val: ConstVariableValue::Unknown {
941                                             universe: self.for_universe,
942                                         },
943                                     },
944                                 );
945                             Ok(self.tcx().mk_const_var(new_var_id, c.ty()))
946                         }
947                     }
948                 }
949             }
950             ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }) => {
951                 let substs = self.relate_with_variance(
952                     ty::Variance::Invariant,
953                     ty::VarianceDiagInfo::default(),
954                     substs,
955                     substs,
956                 )?;
957
958                 Ok(self.tcx().mk_const(ty::ConstS {
959                     ty: c.ty(),
960                     kind: ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }),
961                 }))
962             }
963             _ => relate::super_relate_consts(self, c, c),
964         }
965     }
966 }