rustc_type_ir/
outlives.rs

1//! The outlives relation `T: 'a` or `'a: 'b`. This code frequently
2//! refers to rules defined in RFC 1214 (`OutlivesFooBar`), so see that
3//! RFC for reference.
4
5use derive_where::derive_where;
6use smallvec::{SmallVec, smallvec};
7
8use crate::data_structures::SsoHashSet;
9use crate::inherent::*;
10use crate::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt as _, TypeVisitor};
11use crate::{self as ty, Interner};
12
13#[derive_where(Debug; I: Interner)]
14pub enum Component<I: Interner> {
15    Region(I::Region),
16    Param(I::ParamTy),
17    Placeholder(I::PlaceholderTy),
18    UnresolvedInferenceVariable(ty::InferTy),
19
20    // Projections like `T::Foo` are tricky because a constraint like
21    // `T::Foo: 'a` can be satisfied in so many ways. There may be a
22    // where-clause that says `T::Foo: 'a`, or the defining trait may
23    // include a bound like `type Foo: 'static`, or -- in the most
24    // conservative way -- we can prove that `T: 'a` (more generally,
25    // that all components in the projection outlive `'a`). This code
26    // is not in a position to judge which is the best technique, so
27    // we just product the projection as a component and leave it to
28    // the consumer to decide (but see `EscapingProjection` below).
29    Alias(ty::AliasTy<I>),
30
31    // In the case where a projection has escaping regions -- meaning
32    // regions bound within the type itself -- we always use
33    // the most conservative rule, which requires that all components
34    // outlive the bound. So for example if we had a type like this:
35    //
36    //     for<'a> Trait1<  <T as Trait2<'a,'b>>::Foo  >
37    //                      ~~~~~~~~~~~~~~~~~~~~~~~~~
38    //
39    // then the inner projection (underlined) has an escaping region
40    // `'a`. We consider that outer trait `'c` to meet a bound if `'b`
41    // outlives `'b: 'c`, and we don't consider whether the trait
42    // declares that `Foo: 'static` etc. Therefore, we just return the
43    // free components of such a projection (in this case, `'b`).
44    //
45    // However, in the future, we may want to get smarter, and
46    // actually return a "higher-ranked projection" here. Therefore,
47    // we mark that these components are part of an escaping
48    // projection, so that implied bounds code can avoid relying on
49    // them. This gives us room to improve the regionck reasoning in
50    // the future without breaking backwards compat.
51    EscapingAlias(Vec<Component<I>>),
52}
53
54/// Push onto `out` all the things that must outlive `'a` for the condition
55/// `ty0: 'a` to hold. Note that `ty0` must be a **fully resolved type**.
56pub fn push_outlives_components<I: Interner>(
57    cx: I,
58    ty: I::Ty,
59    out: &mut SmallVec<[Component<I>; 4]>,
60) {
61    ty.visit_with(&mut OutlivesCollector { cx, out, visited: Default::default() });
62}
63
64struct OutlivesCollector<'a, I: Interner> {
65    cx: I,
66    out: &'a mut SmallVec<[Component<I>; 4]>,
67    visited: SsoHashSet<I::Ty>,
68}
69
70impl<I: Interner> TypeVisitor<I> for OutlivesCollector<'_, I> {
71    #[cfg(not(feature = "nightly"))]
72    type Result = ();
73
74    fn visit_ty(&mut self, ty: I::Ty) -> Self::Result {
75        if !self.visited.insert(ty) {
76            return;
77        }
78        // Descend through the types, looking for the various "base"
79        // components and collecting them into `out`. This is not written
80        // with `collect()` because of the need to sometimes skip subtrees
81        // in the `subtys` iterator (e.g., when encountering a
82        // projection).
83        match ty.kind() {
84            ty::FnDef(_, args) => {
85                // HACK(eddyb) ignore lifetimes found shallowly in `args`.
86                // This is inconsistent with `ty::Adt` (including all args)
87                // and with `ty::Closure` (ignoring all args other than
88                // upvars, of which a `ty::FnDef` doesn't have any), but
89                // consistent with previous (accidental) behavior.
90                // See https://github.com/rust-lang/rust/issues/70917
91                // for further background and discussion.
92                for child in args.iter() {
93                    match child.kind() {
94                        ty::GenericArgKind::Lifetime(_) => {}
95                        ty::GenericArgKind::Type(_) | ty::GenericArgKind::Const(_) => {
96                            child.visit_with(self);
97                        }
98                    }
99                }
100            }
101
102            ty::Closure(_, args) => {
103                args.as_closure().tupled_upvars_ty().visit_with(self);
104            }
105
106            ty::CoroutineClosure(_, args) => {
107                args.as_coroutine_closure().tupled_upvars_ty().visit_with(self);
108            }
109
110            ty::Coroutine(_, args) => {
111                args.as_coroutine().tupled_upvars_ty().visit_with(self);
112
113                // Coroutines may not outlive a region unless the resume
114                // ty outlives a region. This is because the resume ty may
115                // store data that lives shorter than this outlives region
116                // across yield points, which may subsequently be accessed
117                // after the coroutine is resumed again.
118                //
119                // Conceptually, you may think of the resume arg as an upvar
120                // of `&mut Option<ResumeArgTy>`, since it is kinda like
121                // storage shared between the callee of the coroutine and the
122                // coroutine body.
123                args.as_coroutine().resume_ty().visit_with(self);
124
125                // We ignore regions in the coroutine interior as we don't
126                // want these to affect region inference
127            }
128
129            // All regions are bound inside a witness, and we don't emit
130            // higher-ranked outlives components currently.
131            ty::CoroutineWitness(..) => {}
132
133            // OutlivesTypeParameterEnv -- the actual checking that `X:'a`
134            // is implied by the environment is done in regionck.
135            ty::Param(p) => {
136                self.out.push(Component::Param(p));
137            }
138
139            ty::Placeholder(p) => {
140                self.out.push(Component::Placeholder(p));
141            }
142
143            // For projections, we prefer to generate an obligation like
144            // `<P0 as Trait<P1...Pn>>::Foo: 'a`, because this gives the
145            // regionck more ways to prove that it holds. However,
146            // regionck is not (at least currently) prepared to deal with
147            // higher-ranked regions that may appear in the
148            // trait-ref. Therefore, if we see any higher-ranked regions,
149            // we simply fallback to the most restrictive rule, which
150            // requires that `Pi: 'a` for all `i`.
151            ty::Alias(kind, alias_ty) => {
152                if !alias_ty.has_escaping_bound_vars() {
153                    // best case: no escaping regions, so push the
154                    // projection and skip the subtree (thus generating no
155                    // constraints for Pi). This defers the choice between
156                    // the rules OutlivesProjectionEnv,
157                    // OutlivesProjectionTraitDef, and
158                    // OutlivesProjectionComponents to regionck.
159                    self.out.push(Component::Alias(alias_ty));
160                } else {
161                    // fallback case: hard code
162                    // OutlivesProjectionComponents. Continue walking
163                    // through and constrain Pi.
164                    let mut subcomponents = smallvec![];
165                    compute_alias_components_recursive(self.cx, kind, alias_ty, &mut subcomponents);
166                    self.out.push(Component::EscapingAlias(subcomponents.into_iter().collect()));
167                }
168            }
169
170            // We assume that inference variables are fully resolved.
171            // So, if we encounter an inference variable, just record
172            // the unresolved variable as a component.
173            ty::Infer(infer_ty) => {
174                self.out.push(Component::UnresolvedInferenceVariable(infer_ty));
175            }
176
177            // Most types do not introduce any region binders, nor
178            // involve any other subtle cases, and so the WF relation
179            // simply constraints any regions referenced directly by
180            // the type and then visits the types that are lexically
181            // contained within.
182            ty::Bool
183            | ty::Char
184            | ty::Int(_)
185            | ty::Uint(_)
186            | ty::Float(_)
187            | ty::Str
188            | ty::Never
189            | ty::Error(_) => {
190                // Trivial
191            }
192
193            ty::Bound(_, _) => {
194                // FIXME: Bound vars matter here!
195            }
196
197            ty::Adt(_, _)
198            | ty::Foreign(_)
199            | ty::Array(_, _)
200            | ty::Pat(_, _)
201            | ty::Slice(_)
202            | ty::RawPtr(_, _)
203            | ty::Ref(_, _, _)
204            | ty::FnPtr(..)
205            | ty::UnsafeBinder(_)
206            | ty::Dynamic(_, _, _)
207            | ty::Tuple(_) => {
208                ty.super_visit_with(self);
209            }
210        }
211    }
212
213    fn visit_region(&mut self, lt: I::Region) -> Self::Result {
214        if !lt.is_bound() {
215            self.out.push(Component::Region(lt));
216        }
217    }
218}
219
220/// Collect [Component]s for *all* the args of `alias_ty`.
221///
222/// This should not be used to get the components of `alias_ty` itself.
223/// Use [push_outlives_components] instead.
224pub fn compute_alias_components_recursive<I: Interner>(
225    cx: I,
226    kind: ty::AliasTyKind,
227    alias_ty: ty::AliasTy<I>,
228    out: &mut SmallVec<[Component<I>; 4]>,
229) {
230    let opt_variances = cx.opt_alias_variances(kind, alias_ty.def_id);
231
232    let mut visitor = OutlivesCollector { cx, out, visited: Default::default() };
233
234    for (index, child) in alias_ty.args.iter().enumerate() {
235        if opt_variances.and_then(|variances| variances.get(index)) == Some(ty::Bivariant) {
236            continue;
237        }
238        child.visit_with(&mut visitor);
239    }
240}