]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/type_check/relate_tys.rs
Auto merge of #99814 - aliemjay:patch-2, r=jackh726
[rust.git] / compiler / rustc_borrowck / src / type_check / relate_tys.rs
1 use rustc_infer::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
2 use rustc_infer::infer::NllRegionVariableOrigin;
3 use rustc_infer::traits::PredicateObligations;
4 use rustc_middle::mir::ConstraintCategory;
5 use rustc_middle::ty::relate::TypeRelation;
6 use rustc_middle::ty::{self, Ty};
7 use rustc_span::Span;
8 use rustc_trait_selection::traits::query::Fallible;
9
10 use crate::constraints::OutlivesConstraint;
11 use crate::diagnostics::UniverseInfo;
12 use crate::type_check::{InstantiateOpaqueType, Locations, TypeChecker};
13
14 impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
15     /// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
16     ///
17     /// - "Covariant" `a <: b`
18     /// - "Invariant" `a == b`
19     /// - "Contravariant" `a :> b`
20     ///
21     /// N.B., the type `a` is permitted to have unresolved inference
22     /// variables, but not the type `b`.
23     #[instrument(skip(self), level = "debug")]
24     pub(super) fn relate_types(
25         &mut self,
26         a: Ty<'tcx>,
27         v: ty::Variance,
28         b: Ty<'tcx>,
29         locations: Locations,
30         category: ConstraintCategory<'tcx>,
31     ) -> Fallible<()> {
32         TypeRelating::new(
33             self.infcx,
34             NllTypeRelatingDelegate::new(self, locations, category, UniverseInfo::relate(a, b)),
35             v,
36         )
37         .relate(a, b)?;
38         Ok(())
39     }
40
41     /// Add sufficient constraints to ensure `a == b`. See also [Self::relate_types].
42     pub(super) fn eq_substs(
43         &mut self,
44         a: ty::SubstsRef<'tcx>,
45         b: ty::SubstsRef<'tcx>,
46         locations: Locations,
47         category: ConstraintCategory<'tcx>,
48     ) -> Fallible<()> {
49         TypeRelating::new(
50             self.infcx,
51             NllTypeRelatingDelegate::new(self, locations, category, UniverseInfo::other()),
52             ty::Variance::Invariant,
53         )
54         .relate(a, b)?;
55         Ok(())
56     }
57 }
58
59 struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
60     type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
61
62     /// Where (and why) is this relation taking place?
63     locations: Locations,
64
65     /// What category do we assign the resulting `'a: 'b` relationships?
66     category: ConstraintCategory<'tcx>,
67
68     /// Information so that error reporting knows what types we are relating
69     /// when reporting a bound region error.
70     universe_info: UniverseInfo<'tcx>,
71 }
72
73 impl<'me, 'bccx, 'tcx> NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
74     fn new(
75         type_checker: &'me mut TypeChecker<'bccx, 'tcx>,
76         locations: Locations,
77         category: ConstraintCategory<'tcx>,
78         universe_info: UniverseInfo<'tcx>,
79     ) -> Self {
80         Self { type_checker, locations, category, universe_info }
81     }
82 }
83
84 impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
85     fn span(&self) -> Span {
86         self.locations.span(self.type_checker.body)
87     }
88
89     fn param_env(&self) -> ty::ParamEnv<'tcx> {
90         self.type_checker.param_env
91     }
92
93     fn create_next_universe(&mut self) -> ty::UniverseIndex {
94         let universe = self.type_checker.infcx.create_next_universe();
95         self.type_checker
96             .borrowck_context
97             .constraints
98             .universe_causes
99             .insert(universe, self.universe_info.clone());
100         universe
101     }
102
103     fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> {
104         let origin = NllRegionVariableOrigin::Existential { from_forall };
105         self.type_checker.infcx.next_nll_region_var(origin)
106     }
107
108     fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
109         self.type_checker
110             .borrowck_context
111             .constraints
112             .placeholder_region(self.type_checker.infcx, placeholder)
113     }
114
115     fn generalize_existential(&mut self, universe: ty::UniverseIndex) -> ty::Region<'tcx> {
116         self.type_checker.infcx.next_nll_region_var_in_universe(
117             NllRegionVariableOrigin::Existential { from_forall: false },
118             universe,
119         )
120     }
121
122     fn push_outlives(
123         &mut self,
124         sup: ty::Region<'tcx>,
125         sub: ty::Region<'tcx>,
126         info: ty::VarianceDiagInfo<'tcx>,
127     ) {
128         let sub = self.type_checker.borrowck_context.universal_regions.to_region_vid(sub);
129         let sup = self.type_checker.borrowck_context.universal_regions.to_region_vid(sup);
130         self.type_checker.borrowck_context.constraints.outlives_constraints.push(
131             OutlivesConstraint {
132                 sup,
133                 sub,
134                 locations: self.locations,
135                 span: self.locations.span(self.type_checker.body),
136                 category: self.category,
137                 variance_info: info,
138                 from_closure: false,
139             },
140         );
141     }
142
143     fn normalization() -> NormalizationStrategy {
144         NormalizationStrategy::Eager
145     }
146
147     fn forbid_inference_vars() -> bool {
148         true
149     }
150
151     fn register_obligations(&mut self, obligations: PredicateObligations<'tcx>) {
152         self.type_checker
153             .fully_perform_op(
154                 self.locations,
155                 self.category,
156                 InstantiateOpaqueType {
157                     obligations,
158                     // These fields are filled in during execution of the operation
159                     base_universe: None,
160                     region_constraints: None,
161                 },
162             )
163             .unwrap();
164     }
165 }