1 use rustc::mir::ConstraintCategory;
2 use rustc::ty::relate::TypeRelation;
3 use rustc::ty::{self, Ty};
4 use rustc_infer::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
5 use rustc_infer::infer::{InferCtxt, NLLRegionVariableOrigin};
6 use rustc_infer::traits::query::Fallible;
7 use rustc_infer::traits::DomainGoal;
9 use crate::borrow_check::constraints::OutlivesConstraint;
10 use crate::borrow_check::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 pub(super) fn relate_types<'tcx>(
21 infcx: &InferCtxt<'_, 'tcx>,
26 category: ConstraintCategory,
27 borrowck_context: Option<&mut BorrowCheckContext<'_, 'tcx>>,
29 debug!("eq_types(a={:?}, b={:?}, locations={:?})", a, b, locations);
32 NllTypeRelatingDelegate::new(infcx, borrowck_context, locations, category),
39 struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
40 infcx: &'me InferCtxt<'me, 'tcx>,
41 borrowck_context: Option<&'me mut BorrowCheckContext<'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,
50 impl NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
52 infcx: &'me InferCtxt<'me, 'tcx>,
53 borrowck_context: Option<&'me mut BorrowCheckContext<'bccx, 'tcx>>,
55 category: ConstraintCategory,
57 Self { infcx, borrowck_context, locations, category }
61 impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
62 fn create_next_universe(&mut self) -> ty::UniverseIndex {
63 self.infcx.create_next_universe()
66 fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> {
67 if self.borrowck_context.is_some() {
68 let origin = NLLRegionVariableOrigin::Existential { from_forall };
69 self.infcx.next_nll_region_var(origin)
71 self.infcx.tcx.lifetimes.re_erased
75 fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
76 if let Some(borrowck_context) = &mut self.borrowck_context {
77 borrowck_context.constraints.placeholder_region(self.infcx, placeholder)
79 self.infcx.tcx.lifetimes.re_erased
83 fn generalize_existential(&mut self, universe: ty::UniverseIndex) -> ty::Region<'tcx> {
84 self.infcx.next_nll_region_var_in_universe(
85 NLLRegionVariableOrigin::Existential { from_forall: false },
90 fn push_outlives(&mut self, sup: ty::Region<'tcx>, sub: ty::Region<'tcx>) {
91 if let Some(borrowck_context) = &mut self.borrowck_context {
92 let sub = borrowck_context.universal_regions.to_region_vid(sub);
93 let sup = borrowck_context.universal_regions.to_region_vid(sup);
94 borrowck_context.constraints.outlives_constraints.push(OutlivesConstraint {
97 locations: self.locations,
98 category: self.category,
103 fn push_domain_goal(&mut self, _: DomainGoal<'tcx>) {
104 bug!("should never be invoked with eager normalization")
107 fn normalization() -> NormalizationStrategy {
108 NormalizationStrategy::Eager
111 fn forbid_inference_vars() -> bool {