]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_mir/src/borrow_check/type_check/relate_tys.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / compiler / rustc_mir / src / borrow_check / type_check / relate_tys.rs
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;
7
8 use crate::borrow_check::constraints::OutlivesConstraint;
9 use crate::borrow_check::type_check::{BorrowCheckContext, Locations};
10
11 /// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
12 ///
13 /// - "Covariant" `a <: b`
14 /// - "Invariant" `a == b`
15 /// - "Contravariant" `a :> b`
16 ///
17 /// N.B., the type `a` is permitted to have unresolved inference
18 /// variables, but not the type `b`.
19 pub(super) fn relate_types<'tcx>(
20 infcx: &InferCtxt<'_, 'tcx>,
21 param_env: ty::ParamEnv<'tcx>,
22 a: Ty<'tcx>,
23 v: ty::Variance,
24 b: Ty<'tcx>,
25 locations: Locations,
26 category: ConstraintCategory,
27 borrowck_context: Option<&mut BorrowCheckContext<'_, 'tcx>>,
28 ) -> Fallible<()> {
29 debug!("relate_types(a={:?}, v={:?}, b={:?}, locations={:?})", a, v, b, locations);
30 TypeRelating::new(
31 infcx,
32 NllTypeRelatingDelegate::new(infcx, borrowck_context, param_env, locations, category),
33 v,
34 )
35 .relate(a, b)?;
36 Ok(())
37 }
38
39 struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
40 infcx: &'me InferCtxt<'me, 'tcx>,
41 borrowck_context: Option<&'me mut BorrowCheckContext<'bccx, 'tcx>>,
42
43 param_env: ty::ParamEnv<'tcx>,
44
45 /// Where (and why) is this relation taking place?
46 locations: Locations,
47
48 /// What category do we assign the resulting `'a: 'b` relationships?
49 category: ConstraintCategory,
50 }
51
52 impl NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
53 fn new(
54 infcx: &'me InferCtxt<'me, 'tcx>,
55 borrowck_context: Option<&'me mut BorrowCheckContext<'bccx, 'tcx>>,
56 param_env: ty::ParamEnv<'tcx>,
57 locations: Locations,
58 category: ConstraintCategory,
59 ) -> Self {
60 Self { infcx, borrowck_context, param_env, locations, category }
61 }
62 }
63
64 impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
65 fn param_env(&self) -> ty::ParamEnv<'tcx> {
66 self.param_env
67 }
68
69 fn create_next_universe(&mut self) -> ty::UniverseIndex {
70 self.infcx.create_next_universe()
71 }
72
73 fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> {
74 if self.borrowck_context.is_some() {
75 let origin = NllRegionVariableOrigin::Existential { from_forall };
76 self.infcx.next_nll_region_var(origin)
77 } else {
78 self.infcx.tcx.lifetimes.re_erased
79 }
80 }
81
82 fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
83 if let Some(borrowck_context) = &mut self.borrowck_context {
84 borrowck_context.constraints.placeholder_region(self.infcx, placeholder)
85 } else {
86 self.infcx.tcx.lifetimes.re_erased
87 }
88 }
89
90 fn generalize_existential(&mut self, universe: ty::UniverseIndex) -> ty::Region<'tcx> {
91 self.infcx.next_nll_region_var_in_universe(
92 NllRegionVariableOrigin::Existential { from_forall: false },
93 universe,
94 )
95 }
96
97 fn push_outlives(&mut self, sup: ty::Region<'tcx>, sub: ty::Region<'tcx>) {
98 if let Some(borrowck_context) = &mut self.borrowck_context {
99 let sub = borrowck_context.universal_regions.to_region_vid(sub);
100 let sup = borrowck_context.universal_regions.to_region_vid(sup);
101 borrowck_context.constraints.outlives_constraints.push(OutlivesConstraint {
102 sup,
103 sub,
104 locations: self.locations,
105 category: self.category,
106 });
107 }
108 }
109
110 // We don't have to worry about the equality of consts during borrow checking
111 // as consts always have a static lifetime.
112 fn const_equate(&mut self, _a: &'tcx Const<'tcx>, _b: &'tcx Const<'tcx>) {}
113
114 fn normalization() -> NormalizationStrategy {
115 NormalizationStrategy::Eager
116 }
117
118 fn forbid_inference_vars() -> bool {
119 true
120 }
121 }