rustc_borrowck/type_check/
constraint_conversion.rs1use rustc_hir::def_id::DefId;
2use rustc_infer::infer::canonical::QueryRegionConstraints;
3use rustc_infer::infer::outlives::env::RegionBoundPairs;
4use rustc_infer::infer::outlives::obligations::{TypeOutlives, TypeOutlivesDelegate};
5use rustc_infer::infer::region_constraints::{GenericKind, VerifyBound};
6use rustc_infer::infer::{self, InferCtxt, SubregionOrigin};
7use rustc_infer::traits::query::type_op::DeeplyNormalize;
8use rustc_middle::bug;
9use rustc_middle::mir::{ClosureOutlivesSubject, ClosureRegionRequirements, ConstraintCategory};
10use rustc_middle::ty::fold::fold_regions;
11use rustc_middle::ty::{self, GenericArgKind, Ty, TyCtxt, TypeFoldable, TypeVisitableExt};
12use rustc_span::Span;
13use rustc_trait_selection::traits::query::type_op::{TypeOp, TypeOpOutput};
14use tracing::{debug, instrument};
15
16use crate::constraints::OutlivesConstraint;
17use crate::region_infer::TypeTest;
18use crate::type_check::{Locations, MirTypeckRegionConstraints};
19use crate::universal_regions::UniversalRegions;
20
21pub(crate) struct ConstraintConversion<'a, 'tcx> {
22 infcx: &'a InferCtxt<'tcx>,
23 tcx: TyCtxt<'tcx>,
24 universal_regions: &'a UniversalRegions<'tcx>,
25 region_bound_pairs: &'a RegionBoundPairs<'tcx>,
36 implicit_region_bound: ty::Region<'tcx>,
37 param_env: ty::ParamEnv<'tcx>,
38 known_type_outlives_obligations: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
39 locations: Locations,
40 span: Span,
41 category: ConstraintCategory<'tcx>,
42 from_closure: bool,
43 constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
44}
45
46impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
47 pub(crate) fn new(
48 infcx: &'a InferCtxt<'tcx>,
49 universal_regions: &'a UniversalRegions<'tcx>,
50 region_bound_pairs: &'a RegionBoundPairs<'tcx>,
51 implicit_region_bound: ty::Region<'tcx>,
52 param_env: ty::ParamEnv<'tcx>,
53 known_type_outlives_obligations: &'a [ty::PolyTypeOutlivesPredicate<'tcx>],
54 locations: Locations,
55 span: Span,
56 category: ConstraintCategory<'tcx>,
57 constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
58 ) -> Self {
59 Self {
60 infcx,
61 tcx: infcx.tcx,
62 universal_regions,
63 region_bound_pairs,
64 implicit_region_bound,
65 param_env,
66 known_type_outlives_obligations,
67 locations,
68 span,
69 category,
70 constraints,
71 from_closure: false,
72 }
73 }
74
75 #[instrument(skip(self), level = "debug")]
76 pub(super) fn convert_all(&mut self, query_constraints: &QueryRegionConstraints<'tcx>) {
77 let QueryRegionConstraints { outlives } = query_constraints;
78
79 for &(predicate, constraint_category) in outlives {
80 self.convert(predicate, constraint_category);
81 }
82 }
83
84 #[instrument(skip(self), level = "debug")]
88 pub(crate) fn apply_closure_requirements(
89 &mut self,
90 closure_requirements: &ClosureRegionRequirements<'tcx>,
91 closure_def_id: DefId,
92 closure_args: ty::GenericArgsRef<'tcx>,
93 ) {
94 let closure_mapping = &UniversalRegions::closure_mapping(
98 self.tcx,
99 closure_args,
100 closure_requirements.num_external_vids,
101 closure_def_id.expect_local(),
102 );
103 debug!(?closure_mapping);
104
105 let backup = (self.category, self.span, self.from_closure);
107 self.from_closure = true;
108 for outlives_requirement in &closure_requirements.outlives_requirements {
109 let outlived_region = closure_mapping[outlives_requirement.outlived_free_region];
110 let subject = match outlives_requirement.subject {
111 ClosureOutlivesSubject::Region(re) => closure_mapping[re].into(),
112 ClosureOutlivesSubject::Ty(subject_ty) => {
113 subject_ty.instantiate(self.tcx, |vid| closure_mapping[vid]).into()
114 }
115 };
116
117 self.category = outlives_requirement.category;
118 self.span = outlives_requirement.blame_span;
119 self.convert(ty::OutlivesPredicate(subject, outlived_region), self.category);
120 }
121 (self.category, self.span, self.from_closure) = backup;
122 }
123
124 fn convert(
125 &mut self,
126 predicate: ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>,
127 constraint_category: ConstraintCategory<'tcx>,
128 ) {
129 debug!("generate: constraints at: {:#?}", self.locations);
130
131 let ConstraintConversion {
133 tcx,
134 infcx,
135 region_bound_pairs,
136 implicit_region_bound,
137 known_type_outlives_obligations,
138 ..
139 } = *self;
140
141 let mut outlives_predicates = vec![(predicate, constraint_category)];
142 for iteration in 0.. {
143 if outlives_predicates.is_empty() {
144 break;
145 }
146
147 if !self.tcx.recursion_limit().value_within_limit(iteration) {
148 bug!(
149 "FIXME(-Znext-solver): Overflowed when processing region obligations: {outlives_predicates:#?}"
150 );
151 }
152
153 let mut next_outlives_predicates = vec![];
154 for (ty::OutlivesPredicate(k1, r2), constraint_category) in outlives_predicates {
155 match k1.unpack() {
156 GenericArgKind::Lifetime(r1) => {
157 let r1_vid = self.to_region_vid(r1);
158 let r2_vid = self.to_region_vid(r2);
159 self.add_outlives(r1_vid, r2_vid, constraint_category);
160 }
161
162 GenericArgKind::Type(mut t1) => {
163 if infcx.next_trait_solver() {
166 t1 = self.normalize_and_add_type_outlives_constraints(
167 t1,
168 &mut next_outlives_predicates,
169 );
170 }
171
172 let origin = infer::RelateParamBound(self.span, t1, None);
175
176 TypeOutlives::new(
177 &mut *self,
178 tcx,
179 region_bound_pairs,
180 Some(implicit_region_bound),
181 known_type_outlives_obligations,
182 )
183 .type_must_outlive(
184 origin,
185 t1,
186 r2,
187 constraint_category,
188 );
189 }
190
191 GenericArgKind::Const(_) => unreachable!(),
192 }
193 }
194
195 outlives_predicates = next_outlives_predicates;
196 }
197 }
198
199 fn replace_placeholders_with_nll<T: TypeFoldable<TyCtxt<'tcx>>>(&mut self, value: T) -> T {
206 if value.has_placeholders() {
207 fold_regions(self.tcx, value, |r, _| match *r {
208 ty::RePlaceholder(placeholder) => {
209 self.constraints.placeholder_region(self.infcx, placeholder)
210 }
211 _ => r,
212 })
213 } else {
214 value
215 }
216 }
217
218 fn verify_to_type_test(
219 &mut self,
220 generic_kind: GenericKind<'tcx>,
221 region: ty::Region<'tcx>,
222 verify_bound: VerifyBound<'tcx>,
223 ) -> TypeTest<'tcx> {
224 let lower_bound = self.to_region_vid(region);
225 TypeTest { generic_kind, lower_bound, span: self.span, verify_bound }
226 }
227
228 fn to_region_vid(&mut self, r: ty::Region<'tcx>) -> ty::RegionVid {
229 if let ty::RePlaceholder(placeholder) = *r {
230 self.constraints.placeholder_region(self.infcx, placeholder).as_var()
231 } else {
232 self.universal_regions.to_region_vid(r)
233 }
234 }
235
236 fn add_outlives(
237 &mut self,
238 sup: ty::RegionVid,
239 sub: ty::RegionVid,
240 category: ConstraintCategory<'tcx>,
241 ) {
242 let category = match self.category {
243 ConstraintCategory::Boring | ConstraintCategory::BoringNoLocation => category,
244 _ => self.category,
245 };
246 self.constraints.outlives_constraints.push(OutlivesConstraint {
247 locations: self.locations,
248 category,
249 span: self.span,
250 sub,
251 sup,
252 variance_info: ty::VarianceDiagInfo::default(),
253 from_closure: self.from_closure,
254 });
255 }
256
257 fn add_type_test(&mut self, type_test: TypeTest<'tcx>) {
258 debug!("add_type_test(type_test={:?})", type_test);
259 self.constraints.type_tests.push(type_test);
260 }
261
262 fn normalize_and_add_type_outlives_constraints(
263 &self,
264 ty: Ty<'tcx>,
265 next_outlives_predicates: &mut Vec<(
266 ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>,
267 ConstraintCategory<'tcx>,
268 )>,
269 ) -> Ty<'tcx> {
270 match self.param_env.and(DeeplyNormalize { value: ty }).fully_perform(self.infcx, self.span)
271 {
272 Ok(TypeOpOutput { output: ty, constraints, .. }) => {
273 if let Some(QueryRegionConstraints { outlives }) = constraints {
274 next_outlives_predicates.extend(outlives.iter().copied());
275 }
276 ty
277 }
278 Err(_) => ty,
279 }
280 }
281}
282
283impl<'a, 'b, 'tcx> TypeOutlivesDelegate<'tcx> for &'a mut ConstraintConversion<'b, 'tcx> {
284 fn push_sub_region_constraint(
285 &mut self,
286 _origin: SubregionOrigin<'tcx>,
287 a: ty::Region<'tcx>,
288 b: ty::Region<'tcx>,
289 constraint_category: ConstraintCategory<'tcx>,
290 ) {
291 let b = self.to_region_vid(b);
292 let a = self.to_region_vid(a);
293 self.add_outlives(b, a, constraint_category);
294 }
295
296 fn push_verify(
297 &mut self,
298 _origin: SubregionOrigin<'tcx>,
299 kind: GenericKind<'tcx>,
300 a: ty::Region<'tcx>,
301 bound: VerifyBound<'tcx>,
302 ) {
303 let kind = self.replace_placeholders_with_nll(kind);
304 let bound = self.replace_placeholders_with_nll(bound);
305 let type_test = self.verify_to_type_test(kind, a, bound);
306 self.add_type_test(type_test);
307 }
308}