1 use rustc_infer::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
2 use rustc_infer::infer::NllRegionVariableOrigin;
3 use rustc_infer::traits::ObligationCause;
4 use rustc_middle::mir::ConstraintCategory;
5 use rustc_middle::ty::error::TypeError;
6 use rustc_middle::ty::relate::TypeRelation;
7 use rustc_middle::ty::{self, Const, Ty};
9 use rustc_trait_selection::traits::query::Fallible;
11 use crate::constraints::OutlivesConstraint;
12 use crate::diagnostics::UniverseInfo;
13 use crate::type_check::{InstantiateOpaqueType, Locations, TypeChecker};
15 impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
16 /// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
18 /// - "Covariant" `a <: b`
19 /// - "Invariant" `a == b`
20 /// - "Contravariant" `a :> b`
22 /// N.B., the type `a` is permitted to have unresolved inference
23 /// variables, but not the type `b`.
24 #[instrument(skip(self), level = "debug")]
25 pub(super) fn relate_types(
31 category: ConstraintCategory,
35 NllTypeRelatingDelegate::new(self, locations, category, UniverseInfo::relate(a, b)),
43 struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
44 type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
46 /// Where (and why) is this relation taking place?
49 /// What category do we assign the resulting `'a: 'b` relationships?
50 category: ConstraintCategory,
52 /// Information so that error reporting knows what types we are relating
53 /// when reporting a bound region error.
54 universe_info: UniverseInfo<'tcx>,
57 impl<'me, 'bccx, 'tcx> NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
59 type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
61 category: ConstraintCategory,
62 universe_info: UniverseInfo<'tcx>,
64 Self { type_checker, locations, category, universe_info }
68 impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
69 fn span(&self) -> Span {
70 self.locations.span(self.type_checker.body)
73 fn param_env(&self) -> ty::ParamEnv<'tcx> {
74 self.type_checker.param_env
77 fn create_next_universe(&mut self) -> ty::UniverseIndex {
78 let universe = self.type_checker.infcx.create_next_universe();
83 .insert(universe, self.universe_info.clone());
87 fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> {
88 let origin = NllRegionVariableOrigin::Existential { from_forall };
89 self.type_checker.infcx.next_nll_region_var(origin)
92 fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
96 .placeholder_region(self.type_checker.infcx, placeholder)
99 fn generalize_existential(&mut self, universe: ty::UniverseIndex) -> ty::Region<'tcx> {
100 self.type_checker.infcx.next_nll_region_var_in_universe(
101 NllRegionVariableOrigin::Existential { from_forall: false },
108 sup: ty::Region<'tcx>,
109 sub: ty::Region<'tcx>,
110 info: ty::VarianceDiagInfo<'tcx>,
112 let sub = self.type_checker.borrowck_context.universal_regions.to_region_vid(sub);
113 let sup = self.type_checker.borrowck_context.universal_regions.to_region_vid(sup);
114 self.type_checker.borrowck_context.constraints.outlives_constraints.push(
118 locations: self.locations,
119 category: self.category,
125 // We don't have to worry about the equality of consts during borrow checking
126 // as consts always have a static lifetime.
127 // FIXME(oli-obk): is this really true? We can at least have HKL and with
128 // inline consts we may have further lifetimes that may be unsound to treat as
130 fn const_equate(&mut self, _a: Const<'tcx>, _b: Const<'tcx>) {}
132 fn normalization() -> NormalizationStrategy {
133 NormalizationStrategy::Eager
136 fn forbid_inference_vars() -> bool {
140 fn register_opaque_type(
145 ) -> Result<(), TypeError<'tcx>> {
146 let param_env = self.param_env();
147 let span = self.span();
148 let def_id = self.type_checker.body.source.def_id().expect_local();
149 let body_id = self.type_checker.tcx().hir().local_def_id_to_hir_id(def_id);
150 let cause = ObligationCause::misc(span, body_id);
155 InstantiateOpaqueType {
159 .handle_opaque_type(a, b, a_is_expected, &cause, param_env)?
161 // These fields are filled in during exectuion of the operation
163 region_constraints: None,