1 use rustc_infer::infer::canonical::QueryOutlivesConstraint;
2 use rustc_infer::infer::canonical::QueryRegionConstraints;
3 use rustc_infer::infer::outlives::env::RegionBoundPairs;
4 use rustc_infer::infer::outlives::obligations::{TypeOutlives, TypeOutlivesDelegate};
5 use rustc_infer::infer::region_constraints::{GenericKind, VerifyBound};
6 use rustc_infer::infer::{self, InferCtxt, SubregionOrigin};
7 use rustc_middle::mir::ConstraintCategory;
8 use rustc_middle::ty::subst::GenericArgKind;
9 use rustc_middle::ty::TypeVisitable;
10 use rustc_middle::ty::{self, TyCtxt};
11 use rustc_span::{Span, DUMMY_SP};
14 constraints::OutlivesConstraint,
16 region_infer::TypeTest,
17 type_check::{Locations, MirTypeckRegionConstraints},
18 universal_regions::UniversalRegions,
21 pub(crate) struct ConstraintConversion<'a, 'tcx> {
22 infcx: &'a InferCtxt<'a, 'tcx>,
24 universal_regions: &'a UniversalRegions<'tcx>,
25 region_bound_pairs: &'a RegionBoundPairs<'tcx>,
26 implicit_region_bound: ty::Region<'tcx>,
27 param_env: ty::ParamEnv<'tcx>,
30 category: ConstraintCategory<'tcx>,
31 constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
34 impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
36 infcx: &'a InferCtxt<'a, 'tcx>,
37 universal_regions: &'a UniversalRegions<'tcx>,
38 region_bound_pairs: &'a RegionBoundPairs<'tcx>,
39 implicit_region_bound: ty::Region<'tcx>,
40 param_env: ty::ParamEnv<'tcx>,
43 category: ConstraintCategory<'tcx>,
44 constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
51 implicit_region_bound,
60 #[instrument(skip(self), level = "debug")]
61 pub(super) fn convert_all(&mut self, query_constraints: &QueryRegionConstraints<'tcx>) {
62 let QueryRegionConstraints { outlives, member_constraints } = query_constraints;
64 // Annoying: to invoke `self.to_region_vid`, we need access to
65 // `self.constraints`, but we also want to be mutating
66 // `self.member_constraints`. For now, just swap out the value
67 // we want and replace at the end.
68 let mut tmp = std::mem::take(&mut self.constraints.member_constraints);
69 for member_constraint in member_constraints {
70 tmp.push_constraint(member_constraint, |r| self.to_region_vid(r));
72 self.constraints.member_constraints = tmp;
74 for query_constraint in outlives {
75 self.convert(query_constraint);
79 pub(super) fn convert(&mut self, query_constraint: &QueryOutlivesConstraint<'tcx>) {
80 debug!("generate: constraints at: {:#?}", self.locations);
82 // Extract out various useful fields we'll need below.
83 let ConstraintConversion {
84 tcx, region_bound_pairs, implicit_region_bound, param_env, ..
87 // At the moment, we never generate any "higher-ranked"
88 // region constraints like `for<'a> 'a: 'b`. At some point
89 // when we move to universes, we will, and this assertion
90 // will start to fail.
91 let ty::OutlivesPredicate(k1, r2) = query_constraint.no_bound_vars().unwrap_or_else(|| {
92 bug!("query_constraint {:?} contained bound vars", query_constraint,);
96 GenericArgKind::Lifetime(r1) => {
97 let r1_vid = self.to_region_vid(r1);
98 let r2_vid = self.to_region_vid(r2);
99 self.add_outlives(r1_vid, r2_vid);
102 GenericArgKind::Type(mut t1) => {
103 // we don't actually use this for anything, but
104 // the `TypeOutlives` code needs an origin.
105 let origin = infer::RelateParamBound(DUMMY_SP, t1, None);
107 // Placeholder regions need to be converted now because it may
108 // create new region variables, which can't be done later when
109 // verifying these bounds.
110 if t1.has_placeholders() {
111 t1 = tcx.fold_regions(t1, |r, _| match *r {
112 ty::RePlaceholder(placeholder) => {
113 self.constraints.placeholder_region(self.infcx, placeholder)
123 Some(implicit_region_bound),
126 .type_must_outlive(origin, t1, r2);
129 GenericArgKind::Const(_) => {
130 // Consts cannot outlive one another, so we
131 // don't need to handle any relations here.
136 fn verify_to_type_test(
138 generic_kind: GenericKind<'tcx>,
139 region: ty::Region<'tcx>,
140 verify_bound: VerifyBound<'tcx>,
141 ) -> TypeTest<'tcx> {
142 let lower_bound = self.to_region_vid(region);
144 TypeTest { generic_kind, lower_bound, locations: self.locations, verify_bound }
147 fn to_region_vid(&mut self, r: ty::Region<'tcx>) -> ty::RegionVid {
148 if let ty::RePlaceholder(placeholder) = *r {
149 self.constraints.placeholder_region(self.infcx, placeholder).to_region_vid()
151 self.universal_regions.to_region_vid(r)
155 fn add_outlives(&mut self, sup: ty::RegionVid, sub: ty::RegionVid) {
156 self.constraints.outlives_constraints.push(OutlivesConstraint {
157 locations: self.locations,
158 category: self.category,
162 variance_info: ty::VarianceDiagInfo::default(),
166 fn add_type_test(&mut self, type_test: TypeTest<'tcx>) {
167 debug!("add_type_test(type_test={:?})", type_test);
168 self.constraints.type_tests.push(type_test);
172 impl<'a, 'b, 'tcx> TypeOutlivesDelegate<'tcx> for &'a mut ConstraintConversion<'b, 'tcx> {
173 fn push_sub_region_constraint(
175 _origin: SubregionOrigin<'tcx>,
179 let b = self.to_region_vid(b);
180 let a = self.to_region_vid(a);
181 self.add_outlives(b, a);
186 _origin: SubregionOrigin<'tcx>,
187 kind: GenericKind<'tcx>,
189 bound: VerifyBound<'tcx>,
191 let type_test = self.verify_to_type_test(kind, a, bound);
192 self.add_type_test(type_test);