1 use rustc_infer::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
2 use rustc_infer::infer::NllRegionVariableOrigin;
3 use rustc_middle::mir::ConstraintCategory;
4 use rustc_middle::ty::relate::TypeRelation;
5 use rustc_middle::ty::{self, Const, Ty};
6 use rustc_trait_selection::traits::query::Fallible;
8 use crate::constraints::OutlivesConstraint;
9 use crate::diagnostics::UniverseInfo;
10 use crate::type_check::{Locations, TypeChecker};
12 /// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
14 /// - "Covariant" `a <: b`
15 /// - "Invariant" `a == b`
16 /// - "Contravariant" `a :> b`
18 /// N.B., the type `a` is permitted to have unresolved inference
19 /// variables, but not the type `b`.
20 #[instrument(skip(type_checker), level = "debug")]
21 pub(super) fn relate_types<'tcx>(
22 type_checker: &mut TypeChecker<'_, 'tcx>,
27 category: ConstraintCategory,
31 NllTypeRelatingDelegate::new(type_checker, locations, category, UniverseInfo::relate(a, b)),
38 struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
39 type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
41 /// Where (and why) is this relation taking place?
44 /// What category do we assign the resulting `'a: 'b` relationships?
45 category: ConstraintCategory,
47 /// Information so that error reporting knows what types we are relating
48 /// when reporting a bound region error.
49 universe_info: UniverseInfo<'tcx>,
52 impl NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
54 type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
56 category: ConstraintCategory,
57 universe_info: UniverseInfo<'tcx>,
59 Self { type_checker, locations, category, universe_info }
63 impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
64 fn param_env(&self) -> ty::ParamEnv<'tcx> {
65 self.type_checker.param_env
68 fn create_next_universe(&mut self) -> ty::UniverseIndex {
69 let universe = self.type_checker.infcx.create_next_universe();
74 .insert(universe, self.universe_info.clone());
78 fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> {
79 let origin = NllRegionVariableOrigin::Existential { from_forall };
80 self.type_checker.infcx.next_nll_region_var(origin)
83 fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
87 .placeholder_region(self.type_checker.infcx, placeholder)
90 fn generalize_existential(&mut self, universe: ty::UniverseIndex) -> ty::Region<'tcx> {
91 self.type_checker.infcx.next_nll_region_var_in_universe(
92 NllRegionVariableOrigin::Existential { from_forall: false },
99 sup: ty::Region<'tcx>,
100 sub: ty::Region<'tcx>,
101 info: ty::VarianceDiagInfo<'tcx>,
103 let sub = self.type_checker.borrowck_context.universal_regions.to_region_vid(sub);
104 let sup = self.type_checker.borrowck_context.universal_regions.to_region_vid(sup);
105 self.type_checker.borrowck_context.constraints.outlives_constraints.push(
109 locations: self.locations,
110 category: self.category,
116 // We don't have to worry about the equality of consts during borrow checking
117 // as consts always have a static lifetime.
118 fn const_equate(&mut self, _a: &'tcx Const<'tcx>, _b: &'tcx Const<'tcx>) {}
120 fn normalization() -> NormalizationStrategy {
121 NormalizationStrategy::Eager
124 fn forbid_inference_vars() -> bool {