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 impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13 /// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
15 /// - "Covariant" `a <: b`
16 /// - "Invariant" `a == b`
17 /// - "Contravariant" `a :> b`
19 /// N.B., the type `a` is permitted to have unresolved inference
20 /// variables, but not the type `b`.
21 #[instrument(skip(self), level = "debug")]
22 pub(super) fn relate_types(
28 category: ConstraintCategory,
32 NllTypeRelatingDelegate::new(self, locations, category, UniverseInfo::relate(a, b)),
40 struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
41 type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
43 /// Where (and why) is this relation taking place?
46 /// What category do we assign the resulting `'a: 'b` relationships?
47 category: ConstraintCategory,
49 /// Information so that error reporting knows what types we are relating
50 /// when reporting a bound region error.
51 universe_info: UniverseInfo<'tcx>,
54 impl NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
56 type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
58 category: ConstraintCategory,
59 universe_info: UniverseInfo<'tcx>,
61 Self { type_checker, locations, category, universe_info }
65 impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
66 fn param_env(&self) -> ty::ParamEnv<'tcx> {
67 self.type_checker.param_env
70 fn create_next_universe(&mut self) -> ty::UniverseIndex {
71 let universe = self.type_checker.infcx.create_next_universe();
76 .insert(universe, self.universe_info.clone());
80 fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> {
81 let origin = NllRegionVariableOrigin::Existential { from_forall };
82 self.type_checker.infcx.next_nll_region_var(origin)
85 fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
89 .placeholder_region(self.type_checker.infcx, placeholder)
92 fn generalize_existential(&mut self, universe: ty::UniverseIndex) -> ty::Region<'tcx> {
93 self.type_checker.infcx.next_nll_region_var_in_universe(
94 NllRegionVariableOrigin::Existential { from_forall: false },
101 sup: ty::Region<'tcx>,
102 sub: ty::Region<'tcx>,
103 info: ty::VarianceDiagInfo<'tcx>,
105 let sub = self.type_checker.borrowck_context.universal_regions.to_region_vid(sub);
106 let sup = self.type_checker.borrowck_context.universal_regions.to_region_vid(sup);
107 self.type_checker.borrowck_context.constraints.outlives_constraints.push(
111 locations: self.locations,
112 category: self.category,
118 // We don't have to worry about the equality of consts during borrow checking
119 // as consts always have a static lifetime.
120 fn const_equate(&mut self, _a: &'tcx Const<'tcx>, _b: &'tcx Const<'tcx>) {}
122 fn normalization() -> NormalizationStrategy {
123 NormalizationStrategy::Eager
126 fn forbid_inference_vars() -> bool {