rustc_borrowck/type_check/
relate_tys.rs

1use rustc_data_structures::fx::FxHashMap;
2use rustc_errors::ErrorGuaranteed;
3use rustc_infer::infer::relate::{
4    PredicateEmittingRelation, Relate, RelateResult, StructurallyRelateAliases, TypeRelation,
5};
6use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
7use rustc_infer::traits::Obligation;
8use rustc_infer::traits::solve::Goal;
9use rustc_middle::mir::ConstraintCategory;
10use rustc_middle::traits::ObligationCause;
11use rustc_middle::traits::query::NoSolution;
12use rustc_middle::ty::relate::combine::{super_combine_consts, super_combine_tys};
13use rustc_middle::ty::{self, FnMutDelegate, Ty, TyCtxt, TypeVisitableExt};
14use rustc_middle::{bug, span_bug};
15use rustc_span::{Span, Symbol, sym};
16use tracing::{debug, instrument};
17
18use crate::constraints::OutlivesConstraint;
19use crate::diagnostics::UniverseInfo;
20use crate::renumber::RegionCtxt;
21use crate::type_check::{InstantiateOpaqueType, Locations, TypeChecker};
22
23impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24    /// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
25    ///
26    /// - "Covariant" `a <: b`
27    /// - "Invariant" `a == b`
28    /// - "Contravariant" `a :> b`
29    ///
30    /// N.B., the type `a` is permitted to have unresolved inference
31    /// variables, but not the type `b`.
32    #[instrument(skip(self), level = "debug")]
33    pub(super) fn relate_types(
34        &mut self,
35        a: Ty<'tcx>,
36        v: ty::Variance,
37        b: Ty<'tcx>,
38        locations: Locations,
39        category: ConstraintCategory<'tcx>,
40    ) -> Result<(), NoSolution> {
41        NllTypeRelating::new(self, locations, category, UniverseInfo::relate(a, b), v)
42            .relate(a, b)?;
43        Ok(())
44    }
45
46    /// Add sufficient constraints to ensure `a == b`. See also [Self::relate_types].
47    pub(super) fn eq_args(
48        &mut self,
49        a: ty::GenericArgsRef<'tcx>,
50        b: ty::GenericArgsRef<'tcx>,
51        locations: Locations,
52        category: ConstraintCategory<'tcx>,
53    ) -> Result<(), NoSolution> {
54        NllTypeRelating::new(self, locations, category, UniverseInfo::other(), ty::Invariant)
55            .relate(a, b)?;
56        Ok(())
57    }
58}
59
60struct NllTypeRelating<'a, 'b, 'tcx> {
61    type_checker: &'a mut TypeChecker<'b, 'tcx>,
62
63    /// Where (and why) is this relation taking place?
64    locations: Locations,
65
66    /// What category do we assign the resulting `'a: 'b` relationships?
67    category: ConstraintCategory<'tcx>,
68
69    /// Information so that error reporting knows what types we are relating
70    /// when reporting a bound region error.
71    universe_info: UniverseInfo<'tcx>,
72
73    /// How are we relating `a` and `b`?
74    ///
75    /// - Covariant means `a <: b`.
76    /// - Contravariant means `b <: a`.
77    /// - Invariant means `a == b`.
78    /// - Bivariant means that it doesn't matter.
79    ambient_variance: ty::Variance,
80
81    ambient_variance_info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
82}
83
84impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
85    fn new(
86        type_checker: &'a mut TypeChecker<'b, 'tcx>,
87        locations: Locations,
88        category: ConstraintCategory<'tcx>,
89        universe_info: UniverseInfo<'tcx>,
90        ambient_variance: ty::Variance,
91    ) -> Self {
92        Self {
93            type_checker,
94            locations,
95            category,
96            universe_info,
97            ambient_variance,
98            ambient_variance_info: ty::VarianceDiagInfo::default(),
99        }
100    }
101
102    fn ambient_covariance(&self) -> bool {
103        match self.ambient_variance {
104            ty::Covariant | ty::Invariant => true,
105            ty::Contravariant | ty::Bivariant => false,
106        }
107    }
108
109    fn ambient_contravariance(&self) -> bool {
110        match self.ambient_variance {
111            ty::Contravariant | ty::Invariant => true,
112            ty::Covariant | ty::Bivariant => false,
113        }
114    }
115
116    fn relate_opaques(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
117        let infcx = self.type_checker.infcx;
118        debug_assert!(!infcx.next_trait_solver());
119        // `handle_opaque_type` cannot handle subtyping, so to support subtyping
120        // we instead eagerly generalize here. This is a bit of a mess but will go
121        // away once we're using the new solver.
122        //
123        // Given `opaque rel B`, we create a new infer var `ty_vid` constrain it
124        // by using `ty_vid rel B` and then finally and end by equating `ty_vid` to
125        // the opaque.
126        let mut enable_subtyping = |ty, opaque_is_expected| {
127            let ty_vid = infcx.next_ty_var_id_in_universe(self.span(), ty::UniverseIndex::ROOT);
128
129            let variance = if opaque_is_expected {
130                self.ambient_variance
131            } else {
132                self.ambient_variance.xform(ty::Contravariant)
133            };
134
135            self.type_checker.infcx.instantiate_ty_var(
136                self,
137                opaque_is_expected,
138                ty_vid,
139                variance,
140                ty,
141            )?;
142            Ok(infcx.resolve_vars_if_possible(Ty::new_infer(infcx.tcx, ty::TyVar(ty_vid))))
143        };
144
145        let (a, b) = match (a.kind(), b.kind()) {
146            (&ty::Alias(ty::Opaque, ..), _) => (a, enable_subtyping(b, true)?),
147            (_, &ty::Alias(ty::Opaque, ..)) => (enable_subtyping(a, false)?, b),
148            _ => unreachable!(
149                "expected at least one opaque type in `relate_opaques`, got {a} and {b}."
150            ),
151        };
152        self.register_goals(infcx.handle_opaque_type(a, b, self.span(), self.param_env())?);
153        Ok(())
154    }
155
156    fn enter_forall<T, U>(
157        &mut self,
158        binder: ty::Binder<'tcx, T>,
159        f: impl FnOnce(&mut Self, T) -> U,
160    ) -> U
161    where
162        T: ty::TypeFoldable<TyCtxt<'tcx>> + Copy,
163    {
164        let value = if let Some(inner) = binder.no_bound_vars() {
165            inner
166        } else {
167            let infcx = self.type_checker.infcx;
168            let mut lazy_universe = None;
169            let delegate = FnMutDelegate {
170                regions: &mut |br: ty::BoundRegion| {
171                    // The first time this closure is called, create a
172                    // new universe for the placeholders we will make
173                    // from here out.
174                    let universe = lazy_universe.unwrap_or_else(|| {
175                        let universe = self.create_next_universe();
176                        lazy_universe = Some(universe);
177                        universe
178                    });
179
180                    let placeholder = ty::PlaceholderRegion { universe, bound: br };
181                    debug!(?placeholder);
182                    let placeholder_reg = self.next_placeholder_region(placeholder);
183                    debug!(?placeholder_reg);
184
185                    placeholder_reg
186                },
187                types: &mut |_bound_ty: ty::BoundTy| {
188                    unreachable!("we only replace regions in nll_relate, not types")
189                },
190                consts: &mut |_bound_const: ty::BoundConst| {
191                    unreachable!("we only replace regions in nll_relate, not consts")
192                },
193            };
194
195            infcx.tcx.replace_bound_vars_uncached(binder, delegate)
196        };
197
198        debug!(?value);
199        f(self, value)
200    }
201
202    #[instrument(skip(self), level = "debug")]
203    fn instantiate_binder_with_existentials<T>(&mut self, binder: ty::Binder<'tcx, T>) -> T
204    where
205        T: ty::TypeFoldable<TyCtxt<'tcx>> + Copy,
206    {
207        if let Some(inner) = binder.no_bound_vars() {
208            return inner;
209        }
210
211        let infcx = self.type_checker.infcx;
212        let mut reg_map = FxHashMap::default();
213        let delegate = FnMutDelegate {
214            regions: &mut |br: ty::BoundRegion| {
215                if let Some(ex_reg_var) = reg_map.get(&br) {
216                    *ex_reg_var
217                } else {
218                    let ex_reg_var =
219                        self.next_existential_region_var(br.kind.get_name(infcx.infcx.tcx));
220                    debug!(?ex_reg_var);
221                    reg_map.insert(br, ex_reg_var);
222
223                    ex_reg_var
224                }
225            },
226            types: &mut |_bound_ty: ty::BoundTy| {
227                unreachable!("we only replace regions in nll_relate, not types")
228            },
229            consts: &mut |_bound_const: ty::BoundConst| {
230                unreachable!("we only replace regions in nll_relate, not consts")
231            },
232        };
233
234        let replaced = infcx.tcx.replace_bound_vars_uncached(binder, delegate);
235        debug!(?replaced);
236
237        replaced
238    }
239
240    fn create_next_universe(&mut self) -> ty::UniverseIndex {
241        let universe = self.type_checker.infcx.create_next_universe();
242        self.type_checker.constraints.universe_causes.insert(universe, self.universe_info.clone());
243        universe
244    }
245
246    #[instrument(skip(self), level = "debug")]
247    fn next_existential_region_var(&mut self, name: Option<Symbol>) -> ty::Region<'tcx> {
248        let origin = NllRegionVariableOrigin::Existential { name };
249        self.type_checker.infcx.next_nll_region_var(origin, || RegionCtxt::Existential(name))
250    }
251
252    #[instrument(skip(self), level = "debug")]
253    fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
254        let reg =
255            self.type_checker.constraints.placeholder_region(self.type_checker.infcx, placeholder);
256
257        let reg_info = match placeholder.bound.kind {
258            ty::BoundRegionKind::Anon => sym::anon,
259            ty::BoundRegionKind::Named(def_id) => self.type_checker.tcx().item_name(def_id),
260            ty::BoundRegionKind::ClosureEnv => sym::env,
261            ty::BoundRegionKind::NamedAnon(_) => bug!("only used for pretty printing"),
262        };
263
264        if cfg!(debug_assertions) {
265            let mut var_to_origin = self.type_checker.infcx.reg_var_to_origin.borrow_mut();
266            let new = RegionCtxt::Placeholder(reg_info);
267            let prev = var_to_origin.insert(reg.as_var(), new);
268            if let Some(prev) = prev {
269                assert_eq!(new, prev);
270            }
271        }
272
273        reg
274    }
275
276    fn push_outlives(
277        &mut self,
278        sup: ty::Region<'tcx>,
279        sub: ty::Region<'tcx>,
280        info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
281    ) {
282        let sub = self.type_checker.universal_regions.to_region_vid(sub);
283        let sup = self.type_checker.universal_regions.to_region_vid(sup);
284        self.type_checker.constraints.outlives_constraints.push(OutlivesConstraint {
285            sup,
286            sub,
287            locations: self.locations,
288            span: self.locations.span(self.type_checker.body),
289            category: self.category,
290            variance_info: info,
291            from_closure: false,
292        });
293    }
294}
295
296impl<'b, 'tcx> TypeRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'b, 'tcx> {
297    fn cx(&self) -> TyCtxt<'tcx> {
298        self.type_checker.infcx.tcx
299    }
300
301    #[instrument(skip(self, info), level = "trace", ret)]
302    fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
303        &mut self,
304        variance: ty::Variance,
305        info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
306        a: T,
307        b: T,
308    ) -> RelateResult<'tcx, T> {
309        let old_ambient_variance = self.ambient_variance;
310        self.ambient_variance = self.ambient_variance.xform(variance);
311        self.ambient_variance_info = self.ambient_variance_info.xform(info);
312
313        debug!(?self.ambient_variance);
314        // In a bivariant context this always succeeds.
315        let r = if self.ambient_variance == ty::Bivariant { Ok(a) } else { self.relate(a, b) };
316
317        self.ambient_variance = old_ambient_variance;
318
319        r
320    }
321
322    #[instrument(skip(self), level = "debug")]
323    fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
324        let infcx = self.type_checker.infcx;
325
326        let a = self.type_checker.infcx.shallow_resolve(a);
327        assert!(!b.has_non_region_infer(), "unexpected inference var {:?}", b);
328
329        if a == b {
330            return Ok(a);
331        }
332
333        match (a.kind(), b.kind()) {
334            (_, &ty::Infer(ty::TyVar(_))) => {
335                span_bug!(
336                    self.span(),
337                    "should not be relating type variables on the right in MIR typeck"
338                );
339            }
340
341            (&ty::Infer(ty::TyVar(a_vid)), _) => {
342                infcx.instantiate_ty_var(self, true, a_vid, self.ambient_variance, b)?
343            }
344
345            (
346                &ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }),
347                &ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }),
348            ) if a_def_id == b_def_id || infcx.next_trait_solver() => {
349                super_combine_tys(&infcx.infcx, self, a, b).map(|_| ()).or_else(|err| {
350                    // This behavior is only there for the old solver, the new solver
351                    // shouldn't ever fail. Instead, it unconditionally emits an
352                    // alias-relate goal.
353                    assert!(!self.type_checker.infcx.next_trait_solver());
354                    self.cx().dcx().span_delayed_bug(
355                        self.span(),
356                        "failure to relate an opaque to itself should result in an error later on",
357                    );
358                    if a_def_id.is_local() { self.relate_opaques(a, b) } else { Err(err) }
359                })?;
360            }
361            (&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
362            | (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
363                if def_id.is_local() && !self.type_checker.infcx.next_trait_solver() =>
364            {
365                self.relate_opaques(a, b)?;
366            }
367
368            _ => {
369                debug!(?a, ?b, ?self.ambient_variance);
370
371                // Will also handle unification of `IntVar` and `FloatVar`.
372                super_combine_tys(&self.type_checker.infcx.infcx, self, a, b)?;
373            }
374        }
375
376        Ok(a)
377    }
378
379    #[instrument(skip(self), level = "trace")]
380    fn regions(
381        &mut self,
382        a: ty::Region<'tcx>,
383        b: ty::Region<'tcx>,
384    ) -> RelateResult<'tcx, ty::Region<'tcx>> {
385        debug!(?self.ambient_variance);
386
387        if self.ambient_covariance() {
388            // Covariant: &'a u8 <: &'b u8. Hence, `'a: 'b`.
389            self.push_outlives(a, b, self.ambient_variance_info);
390        }
391
392        if self.ambient_contravariance() {
393            // Contravariant: &'b u8 <: &'a u8. Hence, `'b: 'a`.
394            self.push_outlives(b, a, self.ambient_variance_info);
395        }
396
397        Ok(a)
398    }
399
400    fn consts(
401        &mut self,
402        a: ty::Const<'tcx>,
403        b: ty::Const<'tcx>,
404    ) -> RelateResult<'tcx, ty::Const<'tcx>> {
405        let a = self.type_checker.infcx.shallow_resolve_const(a);
406        assert!(!a.has_non_region_infer(), "unexpected inference var {:?}", a);
407        assert!(!b.has_non_region_infer(), "unexpected inference var {:?}", b);
408
409        super_combine_consts(&self.type_checker.infcx.infcx, self, a, b)
410    }
411
412    #[instrument(skip(self), level = "trace")]
413    fn binders<T>(
414        &mut self,
415        a: ty::Binder<'tcx, T>,
416        b: ty::Binder<'tcx, T>,
417    ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
418    where
419        T: Relate<TyCtxt<'tcx>>,
420    {
421        // We want that
422        //
423        // ```
424        // for<'a> fn(&'a u32) -> &'a u32 <:
425        //   fn(&'b u32) -> &'b u32
426        // ```
427        //
428        // but not
429        //
430        // ```
431        // fn(&'a u32) -> &'a u32 <:
432        //   for<'b> fn(&'b u32) -> &'b u32
433        // ```
434        //
435        // We therefore proceed as follows:
436        //
437        // - Instantiate binders on `b` universally, yielding a universe U1.
438        // - Instantiate binders on `a` existentially in U1.
439
440        debug!(?self.ambient_variance);
441
442        if let (Some(a), Some(b)) = (a.no_bound_vars(), b.no_bound_vars()) {
443            // Fast path for the common case.
444            self.relate(a, b)?;
445            return Ok(ty::Binder::dummy(a));
446        }
447
448        match self.ambient_variance {
449            ty::Covariant => {
450                // Covariance, so we want `for<..> A <: for<..> B` --
451                // therefore we compare any instantiation of A (i.e., A
452                // instantiated with existentials) against every
453                // instantiation of B (i.e., B instantiated with
454                // universals).
455
456                // Note: the order here is important. Create the placeholders first, otherwise
457                // we assign the wrong universe to the existential!
458                self.enter_forall(b, |this, b| {
459                    let a = this.instantiate_binder_with_existentials(a);
460                    this.relate(a, b)
461                })?;
462            }
463
464            ty::Contravariant => {
465                // Contravariance, so we want `for<..> A :> for<..> B` --
466                // therefore we compare every instantiation of A (i.e., A
467                // instantiated with universals) against any
468                // instantiation of B (i.e., B instantiated with
469                // existentials). Opposite of above.
470
471                // Note: the order here is important. Create the placeholders first, otherwise
472                // we assign the wrong universe to the existential!
473                self.enter_forall(a, |this, a| {
474                    let b = this.instantiate_binder_with_existentials(b);
475                    this.relate(a, b)
476                })?;
477            }
478
479            ty::Invariant => {
480                // Invariant, so we want `for<..> A == for<..> B` --
481                // therefore we want `exists<..> A == for<..> B` and
482                // `exists<..> B == for<..> A`.
483                //
484                // See the comment in `fn Equate::binders` for more details.
485
486                // Note: the order here is important. Create the placeholders first, otherwise
487                // we assign the wrong universe to the existential!
488                self.enter_forall(b, |this, b| {
489                    let a = this.instantiate_binder_with_existentials(a);
490                    this.relate(a, b)
491                })?;
492                // Note: the order here is important. Create the placeholders first, otherwise
493                // we assign the wrong universe to the existential!
494                self.enter_forall(a, |this, a| {
495                    let b = this.instantiate_binder_with_existentials(b);
496                    this.relate(a, b)
497                })?;
498            }
499
500            ty::Bivariant => {}
501        }
502
503        Ok(a)
504    }
505}
506
507impl<'b, 'tcx> PredicateEmittingRelation<InferCtxt<'tcx>> for NllTypeRelating<'_, 'b, 'tcx> {
508    fn span(&self) -> Span {
509        self.locations.span(self.type_checker.body)
510    }
511
512    fn structurally_relate_aliases(&self) -> StructurallyRelateAliases {
513        StructurallyRelateAliases::No
514    }
515
516    fn param_env(&self) -> ty::ParamEnv<'tcx> {
517        self.type_checker.infcx.param_env
518    }
519
520    fn register_predicates(
521        &mut self,
522        obligations: impl IntoIterator<Item: ty::Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
523    ) {
524        let tcx = self.cx();
525        let param_env = self.param_env();
526        self.register_goals(
527            obligations.into_iter().map(|to_pred| Goal::new(tcx, param_env, to_pred)),
528        );
529    }
530
531    fn register_goals(
532        &mut self,
533        obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
534    ) {
535        let _: Result<_, ErrorGuaranteed> = self.type_checker.fully_perform_op(
536            self.locations,
537            self.category,
538            InstantiateOpaqueType {
539                obligations: obligations
540                    .into_iter()
541                    .map(|goal| {
542                        Obligation::new(
543                            self.cx(),
544                            ObligationCause::dummy_with_span(self.span()),
545                            goal.param_env,
546                            goal.predicate,
547                        )
548                    })
549                    .collect(),
550                // These fields are filled in during execution of the operation
551                base_universe: None,
552                region_constraints: None,
553            },
554        );
555    }
556
557    fn register_alias_relate_predicate(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) {
558        self.register_predicates([ty::Binder::dummy(match self.ambient_variance {
559            ty::Covariant => ty::PredicateKind::AliasRelate(
560                a.into(),
561                b.into(),
562                ty::AliasRelationDirection::Subtype,
563            ),
564            // a :> b is b <: a
565            ty::Contravariant => ty::PredicateKind::AliasRelate(
566                b.into(),
567                a.into(),
568                ty::AliasRelationDirection::Subtype,
569            ),
570            ty::Invariant => ty::PredicateKind::AliasRelate(
571                a.into(),
572                b.into(),
573                ty::AliasRelationDirection::Equate,
574            ),
575            ty::Bivariant => {
576                unreachable!("cannot defer an alias-relate goal with Bivariant variance (yet?)")
577            }
578        })]);
579    }
580}