]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/type_check/relate_tys.rs
Rollup merge of #69705 - ehuss:toolstate-remove-redundant-beta, r=Mark-Simulacrum
[rust.git] / src / librustc_mir / borrow_check / type_check / relate_tys.rs
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;
8
9 use crate::borrow_check::constraints::OutlivesConstraint;
10 use crate::borrow_check::type_check::{BorrowCheckContext, Locations};
11
12 /// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
13 ///
14 /// - "Covariant" `a <: b`
15 /// - "Invariant" `a == b`
16 /// - "Contravariant" `a :> b`
17 ///
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>,
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!("eq_types(a={:?}, b={:?}, locations={:?})", a, b, locations);
30     TypeRelating::new(
31         infcx,
32         NllTypeRelatingDelegate::new(infcx, borrowck_context, 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     /// Where (and why) is this relation taking place?
44     locations: Locations,
45
46     /// What category do we assign the resulting `'a: 'b` relationships?
47     category: ConstraintCategory,
48 }
49
50 impl NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
51     fn new(
52         infcx: &'me InferCtxt<'me, 'tcx>,
53         borrowck_context: Option<&'me mut BorrowCheckContext<'bccx, 'tcx>>,
54         locations: Locations,
55         category: ConstraintCategory,
56     ) -> Self {
57         Self { infcx, borrowck_context, locations, category }
58     }
59 }
60
61 impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
62     fn create_next_universe(&mut self) -> ty::UniverseIndex {
63         self.infcx.create_next_universe()
64     }
65
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)
70         } else {
71             self.infcx.tcx.lifetimes.re_erased
72         }
73     }
74
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)
78         } else {
79             self.infcx.tcx.lifetimes.re_erased
80         }
81     }
82
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 },
86             universe,
87         )
88     }
89
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 {
95                 sup,
96                 sub,
97                 locations: self.locations,
98                 category: self.category,
99             });
100         }
101     }
102
103     fn push_domain_goal(&mut self, _: DomainGoal<'tcx>) {
104         bug!("should never be invoked with eager normalization")
105     }
106
107     fn normalization() -> NormalizationStrategy {
108         NormalizationStrategy::Eager
109     }
110
111     fn forbid_inference_vars() -> bool {
112         true
113     }
114 }