1 use rustc_infer::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
2 use rustc_infer::infer::{InferCtxt, 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::{BorrowCheckContext, Locations};
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(infcx, param_env, borrowck_context), level = "debug")]
21 pub(super) fn relate_types<'tcx>(
22 infcx: &InferCtxt<'_, 'tcx>,
23 param_env: ty::ParamEnv<'tcx>,
28 category: ConstraintCategory,
29 borrowck_context: &mut BorrowCheckContext<'_, 'tcx>,
33 NllTypeRelatingDelegate::new(
39 UniverseInfo::relate(a, b),
47 struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
48 infcx: &'me InferCtxt<'me, 'tcx>,
49 borrowck_context: &'me mut BorrowCheckContext<'bccx, 'tcx>,
51 param_env: ty::ParamEnv<'tcx>,
53 /// Where (and why) is this relation taking place?
56 /// What category do we assign the resulting `'a: 'b` relationships?
57 category: ConstraintCategory,
59 /// Information so that error reporting knows what types we are relating
60 /// when reporting a bound region error.
61 universe_info: UniverseInfo<'tcx>,
64 impl NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
66 infcx: &'me InferCtxt<'me, 'tcx>,
67 borrowck_context: &'me mut BorrowCheckContext<'bccx, 'tcx>,
68 param_env: ty::ParamEnv<'tcx>,
70 category: ConstraintCategory,
71 universe_info: UniverseInfo<'tcx>,
73 Self { infcx, borrowck_context, param_env, locations, category, universe_info }
77 impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
78 fn param_env(&self) -> ty::ParamEnv<'tcx> {
82 fn create_next_universe(&mut self) -> ty::UniverseIndex {
83 let universe = self.infcx.create_next_universe();
87 .insert(universe, self.universe_info.clone());
91 fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> {
92 let origin = NllRegionVariableOrigin::Existential { from_forall };
93 self.infcx.next_nll_region_var(origin)
96 fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
97 self.borrowck_context.constraints.placeholder_region(self.infcx, placeholder)
100 fn generalize_existential(&mut self, universe: ty::UniverseIndex) -> ty::Region<'tcx> {
101 self.infcx.next_nll_region_var_in_universe(
102 NllRegionVariableOrigin::Existential { from_forall: false },
109 sup: ty::Region<'tcx>,
110 sub: ty::Region<'tcx>,
111 info: ty::VarianceDiagInfo<'tcx>,
113 let sub = self.borrowck_context.universal_regions.to_region_vid(sub);
114 let sup = self.borrowck_context.universal_regions.to_region_vid(sup);
115 self.borrowck_context.constraints.outlives_constraints.push(OutlivesConstraint {
118 locations: self.locations,
119 category: self.category,
124 // We don't have to worry about the equality of consts during borrow checking
125 // as consts always have a static lifetime.
126 fn const_equate(&mut self, _a: &'tcx Const<'tcx>, _b: &'tcx Const<'tcx>) {}
128 fn normalization() -> NormalizationStrategy {
129 NormalizationStrategy::Eager
132 fn forbid_inference_vars() -> bool {