1 use rustc_infer::infer::outlives::components::{push_outlives_components, Component};
2 use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
3 use rustc_middle::ty::{self, Region, Ty, TyCtxt};
5 use smallvec::smallvec;
6 use std::collections::BTreeMap;
8 /// Tracks the `T: 'a` or `'a: 'a` predicates that we have inferred
9 /// must be added to the struct header.
10 pub(crate) type RequiredPredicates<'tcx> =
11 BTreeMap<ty::OutlivesPredicate<GenericArg<'tcx>, ty::Region<'tcx>>, Span>;
13 /// Given a requirement `T: 'a` or `'b: 'a`, deduce the
14 /// outlives_component and add it to `required_predicates`
15 pub(crate) fn insert_outlives_predicate<'tcx>(
17 kind: GenericArg<'tcx>,
18 outlived_region: Region<'tcx>,
20 required_predicates: &mut RequiredPredicates<'tcx>,
22 // If the `'a` region is bound within the field type itself, we
23 // don't want to propagate this constraint to the header.
24 if !is_free_region(outlived_region) {
29 GenericArgKind::Type(ty) => {
30 // `T: 'outlived_region` for some type `T`
31 // But T could be a lot of things:
32 // e.g., if `T = &'b u32`, then `'b: 'outlived_region` is
33 // what we want to add.
35 // Or if within `struct Foo<U>` you had `T = Vec<U>`, then
36 // we would want to add `U: 'outlived_region`
37 let mut components = smallvec![];
38 push_outlives_components(tcx, ty, &mut components);
39 for component in components {
41 Component::Region(r) => {
42 // This would arise from something like:
45 // struct Foo<'a, 'b> {
50 // Here `outlived_region = 'a` and `kind = &'b
51 // u32`. Decomposing `&'b u32` into
52 // components would yield `'b`, and we add the
53 // where clause that `'b: 'a`.
54 insert_outlives_predicate(
63 Component::Param(param_ty) => {
64 // param_ty: ty::ParamTy
65 // This would arise from something like:
68 // struct Foo<'a, U> {
73 // Here `outlived_region = 'a` and `kind =
74 // Vec<U>`. Decomposing `Vec<U>` into
75 // components would yield `U`, and we add the
76 // where clause that `U: 'a`.
77 let ty: Ty<'tcx> = param_ty.to_ty(tcx);
79 .entry(ty::OutlivesPredicate(ty.into(), outlived_region))
83 Component::Projection(proj_ty) => {
84 // This would arise from something like:
87 // struct Foo<'a, T: Iterator> {
88 // x: &'a <T as Iterator>::Item
92 // Here we want to add an explicit `where <T as Iterator>::Item: 'a`.
93 let ty: Ty<'tcx> = tcx.mk_projection(proj_ty.item_def_id, proj_ty.substs);
95 .entry(ty::OutlivesPredicate(ty.into(), outlived_region))
99 Component::Opaque(def_id, substs) => {
100 // This would arise from something like:
103 // type Opaque<T> = impl Sized;
104 // fn defining<T>() -> Opaque<T> {}
105 // struct Ss<'a, T>(&'a Opaque<T>);
108 // Here we want to have an implied bound `Opaque<T>: 'a`
110 let ty = tcx.mk_opaque(def_id, substs);
112 .entry(ty::OutlivesPredicate(ty.into(), outlived_region))
116 Component::EscapingProjection(_) => {
117 // As above, but the projection involves
118 // late-bound regions. Therefore, the WF
119 // requirement is not checked in type definition
120 // but at fn call site, so ignore it.
123 // struct Foo<'a, T: Iterator> {
124 // x: for<'b> fn(<&'b T as Iterator>::Item)
125 // // ^^^^^^^^^^^^^^^^^^^^^^^^^
129 // Since `'b` is not in scope on `Foo`, can't
130 // do anything here, ignore it.
133 Component::UnresolvedInferenceVariable(_) => bug!("not using infcx"),
138 GenericArgKind::Lifetime(r) => {
139 if !is_free_region(r) {
142 required_predicates.entry(ty::OutlivesPredicate(kind, outlived_region)).or_insert(span);
145 GenericArgKind::Const(_) => {
146 // Generic consts don't impose any constraints.
151 fn is_free_region(region: Region<'_>) -> bool {
152 // First, screen for regions that might appear in a type header.
154 // These correspond to `T: 'a` relationships:
156 // struct Foo<'a, T> {
157 // field: &'a T, // this would generate a ReEarlyBound referencing `'a`
160 // We care about these, so fall through.
161 ty::ReEarlyBound(_) => true,
163 // These correspond to `T: 'static` relationships which can be
164 // rather surprising.
166 // struct Foo<'a, T> {
167 // field: &'static T, // this would generate a ReStatic
169 ty::ReStatic => false,
171 // Late-bound regions can appear in `fn` types:
174 // field: for<'b> fn(&'b T) // e.g., 'b here
177 // The type above might generate a `T: 'b` bound, but we can
178 // ignore it. We can't put it on the struct header anyway.
179 ty::ReLateBound(..) => false,
181 // These regions don't appear in types from type declarations:
182 ty::ReErased | ty::ReVar(..) | ty::RePlaceholder(..) | ty::ReFree(..) => {
183 bug!("unexpected region in outlives inference: {:?}", region);